quantumos commited on
Commit
7eb96c7
·
verified ·
1 Parent(s): 1a38dee

Update app.py

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