Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,16 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
import re
|
| 5 |
-
from transformers import
|
| 6 |
-
from datasets import load_dataset
|
| 7 |
import soundfile as sf
|
| 8 |
import io
|
| 9 |
import tempfile
|
| 10 |
import os
|
| 11 |
from pydub import AudioSegment
|
| 12 |
-
from pydub.silence import split_on_silence
|
| 13 |
import nltk
|
| 14 |
from nltk.tokenize import sent_tokenize
|
| 15 |
import warnings
|
|
|
|
| 16 |
warnings.filterwarnings("ignore")
|
| 17 |
|
| 18 |
# Download required NLTK data
|
|
@@ -25,16 +24,46 @@ class LongFormTTS:
|
|
| 25 |
def __init__(self):
|
| 26 |
print("Loading TTS models...")
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
self.
|
| 30 |
-
self.
|
| 31 |
-
self.vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def preprocess_text(self, text):
|
| 40 |
"""Clean and prepare text for TTS"""
|
|
@@ -52,6 +81,12 @@ class LongFormTTS:
|
|
| 52 |
'vs.': 'versus',
|
| 53 |
'e.g.': 'for example',
|
| 54 |
'i.e.': 'that is',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
for abbr, full in abbreviations.items():
|
|
@@ -60,6 +95,9 @@ class LongFormTTS:
|
|
| 60 |
# Handle numbers (basic)
|
| 61 |
text = re.sub(r'\b(\d+)\b', lambda m: self.number_to_words(int(m.group())), text)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
| 63 |
return text
|
| 64 |
|
| 65 |
def number_to_words(self, num):
|
|
@@ -67,6 +105,9 @@ class LongFormTTS:
|
|
| 67 |
if num == 0:
|
| 68 |
return "zero"
|
| 69 |
|
|
|
|
|
|
|
|
|
|
| 70 |
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
| 71 |
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
|
| 72 |
"sixteen", "seventeen", "eighteen", "nineteen"]
|
|
@@ -81,9 +122,9 @@ class LongFormTTS:
|
|
| 81 |
elif num < 1000:
|
| 82 |
return ones[num // 100] + " hundred" + ("" if num % 100 == 0 else " " + self.number_to_words(num % 100))
|
| 83 |
else:
|
| 84 |
-
return str(num)
|
| 85 |
|
| 86 |
-
def chunk_text(self, text, max_length=
|
| 87 |
"""Split text into manageable chunks while preserving sentence boundaries"""
|
| 88 |
sentences = sent_tokenize(text)
|
| 89 |
chunks = []
|
|
@@ -109,7 +150,7 @@ class LongFormTTS:
|
|
| 109 |
chunks.append(temp_chunk.strip())
|
| 110 |
temp_chunk = word
|
| 111 |
else:
|
| 112 |
-
chunks.append(word)
|
| 113 |
else:
|
| 114 |
temp_chunk += " " + word if temp_chunk else word
|
| 115 |
if temp_chunk:
|
|
@@ -134,16 +175,33 @@ class LongFormTTS:
|
|
| 134 |
def generate_speech_chunk(self, text_chunk):
|
| 135 |
"""Generate speech for a single text chunk"""
|
| 136 |
try:
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error generating speech for chunk: {e}")
|
| 146 |
-
return
|
| 147 |
|
| 148 |
def generate_long_speech(self, text, progress_callback=None):
|
| 149 |
"""Generate speech for long text by processing in chunks"""
|
|
@@ -151,45 +209,69 @@ class LongFormTTS:
|
|
| 151 |
text = self.preprocess_text(text)
|
| 152 |
|
| 153 |
# Split into chunks
|
| 154 |
-
chunks = self.chunk_text(text)
|
| 155 |
print(f"Split text into {len(chunks)} chunks")
|
| 156 |
|
| 157 |
if not chunks:
|
| 158 |
-
return
|
| 159 |
|
| 160 |
# Generate speech for each chunk
|
| 161 |
audio_segments = []
|
|
|
|
| 162 |
total_chunks = len(chunks)
|
| 163 |
|
| 164 |
for i, chunk in enumerate(chunks):
|
| 165 |
if progress_callback:
|
| 166 |
-
progress_callback(f"Processing chunk {i+1}/{total_chunks}: {chunk[:
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
#
|
| 173 |
-
|
| 174 |
-
silence = np.zeros(pause_duration)
|
| 175 |
-
audio_segments.append(silence)
|
| 176 |
|
| 177 |
if not audio_segments:
|
| 178 |
-
return
|
| 179 |
|
| 180 |
# Concatenate all audio segments
|
| 181 |
final_audio = np.concatenate(audio_segments)
|
| 182 |
|
| 183 |
-
return final_audio,
|
| 184 |
|
| 185 |
# Initialize TTS system
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
def text_to_speech_interface(text, progress=gr.Progress()):
|
| 189 |
"""Main interface function for Gradio"""
|
|
|
|
|
|
|
|
|
|
| 190 |
if not text.strip():
|
| 191 |
return None, "Please enter some text to convert to speech."
|
| 192 |
|
|
|
|
|
|
|
|
|
|
| 193 |
def progress_callback(message):
|
| 194 |
progress(0.5, desc=message)
|
| 195 |
|
|
@@ -198,8 +280,8 @@ def text_to_speech_interface(text, progress=gr.Progress()):
|
|
| 198 |
|
| 199 |
audio, sample_rate = tts_system.generate_long_speech(text, progress_callback)
|
| 200 |
|
| 201 |
-
if len(audio) == 0:
|
| 202 |
-
return None, "Failed to generate audio. Please try
|
| 203 |
|
| 204 |
progress(0.9, desc="Finalizing audio...")
|
| 205 |
|
|
@@ -210,7 +292,8 @@ def text_to_speech_interface(text, progress=gr.Progress()):
|
|
| 210 |
|
| 211 |
progress(1.0, desc="Complete!")
|
| 212 |
|
| 213 |
-
|
|
|
|
| 214 |
|
| 215 |
except Exception as e:
|
| 216 |
error_msg = f"β Error: {str(e)}"
|
|
@@ -234,6 +317,12 @@ def create_interface():
|
|
| 234 |
border-radius: 10px;
|
| 235 |
margin: 1rem 0;
|
| 236 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
"""
|
| 238 |
) as demo:
|
| 239 |
|
|
@@ -244,15 +333,35 @@ def create_interface():
|
|
| 244 |
</div>
|
| 245 |
""")
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
with gr.Row():
|
| 248 |
with gr.Column(scale=2):
|
| 249 |
text_input = gr.Textbox(
|
| 250 |
-
label="π Enter your text",
|
| 251 |
-
placeholder="Type or paste
|
| 252 |
-
lines=
|
| 253 |
-
max_lines=
|
| 254 |
)
|
| 255 |
|
|
|
|
|
|
|
| 256 |
generate_btn = gr.Button(
|
| 257 |
"π― Generate Speech",
|
| 258 |
variant="primary",
|
|
@@ -264,11 +373,12 @@ def create_interface():
|
|
| 264 |
<div class="feature-box">
|
| 265 |
<h3>β¨ Features</h3>
|
| 266 |
<ul>
|
| 267 |
-
<li>π
|
| 268 |
-
<li>π€
|
| 269 |
<li>β‘ Smart text chunking</li>
|
| 270 |
-
<li>π Completely free
|
| 271 |
-
<li>π§
|
|
|
|
| 272 |
</ul>
|
| 273 |
</div>
|
| 274 |
""")
|
|
@@ -284,6 +394,18 @@ def create_interface():
|
|
| 284 |
type="filepath"
|
| 285 |
)
|
| 286 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
# Event handlers
|
| 288 |
generate_btn.click(
|
| 289 |
fn=text_to_speech_interface,
|
|
@@ -294,9 +416,10 @@ def create_interface():
|
|
| 294 |
# Example texts
|
| 295 |
gr.Examples(
|
| 296 |
examples=[
|
| 297 |
-
["Hello! This is a test of the
|
| 298 |
-
["The quick brown fox jumps over the lazy dog. This sentence contains every letter of the alphabet
|
| 299 |
-
["In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole
|
|
|
|
| 300 |
],
|
| 301 |
inputs=[text_input]
|
| 302 |
)
|
|
@@ -305,12 +428,12 @@ def create_interface():
|
|
| 305 |
<div style="margin-top: 2rem; padding: 1rem; background: #f0f0f0; border-radius: 5px;">
|
| 306 |
<h4>π§ How it works:</h4>
|
| 307 |
<ol>
|
| 308 |
-
<li><strong>
|
| 309 |
-
<li><strong>Smart Chunking:</strong> Splits long text at
|
| 310 |
-
<li><strong>
|
| 311 |
-
<li><strong>
|
| 312 |
</ol>
|
| 313 |
-
<p><em>π‘ Tip:
|
| 314 |
</div>
|
| 315 |
""")
|
| 316 |
|
|
|
|
| 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
|
|
|
|
| 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 |
+
print("Loading Bark TTS...")
|
| 34 |
+
self.tts_pipeline = pipeline(
|
| 35 |
+
"text-to-speech",
|
| 36 |
+
model="suno/bark-small",
|
| 37 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 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 |
+
# Backup: Try Parler TTS
|
| 45 |
+
try:
|
| 46 |
+
print("Loading Parler TTS...")
|
| 47 |
+
self.tts_pipeline = pipeline(
|
| 48 |
+
"text-to-speech",
|
| 49 |
+
model="parler-tts/parler_tts_mini_v0.1",
|
| 50 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 51 |
+
)
|
| 52 |
+
self.tts_method = "parler"
|
| 53 |
+
print("β
Parler TTS loaded successfully!")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"β Parler TTS failed: {e}")
|
| 56 |
+
|
| 57 |
+
# Final backup: Try FastSpeech2
|
| 58 |
+
try:
|
| 59 |
+
print("Loading FastSpeech2...")
|
| 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"""
|
|
|
|
| 81 |
'vs.': 'versus',
|
| 82 |
'e.g.': 'for example',
|
| 83 |
'i.e.': 'that is',
|
| 84 |
+
'St.': 'Street',
|
| 85 |
+
'Ave.': 'Avenue',
|
| 86 |
+
'Blvd.': 'Boulevard',
|
| 87 |
+
'Inc.': 'Incorporated',
|
| 88 |
+
'Corp.': 'Corporation',
|
| 89 |
+
'Ltd.': 'Limited',
|
| 90 |
}
|
| 91 |
|
| 92 |
for abbr, full in abbreviations.items():
|
|
|
|
| 95 |
# Handle numbers (basic)
|
| 96 |
text = re.sub(r'\b(\d+)\b', lambda m: self.number_to_words(int(m.group())), text)
|
| 97 |
|
| 98 |
+
# Clean up any problematic characters
|
| 99 |
+
text = re.sub(r'[^\w\s\.,!?;:\-\(\)]', '', text)
|
| 100 |
+
|
| 101 |
return text
|
| 102 |
|
| 103 |
def number_to_words(self, num):
|
|
|
|
| 105 |
if num == 0:
|
| 106 |
return "zero"
|
| 107 |
|
| 108 |
+
if num > 9999:
|
| 109 |
+
return str(num) # Keep large numbers as digits
|
| 110 |
+
|
| 111 |
ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
|
| 112 |
teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
|
| 113 |
"sixteen", "seventeen", "eighteen", "nineteen"]
|
|
|
|
| 122 |
elif num < 1000:
|
| 123 |
return ones[num // 100] + " hundred" + ("" if num % 100 == 0 else " " + self.number_to_words(num % 100))
|
| 124 |
else:
|
| 125 |
+
return str(num)
|
| 126 |
|
| 127 |
+
def chunk_text(self, text, max_length=200):
|
| 128 |
"""Split text into manageable chunks while preserving sentence boundaries"""
|
| 129 |
sentences = sent_tokenize(text)
|
| 130 |
chunks = []
|
|
|
|
| 150 |
chunks.append(temp_chunk.strip())
|
| 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:
|
|
|
|
| 175 |
def generate_speech_chunk(self, text_chunk):
|
| 176 |
"""Generate speech for a single text chunk"""
|
| 177 |
try:
|
| 178 |
+
if self.tts_method == "bark":
|
| 179 |
+
# Bark TTS
|
| 180 |
+
speech = self.tts_pipeline(text_chunk)
|
| 181 |
+
audio = speech["audio"]
|
| 182 |
+
sampling_rate = speech["sampling_rate"]
|
| 183 |
+
return audio, sampling_rate
|
| 184 |
+
|
| 185 |
+
elif self.tts_method == "parler":
|
| 186 |
+
# Parler TTS
|
| 187 |
+
speech = self.tts_pipeline(text_chunk)
|
| 188 |
+
audio = speech["audio"]
|
| 189 |
+
sampling_rate = speech["sampling_rate"]
|
| 190 |
+
return audio, sampling_rate
|
| 191 |
+
|
| 192 |
+
elif self.tts_method == "fastspeech2":
|
| 193 |
+
# FastSpeech2 via TTS library
|
| 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 |
+
return None, None
|
| 205 |
|
| 206 |
def generate_long_speech(self, text, progress_callback=None):
|
| 207 |
"""Generate speech for long text by processing in chunks"""
|
|
|
|
| 209 |
text = self.preprocess_text(text)
|
| 210 |
|
| 211 |
# Split into chunks
|
| 212 |
+
chunks = self.chunk_text(text, max_length=150) # Smaller chunks for better compatibility
|
| 213 |
print(f"Split text into {len(chunks)} chunks")
|
| 214 |
|
| 215 |
if not chunks:
|
| 216 |
+
return None, None
|
| 217 |
|
| 218 |
# Generate speech for each chunk
|
| 219 |
audio_segments = []
|
| 220 |
+
sampling_rate = None
|
| 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}/{total_chunks}: {chunk[:30]}...")
|
| 226 |
|
| 227 |
+
audio_chunk, sr = self.generate_speech_chunk(chunk)
|
| 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 = audio_chunk.mean(axis=1)
|
| 236 |
+
|
| 237 |
+
audio_segments.append(audio_chunk)
|
| 238 |
+
|
| 239 |
+
# Add small pause between chunks (300ms of silence)
|
| 240 |
+
pause_duration = int(0.3 * sampling_rate)
|
| 241 |
+
silence = np.zeros(pause_duration)
|
| 242 |
+
audio_segments.append(silence)
|
| 243 |
|
| 244 |
+
# Small delay to prevent overwhelming the system
|
| 245 |
+
time.sleep(0.1)
|
|
|
|
|
|
|
| 246 |
|
| 247 |
if not audio_segments:
|
| 248 |
+
return None, None
|
| 249 |
|
| 250 |
# Concatenate all audio segments
|
| 251 |
final_audio = np.concatenate(audio_segments)
|
| 252 |
|
| 253 |
+
return final_audio, sampling_rate
|
| 254 |
|
| 255 |
# Initialize TTS system
|
| 256 |
+
print("Initializing TTS system...")
|
| 257 |
+
try:
|
| 258 |
+
tts_system = LongFormTTS()
|
| 259 |
+
print("β
TTS system initialized successfully!")
|
| 260 |
+
except Exception as e:
|
| 261 |
+
print(f"β Failed to initialize TTS system: {e}")
|
| 262 |
+
tts_system = None
|
| 263 |
|
| 264 |
def text_to_speech_interface(text, progress=gr.Progress()):
|
| 265 |
"""Main interface function for Gradio"""
|
| 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 |
+
if len(text) > 10000:
|
| 273 |
+
return None, "Text is too long. Please keep it under 10,000 characters for optimal performance."
|
| 274 |
+
|
| 275 |
def progress_callback(message):
|
| 276 |
progress(0.5, desc=message)
|
| 277 |
|
|
|
|
| 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 shorter text or check your input."
|
| 285 |
|
| 286 |
progress(0.9, desc="Finalizing audio...")
|
| 287 |
|
|
|
|
| 292 |
|
| 293 |
progress(1.0, desc="Complete!")
|
| 294 |
|
| 295 |
+
duration = len(audio) / sample_rate
|
| 296 |
+
return audio_path, f"β
Successfully generated {duration:.1f} seconds of audio using {tts_system.tts_method.upper()}!"
|
| 297 |
|
| 298 |
except Exception as e:
|
| 299 |
error_msg = f"β Error: {str(e)}"
|
|
|
|
| 317 |
border-radius: 10px;
|
| 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:
|
| 328 |
|
|
|
|
| 333 |
</div>
|
| 334 |
""")
|
| 335 |
|
| 336 |
+
# Show TTS system status
|
| 337 |
+
if tts_system is not None:
|
| 338 |
+
status_html = f"""
|
| 339 |
+
<div class="status-box">
|
| 340 |
+
<h4>π’ System Status: Ready</h4>
|
| 341 |
+
<p>Using <strong>{tts_system.tts_method.upper()}</strong> TTS engine</p>
|
| 342 |
+
</div>
|
| 343 |
+
"""
|
| 344 |
+
else:
|
| 345 |
+
status_html = """
|
| 346 |
+
<div class="status-box" style="border-left-color: #dc3545;">
|
| 347 |
+
<h4>π΄ System Status: Error</h4>
|
| 348 |
+
<p>TTS system failed to initialize. Please check the logs.</p>
|
| 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 your text (max 10,000 characters)",
|
| 358 |
+
placeholder="Type or paste your text here...",
|
| 359 |
+
lines=8,
|
| 360 |
+
max_lines=15
|
| 361 |
)
|
| 362 |
|
| 363 |
+
char_count = gr.HTML("Character count: 0")
|
| 364 |
+
|
| 365 |
generate_btn = gr.Button(
|
| 366 |
"π― Generate Speech",
|
| 367 |
variant="primary",
|
|
|
|
| 373 |
<div class="feature-box">
|
| 374 |
<h3>β¨ Features</h3>
|
| 375 |
<ul>
|
| 376 |
+
<li>π Long text support</li>
|
| 377 |
+
<li>π€ Multiple TTS engines</li>
|
| 378 |
<li>β‘ Smart text chunking</li>
|
| 379 |
+
<li>π Completely free</li>
|
| 380 |
+
<li>π§ Auto preprocessing</li>
|
| 381 |
+
<li>π± Mobile friendly</li>
|
| 382 |
</ul>
|
| 383 |
</div>
|
| 384 |
""")
|
|
|
|
| 394 |
type="filepath"
|
| 395 |
)
|
| 396 |
|
| 397 |
+
# Character counter
|
| 398 |
+
def update_char_count(text):
|
| 399 |
+
count = len(text)
|
| 400 |
+
color = "green" if count <= 10000 else "red"
|
| 401 |
+
return f'<span style="color: {color};">Character count: {count}/10,000</span>'
|
| 402 |
+
|
| 403 |
+
text_input.change(
|
| 404 |
+
fn=update_char_count,
|
| 405 |
+
inputs=[text_input],
|
| 406 |
+
outputs=[char_count]
|
| 407 |
+
)
|
| 408 |
+
|
| 409 |
# Event handlers
|
| 410 |
generate_btn.click(
|
| 411 |
fn=text_to_speech_interface,
|
|
|
|
| 416 |
# Example texts
|
| 417 |
gr.Examples(
|
| 418 |
examples=[
|
| 419 |
+
["Hello! This is a test of the text-to-speech system. It can handle longer texts by splitting them into smaller chunks."],
|
| 420 |
+
["The quick brown fox jumps over the lazy dog. This sentence contains every letter of the alphabet."],
|
| 421 |
+
["In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, but a comfortable hobbit-hole."],
|
| 422 |
+
["Welcome to our advanced text-to-speech generator. This system uses state-of-the-art AI models to convert your text into natural-sounding speech. You can input texts of various lengths, and the system will intelligently process them to create high-quality audio output."]
|
| 423 |
],
|
| 424 |
inputs=[text_input]
|
| 425 |
)
|
|
|
|
| 428 |
<div style="margin-top: 2rem; padding: 1rem; background: #f0f0f0; border-radius: 5px;">
|
| 429 |
<h4>π§ How it works:</h4>
|
| 430 |
<ol>
|
| 431 |
+
<li><strong>Multiple Engines:</strong> Tries Bark, Parler, or FastSpeech2 TTS models</li>
|
| 432 |
+
<li><strong>Smart Chunking:</strong> Splits long text at natural boundaries</li>
|
| 433 |
+
<li><strong>Audio Processing:</strong> Combines chunks with natural pauses</li>
|
| 434 |
+
<li><strong>Quality Output:</strong> Generates high-quality WAV audio</li>
|
| 435 |
</ol>
|
| 436 |
+
<p><em>π‘ Tip: For best results, use well-formatted text with proper punctuation!</em></p>
|
| 437 |
</div>
|
| 438 |
""")
|
| 439 |
|