Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import subprocess
|
|
6 |
import gradio as gr
|
7 |
import uuid
|
8 |
from dotenv import load_dotenv
|
|
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
@@ -136,23 +137,63 @@ def combine_audio_video(video_path, audio_path, output_path):
|
|
136 |
|
137 |
subprocess.run(cmd, check=True)
|
138 |
|
139 |
-
def
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
progress(0, desc="Generating speech...")
|
142 |
audio_path = text_to_speech(voice, text, session_id)
|
143 |
if not audio_path:
|
144 |
return None, "Failed to generate speech audio."
|
145 |
|
146 |
-
progress(0.2, desc="Processing
|
147 |
|
148 |
try:
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
audio_url = upload_file(audio_path)
|
151 |
|
152 |
if not audio_url:
|
153 |
raise Exception("Failed to upload audio file")
|
154 |
|
155 |
-
progress(0.
|
156 |
job_data = lipsync_api_call(video_url, audio_url)
|
157 |
|
158 |
if "error" in job_data or "message" in job_data:
|
@@ -160,7 +201,7 @@ def process_video(voice, video_url, text, progress=gr.Progress()):
|
|
160 |
|
161 |
job_id = job_data["id"]
|
162 |
|
163 |
-
progress(0.
|
164 |
result_url = check_job_status(job_id)
|
165 |
|
166 |
if result_url:
|
@@ -177,11 +218,12 @@ def process_video(voice, video_url, text, progress=gr.Progress()):
|
|
177 |
except Exception as e:
|
178 |
progress(0.8, desc="Falling back to simple combination...")
|
179 |
try:
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
|
|
185 |
|
186 |
output_path = f"output_{session_id}.mp4"
|
187 |
combine_audio_video(video_path, audio_path, output_path)
|
@@ -204,22 +246,22 @@ def create_interface():
|
|
204 |
with gr.Row():
|
205 |
with gr.Column():
|
206 |
voice_dropdown = gr.Dropdown(choices=[v[0] for v in voices], label="Select Voice", value=voices[0][0] if voices else None)
|
207 |
-
|
208 |
text_input = gr.Textbox(label="Enter text", lines=3)
|
209 |
generate_btn = gr.Button("Generate Video")
|
210 |
with gr.Column():
|
211 |
video_output = gr.Video(label="Generated Video")
|
212 |
status_output = gr.Textbox(label="Status", interactive=False)
|
213 |
|
214 |
-
def on_generate(voice_name,
|
215 |
voice_id = next((v[1] for v in voices if v[0] == voice_name), None)
|
216 |
if not voice_id:
|
217 |
return None, "Invalid voice selected."
|
218 |
-
return process_video(voice_id,
|
219 |
|
220 |
generate_btn.click(
|
221 |
fn=on_generate,
|
222 |
-
inputs=[voice_dropdown,
|
223 |
outputs=[video_output, status_output]
|
224 |
)
|
225 |
|
|
|
6 |
import gradio as gr
|
7 |
import uuid
|
8 |
from dotenv import load_dotenv
|
9 |
+
from urllib.parse import urlparse
|
10 |
|
11 |
# Load environment variables
|
12 |
load_dotenv()
|
|
|
137 |
|
138 |
subprocess.run(cmd, check=True)
|
139 |
|
140 |
+
def is_image_url(url):
|
141 |
+
parsed = urlparse(url)
|
142 |
+
path = parsed.path.lower()
|
143 |
+
return path.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))
|
144 |
+
|
145 |
+
def create_video_from_image(image_url, output_path, duration=10):
|
146 |
+
# Download the image
|
147 |
+
response = requests.get(image_url)
|
148 |
+
if response.status_code != 200:
|
149 |
+
raise Exception("Failed to download the image")
|
150 |
+
|
151 |
+
temp_image_path = f"temp_image_{uuid.uuid4()}.jpg"
|
152 |
+
with open(temp_image_path, 'wb') as f:
|
153 |
+
f.write(response.content)
|
154 |
+
|
155 |
+
# Create a 10-second video from the image
|
156 |
+
cmd = [
|
157 |
+
'ffmpeg', '-loop', '1', '-i', temp_image_path,
|
158 |
+
'-c:v', 'libx264', '-t', str(duration), '-pix_fmt', 'yuv420p',
|
159 |
+
'-vf', 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2',
|
160 |
+
'-y', output_path
|
161 |
+
]
|
162 |
+
subprocess.run(cmd, check=True)
|
163 |
+
|
164 |
+
# Clean up the temporary image file
|
165 |
+
os.remove(temp_image_path)
|
166 |
+
|
167 |
+
return output_path
|
168 |
+
|
169 |
+
def process_video(voice, media_url, text, progress=gr.Progress()):
|
170 |
+
session_id = str(uuid.uuid4())
|
171 |
progress(0, desc="Generating speech...")
|
172 |
audio_path = text_to_speech(voice, text, session_id)
|
173 |
if not audio_path:
|
174 |
return None, "Failed to generate speech audio."
|
175 |
|
176 |
+
progress(0.2, desc="Processing media...")
|
177 |
|
178 |
try:
|
179 |
+
if is_image_url(media_url):
|
180 |
+
progress(0.3, desc="Converting image to video...")
|
181 |
+
video_path = f"temp_video_{session_id}.mp4"
|
182 |
+
create_video_from_image(media_url, video_path)
|
183 |
+
progress(0.4, desc="Uploading converted video...")
|
184 |
+
video_url = upload_file(video_path)
|
185 |
+
if not video_url:
|
186 |
+
raise Exception("Failed to upload converted video")
|
187 |
+
else:
|
188 |
+
video_url = media_url
|
189 |
+
|
190 |
+
progress(0.5, desc="Uploading audio...")
|
191 |
audio_url = upload_file(audio_path)
|
192 |
|
193 |
if not audio_url:
|
194 |
raise Exception("Failed to upload audio file")
|
195 |
|
196 |
+
progress(0.6, desc="Initiating lipsync...")
|
197 |
job_data = lipsync_api_call(video_url, audio_url)
|
198 |
|
199 |
if "error" in job_data or "message" in job_data:
|
|
|
201 |
|
202 |
job_id = job_data["id"]
|
203 |
|
204 |
+
progress(0.7, desc="Processing lipsync...")
|
205 |
result_url = check_job_status(job_id)
|
206 |
|
207 |
if result_url:
|
|
|
218 |
except Exception as e:
|
219 |
progress(0.8, desc="Falling back to simple combination...")
|
220 |
try:
|
221 |
+
if 'video_path' not in locals():
|
222 |
+
# Download the video from the URL if it wasn't created from an image
|
223 |
+
video_response = requests.get(video_url)
|
224 |
+
video_path = f"temp_video_{session_id}.mp4"
|
225 |
+
with open(video_path, "wb") as f:
|
226 |
+
f.write(video_response.content)
|
227 |
|
228 |
output_path = f"output_{session_id}.mp4"
|
229 |
combine_audio_video(video_path, audio_path, output_path)
|
|
|
246 |
with gr.Row():
|
247 |
with gr.Column():
|
248 |
voice_dropdown = gr.Dropdown(choices=[v[0] for v in voices], label="Select Voice", value=voices[0][0] if voices else None)
|
249 |
+
media_url_input = gr.Textbox(label="Enter Video or Image URL")
|
250 |
text_input = gr.Textbox(label="Enter text", lines=3)
|
251 |
generate_btn = gr.Button("Generate Video")
|
252 |
with gr.Column():
|
253 |
video_output = gr.Video(label="Generated Video")
|
254 |
status_output = gr.Textbox(label="Status", interactive=False)
|
255 |
|
256 |
+
def on_generate(voice_name, media_url, text):
|
257 |
voice_id = next((v[1] for v in voices if v[0] == voice_name), None)
|
258 |
if not voice_id:
|
259 |
return None, "Invalid voice selected."
|
260 |
+
return process_video(voice_id, media_url, text)
|
261 |
|
262 |
generate_btn.click(
|
263 |
fn=on_generate,
|
264 |
+
inputs=[voice_dropdown, media_url_input, text_input],
|
265 |
outputs=[video_output, status_output]
|
266 |
)
|
267 |
|