insanecoder69 commited on
Commit
25b096e
·
verified ·
1 Parent(s): d6fcce5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -28
app.py CHANGED
@@ -1,41 +1,73 @@
1
  import gradio as gr
2
  import subprocess
3
  import os
 
 
4
 
5
- # Define the command template
6
- COMMAND_TEMPLATE = "python scripts/demo.py --config_file ./config/LS3DCG.json --infer --audio_file {audio_path} --body_model_name s2g_LS3DCG --body_model_path experiments/2022-10-19-smplx_S2G-LS3DCG/ckpt-99.pth --id 0"
 
 
7
 
8
- # Define the function to process the audio and generate the video
9
- def process_audio(audio):
10
- # Save uploaded audio to a specific file path
11
- audio_path = "demo_audio/style.wav"
12
- with open(audio_path, "wb") as f:
13
- f.write(audio.read())
14
 
15
- # Run the script command with the specified audio file
16
- command = COMMAND_TEMPLATE.format(audio_path=audio_path)
17
- subprocess.run(command, shell=True)
18
 
19
- # Specify the path to the output video
20
- output_video_path = "./output_video.mp4" # Update if output location is different
21
 
22
- # Check if the video file exists and return it for display
23
- if os.path.exists(output_video_path):
24
- return output_video_path
25
- else:
26
- return "Error: Video file not generated."
27
 
28
- # Set up Gradio interface
29
- audio_input = gr.Audio(label="Upload a .wav audio file", type="file")
30
- video_output = gr.Video(label="Generated Video Output")
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  interface = gr.Interface(
33
- fn=process_audio,
34
- inputs=audio_input,
35
- outputs=video_output,
36
- title="Audio-to-Video Generator",
37
- description="Upload a .wav file to generate an animated video output."
38
  )
39
 
40
- # Launch the Gradio app
41
- interface.launch()
 
 
1
  import gradio as gr
2
  import subprocess
3
  import os
4
+ from pydub import AudioSegment
5
+ import tempfile
6
 
7
+ # Set OpenGL platform
8
+ os.environ["PYOPENGL_PLATFORM"] = "egl"
9
+ os.environ["MESA_GL_VERSION_OVERRIDE"] = "4.1"
10
+ os.system('pip install /home/user/app/pyrender')
11
 
12
+ # Paths to TalkShow demo script and configuration
13
+ DEMO_SCRIPT_PATH = "scripts/demo.py"
14
+ CONFIG_FILE = "./config/LS3DCG.json"
15
+ BODY_MODEL_PATH = "experiments/2022-10-19-smplx_S2G-LS3DCG/ckpt-99.pth"
16
+ OUTPUT_MP4_PATH = "./output/demo_output.mp4"
 
17
 
 
 
 
18
 
19
+ # Ensure the output directory exists
20
+ os.makedirs(os.path.dirname(OUTPUT_MP4_PATH), exist_ok=True)
21
 
 
 
 
 
 
22
 
23
+ def convert_audio_to_wav(input_audio):
24
+ # Use a temporary file to save the .wav output
25
+ temp_wav = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
26
+ temp_wav_path = temp_wav.name
27
 
28
+ # Convert the input audio to .wav format
29
+ audio = AudioSegment.from_file(input_audio)
30
+ audio.export(temp_wav_path, format="wav")
31
+ return temp_wav_path
32
+
33
+
34
+ def run_demo_and_get_video(input_audio):
35
+ # Convert the input audio to a temporary .wav file
36
+ wav_path = convert_audio_to_wav(input_audio)
37
+
38
+ try:
39
+ # Run the demo script with the specified arguments
40
+ command = [
41
+ "python", DEMO_SCRIPT_PATH,
42
+ "--config_file", CONFIG_FILE,
43
+ "--infer",
44
+ "--audio_file", wav_path,
45
+ "--body_model_name", "s2g_LS3DCG",
46
+ "--body_model_path", BODY_MODEL_PATH,
47
+ "--id", "0"
48
+ ]
49
+ subprocess.run(command, check=True)
50
+
51
+ # Check if the output file exists
52
+ if os.path.exists(OUTPUT_MP4_PATH):
53
+ return OUTPUT_MP4_PATH
54
+ else:
55
+ return "Error: Output video not generated."
56
+ except subprocess.CalledProcessError as e:
57
+ return f"Error: {str(e)}"
58
+ finally:
59
+ # Clean up the temporary .wav file after processing
60
+ os.remove(wav_path)
61
+
62
+ # Define the Gradio interface
63
  interface = gr.Interface(
64
+ fn=run_demo_and_get_video,
65
+ inputs=gr.Audio(source="upload", type="filepath"),
66
+ outputs=gr.Video(type="file"),
67
+ title="TalkSHOW Demo",
68
+ description="Generate a video from audio input using TalkSHOW model."
69
  )
70
 
71
+ # Launch the app
72
+ if __name__ == "__main__":
73
+ interface.launch()