yukee1992 commited on
Commit
041c542
Β·
verified Β·
1 Parent(s): 6bf1df2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -8
app.py CHANGED
@@ -67,6 +67,34 @@ class VoiceCloneRequest(BaseModel):
67
  description: Optional[str] = ""
68
 
69
  # Helper functions
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  def upload_to_oci(file_path: str, filename: str, project_id: str, file_type="voiceover"):
71
  """Upload file to OCI using your existing API with subfolder support"""
72
  try:
@@ -300,13 +328,18 @@ async def generate_tts(request: TTSRequest):
300
  }
301
 
302
  print(f"πŸ”Š Generating TTS to: {output_path}")
 
 
 
 
 
303
 
304
  # Generate TTS based on model capabilities - WITH ERROR HANDLING
305
  try:
306
  if supports_voice_cloning():
307
  # XTTS model with voice cloning support
308
  tts.tts_to_file(
309
- text=request.text,
310
  speaker_wav=speaker_wav,
311
  language=request.language,
312
  file_path=output_path
@@ -314,7 +347,7 @@ async def generate_tts(request: TTSRequest):
314
  else:
315
  # Fallback model (Tacotron2)
316
  tts.tts_to_file(
317
- text=request.text,
318
  file_path=output_path
319
  )
320
  except Exception as tts_error:
@@ -325,12 +358,12 @@ async def generate_tts(request: TTSRequest):
325
  # Generate audio first, then save
326
  if supports_voice_cloning():
327
  audio = tts.tts(
328
- text=request.text,
329
  speaker_wav=speaker_wav,
330
  language=request.language
331
  )
332
  else:
333
- audio = tts.tts(text=request.text)
334
 
335
  # Save manually
336
  if not save_wav(audio, output_path):
@@ -432,18 +465,22 @@ async def batch_generate_tts(request: BatchTTSRequest):
432
  # Ensure output directory exists
433
  os.makedirs(os.path.dirname(output_path), exist_ok=True)
434
 
 
 
 
 
435
  # Generate TTS based on model capabilities - WITH ERROR HANDLING
436
  try:
437
  if supports_voice_cloning():
438
  tts.tts_to_file(
439
- text=text,
440
  speaker_wav=speaker_wav,
441
  language=request.language,
442
  file_path=output_path
443
  )
444
  else:
445
  tts.tts_to_file(
446
- text=text,
447
  file_path=output_path
448
  )
449
  except Exception as tts_error:
@@ -453,12 +490,12 @@ async def batch_generate_tts(request: BatchTTSRequest):
453
  print("πŸ”„ Trying alternative TTS generation method...")
454
  if supports_voice_cloning():
455
  audio = tts.tts(
456
- text=text,
457
  speaker_wav=speaker_wav,
458
  language=request.language
459
  )
460
  else:
461
- audio = tts.tts(text=text)
462
 
463
  # Save manually
464
  if not save_wav(audio, output_path):
 
67
  description: Optional[str] = ""
68
 
69
  # Helper functions
70
+ def clean_text(text):
71
+ """Clean text for TTS generation"""
72
+ import re
73
+
74
+ if not text or not isinstance(text, str):
75
+ return "Hello" # Default fallback text
76
+
77
+ # Remove any non-ASCII characters
78
+ text = text.encode('ascii', 'ignore').decode('ascii')
79
+
80
+ # Remove any problematic characters but keep basic punctuation
81
+ text = re.sub(r'[^\w\s\.\,\!\?\-\'\"\:]', '', text)
82
+
83
+ # Replace multiple spaces with single space
84
+ text = re.sub(r'\s+', ' ', text)
85
+
86
+ # Ensure text ends with punctuation if it's a sentence
87
+ if len(text) > 10 and not re.search(r'[\.\!\?]$', text):
88
+ text = text + '.'
89
+
90
+ text = text.strip()
91
+
92
+ # If text is empty after cleaning, use default
93
+ if not text:
94
+ text = "Hello world"
95
+
96
+ return text
97
+
98
  def upload_to_oci(file_path: str, filename: str, project_id: str, file_type="voiceover"):
99
  """Upload file to OCI using your existing API with subfolder support"""
100
  try:
 
328
  }
329
 
330
  print(f"πŸ”Š Generating TTS to: {output_path}")
331
+
332
+ # Clean the text before generation
333
+ cleaned_text = clean_text(request.text)
334
+ print(f"πŸ“ Original text: '{request.text}'")
335
+ print(f"πŸ“ Cleaned text: '{cleaned_text}'")
336
 
337
  # Generate TTS based on model capabilities - WITH ERROR HANDLING
338
  try:
339
  if supports_voice_cloning():
340
  # XTTS model with voice cloning support
341
  tts.tts_to_file(
342
+ text=cleaned_text, # Use cleaned text
343
  speaker_wav=speaker_wav,
344
  language=request.language,
345
  file_path=output_path
 
347
  else:
348
  # Fallback model (Tacotron2)
349
  tts.tts_to_file(
350
+ text=cleaned_text, # Use cleaned text
351
  file_path=output_path
352
  )
353
  except Exception as tts_error:
 
358
  # Generate audio first, then save
359
  if supports_voice_cloning():
360
  audio = tts.tts(
361
+ text=cleaned_text, # Use cleaned text
362
  speaker_wav=speaker_wav,
363
  language=request.language
364
  )
365
  else:
366
+ audio = tts.tts(text=cleaned_text) # Use cleaned text
367
 
368
  # Save manually
369
  if not save_wav(audio, output_path):
 
465
  # Ensure output directory exists
466
  os.makedirs(os.path.dirname(output_path), exist_ok=True)
467
 
468
+ # Clean the text for each item
469
+ cleaned_text = clean_text(text)
470
+ print(f"πŸ“ Batch text {i+1}: '{text}' -> '{cleaned_text}'")
471
+
472
  # Generate TTS based on model capabilities - WITH ERROR HANDLING
473
  try:
474
  if supports_voice_cloning():
475
  tts.tts_to_file(
476
+ text=cleaned_text, # Use cleaned text
477
  speaker_wav=speaker_wav,
478
  language=request.language,
479
  file_path=output_path
480
  )
481
  else:
482
  tts.tts_to_file(
483
+ text=cleaned_text, # Use cleaned text
484
  file_path=output_path
485
  )
486
  except Exception as tts_error:
 
490
  print("πŸ”„ Trying alternative TTS generation method...")
491
  if supports_voice_cloning():
492
  audio = tts.tts(
493
+ text=cleaned_text, # Use cleaned text
494
  speaker_wav=speaker_wav,
495
  language=request.language
496
  )
497
  else:
498
+ audio = tts.tts(text=cleaned_text) # Use cleaned text
499
 
500
  # Save manually
501
  if not save_wav(audio, output_path):