Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,39 +20,34 @@ try:
|
|
| 20 |
except LookupError:
|
| 21 |
nltk.download('punkt')
|
| 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"""
|
| 53 |
# Remove extra whitespace
|
| 54 |
text = re.sub(r'\s+', ' ', text.strip())
|
| 55 |
-
|
| 56 |
# Handle common abbreviations
|
| 57 |
abbreviations = {
|
| 58 |
'Dr.': 'Doctor',
|
|
@@ -75,36 +70,28 @@ class LongFormTTS:
|
|
| 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",
|
| 105 |
"sixteen", "seventeen", "eighteen", "nineteen"]
|
| 106 |
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
|
| 107 |
-
|
| 108 |
if num < 10:
|
| 109 |
return ones[num]
|
| 110 |
elif num < 20:
|
|
@@ -120,29 +107,25 @@ class LongFormTTS:
|
|
| 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:
|
|
@@ -153,95 +136,76 @@ class LongFormTTS:
|
|
| 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
|
| 230 |
time.sleep(0.1)
|
| 231 |
-
|
| 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:
|
|
@@ -251,47 +215,40 @@ 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) >
|
| 264 |
-
return None, "β οΈ Text is too long. Please keep it under
|
| 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(
|
|
@@ -323,14 +280,12 @@ def create_interface():
|
|
| 323 |
}
|
| 324 |
"""
|
| 325 |
) as demo:
|
| 326 |
-
|
| 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("""
|
|
@@ -346,26 +301,22 @@ def create_interface():
|
|
| 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
|
| 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">
|
|
@@ -375,38 +326,33 @@ def create_interface():
|
|
| 375 |
<li>π Natural human voice</li>
|
| 376 |
<li>β‘ Smart text processing</li>
|
| 377 |
<li>π§ Auto chunking</li>
|
| 378 |
-
<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 <=
|
| 402 |
-
return f'<span style="color: {color};">Character count: {count:,} /
|
| 403 |
-
|
| 404 |
text_input.change(
|
| 405 |
fn=update_char_count,
|
| 406 |
inputs=[text_input],
|
| 407 |
outputs=[char_count]
|
| 408 |
)
|
| 409 |
-
|
| 410 |
# Generate button click
|
| 411 |
generate_btn.click(
|
| 412 |
fn=text_to_speech_interface,
|
|
@@ -414,7 +360,6 @@ def create_interface():
|
|
| 414 |
outputs=[audio_output, status_output],
|
| 415 |
show_progress=True
|
| 416 |
)
|
| 417 |
-
|
| 418 |
# Example texts
|
| 419 |
gr.Examples(
|
| 420 |
examples=[
|
|
@@ -427,7 +372,6 @@ def create_interface():
|
|
| 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;">
|
|
@@ -438,7 +382,6 @@ def create_interface():
|
|
| 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>
|
|
@@ -448,9 +391,9 @@ def create_interface():
|
|
| 448 |
</ul>
|
| 449 |
</div>
|
| 450 |
""")
|
| 451 |
-
|
| 452 |
return demo
|
| 453 |
|
|
|
|
| 454 |
# Launch the application
|
| 455 |
if __name__ == "__main__":
|
| 456 |
demo = create_interface()
|
|
|
|
| 20 |
except LookupError:
|
| 21 |
nltk.download('punkt')
|
| 22 |
|
| 23 |
+
|
| 24 |
class LongFormTTS:
|
| 25 |
def __init__(self):
|
| 26 |
print("π Loading TTS models...")
|
|
|
|
| 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 |
# Load speaker embeddings
|
| 34 |
print("Loading speaker embeddings...")
|
| 35 |
embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")
|
| 36 |
# Use a different speaker embedding for more variety
|
| 37 |
self.speaker_embeddings = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)
|
|
|
|
| 38 |
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 39 |
self.model = self.model.to(self.device)
|
| 40 |
self.vocoder = self.vocoder.to(self.device)
|
| 41 |
self.speaker_embeddings = self.speaker_embeddings.to(self.device)
|
|
|
|
| 42 |
print("β
SpeechT5 loaded successfully!")
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
print(f"β Failed to load SpeechT5: {e}")
|
| 45 |
raise Exception(f"TTS model loading failed: {e}")
|
| 46 |
+
|
| 47 |
def preprocess_text(self, text):
|
| 48 |
"""Clean and prepare text for TTS"""
|
| 49 |
# Remove extra whitespace
|
| 50 |
text = re.sub(r'\s+', ' ', text.strip())
|
|
|
|
| 51 |
# Handle common abbreviations
|
| 52 |
abbreviations = {
|
| 53 |
'Dr.': 'Doctor',
|
|
|
|
| 70 |
'Ph.D.': 'PhD',
|
| 71 |
'M.D.': 'MD',
|
| 72 |
}
|
|
|
|
| 73 |
for abbr, full in abbreviations.items():
|
| 74 |
text = text.replace(abbr, full)
|
|
|
|
| 75 |
# Convert numbers to words (enhanced)
|
| 76 |
text = re.sub(r'\b(\d{1,4})\b', lambda m: self.number_to_words(int(m.group())), text)
|
|
|
|
| 77 |
# Handle years differently (keep as numbers if between 1000-2100)
|
| 78 |
text = re.sub(r'\b(1[0-9]{3}|20[0-9]{2}|2100)\b', lambda m: m.group(), text)
|
|
|
|
| 79 |
# Clean up problematic characters but keep essential punctuation
|
| 80 |
text = re.sub(r'[^\w\s\.,!?;:\-\(\)\'"]', ' ', text)
|
| 81 |
text = re.sub(r'\s+', ' ', text)
|
|
|
|
| 82 |
return text.strip()
|
| 83 |
+
|
| 84 |
def number_to_words(self, num):
|
| 85 |
"""Convert numbers to words"""
|
| 86 |
if num == 0:
|
| 87 |
return "zero"
|
|
|
|
| 88 |
# Keep larger numbers as digits to avoid very long text
|
| 89 |
if num > 9999:
|
| 90 |
return str(num)
|
|
|
|
| 91 |
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
| 92 |
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
|
| 93 |
"sixteen", "seventeen", "eighteen", "nineteen"]
|
| 94 |
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
|
|
|
|
| 95 |
if num < 10:
|
| 96 |
return ones[num]
|
| 97 |
elif num < 20:
|
|
|
|
| 107 |
if remainder > 0:
|
| 108 |
result += " " + self.number_to_words(remainder)
|
| 109 |
return result
|
| 110 |
+
|
| 111 |
def chunk_text(self, text, max_length=400):
|
| 112 |
"""Split text into manageable chunks"""
|
| 113 |
sentences = sent_tokenize(text)
|
| 114 |
chunks = []
|
| 115 |
current_chunk = ""
|
|
|
|
| 116 |
for sentence in sentences:
|
| 117 |
sentence = sentence.strip()
|
| 118 |
if not sentence:
|
| 119 |
continue
|
|
|
|
| 120 |
# If adding this sentence would exceed limit
|
| 121 |
if len(current_chunk + " " + sentence) > max_length:
|
| 122 |
# Save current chunk if it exists
|
| 123 |
if current_chunk:
|
| 124 |
chunks.append(current_chunk.strip())
|
|
|
|
| 125 |
# If single sentence is too long, split it
|
| 126 |
if len(sentence) > max_length:
|
| 127 |
words = sentence.split()
|
| 128 |
temp_chunk = ""
|
|
|
|
| 129 |
for word in words:
|
| 130 |
if len(temp_chunk + " " + word) > max_length:
|
| 131 |
if temp_chunk:
|
|
|
|
| 136 |
chunks.append(word)
|
| 137 |
else:
|
| 138 |
temp_chunk = temp_chunk + " " + word if temp_chunk else word
|
|
|
|
| 139 |
current_chunk = temp_chunk
|
| 140 |
else:
|
| 141 |
current_chunk = sentence
|
| 142 |
else:
|
| 143 |
current_chunk = current_chunk + " " + sentence if current_chunk else sentence
|
|
|
|
| 144 |
# Add the last chunk
|
| 145 |
if current_chunk:
|
| 146 |
chunks.append(current_chunk.strip())
|
|
|
|
| 147 |
return [chunk for chunk in chunks if chunk.strip()]
|
| 148 |
+
|
| 149 |
def generate_speech_chunk(self, text_chunk):
|
| 150 |
"""Generate speech for a single chunk"""
|
| 151 |
try:
|
| 152 |
# Process text through the model
|
| 153 |
inputs = self.processor(text=text_chunk, return_tensors="pt").to(self.device)
|
|
|
|
| 154 |
with torch.no_grad():
|
| 155 |
speech = self.model.generate_speech(
|
| 156 |
inputs["input_ids"],
|
| 157 |
self.speaker_embeddings,
|
| 158 |
vocoder=self.vocoder
|
| 159 |
)
|
|
|
|
| 160 |
# Convert to numpy and move to CPU
|
| 161 |
if isinstance(speech, torch.Tensor):
|
| 162 |
speech = speech.cpu().numpy()
|
|
|
|
| 163 |
return speech
|
|
|
|
| 164 |
except Exception as e:
|
| 165 |
print(f"Error generating speech for chunk: {e}")
|
| 166 |
print(f"Chunk text: {text_chunk}")
|
| 167 |
return None
|
| 168 |
+
|
| 169 |
def generate_long_speech(self, text, progress_callback=None):
|
| 170 |
"""Generate speech for long text"""
|
| 171 |
# Preprocess text
|
| 172 |
processed_text = self.preprocess_text(text)
|
| 173 |
print(f"Original length: {len(text)}, Processed length: {len(processed_text)}")
|
|
|
|
| 174 |
# Split into chunks
|
| 175 |
chunks = self.chunk_text(processed_text)
|
| 176 |
print(f"Split into {len(chunks)} chunks")
|
|
|
|
| 177 |
if not chunks:
|
| 178 |
return None, None
|
|
|
|
| 179 |
# Generate speech for each chunk
|
| 180 |
audio_segments = []
|
| 181 |
sample_rate = 16000 # SpeechT5 uses 16kHz
|
|
|
|
| 182 |
for i, chunk in enumerate(chunks):
|
| 183 |
if progress_callback:
|
| 184 |
progress_callback(f"Processing chunk {i+1}/{len(chunks)}: {chunk[:40]}{'...' if len(chunk) > 40 else ''}")
|
|
|
|
| 185 |
print(f"Processing chunk {i+1}: {chunk}")
|
| 186 |
audio_chunk = self.generate_speech_chunk(chunk)
|
|
|
|
| 187 |
if audio_chunk is not None and len(audio_chunk) > 0:
|
| 188 |
# Ensure audio is 1D
|
| 189 |
if len(audio_chunk.shape) > 1:
|
| 190 |
audio_chunk = np.mean(audio_chunk, axis=0)
|
|
|
|
| 191 |
audio_segments.append(audio_chunk)
|
|
|
|
| 192 |
# Add pause between chunks (400ms)
|
| 193 |
pause_samples = int(0.4 * sample_rate)
|
| 194 |
silence = np.zeros(pause_samples)
|
| 195 |
audio_segments.append(silence)
|
|
|
|
| 196 |
# Small delay to prevent overwhelming the system
|
| 197 |
time.sleep(0.1)
|
|
|
|
| 198 |
if not audio_segments:
|
| 199 |
return None, None
|
|
|
|
| 200 |
# Concatenate all segments
|
| 201 |
final_audio = np.concatenate(audio_segments)
|
|
|
|
| 202 |
# Normalize audio to prevent clipping
|
| 203 |
max_val = np.max(np.abs(final_audio))
|
| 204 |
if max_val > 0:
|
| 205 |
final_audio = final_audio / max_val * 0.95
|
|
|
|
| 206 |
return final_audio, sample_rate
|
| 207 |
|
| 208 |
+
|
| 209 |
# Global TTS system
|
| 210 |
print("π Initializing TTS system...")
|
| 211 |
try:
|
|
|
|
| 215 |
print(f"β TTS initialization failed: {e}")
|
| 216 |
tts_system = None
|
| 217 |
|
| 218 |
+
|
| 219 |
def text_to_speech_interface(text, progress=gr.Progress()):
|
| 220 |
"""Main Gradio interface function"""
|
| 221 |
if tts_system is None:
|
| 222 |
return None, "β TTS system is not available. Please check the logs."
|
|
|
|
| 223 |
if not text or not text.strip():
|
| 224 |
return None, "β οΈ Please enter some text to convert to speech."
|
|
|
|
| 225 |
# Text length check
|
| 226 |
+
if len(text) > 50000:
|
| 227 |
+
return None, "β οΈ Text is too long. Please keep it under 50,000 characters for optimal performance."
|
| 228 |
+
|
| 229 |
def progress_callback(message):
|
| 230 |
progress(0.5, desc=message)
|
| 231 |
+
|
| 232 |
try:
|
| 233 |
progress(0.1, desc="π Starting text-to-speech conversion...")
|
|
|
|
| 234 |
# Generate audio
|
| 235 |
audio, sample_rate = tts_system.generate_long_speech(text, progress_callback)
|
|
|
|
| 236 |
if audio is None or len(audio) == 0:
|
| 237 |
return None, "β Failed to generate audio. Please try with different text."
|
|
|
|
| 238 |
progress(0.9, desc="πΎ Saving audio file...")
|
|
|
|
| 239 |
# Save to temporary file
|
| 240 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 241 |
sf.write(tmp_file.name, audio, sample_rate)
|
| 242 |
audio_path = tmp_file.name
|
|
|
|
| 243 |
progress(1.0, desc="β
Complete!")
|
|
|
|
| 244 |
duration = len(audio) / sample_rate
|
| 245 |
return audio_path, f"β
Generated {duration:.1f} seconds of audio successfully!"
|
|
|
|
| 246 |
except Exception as e:
|
| 247 |
error_msg = f"β Error: {str(e)}"
|
| 248 |
print(f"TTS Error: {e}")
|
| 249 |
return None, error_msg
|
| 250 |
|
| 251 |
+
|
| 252 |
# Create Gradio interface
|
| 253 |
def create_interface():
|
| 254 |
with gr.Blocks(
|
|
|
|
| 280 |
}
|
| 281 |
"""
|
| 282 |
) as demo:
|
|
|
|
| 283 |
gr.HTML("""
|
| 284 |
<div class="main-header">
|
| 285 |
<h1>π€ Long-Form Text-to-Speech Generator</h1>
|
| 286 |
<p style="color: #666; font-size: 1.1em;">Transform any text into natural human-like speech using advanced AI</p>
|
| 287 |
</div>
|
| 288 |
""")
|
|
|
|
| 289 |
# System status
|
| 290 |
if tts_system:
|
| 291 |
gr.HTML("""
|
|
|
|
| 301 |
<p>TTS system failed to initialize. Please refresh the page.</p>
|
| 302 |
</div>
|
| 303 |
""")
|
|
|
|
| 304 |
with gr.Row():
|
| 305 |
with gr.Column(scale=2):
|
| 306 |
text_input = gr.Textbox(
|
| 307 |
label="π Enter Your Text",
|
| 308 |
+
placeholder="Type or paste your text here... (Max 50,000 characters)",
|
| 309 |
lines=10,
|
| 310 |
max_lines=20,
|
| 311 |
info="Supports any length text with automatic chunking for optimal quality"
|
| 312 |
)
|
| 313 |
+
char_count = gr.HTML("<span style='color: #666;'>Character count: 0 / 50,000</span>")
|
|
|
|
|
|
|
| 314 |
generate_btn = gr.Button(
|
| 315 |
"π― Generate Speech",
|
| 316 |
variant="primary",
|
| 317 |
size="lg",
|
| 318 |
scale=1
|
| 319 |
)
|
|
|
|
| 320 |
with gr.Column(scale=1):
|
| 321 |
gr.HTML("""
|
| 322 |
<div class="feature-box">
|
|
|
|
| 326 |
<li>π Natural human voice</li>
|
| 327 |
<li>β‘ Smart text processing</li>
|
| 328 |
<li>π§ Auto chunking</li>
|
| 329 |
+
<li>_FREE_ Completely free</li>
|
| 330 |
<li>π± Mobile friendly</li>
|
| 331 |
<li>π΅ High quality audio</li>
|
| 332 |
</ul>
|
| 333 |
</div>
|
| 334 |
""")
|
|
|
|
| 335 |
# Status and output
|
| 336 |
status_output = gr.Textbox(
|
| 337 |
label="π Status",
|
| 338 |
interactive=False,
|
| 339 |
value="Ready to generate speech! Enter some text above."
|
| 340 |
)
|
|
|
|
| 341 |
audio_output = gr.Audio(
|
| 342 |
label="π Generated Speech",
|
| 343 |
type="filepath",
|
| 344 |
show_download_button=True
|
| 345 |
)
|
|
|
|
| 346 |
# Character counter
|
| 347 |
def update_char_count(text):
|
| 348 |
count = len(text) if text else 0
|
| 349 |
+
color = "#28a745" if count <= 50000 else "#dc3545"
|
| 350 |
+
return f'<span style="color: {color};">Character count: {count:,} / 50,000</span>'
|
|
|
|
| 351 |
text_input.change(
|
| 352 |
fn=update_char_count,
|
| 353 |
inputs=[text_input],
|
| 354 |
outputs=[char_count]
|
| 355 |
)
|
|
|
|
| 356 |
# Generate button click
|
| 357 |
generate_btn.click(
|
| 358 |
fn=text_to_speech_interface,
|
|
|
|
| 360 |
outputs=[audio_output, status_output],
|
| 361 |
show_progress=True
|
| 362 |
)
|
|
|
|
| 363 |
# Example texts
|
| 364 |
gr.Examples(
|
| 365 |
examples=[
|
|
|
|
| 372 |
inputs=[text_input],
|
| 373 |
label="π Try These Examples"
|
| 374 |
)
|
|
|
|
| 375 |
# Information section
|
| 376 |
gr.HTML("""
|
| 377 |
<div style="margin-top: 2rem; padding: 1.5rem; background: #f8f9fa; border-radius: 10px; border-left: 4px solid #007bff;">
|
|
|
|
| 382 |
<li><strong>Neural Synthesis:</strong> Uses Microsoft's SpeechT5 model for speech generation</li>
|
| 383 |
<li><strong>Audio Assembly:</strong> Combines all chunks with natural pauses</li>
|
| 384 |
</ol>
|
|
|
|
| 385 |
<h4 style="margin-top: 1rem;">π‘ Tips for Best Results</h4>
|
| 386 |
<ul style="margin: 0.5rem 0; padding-left: 1.5rem;">
|
| 387 |
<li>Use proper punctuation for natural pauses and intonation</li>
|
|
|
|
| 391 |
</ul>
|
| 392 |
</div>
|
| 393 |
""")
|
|
|
|
| 394 |
return demo
|
| 395 |
|
| 396 |
+
|
| 397 |
# Launch the application
|
| 398 |
if __name__ == "__main__":
|
| 399 |
demo = create_interface()
|