Spaces:
Sleeping
Sleeping
Bug fix for very long text
Browse filesFiles changed (2)
- app.py+34-34
- text_cleaning.py+11-6
133 unmodified lines134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193133 unmodified linesimport ioasync def generate_audio_segment(text_segment, voice_short_name, rate_str, volume_str, pitch_str, segment_index):"""Generate audio for a single text segment and return as BytesIO"""logger.info(f"Generating segment {segment_index}...")communicate = edge_tts.Communicate(text_segment, voice_short_name, rate=rate_str, volume=volume_str, pitch=pitch_str)audio_data = io.BytesIO()try:async for chunk in communicate.stream():if chunk["type"] == "audio":audio_data.write(chunk["data"])except Exception as e:logger.error(f"Error generating segment {segment_index} (Length: {len(text_segment)} chars): {e}")raise gr.Error(f"Error generating segment {segment_index}: {e}")audio_data.seek(0)# Verify segment durationtry:# Make a copy for verification so we don't consume the main bufferverify_buffer = io.BytesIO(audio_data.getvalue())seg_audio = AudioSegment.from_mp3(verify_buffer)duration_min = len(seg_audio) / 1000 / 60logger.info(f"Segment {segment_index} generated in memory (Duration: {duration_min:.2f} min)")except Exception as e:logger.error(f"Error checking segment {segment_index} duration: {e}")audio_data.seek(0)return audio_dataasync def merge_audio_files(audio_objects):"""Merge multiple audio BytesIO objects into one file"""if not audio_objects:return Nonelogger.info(f"Merging {len(audio_objects)} audio segments...")# Load and merge audio segmentscombined = AudioSegment.empty()for i, audio_obj in enumerate(audio_objects):try:audio_obj.seek(0)segment = AudioSegment.from_mp3(audio_obj)combined += segment# Explicitly close/clear the BytesIO object to free memoryaudio_obj.close()except Exception as e:logger.error(f"Error merging segment {i+1}: {e}")# Save merged audio to a single temporary filewith tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:merged_path = tmp_file.namecombined.export(merged_path, format="mp3")total_duration_min = len(combined) / 1000 / 60logger.info(f"Merged audio saved to {merged_path} (Total Duration: {total_duration_min:.2f} min)")return merged_pathasync def text_to_speech_generator(text, voice, rate, volume, pitch, cleaning_options=None):133 unmodified lines134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193133 unmodified linesimport ioasync def generate_audio_segment(text_segment, voice_short_name, rate_str, volume_str, pitch_str, segment_index):"""Generate audio for a single text segment and save to temporary file"""logger.info(f"Generating segment {segment_index}...")communicate = edge_tts.Communicate(text_segment, voice_short_name, rate=rate_str, volume=volume_str, pitch=pitch_str)# Save directly to temporary file instead of memorytmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=f"_seg{segment_index}.mp3")tmp_path = tmp_file.nametmp_file.close()try:await communicate.save(tmp_path)except Exception as e:logger.error(f"Error generating segment {segment_index} (Length: {len(text_segment)} chars): {e}")if os.path.exists(tmp_path):os.remove(tmp_path)raise gr.Error(f"Error generating segment {segment_index}: {e}")# Verify segment durationtry:seg_audio = AudioSegment.from_mp3(tmp_path)duration_min = len(seg_audio) / 1000 / 60logger.info(f"Segment {segment_index} saved to temp file (Duration: {duration_min:.2f} min)")except Exception as e:logger.error(f"Error checking segment {segment_index} duration: {e}")return tmp_pathasync def merge_audio_files(audio_paths):"""Merge multiple audio files into one file using binary concatenation"""if not audio_paths:return Nonelogger.info(f"Merging {len(audio_paths)} audio segments...")# Create output filewith tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:merged_path = tmp_file.name# Binary concatenation of MP3 files (avoids WAV size limit)total_size = 0with open(merged_path, 'wb') as outfile:for i, audio_path in enumerate(audio_paths):try:with open(audio_path, 'rb') as infile:data = infile.read()outfile.write(data)total_size += len(data)# Delete temporary segment file after mergingos.remove(audio_path)logger.info(f"Merged and deleted segment {i+1}")except Exception as e:logger.error(f"Error merging segment {i+1}: {e}")logger.info(f"Merged audio saved to {merged_path} (Total size: {total_size / 1024 / 1024:.2f} MB)")return merged_pathasync def text_to_speech_generator(text, voice, rate, volume, pitch, cleaning_options=None):
124 unmodified lines125126127128129130131132133134135136124 unmodified lines@staticmethoddef remove_special_chars(text):"""Remove excessive special characters"""# Keep alphanumeric, basic punctuation, and common CJK characters# This is a conservative regex to avoid removing valid text# \w matches [a-zA-Z0-9_] and unicode word chars (including Chinese)# We add some common punctuationreturn re.sub(r'[^\w\s.,!?;:()"\'-,。!?;:()“”‘’]', '', text)@staticmethoddef wetext_normalize(text):124 unmodified lines125126127128129130131132133134135136137138139140141124 unmodified lines@staticmethoddef remove_special_chars(text):"""Remove special characters that affect TTS but keep normal punctuation"""# Only remove characters that TTS engines typically read aloud incorrectly# Keep: letters, numbers, spaces, newlines, and common punctuation# Characters to remove (symbols that TTS might read literally)text = re.sub(r'[@#$%^&*+=|\\<>{}\[\]~`]', '', text)# Remove multiple consecutive special punctuation (like *** or ---)text = re.sub(r'([!?.,;:\-])\1{2,}', r'\1', text)return text@staticmethoddef wetext_normalize(text):
app.py+34-34
text_cleaning.py+11-6