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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -75
app.py CHANGED
@@ -1,14 +1,17 @@
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/',
@@ -19,16 +22,11 @@ DEFAULT_MODEL_ARGS = {
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,
@@ -37,82 +35,81 @@ DEFAULT_MODEL_ARGS = {
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),
@@ -124,10 +121,8 @@ output = [
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)
 
1
  import gradio as gr
2
  import shutil
3
  import subprocess
4
+ import logging
5
 
6
  from inference import InferenceEngine
7
  from sequence_utils import extract_kps_sequence_from_video
8
 
9
+ # Configure logging
10
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
11
+
12
  output_dir = "output"
13
  temp_audio_path = "temp.mp3"
14
 
 
15
  DEFAULT_MODEL_ARGS = {
16
  'unet_config_path': './model_ckpts/stable-diffusion-v1-5/unet/config.json',
17
  'vae_path': './model_ckpts/sd-vae-ft-mse/',
 
22
  'v_kps_guider_path': './model_ckpts/v-express/v_kps_guider.pth',
23
  'audio_projection_path': './model_ckpts/v-express/audio_projection.pth',
24
  'motion_module_path': './model_ckpts/v-express/motion_module.pth',
 
25
  'device': 'cuda',
26
  'gpu_id': 0,
27
  'dtype': 'fp16',
28
  'num_pad_audio_frames': 2,
29
  'standard_audio_sampling_rate': 16000,
 
 
 
 
30
  'image_width': 512,
31
  'image_height': 512,
32
  'fps': 30.0,
 
35
  'guidance_scale': 3.5,
36
  'context_frames': 12,
37
  'context_stride': 1,
38
+ 'context_overlap': 4
 
 
39
  }
40
 
41
+ logging.info("Initializing Inference Engine")
42
  INFERENCE_ENGINE = InferenceEngine(DEFAULT_MODEL_ARGS)
43
 
44
+ def infer(reference_image, audio_path, kps_sequence_save_path, output_path, retarget_strategy, reference_attention_weight, audio_attention_weight):
45
+ try:
46
+ logging.info("Starting inference")
47
+ INFERENCE_ENGINE.infer(
48
+ reference_image, audio_path, kps_sequence_save_path,
49
+ output_path,
50
+ retarget_strategy,
51
+ reference_attention_weight, audio_attention_weight
52
+ )
53
+ logging.info("Inference completed successfully")
54
+ return output_path, kps_sequence_save_path
55
+ except Exception as e:
56
+ logging.error(f"Inference failed: {e}")
57
+ return None, None
58
+
59
+ def run_demo(reference_image, audio, video, kps_path, output_path, retarget_strategy, reference_attention_weight=0.95, audio_attention_weight=3.0, progress=gr.Progress()):
60
+ try:
61
+ progress((0, 100), desc="Starting...")
62
+ logging.info("Demo started")
63
+
64
+ kps_sequence_save_path = f"{output_dir}/kps.pth"
65
+
66
+ if video is not None:
67
+ progress((25, 100), desc="Extracting keypoints and audio...")
68
+ logging.info("Extracting keypoints and audio from video")
69
+ audio_path = video.replace(".mp4", ".mp3")
70
+ extract_kps_sequence_from_video(
71
+ INFERENCE_ENGINE.app,
72
+ video,
73
+ audio_path,
74
+ kps_sequence_save_path
75
+ )
76
+ progress((50, 100), desc="Keypoints and audio extracted successfully.")
77
+ rem_progress = (75, 100)
78
+ else:
79
+ rem_progress = (50, 100)
80
+ audio_path = audio
81
+ shutil.copy(kps_path.name, kps_sequence_save_path)
82
+
83
+ subprocess.run(["ffmpeg", "-i", audio_path, "-c:v", "libx264", "-crf", "18", "-preset", "slow", temp_audio_path])
84
+ shutil.move(temp_audio_path, audio_path)
85
+
86
+ progress(rem_progress, desc="Running inference...")
87
+ output_path, kps_sequence_save_path = infer(
88
+ reference_image, audio_path, kps_sequence_save_path,
89
+ output_path,
90
+ retarget_strategy,
91
+ reference_attention_weight, audio_attention_weight
92
  )
93
 
94
+ if output_path and kps_sequence_save_path:
95
+ status = f"Video generated successfully. Saved at: {output_path}"
96
+ logging.info(status)
97
+ else:
98
+ status = "Failed to generate video."
99
+ logging.error(status)
100
+
101
+ progress((100, 100), desc=status)
102
+ return output_path, kps_sequence_save_path
103
+ except Exception as e:
104
+ logging.error(f"Demo failed: {e}")
105
+ progress((100, 100), desc="Error occurred")
106
+ return None, None
107
+
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  inputs = [
109
  gr.Image(label="Reference Image", type="filepath"),
110
  gr.Audio(label="Audio", type="filepath"),
111
  gr.Video(label="Video"),
112
+ gr.File(label="KPS sequences", value="test_samples/short_case/10/kps.pth"),
113
  gr.Textbox(label="Output Path for generated video", value=f"{output_dir}/output_video.mp4"),
114
  gr.Dropdown(label="Retargeting Strategy", choices=["no_retarget", "fix_face", "offset_retarget", "naive_retarget"], value="no_retarget"),
115
  gr.Slider(label="Reference Attention Weight", minimum=0.0, maximum=1.0, step=0.01, value=0.95),
 
121
  gr.File(label="Generated KPS Sequences File (kps.pth)")
122
  ]
123
 
 
124
  title = "V-Express Gradio Interface"
125
  description = "An interactive interface for generating talking face videos using V-Express."
126
 
 
127
  demo = gr.Interface(run_demo, inputs, output, title=title, description=description)
128
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)