quantumos commited on
Commit
ec55bbe
·
verified ·
1 Parent(s): 4d93b8e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -133
app.py CHANGED
@@ -1,133 +1,133 @@
1
- import gradio as gr
2
- import shutil
3
- import subprocess
4
-
5
- from inference import InferenceEngine
6
- from sequence_utils import extract_kps_sequence_from_video
7
-
8
- output_dir = "output"
9
- temp_audio_path = "temp.mp3"
10
-
11
-
12
- DEFAULT_MODEL_ARGS = {
13
- 'unet_config_path': './model_ckpts/stable-diffusion-v1-5/unet/config.json',
14
- 'vae_path': './model_ckpts/sd-vae-ft-mse/',
15
- 'audio_encoder_path': './model_ckpts/wav2vec2-base-960h/',
16
- 'insightface_model_path': './model_ckpts/insightface_models/',
17
- 'denoising_unet_path': './model_ckpts/v-express/denoising_unet.pth',
18
- 'reference_net_path': './model_ckpts/v-express/reference_net.pth',
19
- 'v_kps_guider_path': './model_ckpts/v-express/v_kps_guider.pth',
20
- 'audio_projection_path': './model_ckpts/v-express/audio_projection.pth',
21
- 'motion_module_path': './model_ckpts/v-express/motion_module.pth',
22
- #'retarget_strategy': 'fix_face', # fix_face, no_retarget, offset_retarget, naive_retarget
23
- 'device': 'cuda',
24
- 'gpu_id': 0,
25
- 'dtype': 'fp16',
26
- 'num_pad_audio_frames': 2,
27
- 'standard_audio_sampling_rate': 16000,
28
- #'reference_image_path': './test_samples/emo/talk_emotion/ref.jpg',
29
- #'audio_path': './test_samples/emo/talk_emotion/aud.mp3',
30
- #'kps_path': './test_samples/emo/talk_emotion/kps.pth',
31
- #'output_path': './output/emo/talk_emotion.mp4',
32
- 'image_width': 512,
33
- 'image_height': 512,
34
- 'fps': 30.0,
35
- 'seed': 42,
36
- 'num_inference_steps': 25,
37
- 'guidance_scale': 3.5,
38
- 'context_frames': 12,
39
- 'context_stride': 1,
40
- 'context_overlap': 4,
41
- #'reference_attention_weight': 0.95,
42
- #'audio_attention_weight': 3.0
43
- }
44
-
45
- INFERENCE_ENGINE = InferenceEngine(DEFAULT_MODEL_ARGS)
46
-
47
- def infer(reference_image, audio_path, kps_sequence_save_path,
48
- output_path,
49
- retarget_strategy,
50
- reference_attention_weight, audio_attention_weight):
51
- global INFERENCE_ENGINE
52
- INFERENCE_ENGINE.infer(
53
- reference_image, audio_path, kps_sequence_save_path,
54
- output_path,
55
- retarget_strategy,
56
- reference_attention_weight, audio_attention_weight
57
- )
58
- return output_path, kps_sequence_save_path
59
-
60
- # Function to run V-Express demo
61
- def run_demo(
62
- reference_image, audio, video,
63
- kps_path, output_path, retarget_strategy,
64
- reference_attention_weight=0.95,
65
- audio_attention_weight=3.0,
66
- progress=gr.Progress()):
67
- # Step 1: Extract Keypoints from Video
68
- progress((0,100), desc="Starting...")
69
-
70
- kps_sequence_save_path = f"{output_dir}/kps.pth"
71
-
72
- if video is not None:
73
- # Run the script to extract keypoints and audio from the video
74
- progress((25,100), desc="Extract keypoints and audio...")
75
- audio_path = video.replace(".mp4", ".mp3")
76
-
77
- extract_kps_sequence_from_video(
78
- INFERENCE_ENGINE.app,
79
- video,
80
- audio_path,
81
- kps_sequence_save_path
82
- )
83
-
84
- progress((50,100), desc="Keypoints and audio extracted successfully.")
85
- #return "Keypoints and audio extracted successfully."
86
- rem_progress = (75,100)
87
- else:
88
- rem_progress = (50,100)
89
- audio_path = audio
90
- shutil.copy(kps_path.name, kps_sequence_save_path)
91
-
92
- subprocess.run(["ffmpeg", "-i", audio_path, "-c:v", "libx264", "-crf", "18", "-preset", "slow", temp_audio_path])
93
- shutil.move(temp_audio_path, audio_path)
94
-
95
- # Step 2: Run Inference with Reference Image and Audio
96
- # Determine the inference script and parameters based on the selected retargeting strategy
97
- progress(rem_progress, desc="Inference...")
98
-
99
- output_path, kps_sequence_save_path = infer(
100
- reference_image, audio_path, kps_sequence_save_path,
101
- output_path,
102
- retarget_strategy,
103
- reference_attention_weight, audio_attention_weight
104
- )
105
-
106
- status = f"Video generated successfully. Saved at: {output_path}"
107
- progress((100,100), desc=status)
108
- return output_path, kps_sequence_save_path
109
-
110
- # Create Gradio interface
111
- inputs = [
112
- gr.Image(label="Reference Image", type="filepath"),
113
- gr.Audio(label="Audio", type="filepath"),
114
- gr.Video(label="Video"),
115
- gr.File(label="KPS sequences", value=f"test_samples/short_case/10/kps.pth"),
116
- gr.Textbox(label="Output Path for generated video", value=f"{output_dir}/output_video.mp4"),
117
- gr.Dropdown(label="Retargeting Strategy", choices=["no_retarget", "fix_face", "offset_retarget", "naive_retarget"], value="no_retarget"),
118
- gr.Slider(label="Reference Attention Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.95),
119
- gr.Slider(label="Audio Attention Weight", minimum=1.0, maximum=5.0, step=0.1, value=3.0)
120
- ]
121
-
122
- output = [
123
- gr.Video(label="Generated Video"),
124
- gr.File(label="Generated KPS Sequences File (kps.pth)")
125
- ]
126
-
127
- # Title and description for the interface
128
- title = "V-Express Gradio Interface"
129
- description = "An interactive interface for generating talking face videos using V-Express."
130
-
131
- # Launch Gradio app
132
- demo = gr.Interface(run_demo, inputs, output, title=title, description=description)
133
- demo.queue().launch()
 
1
+ import gradio as gr
2
+ import shutil
3
+ import subprocess
4
+
5
+ from inference import InferenceEngine
6
+ from sequence_utils import extract_kps_sequence_from_video
7
+
8
+ output_dir = "output"
9
+ temp_audio_path = "temp.mp3"
10
+
11
+
12
+ DEFAULT_MODEL_ARGS = {
13
+ 'unet_config_path': './model_ckpts/stable-diffusion-v1-5/unet/config.json',
14
+ 'vae_path': './model_ckpts/sd-vae-ft-mse/',
15
+ 'audio_encoder_path': './model_ckpts/wav2vec2-base-960h/',
16
+ 'insightface_model_path': './model_ckpts/insightface_models/',
17
+ 'denoising_unet_path': './model_ckpts/v-express/denoising_unet.pth',
18
+ 'reference_net_path': './model_ckpts/v-express/reference_net.pth',
19
+ 'v_kps_guider_path': './model_ckpts/v-express/v_kps_guider.pth',
20
+ 'audio_projection_path': './model_ckpts/v-express/audio_projection.pth',
21
+ 'motion_module_path': './model_ckpts/v-express/motion_module.pth',
22
+ #'retarget_strategy': 'fix_face', # fix_face, no_retarget, offset_retarget, naive_retarget
23
+ 'device': 'cuda',
24
+ 'gpu_id': 0,
25
+ 'dtype': 'fp16',
26
+ 'num_pad_audio_frames': 2,
27
+ 'standard_audio_sampling_rate': 16000,
28
+ #'reference_image_path': './test_samples/emo/talk_emotion/ref.jpg',
29
+ #'audio_path': './test_samples/emo/talk_emotion/aud.mp3',
30
+ #'kps_path': './test_samples/emo/talk_emotion/kps.pth',
31
+ #'output_path': './output/emo/talk_emotion.mp4',
32
+ 'image_width': 512,
33
+ 'image_height': 512,
34
+ 'fps': 30.0,
35
+ 'seed': 42,
36
+ 'num_inference_steps': 25,
37
+ 'guidance_scale': 3.5,
38
+ 'context_frames': 12,
39
+ 'context_stride': 1,
40
+ 'context_overlap': 4,
41
+ #'reference_attention_weight': 0.95,
42
+ #'audio_attention_weight': 3.0
43
+ }
44
+
45
+ INFERENCE_ENGINE = InferenceEngine(DEFAULT_MODEL_ARGS)
46
+
47
+ def infer(reference_image, audio_path, kps_sequence_save_path,
48
+ output_path,
49
+ retarget_strategy,
50
+ reference_attention_weight, audio_attention_weight):
51
+ global INFERENCE_ENGINE
52
+ INFERENCE_ENGINE.infer(
53
+ reference_image, audio_path, kps_sequence_save_path,
54
+ output_path,
55
+ retarget_strategy,
56
+ reference_attention_weight, audio_attention_weight
57
+ )
58
+ return output_path, kps_sequence_save_path
59
+
60
+ # Function to run V-Express demo
61
+ def run_demo(
62
+ reference_image, audio, video,
63
+ kps_path, output_path, retarget_strategy,
64
+ reference_attention_weight=0.95,
65
+ audio_attention_weight=3.0,
66
+ progress=gr.Progress()):
67
+ # Step 1: Extract Keypoints from Video
68
+ progress((0,100), desc="Starting...")
69
+
70
+ kps_sequence_save_path = f"{output_dir}/kps.pth"
71
+
72
+ if video is not None:
73
+ # Run the script to extract keypoints and audio from the video
74
+ progress((25,100), desc="Extract keypoints and audio...")
75
+ audio_path = video.replace(".mp4", ".mp3")
76
+
77
+ extract_kps_sequence_from_video(
78
+ INFERENCE_ENGINE.app,
79
+ video,
80
+ audio_path,
81
+ kps_sequence_save_path
82
+ )
83
+
84
+ progress((50,100), desc="Keypoints and audio extracted successfully.")
85
+ #return "Keypoints and audio extracted successfully."
86
+ rem_progress = (75,100)
87
+ else:
88
+ rem_progress = (50,100)
89
+ audio_path = audio
90
+ shutil.copy(kps_path.name, kps_sequence_save_path)
91
+
92
+ subprocess.run(["ffmpeg", "-i", audio_path, "-c:v", "libx264", "-crf", "18", "-preset", "slow", temp_audio_path])
93
+ shutil.move(temp_audio_path, audio_path)
94
+
95
+ # Step 2: Run Inference with Reference Image and Audio
96
+ # Determine the inference script and parameters based on the selected retargeting strategy
97
+ progress(rem_progress, desc="Inference...")
98
+
99
+ output_path, kps_sequence_save_path = infer(
100
+ reference_image, audio_path, kps_sequence_save_path,
101
+ output_path,
102
+ retarget_strategy,
103
+ reference_attention_weight, audio_attention_weight
104
+ )
105
+
106
+ status = f"Video generated successfully. Saved at: {output_path}"
107
+ progress((100,100), desc=status)
108
+ return output_path, kps_sequence_save_path
109
+
110
+ # Create Gradio interface
111
+ inputs = [
112
+ gr.Image(label="Reference Image", type="filepath"),
113
+ gr.Audio(label="Audio", type="filepath"),
114
+ gr.Video(label="Video"),
115
+ gr.File(label="KPS sequences", value=f"test_samples/short_case/10/kps.pth"),
116
+ gr.Textbox(label="Output Path for generated video", value=f"{output_dir}/output_video.mp4"),
117
+ gr.Dropdown(label="Retargeting Strategy", choices=["no_retarget", "fix_face", "offset_retarget", "naive_retarget"], value="no_retarget"),
118
+ gr.Slider(label="Reference Attention Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.95),
119
+ gr.Slider(label="Audio Attention Weight", minimum=1.0, maximum=5.0, step=0.1, value=3.0)
120
+ ]
121
+
122
+ output = [
123
+ gr.Video(label="Generated Video"),
124
+ gr.File(label="Generated KPS Sequences File (kps.pth)")
125
+ ]
126
+
127
+ # Title and description for the interface
128
+ title = "V-Express Gradio Interface"
129
+ description = "An interactive interface for generating talking face videos using V-Express."
130
+
131
+ # Launch Gradio app
132
+ demo = gr.Interface(run_demo, inputs, output, title=title, description=description)
133
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)