Spaces:
Sleeping
Sleeping
Update app.py
Browse files33 unmodified lines343536373839404112 unmodified lines5455565758596097 unmodified lines15815916016116216316446 unmodified lines2112122132142152162172182192202212222236 unmodified lines2302312322332342354 unmodified lines2402412422432442452465 unmodified lines25225325425525625725825926033 unmodified linesclass LongFormTTS:def __init__(self):self.tts = ttsself.speakers = speakers # Store available speakersself.sample_rate = 22050 # Coqui default sample ratedef preprocess_text(self, text):"""Clean and prepare text for TTS"""12 unmodified linestext = 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)text = re.sub(r'\s+', ' ', text)return text.strip()def number_to_words(self, num):97 unmodified linesif 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!")46 unmodified linesplaceholder="Type or paste your text here... (Max 50,000 characters)",lines=10,max_lines=20,info="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=tts_system.speakers[0] if tts_system else None,label="π£οΈ Choose Voice")generate_btn = gr.Button("π― Generate Speech", variant="primary", size="lg", scale=1)with gr.Column(scale=1):6 unmodified lines<li>β‘ Smart text processing</li><li>π§ Auto chunking</li><li>π΅ Natural-sounding speech</li></ul></div>""")4 unmodified linescount = 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(5 unmodified linesgr.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"33 unmodified lines343536373839404112 unmodified lines54555657585997 unmodified lines15715815916016116216346 unmodified lines2102112122132142152162172182192202212226 unmodified lines2292302312322332342354 unmodified lines2402412422432442452465 unmodified lines25225325425525625725825926033 unmodified linesclass LongFormTTS:def __init__(self):self.tts = ttsself.speakers = speakers or ["p225", "p226", "p227", "p228"] # fallback static listself.sample_rate = 22050 # Default sample rate for Coqui TTSdef preprocess_text(self, text):"""Clean and prepare text for TTS"""12 unmodified linestext = 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):97 unmodified linesif audio is None:return None, "β Failed to generate audio."progress(0.9, desc="πΎ Saving audio file...")with 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!")46 unmodified linesplaceholder="Type or paste your text here... (Max 50,000 characters)",lines=10,max_lines=20,info="Supports any length text with automatic chunking")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=tts_system.speakers[0] if tts_system and tts_system.speakers else None,label="π£οΈ Select Voice")generate_btn = gr.Button("π― Generate Speech", variant="primary", size="lg", scale=1)with gr.Column(scale=1):6 unmodified lines<li>β‘ Smart text processing</li><li>π§ Auto chunking</li><li>π΅ Natural-sounding speech</li><li>π MP3 audio output</li></ul></div>""")4 unmodified linescount = 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(5 unmodified linesgr.Examples(examples=[["Hello! Welcome to our advanced text-to-speech system.", "p225"],["The quick brown fox jumps over the lazy dog.", "p226"],["Artificial intelligence has revolutionized many aspects of our lives.", "p227"],],inputs=[text_input, speaker_dropdown],label="π Try These Examples"
app.py+11-11