sheikhed commited on
Commit
9d19ca3
·
verified ·
1 Parent(s): 7eea0f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -63
app.py CHANGED
@@ -55,7 +55,6 @@ def text_to_speech(voice_id, text, session_id):
55
  if response.status_code != 200:
56
  return None
57
 
58
- # Save temporary audio file with session ID
59
  audio_file_path = f'temp_voice_{session_id}.mp3'
60
  with open(audio_file_path, 'wb') as audio_file:
61
  audio_file.write(response.content)
@@ -135,30 +134,33 @@ def combine_audio_video(video_path, audio_path, output_path):
135
 
136
  subprocess.run(cmd, check=True)
137
 
138
- def process_video(voice_id, model, text, audio_file, input_type, progress=gr.Progress()):
139
  session_id = str(uuid.uuid4())
140
-
141
- # Handle audio source based on input type
142
- if input_type == "text":
 
 
 
143
  progress(0, desc="Generating speech...")
144
- audio_path = text_to_speech(voice_id, text, session_id)
145
- if not audio_path:
146
  return None, "Failed to generate speech audio."
147
- else: # audio upload
148
- progress(0, desc="Processing uploaded audio...")
149
- # Save uploaded audio to temporary file
150
- temp_audio_path = f'temp_voice_{session_id}.mp3'
151
- with open(temp_audio_path, 'wb') as f:
152
- f.write(open(audio_file, 'rb').read())
153
- audio_path = temp_audio_path
154
-
155
  progress(0.2, desc="Processing video...")
156
  video_path = os.path.join("models", model)
157
 
158
  try:
159
  progress(0.3, desc="Uploading files...")
160
  video_url = upload_file(video_path)
161
- audio_url = upload_file(audio_path)
162
 
163
  if not video_url or not audio_url:
164
  raise Exception("Failed to upload files")
@@ -189,15 +191,14 @@ def process_video(voice_id, model, text, audio_file, input_type, progress=gr.Pro
189
  progress(0.8, desc="Falling back to simple combination...")
190
  try:
191
  output_path = f"output_{session_id}.mp4"
192
- combine_audio_video(video_path, audio_path, output_path)
193
  progress(1.0, desc="Complete!")
194
  return output_path, f"Used fallback method. Original error: {str(e)}"
195
  except Exception as fallback_error:
196
  return None, f"All methods failed. Error: {str(fallback_error)}"
197
  finally:
198
- # Cleanup
199
- if os.path.exists(audio_path):
200
- os.remove(audio_path)
201
 
202
  def create_interface():
203
  voices = get_voices()
@@ -205,61 +206,48 @@ def create_interface():
205
 
206
  with gr.Blocks() as app:
207
  gr.Markdown("# JSON Train")
 
 
 
 
208
  with gr.Row():
209
  with gr.Column():
210
- input_type = gr.Radio(
211
- choices=["text", "audio"],
212
- label="Input Type",
213
- value="text"
214
- )
215
 
216
- with gr.Column(visible=True) as text_inputs:
217
- voice_dropdown = gr.Dropdown(
218
- choices=[v[0] for v in voices],
219
- label="Select Voice",
220
- value=voices[0][0] if voices else None
221
- )
222
- text_input = gr.Textbox(label="Enter text", lines=3)
223
-
224
- with gr.Column(visible=False) as audio_inputs:
225
- audio_upload = gr.Audio(label="Upload Audio", type="filepath")
226
-
227
- model_dropdown = gr.Dropdown(
228
- choices=models,
229
- label="Select Video Model",
230
- value=models[0] if models else None
 
231
  )
 
232
  generate_btn = gr.Button("Generate Video")
233
-
234
  with gr.Column():
235
  video_output = gr.Video(label="Generated Video")
236
  status_output = gr.Textbox(label="Status", interactive=False)
237
 
238
- def toggle_inputs(input_type):
239
- return (
240
- gr.Column.update(visible=(input_type == "text")),
241
- gr.Column.update(visible=(input_type == "audio"))
242
- )
243
-
244
- input_type.change(
245
- fn=toggle_inputs,
246
- inputs=[input_type],
247
- outputs=[text_inputs, audio_inputs]
248
- )
249
-
250
- def on_generate(voice_name, model_name, text, audio_file, input_type):
251
- if input_type == "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
- else:
256
- voice_id = None # Not needed for audio upload
257
-
258
- return process_video(voice_id, model_name, text, audio_file, input_type)
259
 
260
  generate_btn.click(
261
  fn=on_generate,
262
- inputs=[voice_dropdown, model_dropdown, text_input, audio_upload, input_type],
263
  outputs=[video_output, status_output]
264
  )
265
 
 
55
  if response.status_code != 200:
56
  return None
57
 
 
58
  audio_file_path = f'temp_voice_{session_id}.mp3'
59
  with open(audio_file_path, 'wb') as audio_file:
60
  audio_file.write(response.content)
 
134
 
135
  subprocess.run(cmd, check=True)
136
 
137
+ def process_input(voice, model, text, audio_file, progress=gr.Progress()):
138
  session_id = str(uuid.uuid4())
139
+ input_audio_path = None
140
+
141
+ if text and audio_file:
142
+ return None, "Please choose either text input or audio upload, not both."
143
+
144
+ if text:
145
  progress(0, desc="Generating speech...")
146
+ input_audio_path = text_to_speech(voice, text, session_id)
147
+ if not input_audio_path:
148
  return None, "Failed to generate speech audio."
149
+
150
+ elif audio_file:
151
+ progress(0, desc="Using uploaded audio...")
152
+ input_audio_path = audio_file.name
153
+
154
+ else:
155
+ return None, "Please provide either text or upload an audio file."
156
+
157
  progress(0.2, desc="Processing video...")
158
  video_path = os.path.join("models", model)
159
 
160
  try:
161
  progress(0.3, desc="Uploading files...")
162
  video_url = upload_file(video_path)
163
+ audio_url = upload_file(input_audio_path)
164
 
165
  if not video_url or not audio_url:
166
  raise Exception("Failed to upload files")
 
191
  progress(0.8, desc="Falling back to simple combination...")
192
  try:
193
  output_path = f"output_{session_id}.mp4"
194
+ combine_audio_video(video_path, input_audio_path, output_path)
195
  progress(1.0, desc="Complete!")
196
  return output_path, f"Used fallback method. Original error: {str(e)}"
197
  except Exception as fallback_error:
198
  return None, f"All methods failed. Error: {str(fallback_error)}"
199
  finally:
200
+ if text and os.path.exists(input_audio_path):
201
+ os.remove(input_audio_path)
 
202
 
203
  def create_interface():
204
  voices = get_voices()
 
206
 
207
  with gr.Blocks() as app:
208
  gr.Markdown("# JSON Train")
209
+
210
+ with gr.Row():
211
+ input_type = gr.Radio(choices=["Text", "Audio"], value="Text", label="Input Type")
212
+
213
  with gr.Row():
214
  with gr.Column():
215
+ voice_dropdown = gr.Dropdown(choices=[v[0] for v in voices], label="Select Voice", value=voices[0][0] if voices else None)
216
+ model_dropdown = gr.Dropdown(choices=models, label="Select Model", value=models[0] if models else None)
 
 
 
217
 
218
+ text_input = gr.Textbox(label="Enter Text", lines=3)
219
+ audio_input = gr.Audio(label="Upload Audio", type="filepath")
220
+
221
+ text_input.show(True)
222
+ audio_input.show(False)
223
+
224
+ def toggle_input(input_type):
225
+ if input_type == "Text":
226
+ return gr.update(visible=True), gr.update(visible=False)
227
+ else:
228
+ return gr.update(visible=False), gr.update(visible=True)
229
+
230
+ input_type.change(
231
+ fn=toggle_input,
232
+ inputs=[input_type],
233
+ outputs=[text_input, audio_input],
234
  )
235
+
236
  generate_btn = gr.Button("Generate Video")
237
+
238
  with gr.Column():
239
  video_output = gr.Video(label="Generated Video")
240
  status_output = gr.Textbox(label="Status", interactive=False)
241
 
242
+ def on_generate(voice_name, model_name, text, audio_file):
243
+ voice_id = next((v[1] for v in voices if v[0] == voice_name), None)
244
+ if not voice_id:
245
+ return None, "Invalid voice selected."
246
+ return process_input(voice_id, model_name, text, audio_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
 
248
  generate_btn.click(
249
  fn=on_generate,
250
+ inputs=[voice_dropdown, model_dropdown, text_input, audio_input],
251
  outputs=[video_output, status_output]
252
  )
253