Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
@@ -403,24 +403,50 @@ Follow this example structure:
|
|
403 |
if progress:
|
404 |
progress.progress(0.5, "Converting text to speech...")
|
405 |
|
406 |
-
# Process TTS in batches
|
407 |
audio_files = []
|
408 |
total_lines = len(podcast_json['podcast'])
|
409 |
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
try:
|
416 |
-
|
417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
except Exception as e:
|
419 |
# Clean up any files already created
|
420 |
for file in audio_files:
|
421 |
if os.path.exists(file):
|
422 |
os.remove(file)
|
423 |
-
raise Exception(f"Error
|
424 |
|
425 |
combined_audio = await self.combine_audio_files(audio_files, progress)
|
426 |
return combined_audio
|
|
|
403 |
if progress:
|
404 |
progress.progress(0.5, "Converting text to speech...")
|
405 |
|
406 |
+
# Process TTS in batches for concurrent processing
|
407 |
audio_files = []
|
408 |
total_lines = len(podcast_json['podcast'])
|
409 |
|
410 |
+
# Define batch size to control concurrency
|
411 |
+
batch_size = 10 # Adjust based on system resources
|
412 |
+
|
413 |
+
# Process in batches
|
414 |
+
for batch_start in range(0, total_lines, batch_size):
|
415 |
+
batch_end = min(batch_start + batch_size, total_lines)
|
416 |
+
batch = podcast_json['podcast'][batch_start:batch_end]
|
417 |
+
|
418 |
+
# Create tasks for concurrent processing
|
419 |
+
tts_tasks = []
|
420 |
+
for item in batch:
|
421 |
+
tts_task = self.tts_generate(item['line'], item['speaker'], speaker1, speaker2)
|
422 |
+
tts_tasks.append(tts_task)
|
423 |
+
|
424 |
try:
|
425 |
+
# Process batch concurrently
|
426 |
+
batch_results = await asyncio.gather(*tts_tasks, return_exceptions=True)
|
427 |
+
|
428 |
+
# Check for exceptions and handle results
|
429 |
+
for i, result in enumerate(batch_results):
|
430 |
+
if isinstance(result, Exception):
|
431 |
+
# Clean up any files already created
|
432 |
+
for file in audio_files:
|
433 |
+
if os.path.exists(file):
|
434 |
+
os.remove(file)
|
435 |
+
raise Exception(f"Error generating speech: {str(result)}")
|
436 |
+
else:
|
437 |
+
audio_files.append(result)
|
438 |
+
|
439 |
+
# Update progress
|
440 |
+
if progress:
|
441 |
+
current_progress = 0.5 + (0.4 * (batch_end / total_lines))
|
442 |
+
progress.progress(current_progress, f"Processed {batch_end}/{total_lines} speech segments...")
|
443 |
+
|
444 |
except Exception as e:
|
445 |
# Clean up any files already created
|
446 |
for file in audio_files:
|
447 |
if os.path.exists(file):
|
448 |
os.remove(file)
|
449 |
+
raise Exception(f"Error in batch TTS generation: {str(e)}")
|
450 |
|
451 |
combined_audio = await self.combine_audio_files(audio_files, progress)
|
452 |
return combined_audio
|