Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -29,9 +29,9 @@ def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
|
|
29 |
min_resolution = 512
|
30 |
inference_steps = 25
|
31 |
|
32 |
-
# Get audio duration (
|
33 |
audio = AudioSegment.from_file(audio_path)
|
34 |
-
duration = len(audio) / 1000.0 #
|
35 |
|
36 |
face_info = pipe.preprocess(img_path, expand_ratio=expand_ratio)
|
37 |
print(f"Face detection info: {face_info}")
|
@@ -43,7 +43,7 @@ def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
|
|
43 |
img_path = crop_image_path
|
44 |
os.makedirs(os.path.dirname(res_video_path), exist_ok=True)
|
45 |
|
46 |
-
#
|
47 |
pipe.process(
|
48 |
img_path,
|
49 |
audio_path,
|
@@ -52,6 +52,8 @@ def get_video_res(img_path, audio_path, res_video_path, dynamic_scale=1.0):
|
|
52 |
inference_steps=inference_steps,
|
53 |
dynamic_scale=dynamic_scale
|
54 |
)
|
|
|
|
|
55 |
else:
|
56 |
return -1
|
57 |
|
@@ -61,7 +63,7 @@ os.makedirs(tmp_path, exist_ok=True)
|
|
61 |
os.makedirs(res_path, exist_ok=True)
|
62 |
|
63 |
def process_sonic(image, audio, dynamic_scale):
|
64 |
-
#
|
65 |
if image is None:
|
66 |
raise gr.Error("Please upload an image")
|
67 |
if audio is None:
|
@@ -75,7 +77,7 @@ def process_sonic(image, audio, dynamic_scale):
|
|
75 |
if len(arr.shape) == 1:
|
76 |
arr = arr[:, None]
|
77 |
|
78 |
-
#
|
79 |
audio_segment = AudioSegment(
|
80 |
arr.tobytes(),
|
81 |
frame_rate=sampling_rate,
|
@@ -84,18 +86,18 @@ def process_sonic(image, audio, dynamic_scale):
|
|
84 |
)
|
85 |
audio_segment = audio_segment.set_frame_rate(sampling_rate)
|
86 |
|
87 |
-
#
|
88 |
image_path = os.path.abspath(os.path.join(tmp_path, f'{img_md5}.png'))
|
89 |
audio_path = os.path.abspath(os.path.join(tmp_path, f'{audio_md5}.wav'))
|
90 |
res_video_path = os.path.abspath(os.path.join(res_path, f'{img_md5}_{audio_md5}_{dynamic_scale}.mp4'))
|
91 |
|
92 |
-
#
|
93 |
if not os.path.exists(image_path):
|
94 |
image.save(image_path)
|
95 |
if not os.path.exists(audio_path):
|
96 |
audio_segment.export(audio_path, format="wav")
|
97 |
|
98 |
-
#
|
99 |
if os.path.exists(res_video_path):
|
100 |
print(f"Using cached result: {res_video_path}")
|
101 |
return res_video_path
|
@@ -103,7 +105,7 @@ def process_sonic(image, audio, dynamic_scale):
|
|
103 |
print(f"Generating new video with dynamic scale: {dynamic_scale}")
|
104 |
return get_video_res(image_path, audio_path, res_video_path, dynamic_scale)
|
105 |
|
106 |
-
#
|
107 |
def get_example():
|
108 |
return []
|
109 |
|
@@ -171,7 +173,7 @@ with gr.Blocks(css=css) as demo:
|
|
171 |
elem_id="video_output"
|
172 |
)
|
173 |
|
174 |
-
#
|
175 |
process_btn.click(
|
176 |
fn=process_sonic,
|
177 |
inputs=[image_input, audio_input, dynamic_scale],
|
@@ -179,7 +181,7 @@ with gr.Blocks(css=css) as demo:
|
|
179 |
api_name="animate"
|
180 |
)
|
181 |
|
182 |
-
#
|
183 |
gr.Examples(
|
184 |
examples=get_example(),
|
185 |
fn=process_sonic,
|
@@ -188,7 +190,7 @@ with gr.Blocks(css=css) as demo:
|
|
188 |
cache_examples=False
|
189 |
)
|
190 |
|
191 |
-
# Footer
|
192 |
gr.HTML("""
|
193 |
<div style="text-align: center; margin-top: 2em;">
|
194 |
<div style="margin-bottom: 1em;">
|
@@ -203,5 +205,5 @@ with gr.Blocks(css=css) as demo:
|
|
203 |
</div>
|
204 |
""")
|
205 |
|
206 |
-
#
|
207 |
demo.launch(share=True)
|
|
|
29 |
min_resolution = 512
|
30 |
inference_steps = 25
|
31 |
|
32 |
+
# Get audio duration (for logging)
|
33 |
audio = AudioSegment.from_file(audio_path)
|
34 |
+
duration = len(audio) / 1000.0 # Convert ms to seconds
|
35 |
|
36 |
face_info = pipe.preprocess(img_path, expand_ratio=expand_ratio)
|
37 |
print(f"Face detection info: {face_info}")
|
|
|
43 |
img_path = crop_image_path
|
44 |
os.makedirs(os.path.dirname(res_video_path), exist_ok=True)
|
45 |
|
46 |
+
# Process the video (duration parameter removed)
|
47 |
pipe.process(
|
48 |
img_path,
|
49 |
audio_path,
|
|
|
52 |
inference_steps=inference_steps,
|
53 |
dynamic_scale=dynamic_scale
|
54 |
)
|
55 |
+
# ★ 수정: 생성된 비디오 파일 경로를 반환하도록 함.
|
56 |
+
return res_video_path
|
57 |
else:
|
58 |
return -1
|
59 |
|
|
|
63 |
os.makedirs(res_path, exist_ok=True)
|
64 |
|
65 |
def process_sonic(image, audio, dynamic_scale):
|
66 |
+
# Input validation
|
67 |
if image is None:
|
68 |
raise gr.Error("Please upload an image")
|
69 |
if audio is None:
|
|
|
77 |
if len(arr.shape) == 1:
|
78 |
arr = arr[:, None]
|
79 |
|
80 |
+
# Create an audio segment from numpy array
|
81 |
audio_segment = AudioSegment(
|
82 |
arr.tobytes(),
|
83 |
frame_rate=sampling_rate,
|
|
|
86 |
)
|
87 |
audio_segment = audio_segment.set_frame_rate(sampling_rate)
|
88 |
|
89 |
+
# Generate file paths
|
90 |
image_path = os.path.abspath(os.path.join(tmp_path, f'{img_md5}.png'))
|
91 |
audio_path = os.path.abspath(os.path.join(tmp_path, f'{audio_md5}.wav'))
|
92 |
res_video_path = os.path.abspath(os.path.join(res_path, f'{img_md5}_{audio_md5}_{dynamic_scale}.mp4'))
|
93 |
|
94 |
+
# Save input files if they don't exist
|
95 |
if not os.path.exists(image_path):
|
96 |
image.save(image_path)
|
97 |
if not os.path.exists(audio_path):
|
98 |
audio_segment.export(audio_path, format="wav")
|
99 |
|
100 |
+
# If cached video exists, return it; otherwise, generate a new one
|
101 |
if os.path.exists(res_video_path):
|
102 |
print(f"Using cached result: {res_video_path}")
|
103 |
return res_video_path
|
|
|
105 |
print(f"Generating new video with dynamic scale: {dynamic_scale}")
|
106 |
return get_video_res(image_path, audio_path, res_video_path, dynamic_scale)
|
107 |
|
108 |
+
# Dummy get_example function to prevent errors in examples section
|
109 |
def get_example():
|
110 |
return []
|
111 |
|
|
|
173 |
elem_id="video_output"
|
174 |
)
|
175 |
|
176 |
+
# Process button click: when clicked, process_sonic() is called and its return value is sent to video_output.
|
177 |
process_btn.click(
|
178 |
fn=process_sonic,
|
179 |
inputs=[image_input, audio_input, dynamic_scale],
|
|
|
181 |
api_name="animate"
|
182 |
)
|
183 |
|
184 |
+
# Examples section
|
185 |
gr.Examples(
|
186 |
examples=get_example(),
|
187 |
fn=process_sonic,
|
|
|
190 |
cache_examples=False
|
191 |
)
|
192 |
|
193 |
+
# Footer with attribution and links
|
194 |
gr.HTML("""
|
195 |
<div style="text-align: center; margin-top: 2em;">
|
196 |
<div style="margin-bottom: 1em;">
|
|
|
205 |
</div>
|
206 |
""")
|
207 |
|
208 |
+
# To create a public link, share=True is set.
|
209 |
demo.launch(share=True)
|