sheikhed commited on
Commit
b2b31f2
·
verified ·
1 Parent(s): 8882e69

Update app.py

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