Spaces:
Sleeping
Sleeping
Update app.py
Browse files123453 unmodified lines910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697981 unmodified line1001011021031041051062 unmodified lines1091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092106 unmodified lines2172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582594 unmodified lines2642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023038 unmodified lines312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406import gradio as grimport torchimport numpy as npimport reimport soundfile as sf3 unmodified linesfrom nltk.tokenize import sent_tokenizeimport warningsimport timefrom transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGanfrom datasets import load_datasetwarnings.filterwarnings("ignore")# Download required NLTK data including punkt_tabtry:nltk.data.find('tokenizers/punkt')nltk.data.find('tokenizers/punkt_tab') # This is the missing one!except LookupError:nltk.download(['punkt', 'punkt_tab'], quiet=True)class LongFormTTS:def __init__(self):print("π Loading TTS models...")try:# Load SpeechT5 - most reliable for HF Spacesprint("Loading SpeechT5 TTS...")self.processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")self.model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")self.vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")# Load speaker embeddingsprint("Loading speaker embeddings...")embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")# Use a different speaker embedding for more varietyself.speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)self.device = "cuda" if torch.cuda.is_available() else "cpu"self.model = self.model.to(self.device)self.vocoder = self.vocoder.to(self.device)self.speaker_embeddings = self.speaker_embeddings.to(self.device)print("β SpeechT5 loaded successfully!")except Exception as e:print(f"β Failed to load SpeechT5: {e}")raise Exception(f"TTS model loading failed: {e}")def preprocess_text(self, text):"""Clean and prepare text for TTS"""# Remove extra whitespacetext = re.sub(r'\s+', ' ', text.strip())# Handle common abbreviationsabbreviations = {'Dr.': 'Doctor','Mr.': 'Mister','Mrs.': 'Missus','Ms.': 'Miss','Prof.': 'Professor','etc.': 'etcetera','vs.': 'versus','e.g.': 'for example','i.e.': 'that is','St.': 'Street','Ave.': 'Avenue','Blvd.': 'Boulevard','Inc.': 'Incorporated','Corp.': 'Corporation','Ltd.': 'Limited','U.S.': 'United States','U.K.': 'United Kingdom','Ph.D.': 'PhD','M.D.': 'MD',}for abbr, full in abbreviations.items():text = text.replace(abbr, full)# Convert numbers to words (enhanced)text = re.sub(r'\b(\d{1,4})\b', lambda m: self.number_to_words(int(m.group())), text)# Handle years differently (keep as numbers if between 1000-2100)text = re.sub(r'\b(1[0-9]{3}|20[0-9]{2}|2100)\b', lambda m: m.group(), text)# Clean up problematic characters but keep essential punctuationtext = re.sub(r'[^\w\s\.,!?;:\-\(\)\'"]', ' ', text)text = re.sub(r'\s+', ' ', text)return text.strip()def number_to_words(self, num):"""Convert numbers to words"""if num == 0:return "zero"# Keep larger numbers as digits to avoid very long textif num > 9999:return str(num)ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen"]tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]if num < 10:return ones[num]elif num < 20:1 unmodified lineelif num < 100:return tens[num // 10] + ("" if num % 10 == 0 else " " + ones[num % 10])elif num < 1000:return ones[num // 100] + " hundred" + ("" if num % 100 == 0 else " " + self.number_to_words(num % 100))else:thousands = num // 1000remainder = num % 10002 unmodified linesresult += " " + self.number_to_words(remainder)return resultdef chunk_text(self, text, max_length=400):"""Split text into manageable chunks"""sentences = sent_tokenize(text)chunks = []current_chunk = ""for sentence in sentences:sentence = sentence.strip()if not sentence:continue# If adding this sentence would exceed limitif len(current_chunk + " " + sentence) > max_length:# Save current chunk if it existsif current_chunk:chunks.append(current_chunk.strip())# If single sentence is too long, split itif len(sentence) > max_length:words = sentence.split()temp_chunk = ""for word in words:if len(temp_chunk + " " + word) > max_length:if temp_chunk:chunks.append(temp_chunk.strip())temp_chunk = wordelse:# Single word too long, just add itchunks.append(word)else:temp_chunk = temp_chunk + " " + word if temp_chunk else wordcurrent_chunk = temp_chunkelse:current_chunk = sentenceelse:current_chunk = current_chunk + " " + sentence if current_chunk else sentence# Add the last chunkif current_chunk:chunks.append(current_chunk.strip())return [chunk for chunk in chunks if chunk.strip()]def generate_speech_chunk(self, text_chunk):"""Generate speech for a single chunk"""try:# Process text through the modelinputs = self.processor(text=text_chunk, return_tensors="pt").to(self.device)with torch.no_grad():speech = self.model.generate_speech(inputs["input_ids"],self.speaker_embeddings,vocoder=self.vocoder)# Convert to numpy and move to CPUif isinstance(speech, torch.Tensor):speech = speech.cpu().numpy()return speechexcept Exception as e:print(f"Error generating speech for chunk: {e}")print(f"Chunk text: {text_chunk}")return Nonedef generate_long_speech(self, text, progress_callback=None):"""Generate speech for long text"""# Preprocess textprocessed_text = self.preprocess_text(text)print(f"Original length: {len(text)}, Processed length: {len(processed_text)}")# Split into chunkschunks = self.chunk_text(processed_text)print(f"Split into {len(chunks)} chunks")if not chunks:return None, None# Generate speech for each chunkaudio_segments = []sample_rate = 16000 # SpeechT5 uses 16kHzfor i, chunk in enumerate(chunks):if progress_callback:progress_callback(f"Processing chunk {i+1}/{len(chunks)}: {chunk[:40]}{'...' if len(chunk) > 40 else ''}")print(f"Processing chunk {i+1}: {chunk}")audio_chunk = self.generate_speech_chunk(chunk)if audio_chunk is not None and len(audio_chunk) > 0:# Ensure audio is 1Dif len(audio_chunk.shape) > 1:audio_chunk = np.mean(audio_chunk, axis=0)audio_segments.append(audio_chunk)# Add pause between chunks (400ms)pause_samples = int(0.4 * sample_rate)silence = np.zeros(pause_samples)audio_segments.append(silence)# Small delay to prevent overwhelming the systemtime.sleep(0.1)if not audio_segments:return None, None# Concatenate all segmentsfinal_audio = np.concatenate(audio_segments)# Normalize audio to prevent clippingmax_val = np.max(np.abs(final_audio))if max_val > 0:final_audio = final_audio / max_val * 0.95return final_audio, sample_rate# Global TTS system6 unmodified linestts_system = Nonedef text_to_speech_interface(text, progress=gr.Progress()):"""Main Gradio interface function"""if tts_system is None:return None, "β TTS system is not available. Please check the logs."if not text or not text.strip():return None, "β οΈ Please enter some text to convert to speech."# Text length checkif len(text) > 50000:return None, "β οΈ Text is too long. Please keep it under 50,000 characters for optimal performance."def progress_callback(message):progress(0.5, desc=message)try:progress(0.1, desc="π Starting text-to-speech conversion...")# Generate audioaudio, sample_rate = tts_system.generate_long_speech(text, progress_callback)if audio is None or len(audio) == 0:return None, "β Failed to generate audio. Please try with different text."progress(0.9, desc="πΎ Saving audio file...")# Save to temporary filewith tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:sf.write(tmp_file.name, audio, sample_rate)audio_path = tmp_file.nameprogress(1.0, desc="β Complete!")duration = len(audio) / sample_ratereturn audio_path, f"β Generated {duration:.1f} seconds of audio successfully!"except Exception as e:error_msg = f"β Error: {str(e)}"print(f"TTS Error: {e}")return None, error_msg# Create Gradio interfacedef create_interface():with gr.Blocks(title="π€ Long-Form Text-to-Speech",theme=gr.themes.Soft(),css=""".main-header {4 unmodified lines-webkit-text-fill-color: transparent;background-clip: text;}.feature-box {background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);color: white;padding: 1.5rem;border-radius: 15px;margin: 1rem 0;box-shadow: 0 4px 15px rgba(0,0,0,0.1);}.status-box {padding: 1rem;border-radius: 10px;margin: 1rem 0;border-left: 4px solid #28a745;background: #f8f9fa;}""") as demo:gr.HTML("""<div class="main-header"><h1>π€ Long-Form Text-to-Speech Generator</h1><p style="color: #666; font-size: 1.1em;">Transform any text into natural human-like speech using advanced AI</p></div>""")# System statusif tts_system:gr.HTML("""<div class="status-box"><h4>π’ System Ready</h4><p>Using <strong>Microsoft SpeechT5</strong> - High quality neural text-to-speech</p></div>""")else:gr.HTML("""<div class="status-box" style="border-left-color: #dc3545; background: #f8d7da;"><h4>π΄ System Error</h4><p>TTS system failed to initialize. Please refresh the page.</p></div>8 unmodified linesinfo="Supports any length text with automatic chunking for optimal quality")char_count = gr.HTML("<span style='color: #666;'>Character count: 0 / 50,000</span>")generate_btn = gr.Button("π― Generate Speech",variant="primary",size="lg",scale=1)with gr.Column(scale=1):gr.HTML("""<div class="feature-box"><h3>β¨ Key Features</h3><ul style="margin: 0; padding-left: 1.2em;"><li>π Handles long texts</li><li>π Natural human voice</li><li>β‘ Smart text processing</li><li>π§ Auto chunking</li><li>π Completely free</li><li>π± Mobile friendly</li><li>π΅ High quality audio</li></ul></div>""")# Status and outputstatus_output = gr.Textbox(label="π Status",interactive=False,value="Ready to generate speech! Enter some text above.")audio_output = gr.Audio(label="π Generated Speech",type="filepath",show_download_button=True)# Character counterdef update_char_count(text):count = len(text) if text else 0color = "#28a745" if count <= 50000 else "#dc3545"return f'<span style="color: {color};">Character count: {count:,} / 50,000</span>'text_input.change(fn=update_char_count,inputs=[text_input],outputs=[char_count])# Generate button clickgenerate_btn.click(fn=text_to_speech_interface,inputs=[text_input],outputs=[audio_output, status_output],show_progress=True)# Example textsgr.Examples(examples=[["Hello! Welcome to our advanced text-to-speech system. This technology can convert any written text into natural-sounding human speech."],["The quick brown fox jumps over the lazy dog. This pangram contains every letter of the English alphabet and is perfect for testing speech synthesis."],["In the beginning was the Word, and the Word was with God, and the Word was God. This famous opening from the Gospel of John demonstrates the power of language."],["Artificial intelligence has revolutionized many aspects of our daily lives. From voice assistants to recommendation systems, AI technologies are becoming increasingly sophisticated and accessible to everyone."],["Once upon a time, in a land far away, there lived a wise old wizard who possessed the power to transform written words into spoken language. This magical ability brought stories to life for all who listened."]],inputs=[text_input],label="π Try These Examples")# Information sectiongr.HTML("""<div style="margin-top: 2rem; padding: 1.5rem; background: #f8f9fa; border-radius: 10px; border-left: 4px solid #007bff;"><h4>π§ How It Works</h4><ol style="margin: 0.5rem 0; padding-left: 1.5rem;"><li><strong>Text Processing:</strong> Automatically cleans and normalizes your input text</li><li><strong>Smart Chunking:</strong> Splits long text at natural sentence boundaries</li><li><strong>Neural Synthesis:</strong> Uses Microsoft's SpeechT5 model for speech generation</li><li><strong>Audio Assembly:</strong> Combines all chunks with natural pauses</li></ol><h4 style="margin-top: 1rem;">π‘ Tips for Best Results</h4><ul style="margin: 0.5rem 0; padding-left: 1.5rem;"><li>Use proper punctuation for natural pauses and intonation</li><li>Spell out abbreviations if you want them pronounced fully</li><li>Well-formatted text produces the most natural speech</li><li>The system automatically handles common abbreviations and numbers</li></ul></div>""")return demo# Launch the applicationif __name__ == "__main__":demo = create_interface()demo.launch(server_name="0.0.0.0",server_port=7860,share=True,ssr_mode=False)12343 unmodified lines891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768691 unmodified line717273747576772 unmodified lines80818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321336 unmodified lines1401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751764 unmodified lines1811821831841851861871881891901911921931941951961971981992002012022032048 unmodified lines213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264import gradio as grimport numpy as npimport reimport soundfile as sf3 unmodified linesfrom nltk.tokenize import sent_tokenizeimport warningsimport timefrom TTS.api import TTSwarnings.filterwarnings("ignore")# Download required NLTK datatry:nltk.data.find('tokenizers/punkt')except LookupError:nltk.download('punkt')# Load Coqui TTS modelprint("π Loading Coqui TTS models...")try:# This will download the model automatically if not foundtts = TTS(model_name="tts_models/en/vctk/vits", progress_bar=False, gpu=False)print("β Coqui TTS loaded successfully!")speakers = tts.speakersprint(f"Available Speakers: {speakers}")except Exception as e:print(f"β Failed to load Coqui TTS: {e}")tts = Nonespeakers = []class LongFormTTS:def __init__(self):self.tts = ttsself.speakers = speakersself.sample_rate = 22050 # Coqui default sample ratedef preprocess_text(self, text):"""Clean and prepare text for TTS"""text = re.sub(r'\s+', ' ', text.strip())abbreviations = {'Dr.': 'Doctor', 'Mr.': 'Mister', 'Mrs.': 'Missus','Ms.': 'Miss', 'Prof.': 'Professor', 'etc.': 'etcetera','vs.': 'versus', 'e.g.': 'for example', 'i.e.': 'that is','St.': 'Street', 'Ave.': 'Avenue', 'Blvd.': 'Boulevard','Inc.': 'Incorporated', 'Corp.': 'Corporation', 'Ltd.': 'Limited','U.S.': 'United States', 'U.K.': 'United Kingdom','Ph.D.': 'PhD', 'M.D.': 'MD'}for abbr, full in abbreviations.items():text = text.replace(abbr, full)text = re.sub(r'\b(\d{1,4})\b', lambda m: self.number_to_words(int(m.group())), text)text = re.sub(r'\b(1[0-9]{3}|20[0-9]{2}|2100)\b', lambda m: m.group(), text)text = re.sub(r'[^\w\s\.,!?;:\-\(\)\'"]', ' ', text)return text.strip()def number_to_words(self, num):ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen","sixteen", "seventeen", "eighteen", "nineteen"]tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy","eighty", "ninety"]if num == 0:return "zero"if num < 10:return ones[num]elif num < 20:1 unmodified lineelif num < 100:return tens[num // 10] + ("" if num % 10 == 0 else " " + ones[num % 10])elif num < 1000:return ones[num // 100] + " hundred" + (" " + self.number_to_words(num % 100)).strip()else:thousands = num // 1000remainder = num % 10002 unmodified linesresult += " " + self.number_to_words(remainder)return resultdef chunk_text(self, text, max_length=200):sentences = sent_tokenize(text)chunks = []current_chunk = ""for sentence in sentences:if len(current_chunk + " " + sentence) > max_length:if current_chunk:chunks.append(current_chunk.strip())current_chunk = sentenceelse:current_chunk = current_chunk + " " + sentence if current_chunk else sentenceif current_chunk:chunks.append(current_chunk.strip())return [chunk for chunk in chunks if chunk.strip()]def generate_speech_chunk(self, text_chunk, speaker):try:audio = self.tts.tts(text=text_chunk, speaker=speaker)return audioexcept Exception as e:print(f"Error generating speech for chunk: {e}")return Nonedef generate_long_speech(self, text, speaker=None, progress_callback=None):processed_text = self.preprocess_text(text)chunks = self.chunk_text(processed_text)print(f"Split into {len(chunks)} chunks")audio_segments = []silence = np.zeros(int(0.4 * self.sample_rate), dtype=np.float32)for i, chunk in enumerate(chunks):if progress_callback:progress_callback(f"Processing chunk {i+1}/{len(chunks)}: {chunk[:40]}{'...' if len(chunk) > 40 else ''}")print(f"Processing chunk {i+1}: {chunk}")audio_chunk = self.generate_speech_chunk(chunk, speaker)if audio_chunk is not None:audio_segments.append(np.array(audio_chunk))audio_segments.append(silence)time.sleep(0.1)if not audio_segments:return None, Nonefinal_audio = np.concatenate(audio_segments)max_val = np.max(np.abs(final_audio))if max_val > 0:final_audio = final_audio / max_val * 0.95return final_audio, self.sample_rate# Global TTS system6 unmodified linestts_system = Nonedef text_to_speech_interface(text, speaker="p225", progress=gr.Progress()):if tts_system is None:return None, "β TTS system is not available. Please check the logs."if not text or not text.strip():return None, "β οΈ Please enter some text to convert to speech."if len(text) > 50000:return None, "β οΈ Text is too long. Please keep it under 50,000 characters."def progress_callback(message):progress(0.5, desc=message)try:progress(0.1, desc="π Starting text-to-speech conversion...")audio, sample_rate = tts_system.generate_long_speech(text, speaker, progress_callback)if audio is None:return None, "β Failed to generate audio."progress(0.9, desc="πΎ Saving audio file...")with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:sf.write(tmp_file.name, audio, sample_rate)audio_path = tmp_file.nameprogress(1.0, desc="β Complete!")duration = len(audio) / sample_ratereturn audio_path, f"β Generated {duration:.1f} seconds of audio successfully!"except Exception as e:return None, f"β Error: {str(e)}"# Create Gradio interfacedef create_interface():with gr.Blocks(title="π€ Long-Form Text-to-Speech (Coqui)",theme=gr.themes.Soft(),css=""".main-header {4 unmodified lines-webkit-text-fill-color: transparent;background-clip: text;}""") as demo:gr.HTML("""<div class="main-header"><h1>π€ Long-Form Text-to-Speech Generator</h1><p style="color: #666; font-size: 1.1em;">Choose a voice and transform any text into expressive human-like speech</p></div>""")if tts_system:gr.HTML("""<div style="padding: 1rem; border-radius: 10px; margin: 1rem 0; border-left: 4px solid #28a745; background: #f8f9fa;"><h4>π’ System Ready</h4><p>Using <strong>Coqui TTS</strong> with multiple speaker support</p></div>""")else:gr.HTML("""<div style="padding: 1rem; border-radius: 10px; margin: 1rem 0; border-left: 4px solid #dc3545; background: #f8d7da;"><h4>π΄ System Error</h4><p>TTS system failed to initialize. Please refresh the page.</p></div>8 unmodified linesinfo="Supports any length text with automatic chunking for optimal quality")char_count = gr.HTML("<span style='color: #666;'>Character count: 0 / 50,000</span>")speaker_dropdown = gr.Dropdown(choices=tts_system.speakers if tts_system else [], value="p225", label="π£οΈ Choose Voice")generate_btn = gr.Button("π― Generate Speech", variant="primary", size="lg", scale=1)with gr.Column(scale=1):gr.HTML("""<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 1.5rem; border-radius: 15px; margin: 1rem 0; box-shadow: 0 4px 15px rgba(0,0,0,0.1);"><h3>β¨ Key Features</h3><ul style="margin: 0; padding-left: 1.2em;"><li>π Handles long texts</li><li>π Multiple human voices</li><li>β‘ Smart text processing</li><li>π§ Auto chunking</li><li>π΅ Natural-sounding speech</li></ul></div>""")status_output = gr.Textbox(label="π Status", interactive=False, value="Ready to generate speech! Enter some text above.")audio_output = gr.Audio(label="π Generated Speech", type="filepath", show_download_button=True)def update_char_count(text):count = len(text) if text else 0color = "#28a745" if count <= 50000 else "#dc3545"return f'<span style="color: {color};">Character count: {count:,} / 50,000</span>'text_input.change(fn=update_char_count, inputs=[text_input], outputs=[char_count])generate_btn.click(fn=text_to_speech_interface,inputs=[text_input, speaker_dropdown],outputs=[audio_output, status_output],show_progress=True)gr.Examples(examples=[["Hello! Welcome to our advanced text-to-speech system. This technology can convert any written text into natural-sounding human speech.", "p225"],["The quick brown fox jumps over the lazy dog. This pangram contains every letter of the English alphabet and is perfect for testing speech synthesis.", "p226"],["Artificial intelligence has revolutionized many aspects of our daily lives.", "p227"],],inputs=[text_input, speaker_dropdown],label="π Try These Examples")return demo# Launch the applicationif __name__ == "__main__":demo = create_interface()demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
app.py+75-217