Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,16 +2,16 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
import re
|
| 5 |
-
from transformers import pipeline
|
| 6 |
import soundfile as sf
|
| 7 |
-
import io
|
| 8 |
import tempfile
|
| 9 |
import os
|
| 10 |
-
from pydub import AudioSegment
|
| 11 |
import nltk
|
| 12 |
from nltk.tokenize import sent_tokenize
|
| 13 |
import warnings
|
| 14 |
import time
|
|
|
|
|
|
|
|
|
|
| 15 |
warnings.filterwarnings("ignore")
|
| 16 |
|
| 17 |
# Download required NLTK data
|
|
@@ -22,48 +22,31 @@ except LookupError:
|
|
| 22 |
|
| 23 |
class LongFormTTS:
|
| 24 |
def __init__(self):
|
| 25 |
-
print("Loading TTS models...")
|
| 26 |
-
|
| 27 |
-
# Try multiple TTS approaches for better compatibility
|
| 28 |
-
self.tts_pipeline = None
|
| 29 |
-
self.backup_tts = None
|
| 30 |
|
| 31 |
-
# Primary: Try Bark (works well on HF Spaces)
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
)
|
| 39 |
-
self.tts_method = "bark"
|
| 40 |
-
print("β
Bark TTS loaded successfully!")
|
| 41 |
-
except Exception as e:
|
| 42 |
-
print(f"β Bark TTS failed: {e}")
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
from TTS.api import TTS
|
| 61 |
-
self.backup_tts = TTS(model_name="tts_models/en/ljspeech/fastspeech2")
|
| 62 |
-
self.tts_method = "fastspeech2"
|
| 63 |
-
print("β
FastSpeech2 loaded successfully!")
|
| 64 |
-
except Exception as e:
|
| 65 |
-
print(f"β All TTS models failed: {e}")
|
| 66 |
-
raise Exception("No TTS model could be loaded. Please check the requirements.")
|
| 67 |
|
| 68 |
def preprocess_text(self, text):
|
| 69 |
"""Clean and prepare text for TTS"""
|
|
@@ -73,7 +56,7 @@ class LongFormTTS:
|
|
| 73 |
# Handle common abbreviations
|
| 74 |
abbreviations = {
|
| 75 |
'Dr.': 'Doctor',
|
| 76 |
-
'Mr.': 'Mister',
|
| 77 |
'Mrs.': 'Missus',
|
| 78 |
'Ms.': 'Miss',
|
| 79 |
'Prof.': 'Professor',
|
|
@@ -87,26 +70,35 @@ class LongFormTTS:
|
|
| 87 |
'Inc.': 'Incorporated',
|
| 88 |
'Corp.': 'Corporation',
|
| 89 |
'Ltd.': 'Limited',
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
for abbr, full in abbreviations.items():
|
| 93 |
text = text.replace(abbr, full)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
text = re.sub(r'\b(\d
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
# Clean up
|
| 99 |
-
text = re.sub(r'[^\w\s\.,!?;:\-\(\)]', '', text)
|
|
|
|
| 100 |
|
| 101 |
-
return text
|
| 102 |
|
| 103 |
def number_to_words(self, num):
|
| 104 |
-
"""Convert numbers to words
|
| 105 |
if num == 0:
|
| 106 |
return "zero"
|
| 107 |
|
|
|
|
| 108 |
if num > 9999:
|
| 109 |
-
return str(num)
|
| 110 |
|
| 111 |
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
| 112 |
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
|
|
@@ -122,123 +114,116 @@ class LongFormTTS:
|
|
| 122 |
elif num < 1000:
|
| 123 |
return ones[num // 100] + " hundred" + ("" if num % 100 == 0 else " " + self.number_to_words(num % 100))
|
| 124 |
else:
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
def chunk_text(self, text, max_length=
|
| 128 |
-
"""Split text into manageable chunks
|
| 129 |
sentences = sent_tokenize(text)
|
| 130 |
chunks = []
|
| 131 |
current_chunk = ""
|
| 132 |
|
| 133 |
for sentence in sentences:
|
| 134 |
-
|
| 135 |
-
if
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
temp_chunk = word
|
| 152 |
-
else:
|
| 153 |
-
chunks.append(word)
|
| 154 |
-
else:
|
| 155 |
-
temp_chunk += " " + word if temp_chunk else word
|
| 156 |
if temp_chunk:
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
chunks.append(sentence)
|
| 167 |
else:
|
| 168 |
-
current_chunk
|
|
|
|
|
|
|
| 169 |
|
|
|
|
| 170 |
if current_chunk:
|
| 171 |
chunks.append(current_chunk.strip())
|
| 172 |
|
| 173 |
return [chunk for chunk in chunks if chunk.strip()]
|
| 174 |
|
| 175 |
def generate_speech_chunk(self, text_chunk):
|
| 176 |
-
"""Generate speech for a single
|
| 177 |
try:
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
|
| 195 |
-
self.backup_tts.tts_to_file(text=text_chunk, file_path=tmp_file.name)
|
| 196 |
-
audio, sr = sf.read(tmp_file.name)
|
| 197 |
-
os.unlink(tmp_file.name)
|
| 198 |
-
return audio, sr
|
| 199 |
-
else:
|
| 200 |
-
raise Exception("No TTS method available")
|
| 201 |
-
|
| 202 |
except Exception as e:
|
| 203 |
print(f"Error generating speech for chunk: {e}")
|
| 204 |
-
|
|
|
|
| 205 |
|
| 206 |
def generate_long_speech(self, text, progress_callback=None):
|
| 207 |
-
"""Generate speech for long text
|
| 208 |
# Preprocess text
|
| 209 |
-
|
|
|
|
| 210 |
|
| 211 |
# Split into chunks
|
| 212 |
-
chunks = self.chunk_text(
|
| 213 |
-
print(f"Split
|
| 214 |
|
| 215 |
if not chunks:
|
| 216 |
return None, None
|
| 217 |
|
| 218 |
# Generate speech for each chunk
|
| 219 |
audio_segments = []
|
| 220 |
-
|
| 221 |
-
total_chunks = len(chunks)
|
| 222 |
|
| 223 |
for i, chunk in enumerate(chunks):
|
| 224 |
if progress_callback:
|
| 225 |
-
progress_callback(f"Processing chunk {i+1}/{
|
| 226 |
|
| 227 |
-
|
|
|
|
| 228 |
|
| 229 |
if audio_chunk is not None and len(audio_chunk) > 0:
|
| 230 |
-
if sampling_rate is None:
|
| 231 |
-
sampling_rate = sr
|
| 232 |
-
|
| 233 |
# Ensure audio is 1D
|
| 234 |
if len(audio_chunk.shape) > 1:
|
| 235 |
-
audio_chunk =
|
| 236 |
|
| 237 |
audio_segments.append(audio_chunk)
|
| 238 |
|
| 239 |
-
# Add
|
| 240 |
-
|
| 241 |
-
silence = np.zeros(
|
| 242 |
audio_segments.append(silence)
|
| 243 |
|
| 244 |
# Small delay to prevent overwhelming the system
|
|
@@ -247,81 +232,94 @@ class LongFormTTS:
|
|
| 247 |
if not audio_segments:
|
| 248 |
return None, None
|
| 249 |
|
| 250 |
-
# Concatenate all
|
| 251 |
final_audio = np.concatenate(audio_segments)
|
| 252 |
|
| 253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
|
| 255 |
-
#
|
| 256 |
-
print("Initializing TTS system...")
|
| 257 |
try:
|
| 258 |
tts_system = LongFormTTS()
|
| 259 |
-
print("β
TTS system
|
| 260 |
except Exception as e:
|
| 261 |
-
print(f"β
|
| 262 |
tts_system = None
|
| 263 |
|
| 264 |
def text_to_speech_interface(text, progress=gr.Progress()):
|
| 265 |
-
"""Main interface function
|
| 266 |
if tts_system is None:
|
| 267 |
-
return None, "β TTS system not available. Please check the logs."
|
| 268 |
|
| 269 |
-
if not text.strip():
|
| 270 |
-
return None, "Please enter some text to convert to speech."
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
|
|
|
| 274 |
|
| 275 |
def progress_callback(message):
|
| 276 |
progress(0.5, desc=message)
|
| 277 |
|
| 278 |
try:
|
| 279 |
-
progress(0.1, desc="Starting text-to-speech conversion...")
|
| 280 |
|
|
|
|
| 281 |
audio, sample_rate = tts_system.generate_long_speech(text, progress_callback)
|
| 282 |
|
| 283 |
if audio is None or len(audio) == 0:
|
| 284 |
-
return None, "Failed to generate audio. Please try with
|
| 285 |
|
| 286 |
-
progress(0.9, desc="
|
| 287 |
|
| 288 |
# Save to temporary file
|
| 289 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 290 |
sf.write(tmp_file.name, audio, sample_rate)
|
| 291 |
audio_path = tmp_file.name
|
| 292 |
|
| 293 |
-
progress(1.0, desc="Complete!")
|
| 294 |
|
| 295 |
duration = len(audio) / sample_rate
|
| 296 |
-
return audio_path, f"β
|
| 297 |
|
| 298 |
except Exception as e:
|
| 299 |
error_msg = f"β Error: {str(e)}"
|
| 300 |
-
print(
|
| 301 |
return None, error_msg
|
| 302 |
|
| 303 |
# Create Gradio interface
|
| 304 |
def create_interface():
|
| 305 |
with gr.Blocks(
|
| 306 |
-
title="π€ Long-Form Text-to-Speech
|
| 307 |
theme=gr.themes.Soft(),
|
| 308 |
css="""
|
| 309 |
.main-header {
|
| 310 |
text-align: center;
|
| 311 |
margin-bottom: 2rem;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
}
|
| 313 |
.feature-box {
|
| 314 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 315 |
color: white;
|
| 316 |
-
padding:
|
| 317 |
-
border-radius:
|
| 318 |
margin: 1rem 0;
|
|
|
|
| 319 |
}
|
| 320 |
.status-box {
|
| 321 |
-
background: #f8f9fa;
|
| 322 |
-
border-left: 4px solid #007bff;
|
| 323 |
padding: 1rem;
|
|
|
|
| 324 |
margin: 1rem 0;
|
|
|
|
|
|
|
| 325 |
}
|
| 326 |
"""
|
| 327 |
) as demo:
|
|
@@ -329,76 +327,79 @@ def create_interface():
|
|
| 329 |
gr.HTML("""
|
| 330 |
<div class="main-header">
|
| 331 |
<h1>π€ Long-Form Text-to-Speech Generator</h1>
|
| 332 |
-
<p>
|
| 333 |
</div>
|
| 334 |
""")
|
| 335 |
|
| 336 |
-
#
|
| 337 |
-
if tts_system
|
| 338 |
-
|
| 339 |
<div class="status-box">
|
| 340 |
-
<h4>π’ System
|
| 341 |
-
<p>Using <strong>
|
| 342 |
</div>
|
| 343 |
-
"""
|
| 344 |
else:
|
| 345 |
-
|
| 346 |
-
<div class="status-box" style="border-left-color: #dc3545;">
|
| 347 |
-
<h4>π΄ System
|
| 348 |
-
<p>TTS system failed to initialize. Please
|
| 349 |
</div>
|
| 350 |
-
"""
|
| 351 |
-
|
| 352 |
-
gr.HTML(status_html)
|
| 353 |
|
| 354 |
with gr.Row():
|
| 355 |
with gr.Column(scale=2):
|
| 356 |
text_input = gr.Textbox(
|
| 357 |
-
label="π Enter
|
| 358 |
-
placeholder="Type or paste your text here...",
|
| 359 |
-
lines=
|
| 360 |
-
max_lines=
|
|
|
|
| 361 |
)
|
| 362 |
|
| 363 |
-
char_count = gr.HTML("Character count: 0")
|
| 364 |
|
| 365 |
generate_btn = gr.Button(
|
| 366 |
"π― Generate Speech",
|
| 367 |
variant="primary",
|
| 368 |
-
size="lg"
|
|
|
|
| 369 |
)
|
| 370 |
|
| 371 |
with gr.Column(scale=1):
|
| 372 |
gr.HTML("""
|
| 373 |
<div class="feature-box">
|
| 374 |
-
<h3>β¨ Features</h3>
|
| 375 |
-
<ul>
|
| 376 |
-
<li>π
|
| 377 |
-
<li
|
| 378 |
-
<li>β‘ Smart text
|
|
|
|
| 379 |
<li>π Completely free</li>
|
| 380 |
-
<li>π§ Auto preprocessing</li>
|
| 381 |
<li>π± Mobile friendly</li>
|
|
|
|
| 382 |
</ul>
|
| 383 |
</div>
|
| 384 |
""")
|
| 385 |
|
| 386 |
-
|
|
|
|
| 387 |
label="π Status",
|
| 388 |
interactive=False,
|
| 389 |
-
value="Ready to generate speech!"
|
| 390 |
)
|
| 391 |
|
| 392 |
audio_output = gr.Audio(
|
| 393 |
label="π Generated Speech",
|
| 394 |
-
type="filepath"
|
|
|
|
| 395 |
)
|
| 396 |
|
| 397 |
# Character counter
|
| 398 |
def update_char_count(text):
|
| 399 |
-
count = len(text)
|
| 400 |
-
color = "
|
| 401 |
-
return f'<span style="color: {color};">Character count: {count}/
|
| 402 |
|
| 403 |
text_input.change(
|
| 404 |
fn=update_char_count,
|
|
@@ -406,40 +407,51 @@ def create_interface():
|
|
| 406 |
outputs=[char_count]
|
| 407 |
)
|
| 408 |
|
| 409 |
-
#
|
| 410 |
generate_btn.click(
|
| 411 |
fn=text_to_speech_interface,
|
| 412 |
inputs=[text_input],
|
| 413 |
-
outputs=[audio_output,
|
|
|
|
| 414 |
)
|
| 415 |
|
| 416 |
# Example texts
|
| 417 |
gr.Examples(
|
| 418 |
examples=[
|
| 419 |
-
["Hello!
|
| 420 |
-
["The quick brown fox jumps over the lazy dog. This
|
| 421 |
-
["In
|
| 422 |
-
["
|
|
|
|
| 423 |
],
|
| 424 |
-
inputs=[text_input]
|
|
|
|
| 425 |
)
|
| 426 |
|
|
|
|
| 427 |
gr.HTML("""
|
| 428 |
-
<div style="margin-top: 2rem; padding:
|
| 429 |
-
<h4>π§ How
|
| 430 |
-
<ol>
|
| 431 |
-
<li><strong>
|
| 432 |
-
<li><strong>Smart Chunking:</strong> Splits long text at natural boundaries</li>
|
| 433 |
-
<li><strong>
|
| 434 |
-
<li><strong>
|
| 435 |
</ol>
|
| 436 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
</div>
|
| 438 |
""")
|
| 439 |
|
| 440 |
return demo
|
| 441 |
|
| 442 |
-
# Launch the
|
| 443 |
if __name__ == "__main__":
|
| 444 |
demo = create_interface()
|
| 445 |
demo.launch(
|
|
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
import re
|
|
|
|
| 5 |
import soundfile as sf
|
|
|
|
| 6 |
import tempfile
|
| 7 |
import os
|
|
|
|
| 8 |
import nltk
|
| 9 |
from nltk.tokenize import sent_tokenize
|
| 10 |
import warnings
|
| 11 |
import time
|
| 12 |
+
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 13 |
+
from datasets import load_dataset
|
| 14 |
+
|
| 15 |
warnings.filterwarnings("ignore")
|
| 16 |
|
| 17 |
# Download required NLTK data
|
|
|
|
| 22 |
|
| 23 |
class LongFormTTS:
|
| 24 |
def __init__(self):
|
| 25 |
+
print("π Loading TTS models...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
| 27 |
try:
|
| 28 |
+
# Load SpeechT5 - most reliable for HF Spaces
|
| 29 |
+
print("Loading SpeechT5 TTS...")
|
| 30 |
+
self.processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")
|
| 31 |
+
self.model = SpeechT5ForTextToSpeech.from_pretrained("microsoft/speecht5_tts")
|
| 32 |
+
self.vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Load speaker embeddings
|
| 35 |
+
print("Loading speaker embeddings...")
|
| 36 |
+
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 37 |
+
# Use a different speaker embedding for more variety
|
| 38 |
+
self.speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
| 39 |
+
|
| 40 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
+
self.model = self.model.to(self.device)
|
| 42 |
+
self.vocoder = self.vocoder.to(self.device)
|
| 43 |
+
self.speaker_embeddings = self.speaker_embeddings.to(self.device)
|
| 44 |
+
|
| 45 |
+
print("β
SpeechT5 loaded successfully!")
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"β Failed to load SpeechT5: {e}")
|
| 49 |
+
raise Exception(f"TTS model loading failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def preprocess_text(self, text):
|
| 52 |
"""Clean and prepare text for TTS"""
|
|
|
|
| 56 |
# Handle common abbreviations
|
| 57 |
abbreviations = {
|
| 58 |
'Dr.': 'Doctor',
|
| 59 |
+
'Mr.': 'Mister',
|
| 60 |
'Mrs.': 'Missus',
|
| 61 |
'Ms.': 'Miss',
|
| 62 |
'Prof.': 'Professor',
|
|
|
|
| 70 |
'Inc.': 'Incorporated',
|
| 71 |
'Corp.': 'Corporation',
|
| 72 |
'Ltd.': 'Limited',
|
| 73 |
+
'U.S.': 'United States',
|
| 74 |
+
'U.K.': 'United Kingdom',
|
| 75 |
+
'Ph.D.': 'PhD',
|
| 76 |
+
'M.D.': 'MD',
|
| 77 |
}
|
| 78 |
|
| 79 |
for abbr, full in abbreviations.items():
|
| 80 |
text = text.replace(abbr, full)
|
| 81 |
|
| 82 |
+
# Convert numbers to words (enhanced)
|
| 83 |
+
text = re.sub(r'\b(\d{1,4})\b', lambda m: self.number_to_words(int(m.group())), text)
|
| 84 |
+
|
| 85 |
+
# Handle years differently (keep as numbers if between 1000-2100)
|
| 86 |
+
text = re.sub(r'\b(1[0-9]{3}|20[0-9]{2}|2100)\b', lambda m: m.group(), text)
|
| 87 |
|
| 88 |
+
# Clean up problematic characters but keep essential punctuation
|
| 89 |
+
text = re.sub(r'[^\w\s\.,!?;:\-\(\)\'"]', ' ', text)
|
| 90 |
+
text = re.sub(r'\s+', ' ', text)
|
| 91 |
|
| 92 |
+
return text.strip()
|
| 93 |
|
| 94 |
def number_to_words(self, num):
|
| 95 |
+
"""Convert numbers to words"""
|
| 96 |
if num == 0:
|
| 97 |
return "zero"
|
| 98 |
|
| 99 |
+
# Keep larger numbers as digits to avoid very long text
|
| 100 |
if num > 9999:
|
| 101 |
+
return str(num)
|
| 102 |
|
| 103 |
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
| 104 |
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
|
|
|
|
| 114 |
elif num < 1000:
|
| 115 |
return ones[num // 100] + " hundred" + ("" if num % 100 == 0 else " " + self.number_to_words(num % 100))
|
| 116 |
else:
|
| 117 |
+
thousands = num // 1000
|
| 118 |
+
remainder = num % 1000
|
| 119 |
+
result = self.number_to_words(thousands) + " thousand"
|
| 120 |
+
if remainder > 0:
|
| 121 |
+
result += " " + self.number_to_words(remainder)
|
| 122 |
+
return result
|
| 123 |
|
| 124 |
+
def chunk_text(self, text, max_length=400):
|
| 125 |
+
"""Split text into manageable chunks"""
|
| 126 |
sentences = sent_tokenize(text)
|
| 127 |
chunks = []
|
| 128 |
current_chunk = ""
|
| 129 |
|
| 130 |
for sentence in sentences:
|
| 131 |
+
sentence = sentence.strip()
|
| 132 |
+
if not sentence:
|
| 133 |
+
continue
|
| 134 |
+
|
| 135 |
+
# If adding this sentence would exceed limit
|
| 136 |
+
if len(current_chunk + " " + sentence) > max_length:
|
| 137 |
+
# Save current chunk if it exists
|
| 138 |
+
if current_chunk:
|
| 139 |
+
chunks.append(current_chunk.strip())
|
| 140 |
+
|
| 141 |
+
# If single sentence is too long, split it
|
| 142 |
+
if len(sentence) > max_length:
|
| 143 |
+
words = sentence.split()
|
| 144 |
+
temp_chunk = ""
|
| 145 |
+
|
| 146 |
+
for word in words:
|
| 147 |
+
if len(temp_chunk + " " + word) > max_length:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
if temp_chunk:
|
| 149 |
+
chunks.append(temp_chunk.strip())
|
| 150 |
+
temp_chunk = word
|
| 151 |
+
else:
|
| 152 |
+
# Single word too long, just add it
|
| 153 |
+
chunks.append(word)
|
| 154 |
+
else:
|
| 155 |
+
temp_chunk = temp_chunk + " " + word if temp_chunk else word
|
| 156 |
+
|
| 157 |
+
current_chunk = temp_chunk
|
|
|
|
| 158 |
else:
|
| 159 |
+
current_chunk = sentence
|
| 160 |
+
else:
|
| 161 |
+
current_chunk = current_chunk + " " + sentence if current_chunk else sentence
|
| 162 |
|
| 163 |
+
# Add the last chunk
|
| 164 |
if current_chunk:
|
| 165 |
chunks.append(current_chunk.strip())
|
| 166 |
|
| 167 |
return [chunk for chunk in chunks if chunk.strip()]
|
| 168 |
|
| 169 |
def generate_speech_chunk(self, text_chunk):
|
| 170 |
+
"""Generate speech for a single chunk"""
|
| 171 |
try:
|
| 172 |
+
# Process text through the model
|
| 173 |
+
inputs = self.processor(text=text_chunk, return_tensors="pt").to(self.device)
|
| 174 |
+
|
| 175 |
+
with torch.no_grad():
|
| 176 |
+
speech = self.model.generate_speech(
|
| 177 |
+
inputs["input_ids"],
|
| 178 |
+
self.speaker_embeddings,
|
| 179 |
+
vocoder=self.vocoder
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
# Convert to numpy and move to CPU
|
| 183 |
+
if isinstance(speech, torch.Tensor):
|
| 184 |
+
speech = speech.cpu().numpy()
|
| 185 |
+
|
| 186 |
+
return speech
|
| 187 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
except Exception as e:
|
| 189 |
print(f"Error generating speech for chunk: {e}")
|
| 190 |
+
print(f"Chunk text: {text_chunk}")
|
| 191 |
+
return None
|
| 192 |
|
| 193 |
def generate_long_speech(self, text, progress_callback=None):
|
| 194 |
+
"""Generate speech for long text"""
|
| 195 |
# Preprocess text
|
| 196 |
+
processed_text = self.preprocess_text(text)
|
| 197 |
+
print(f"Original length: {len(text)}, Processed length: {len(processed_text)}")
|
| 198 |
|
| 199 |
# Split into chunks
|
| 200 |
+
chunks = self.chunk_text(processed_text)
|
| 201 |
+
print(f"Split into {len(chunks)} chunks")
|
| 202 |
|
| 203 |
if not chunks:
|
| 204 |
return None, None
|
| 205 |
|
| 206 |
# Generate speech for each chunk
|
| 207 |
audio_segments = []
|
| 208 |
+
sample_rate = 16000 # SpeechT5 uses 16kHz
|
|
|
|
| 209 |
|
| 210 |
for i, chunk in enumerate(chunks):
|
| 211 |
if progress_callback:
|
| 212 |
+
progress_callback(f"Processing chunk {i+1}/{len(chunks)}: {chunk[:40]}{'...' if len(chunk) > 40 else ''}")
|
| 213 |
|
| 214 |
+
print(f"Processing chunk {i+1}: {chunk}")
|
| 215 |
+
audio_chunk = self.generate_speech_chunk(chunk)
|
| 216 |
|
| 217 |
if audio_chunk is not None and len(audio_chunk) > 0:
|
|
|
|
|
|
|
|
|
|
| 218 |
# Ensure audio is 1D
|
| 219 |
if len(audio_chunk.shape) > 1:
|
| 220 |
+
audio_chunk = np.mean(audio_chunk, axis=0)
|
| 221 |
|
| 222 |
audio_segments.append(audio_chunk)
|
| 223 |
|
| 224 |
+
# Add pause between chunks (400ms)
|
| 225 |
+
pause_samples = int(0.4 * sample_rate)
|
| 226 |
+
silence = np.zeros(pause_samples)
|
| 227 |
audio_segments.append(silence)
|
| 228 |
|
| 229 |
# Small delay to prevent overwhelming the system
|
|
|
|
| 232 |
if not audio_segments:
|
| 233 |
return None, None
|
| 234 |
|
| 235 |
+
# Concatenate all segments
|
| 236 |
final_audio = np.concatenate(audio_segments)
|
| 237 |
|
| 238 |
+
# Normalize audio to prevent clipping
|
| 239 |
+
max_val = np.max(np.abs(final_audio))
|
| 240 |
+
if max_val > 0:
|
| 241 |
+
final_audio = final_audio / max_val * 0.95
|
| 242 |
+
|
| 243 |
+
return final_audio, sample_rate
|
| 244 |
|
| 245 |
+
# Global TTS system
|
| 246 |
+
print("π Initializing TTS system...")
|
| 247 |
try:
|
| 248 |
tts_system = LongFormTTS()
|
| 249 |
+
print("β
TTS system ready!")
|
| 250 |
except Exception as e:
|
| 251 |
+
print(f"β TTS initialization failed: {e}")
|
| 252 |
tts_system = None
|
| 253 |
|
| 254 |
def text_to_speech_interface(text, progress=gr.Progress()):
|
| 255 |
+
"""Main Gradio interface function"""
|
| 256 |
if tts_system is None:
|
| 257 |
+
return None, "β TTS system is not available. Please check the logs."
|
| 258 |
|
| 259 |
+
if not text or not text.strip():
|
| 260 |
+
return None, "β οΈ Please enter some text to convert to speech."
|
| 261 |
|
| 262 |
+
# Text length check
|
| 263 |
+
if len(text) > 5000:
|
| 264 |
+
return None, "β οΈ Text is too long. Please keep it under 5,000 characters for optimal performance."
|
| 265 |
|
| 266 |
def progress_callback(message):
|
| 267 |
progress(0.5, desc=message)
|
| 268 |
|
| 269 |
try:
|
| 270 |
+
progress(0.1, desc="π Starting text-to-speech conversion...")
|
| 271 |
|
| 272 |
+
# Generate audio
|
| 273 |
audio, sample_rate = tts_system.generate_long_speech(text, progress_callback)
|
| 274 |
|
| 275 |
if audio is None or len(audio) == 0:
|
| 276 |
+
return None, "β Failed to generate audio. Please try with different text."
|
| 277 |
|
| 278 |
+
progress(0.9, desc="πΎ Saving audio file...")
|
| 279 |
|
| 280 |
# Save to temporary file
|
| 281 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 282 |
sf.write(tmp_file.name, audio, sample_rate)
|
| 283 |
audio_path = tmp_file.name
|
| 284 |
|
| 285 |
+
progress(1.0, desc="β
Complete!")
|
| 286 |
|
| 287 |
duration = len(audio) / sample_rate
|
| 288 |
+
return audio_path, f"β
Generated {duration:.1f} seconds of audio successfully!"
|
| 289 |
|
| 290 |
except Exception as e:
|
| 291 |
error_msg = f"β Error: {str(e)}"
|
| 292 |
+
print(f"TTS Error: {e}")
|
| 293 |
return None, error_msg
|
| 294 |
|
| 295 |
# Create Gradio interface
|
| 296 |
def create_interface():
|
| 297 |
with gr.Blocks(
|
| 298 |
+
title="π€ Long-Form Text-to-Speech",
|
| 299 |
theme=gr.themes.Soft(),
|
| 300 |
css="""
|
| 301 |
.main-header {
|
| 302 |
text-align: center;
|
| 303 |
margin-bottom: 2rem;
|
| 304 |
+
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
| 305 |
+
-webkit-background-clip: text;
|
| 306 |
+
-webkit-text-fill-color: transparent;
|
| 307 |
+
background-clip: text;
|
| 308 |
}
|
| 309 |
.feature-box {
|
| 310 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 311 |
color: white;
|
| 312 |
+
padding: 1.5rem;
|
| 313 |
+
border-radius: 15px;
|
| 314 |
margin: 1rem 0;
|
| 315 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
|
| 316 |
}
|
| 317 |
.status-box {
|
|
|
|
|
|
|
| 318 |
padding: 1rem;
|
| 319 |
+
border-radius: 10px;
|
| 320 |
margin: 1rem 0;
|
| 321 |
+
border-left: 4px solid #28a745;
|
| 322 |
+
background: #f8f9fa;
|
| 323 |
}
|
| 324 |
"""
|
| 325 |
) as demo:
|
|
|
|
| 327 |
gr.HTML("""
|
| 328 |
<div class="main-header">
|
| 329 |
<h1>π€ Long-Form Text-to-Speech Generator</h1>
|
| 330 |
+
<p style="color: #666; font-size: 1.1em;">Transform any text into natural human-like speech using advanced AI</p>
|
| 331 |
</div>
|
| 332 |
""")
|
| 333 |
|
| 334 |
+
# System status
|
| 335 |
+
if tts_system:
|
| 336 |
+
gr.HTML("""
|
| 337 |
<div class="status-box">
|
| 338 |
+
<h4>π’ System Ready</h4>
|
| 339 |
+
<p>Using <strong>Microsoft SpeechT5</strong> - High quality neural text-to-speech</p>
|
| 340 |
</div>
|
| 341 |
+
""")
|
| 342 |
else:
|
| 343 |
+
gr.HTML("""
|
| 344 |
+
<div class="status-box" style="border-left-color: #dc3545; background: #f8d7da;">
|
| 345 |
+
<h4>π΄ System Error</h4>
|
| 346 |
+
<p>TTS system failed to initialize. Please refresh the page.</p>
|
| 347 |
</div>
|
| 348 |
+
""")
|
|
|
|
|
|
|
| 349 |
|
| 350 |
with gr.Row():
|
| 351 |
with gr.Column(scale=2):
|
| 352 |
text_input = gr.Textbox(
|
| 353 |
+
label="π Enter Your Text",
|
| 354 |
+
placeholder="Type or paste your text here... (Max 5,000 characters)",
|
| 355 |
+
lines=10,
|
| 356 |
+
max_lines=20,
|
| 357 |
+
info="Supports any length text with automatic chunking for optimal quality"
|
| 358 |
)
|
| 359 |
|
| 360 |
+
char_count = gr.HTML("<span style='color: #666;'>Character count: 0 / 5,000</span>")
|
| 361 |
|
| 362 |
generate_btn = gr.Button(
|
| 363 |
"π― Generate Speech",
|
| 364 |
variant="primary",
|
| 365 |
+
size="lg",
|
| 366 |
+
scale=1
|
| 367 |
)
|
| 368 |
|
| 369 |
with gr.Column(scale=1):
|
| 370 |
gr.HTML("""
|
| 371 |
<div class="feature-box">
|
| 372 |
+
<h3>β¨ Key Features</h3>
|
| 373 |
+
<ul style="margin: 0; padding-left: 1.2em;">
|
| 374 |
+
<li>π Handles long texts</li>
|
| 375 |
+
<li>π Natural human voice</li>
|
| 376 |
+
<li>β‘ Smart text processing</li>
|
| 377 |
+
<li>π§ Auto chunking</li>
|
| 378 |
<li>π Completely free</li>
|
|
|
|
| 379 |
<li>π± Mobile friendly</li>
|
| 380 |
+
<li>π΅ High quality audio</li>
|
| 381 |
</ul>
|
| 382 |
</div>
|
| 383 |
""")
|
| 384 |
|
| 385 |
+
# Status and output
|
| 386 |
+
status_output = gr.Textbox(
|
| 387 |
label="π Status",
|
| 388 |
interactive=False,
|
| 389 |
+
value="Ready to generate speech! Enter some text above."
|
| 390 |
)
|
| 391 |
|
| 392 |
audio_output = gr.Audio(
|
| 393 |
label="π Generated Speech",
|
| 394 |
+
type="filepath",
|
| 395 |
+
show_download_button=True
|
| 396 |
)
|
| 397 |
|
| 398 |
# Character counter
|
| 399 |
def update_char_count(text):
|
| 400 |
+
count = len(text) if text else 0
|
| 401 |
+
color = "#28a745" if count <= 5000 else "#dc3545"
|
| 402 |
+
return f'<span style="color: {color};">Character count: {count:,} / 5,000</span>'
|
| 403 |
|
| 404 |
text_input.change(
|
| 405 |
fn=update_char_count,
|
|
|
|
| 407 |
outputs=[char_count]
|
| 408 |
)
|
| 409 |
|
| 410 |
+
# Generate button click
|
| 411 |
generate_btn.click(
|
| 412 |
fn=text_to_speech_interface,
|
| 413 |
inputs=[text_input],
|
| 414 |
+
outputs=[audio_output, status_output],
|
| 415 |
+
show_progress=True
|
| 416 |
)
|
| 417 |
|
| 418 |
# Example texts
|
| 419 |
gr.Examples(
|
| 420 |
examples=[
|
| 421 |
+
["Hello! Welcome to our advanced text-to-speech system. This technology can convert any written text into natural-sounding human speech."],
|
| 422 |
+
["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."],
|
| 423 |
+
["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."],
|
| 424 |
+
["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."],
|
| 425 |
+
["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."]
|
| 426 |
],
|
| 427 |
+
inputs=[text_input],
|
| 428 |
+
label="π Try These Examples"
|
| 429 |
)
|
| 430 |
|
| 431 |
+
# Information section
|
| 432 |
gr.HTML("""
|
| 433 |
+
<div style="margin-top: 2rem; padding: 1.5rem; background: #f8f9fa; border-radius: 10px; border-left: 4px solid #007bff;">
|
| 434 |
+
<h4>π§ How It Works</h4>
|
| 435 |
+
<ol style="margin: 0.5rem 0; padding-left: 1.5rem;">
|
| 436 |
+
<li><strong>Text Processing:</strong> Automatically cleans and normalizes your input text</li>
|
| 437 |
+
<li><strong>Smart Chunking:</strong> Splits long text at natural sentence boundaries</li>
|
| 438 |
+
<li><strong>Neural Synthesis:</strong> Uses Microsoft's SpeechT5 model for speech generation</li>
|
| 439 |
+
<li><strong>Audio Assembly:</strong> Combines all chunks with natural pauses</li>
|
| 440 |
</ol>
|
| 441 |
+
|
| 442 |
+
<h4 style="margin-top: 1rem;">π‘ Tips for Best Results</h4>
|
| 443 |
+
<ul style="margin: 0.5rem 0; padding-left: 1.5rem;">
|
| 444 |
+
<li>Use proper punctuation for natural pauses and intonation</li>
|
| 445 |
+
<li>Spell out abbreviations if you want them pronounced fully</li>
|
| 446 |
+
<li>Well-formatted text produces the most natural speech</li>
|
| 447 |
+
<li>The system automatically handles common abbreviations and numbers</li>
|
| 448 |
+
</ul>
|
| 449 |
</div>
|
| 450 |
""")
|
| 451 |
|
| 452 |
return demo
|
| 453 |
|
| 454 |
+
# Launch the application
|
| 455 |
if __name__ == "__main__":
|
| 456 |
demo = create_interface()
|
| 457 |
demo.launch(
|