ramimu commited on
Commit
5961c78
·
verified ·
1 Parent(s): 3c59845

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -72
app.py CHANGED
@@ -233,92 +233,111 @@ def clone_voice_api(text_to_speak, reference_audio_url, exaggeration=0.6, cfg_pa
233
 
234
  temp_audio_path = None
235
  try:
236
- print(f"API call received by clone_voice_api:")
237
- print(f" Text: {text_to_speak}")
238
- print(f" Audio URL type: {type(reference_audio_url)}")
239
- print(f" Audio URL preview: {str(reference_audio_url)[:100]}...")
240
- print(f" Parameters: exag={exaggeration}, cfg={cfg_pace}, seed={random_seed}, temp={temperature}")
241
-
242
- if isinstance(reference_audio_url, str) and reference_audio_url.startswith('data:audio'):
 
 
 
 
 
 
 
 
 
 
243
  print("Processing base64 audio data...")
244
- header, encoded = reference_audio_url.split(',', 1)
245
- audio_data = base64.b64decode(encoded)
246
- print(f"Decoded audio data size: {len(audio_data)} bytes")
247
-
248
- if 'mp3' in header:
249
- ext = '.mp3'
250
- elif 'wav' in header:
251
- ext = '.wav'
252
- else:
253
- ext = '.wav'
254
-
255
- with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as temp_file:
256
- temp_file.write(audio_data)
257
- temp_audio_path = temp_file.name
258
-
259
- print(f"Created temporary audio file from base64: {temp_audio_path}")
260
-
261
- elif isinstance(reference_audio_url, str) and reference_audio_url.startswith('http'):
 
 
 
 
 
 
 
 
 
 
262
  print("Processing HTTP audio URL...")
263
- response = requests.get(reference_audio_url)
264
- response.raise_for_status()
265
- if reference_audio_url.endswith('.mp3'):
266
- ext = '.mp3'
267
- elif reference_audio_url.endswith('.wav'):
268
- ext = '.wav'
269
- else:
270
- ext = '.wav' # Default
271
- with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as temp_file:
272
- temp_file.write(response.content)
273
- temp_audio_path = temp_file.name
274
- print(f"Created temporary audio file from URL: {temp_audio_path}")
275
- elif isinstance(reference_audio_url, str) and os.path.exists(reference_audio_url):
276
- print("Using direct file path provided as string...")
277
- temp_audio_path = reference_audio_url
 
278
  else:
279
- # This case might occur if Gradio passes a TemporaryFileWrapper or similar
280
- if hasattr(reference_audio_url, 'name'): # Check if it's a file-like object from Gradio
281
- temp_audio_path = reference_audio_url.name
282
- print(f"Using file path from Gradio object: {temp_audio_path}")
283
- else:
284
- print(f"Warning: Unrecognized audio input type or path: {reference_audio_url}. Assuming it's a direct path.")
285
- temp_audio_path = str(reference_audio_url) # Fallback, attempt to use as path
286
-
287
- if not temp_audio_path or not os.path.exists(temp_audio_path):
288
- raise ValueError(f"Failed to obtain a valid audio file path from input: {reference_audio_url}")
289
-
290
- print(f"Calling core clone_voice function with audio path: {temp_audio_path}")
291
  audio_output, status = clone_voice(text_to_speak, temp_audio_path, exaggeration, cfg_pace, random_seed, temperature)
292
- print(f"clone_voice returned: {type(audio_output)}, {status}")
 
 
 
293
 
294
- # Clean up temporary file only if we created one from base64 or URL
295
- if temp_audio_path and isinstance(reference_audio_url, str) and \
296
- (reference_audio_url.startswith('data:audio') or reference_audio_url.startswith('http')):
297
  try:
298
  os.unlink(temp_audio_path)
299
  print(f"Cleaned up temporary file: {temp_audio_path}")
300
- except Exception as e:
301
- print(f"Failed to clean up temp file {temp_audio_path}: {e}")
302
-
303
  return audio_output, status
304
-
305
  except Exception as e:
306
- print(f"ERROR in clone_voice_api: {e}")
307
- import traceback # Ensure traceback is imported here if not globally
 
 
308
  traceback.print_exc()
309
-
310
- # Attempt to clean up temporary file in case of error too
311
- if temp_audio_path and isinstance(reference_audio_url, str) and \
312
- (reference_audio_url.startswith('data:audio') or reference_audio_url.startswith('http')):
313
  try:
314
- if os.path.exists(temp_audio_path): # Check existence before unlinking
315
- os.unlink(temp_audio_path)
316
- print(f"Cleaned up temporary file after error: {temp_audio_path}")
317
- except Exception as e_clean:
318
- print(f"Failed to clean up temp file {temp_audio_path} after error: {e_clean}")
319
  return None, f"API Error: {str(e)}"
320
 
321
-
322
  def main():
323
  print("Starting Advanced Gradio interface...")
324
 
 
233
 
234
  temp_audio_path = None
235
  try:
236
+ print(f"=== API CALL DEBUG ===")
237
+ print(f"Text: {text_to_speak}")
238
+ print(f"Audio URL type: {type(reference_audio_url)}")
239
+ print(f"Audio URL length: {len(str(reference_audio_url)) if reference_audio_url else 0}")
240
+ print(f"Audio URL preview: {str(reference_audio_url)[:100]}...")
241
+ print(f"Parameters: exag={exaggeration}, cfg={cfg_pace}, seed={random_seed}, temp={temperature}")
242
+
243
+ # Validate inputs
244
+ if not text_to_speak or text_to_speak.strip() == "":
245
+ return None, "Error: Please enter some text to speak."
246
+
247
+ if not reference_audio_url:
248
+ return None, "Error: Please provide reference audio."
249
+
250
+ print("Processing audio data...")
251
+
252
+ if reference_audio_url.startswith('data:audio'):
253
  print("Processing base64 audio data...")
254
+ try:
255
+ header, encoded = reference_audio_url.split(',', 1)
256
+ print(f"Header: {header}")
257
+ print(f"Encoded data length: {len(encoded)}")
258
+
259
+ audio_data = base64.b64decode(encoded)
260
+ print(f"Decoded audio data size: {len(audio_data)} bytes")
261
+
262
+ if 'mp3' in header:
263
+ ext = '.mp3'
264
+ elif 'wav' in header:
265
+ ext = '.wav'
266
+ else:
267
+ ext = '.wav'
268
+
269
+ with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as temp_file:
270
+ temp_file.write(audio_data)
271
+ temp_audio_path = temp_file.name
272
+
273
+ print(f"Created temporary audio file: {temp_audio_path}")
274
+ print(f"File exists: {os.path.exists(temp_audio_path)}")
275
+ print(f"File size: {os.path.getsize(temp_audio_path)} bytes")
276
+
277
+ except Exception as audio_error:
278
+ print(f"Audio processing error: {audio_error}")
279
+ return None, f"Error processing audio data: {str(audio_error)}"
280
+
281
+ elif reference_audio_url.startswith('http'):
282
  print("Processing HTTP audio URL...")
283
+ try:
284
+ response = requests.get(reference_audio_url)
285
+ response.raise_for_status()
286
+ if reference_audio_url.endswith('.mp3'):
287
+ ext = '.mp3'
288
+ elif reference_audio_url.endswith('.wav'):
289
+ ext = '.wav'
290
+ else:
291
+ ext = '.wav'
292
+ with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as temp_file:
293
+ temp_file.write(response.content)
294
+ temp_audio_path = temp_file.name
295
+ print(f"Downloaded audio to: {temp_audio_path}")
296
+ except Exception as download_error:
297
+ print(f"Download error: {download_error}")
298
+ return None, f"Error downloading audio: {str(download_error)}"
299
  else:
300
+ print("Using direct file path...")
301
+ temp_audio_path = reference_audio_url
302
+
303
+ print(f"Calling clone_voice with:")
304
+ print(f" Text: {text_to_speak}")
305
+ print(f" Audio path: {temp_audio_path}")
306
+ print(f" Parameters: {exaggeration}, {cfg_pace}, {random_seed}, {temperature}")
307
+
308
+ # Call the main function
 
 
 
309
  audio_output, status = clone_voice(text_to_speak, temp_audio_path, exaggeration, cfg_pace, random_seed, temperature)
310
+
311
+ print(f"clone_voice returned:")
312
+ print(f" Audio output type: {type(audio_output)}")
313
+ print(f" Status: {status}")
314
 
315
+ # Cleanup
316
+ if temp_audio_path and temp_audio_path != reference_audio_url:
 
317
  try:
318
  os.unlink(temp_audio_path)
319
  print(f"Cleaned up temporary file: {temp_audio_path}")
320
+ except Exception as cleanup_error:
321
+ print(f"Cleanup error: {cleanup_error}")
322
+
323
  return audio_output, status
324
+
325
  except Exception as e:
326
+ print(f"=== CRITICAL ERROR ===")
327
+ print(f"Error type: {type(e)}")
328
+ print(f"Error message: {str(e)}")
329
+ import traceback
330
  traceback.print_exc()
331
+
332
+ # Cleanup on error
333
+ if temp_audio_path and temp_audio_path != reference_audio_url:
 
334
  try:
335
+ os.unlink(temp_audio_path)
336
+ except:
337
+ pass
338
+
 
339
  return None, f"API Error: {str(e)}"
340
 
 
341
  def main():
342
  print("Starting Advanced Gradio interface...")
343