quantumos commited on
Commit
4eb7c59
·
verified ·
1 Parent(s): a6bbb5b

Update app.py

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