sheikhed commited on
Commit
d7e99cb
·
verified ·
1 Parent(s): f959be9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -30,9 +30,6 @@ def get_voices():
30
  return []
31
  return [(voice['name'], voice['voice_id']) for voice in response.json().get('voices', [])]
32
 
33
- def get_video_models():
34
- return [f for f in os.listdir("models") if f.endswith((".mp4", ".avi", ".mov"))]
35
-
36
  def text_to_speech(voice_id, text, session_id):
37
  url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
38
 
@@ -139,7 +136,7 @@ def combine_audio_video(video_path, audio_path, output_path):
139
 
140
  subprocess.run(cmd, check=True)
141
 
142
- def process_video(voice, model, text, progress=gr.Progress()):
143
  session_id = str(uuid.uuid4()) # Generate a unique session ID
144
  progress(0, desc="Generating speech...")
145
  audio_path = text_to_speech(voice, text, session_id)
@@ -147,15 +144,13 @@ def process_video(voice, model, text, progress=gr.Progress()):
147
  return None, "Failed to generate speech audio."
148
 
149
  progress(0.2, desc="Processing video...")
150
- video_path = os.path.join("models", model)
151
 
152
  try:
153
- progress(0.3, desc="Uploading files...")
154
- video_url = upload_file(video_path)
155
  audio_url = upload_file(audio_path)
156
 
157
- if not video_url or not audio_url:
158
- raise Exception("Failed to upload files")
159
 
160
  progress(0.4, desc="Initiating lipsync...")
161
  job_data = lipsync_api_call(video_url, audio_url)
@@ -182,6 +177,12 @@ def process_video(voice, model, text, progress=gr.Progress()):
182
  except Exception as e:
183
  progress(0.8, desc="Falling back to simple combination...")
184
  try:
 
 
 
 
 
 
185
  output_path = f"output_{session_id}.mp4"
186
  combine_audio_video(video_path, audio_path, output_path)
187
  progress(1.0, desc="Complete!")
@@ -192,32 +193,33 @@ def process_video(voice, model, text, progress=gr.Progress()):
192
  # Cleanup
193
  if os.path.exists(audio_path):
194
  os.remove(audio_path)
 
 
195
 
196
  def create_interface():
197
  voices = get_voices()
198
- models = get_video_models()
199
 
200
  with gr.Blocks() as app:
201
  gr.Markdown("# JSON Train")
202
  with gr.Row():
203
  with gr.Column():
204
- voice_dropdown = gr.Dropdown(choices=[v[0] for v in voices], label="Select", value=voices[0][0] if voices else None)
205
- model_dropdown = gr.Dropdown(choices=models, label="Select", value=models[0] if models else None)
206
  text_input = gr.Textbox(label="Enter text", lines=3)
207
  generate_btn = gr.Button("Generate Video")
208
  with gr.Column():
209
  video_output = gr.Video(label="Generated Video")
210
  status_output = gr.Textbox(label="Status", interactive=False)
211
 
212
- def on_generate(voice_name, model_name, text):
213
  voice_id = next((v[1] for v in voices if v[0] == voice_name), None)
214
  if not voice_id:
215
  return None, "Invalid voice selected."
216
- return process_video(voice_id, model_name, text)
217
 
218
  generate_btn.click(
219
  fn=on_generate,
220
- inputs=[voice_dropdown, model_dropdown, text_input],
221
  outputs=[video_output, status_output]
222
  )
223
 
@@ -225,4 +227,4 @@ def create_interface():
225
 
226
  if __name__ == "__main__":
227
  app = create_interface()
228
- app.launch()
 
30
  return []
31
  return [(voice['name'], voice['voice_id']) for voice in response.json().get('voices', [])]
32
 
 
 
 
33
  def text_to_speech(voice_id, text, session_id):
34
  url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice_id}"
35
 
 
136
 
137
  subprocess.run(cmd, check=True)
138
 
139
+ def process_video(voice, video_url, text, progress=gr.Progress()):
140
  session_id = str(uuid.uuid4()) # Generate a unique session ID
141
  progress(0, desc="Generating speech...")
142
  audio_path = text_to_speech(voice, text, session_id)
 
144
  return None, "Failed to generate speech audio."
145
 
146
  progress(0.2, desc="Processing video...")
 
147
 
148
  try:
149
+ progress(0.3, desc="Uploading audio...")
 
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.4, desc="Initiating lipsync...")
156
  job_data = lipsync_api_call(video_url, audio_url)
 
177
  except Exception as e:
178
  progress(0.8, desc="Falling back to simple combination...")
179
  try:
180
+ # Download the video from the URL
181
+ video_response = requests.get(video_url)
182
+ video_path = f"temp_video_{session_id}.mp4"
183
+ with open(video_path, "wb") as f:
184
+ f.write(video_response.content)
185
+
186
  output_path = f"output_{session_id}.mp4"
187
  combine_audio_video(video_path, audio_path, output_path)
188
  progress(1.0, desc="Complete!")
 
193
  # Cleanup
194
  if os.path.exists(audio_path):
195
  os.remove(audio_path)
196
+ if os.path.exists(f"temp_video_{session_id}.mp4"):
197
+ os.remove(f"temp_video_{session_id}.mp4")
198
 
199
  def create_interface():
200
  voices = get_voices()
 
201
 
202
  with gr.Blocks() as app:
203
  gr.Markdown("# JSON Train")
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
+ video_url_input = gr.Textbox(label="Enter Video URL")
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, video_url, text):
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, video_url, text)
219
 
220
  generate_btn.click(
221
  fn=on_generate,
222
+ inputs=[voice_dropdown, video_url_input, text_input],
223
  outputs=[video_output, status_output]
224
  )
225
 
 
227
 
228
  if __name__ == "__main__":
229
  app = create_interface()
230
+ app.launch()