Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,8 +34,8 @@ except Exception as e:
|
|
| 34 |
class LongFormTTS:
|
| 35 |
def __init__(self):
|
| 36 |
self.tts = tts
|
| 37 |
-
self.speakers = speakers #
|
| 38 |
-
self.sample_rate = 22050 #
|
| 39 |
|
| 40 |
def preprocess_text(self, text):
|
| 41 |
"""Clean and prepare text for TTS"""
|
|
@@ -54,7 +54,6 @@ class LongFormTTS:
|
|
| 54 |
text = re.sub(r'\b(\d{1,4})\b', lambda m: self.number_to_words(int(m.group())), text)
|
| 55 |
text = re.sub(r'\b(1[0-9]{3}|20[0-9]{2}|2100)\b', lambda m: m.group(), text)
|
| 56 |
text = re.sub(r'[^\w\s\.,!?;:\-\(\)\'"]', ' ', text)
|
| 57 |
-
text = re.sub(r'\s+', ' ', text)
|
| 58 |
return text.strip()
|
| 59 |
|
| 60 |
def number_to_words(self, num):
|
|
@@ -158,7 +157,7 @@ def text_to_speech_interface(text, speaker="p225", progress=gr.Progress()):
|
|
| 158 |
if audio is None:
|
| 159 |
return None, "β Failed to generate audio."
|
| 160 |
progress(0.9, desc="πΎ Saving audio file...")
|
| 161 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".
|
| 162 |
sf.write(tmp_file.name, audio, sample_rate)
|
| 163 |
audio_path = tmp_file.name
|
| 164 |
progress(1.0, desc="β
Complete!")
|
|
@@ -211,13 +210,13 @@ def create_interface():
|
|
| 211 |
placeholder="Type or paste your text here... (Max 50,000 characters)",
|
| 212 |
lines=10,
|
| 213 |
max_lines=20,
|
| 214 |
-
info="Supports any length text with automatic chunking
|
| 215 |
)
|
| 216 |
char_count = gr.HTML("<span style='color: #666;'>Character count: 0 / 50,000</span>")
|
| 217 |
speaker_dropdown = gr.Dropdown(
|
| 218 |
choices=tts_system.speakers if tts_system else [],
|
| 219 |
-
value=tts_system.speakers[0] if tts_system else None,
|
| 220 |
-
label="π£οΈ
|
| 221 |
)
|
| 222 |
generate_btn = gr.Button("π― Generate Speech", variant="primary", size="lg", scale=1)
|
| 223 |
with gr.Column(scale=1):
|
|
@@ -230,6 +229,7 @@ def create_interface():
|
|
| 230 |
<li>β‘ Smart text processing</li>
|
| 231 |
<li>π§ Auto chunking</li>
|
| 232 |
<li>π΅ Natural-sounding speech</li>
|
|
|
|
| 233 |
</ul>
|
| 234 |
</div>
|
| 235 |
""")
|
|
@@ -240,7 +240,7 @@ def create_interface():
|
|
| 240 |
count = len(text) if text else 0
|
| 241 |
color = "#28a745" if count <= 50000 else "#dc3545"
|
| 242 |
return f'<span style="color: {color};">Character count: {count:,} / 50,000</span>'
|
| 243 |
-
|
| 244 |
text_input.change(fn=update_char_count, inputs=[text_input], outputs=[char_count])
|
| 245 |
|
| 246 |
generate_btn.click(
|
|
@@ -252,9 +252,9 @@ def create_interface():
|
|
| 252 |
|
| 253 |
gr.Examples(
|
| 254 |
examples=[
|
| 255 |
-
["Hello! Welcome to our advanced text-to-speech system.
|
| 256 |
-
["The quick brown fox jumps over the lazy dog.
|
| 257 |
-
["Artificial intelligence has revolutionized many aspects of our
|
| 258 |
],
|
| 259 |
inputs=[text_input, speaker_dropdown],
|
| 260 |
label="π Try These Examples"
|
|
|
|
| 34 |
class LongFormTTS:
|
| 35 |
def __init__(self):
|
| 36 |
self.tts = tts
|
| 37 |
+
self.speakers = speakers or ["p225", "p226", "p227", "p228"] # fallback static list
|
| 38 |
+
self.sample_rate = 22050 # Default sample rate for Coqui TTS
|
| 39 |
|
| 40 |
def preprocess_text(self, text):
|
| 41 |
"""Clean and prepare text for TTS"""
|
|
|
|
| 54 |
text = re.sub(r'\b(\d{1,4})\b', lambda m: self.number_to_words(int(m.group())), text)
|
| 55 |
text = re.sub(r'\b(1[0-9]{3}|20[0-9]{2}|2100)\b', lambda m: m.group(), text)
|
| 56 |
text = re.sub(r'[^\w\s\.,!?;:\-\(\)\'"]', ' ', text)
|
|
|
|
| 57 |
return text.strip()
|
| 58 |
|
| 59 |
def number_to_words(self, num):
|
|
|
|
| 157 |
if audio is None:
|
| 158 |
return None, "β Failed to generate audio."
|
| 159 |
progress(0.9, desc="πΎ Saving audio file...")
|
| 160 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
| 161 |
sf.write(tmp_file.name, audio, sample_rate)
|
| 162 |
audio_path = tmp_file.name
|
| 163 |
progress(1.0, desc="β
Complete!")
|
|
|
|
| 210 |
placeholder="Type or paste your text here... (Max 50,000 characters)",
|
| 211 |
lines=10,
|
| 212 |
max_lines=20,
|
| 213 |
+
info="Supports any length text with automatic chunking"
|
| 214 |
)
|
| 215 |
char_count = gr.HTML("<span style='color: #666;'>Character count: 0 / 50,000</span>")
|
| 216 |
speaker_dropdown = gr.Dropdown(
|
| 217 |
choices=tts_system.speakers if tts_system else [],
|
| 218 |
+
value=tts_system.speakers[0] if tts_system and tts_system.speakers else None,
|
| 219 |
+
label="π£οΈ Select Voice"
|
| 220 |
)
|
| 221 |
generate_btn = gr.Button("π― Generate Speech", variant="primary", size="lg", scale=1)
|
| 222 |
with gr.Column(scale=1):
|
|
|
|
| 229 |
<li>β‘ Smart text processing</li>
|
| 230 |
<li>π§ Auto chunking</li>
|
| 231 |
<li>π΅ Natural-sounding speech</li>
|
| 232 |
+
<li>π MP3 audio output</li>
|
| 233 |
</ul>
|
| 234 |
</div>
|
| 235 |
""")
|
|
|
|
| 240 |
count = len(text) if text else 0
|
| 241 |
color = "#28a745" if count <= 50000 else "#dc3545"
|
| 242 |
return f'<span style="color: {color};">Character count: {count:,} / 50,000</span>'
|
| 243 |
+
|
| 244 |
text_input.change(fn=update_char_count, inputs=[text_input], outputs=[char_count])
|
| 245 |
|
| 246 |
generate_btn.click(
|
|
|
|
| 252 |
|
| 253 |
gr.Examples(
|
| 254 |
examples=[
|
| 255 |
+
["Hello! Welcome to our advanced text-to-speech system.", "p225"],
|
| 256 |
+
["The quick brown fox jumps over the lazy dog.", "p226"],
|
| 257 |
+
["Artificial intelligence has revolutionized many aspects of our lives.", "p227"],
|
| 258 |
],
|
| 259 |
inputs=[text_input, speaker_dropdown],
|
| 260 |
label="π Try These Examples"
|