Spaces:
Runtime error
Runtime error
File size: 12,859 Bytes
b163aa7 797f6a7 b163aa7 d2f6021 b163aa7 d2f6021 b163aa7 d2f6021 b163aa7 9fb8195 b163aa7 9fb8195 b163aa7 d2f6021 b163aa7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
"""
Optimized SpeechT5 Armenian TTS Application
==========================================
High-performance Gradio application with advanced optimization features.
"""
import gradio as gr
import numpy as np
import logging
import time
from typing import Tuple, Optional
import os
import sys
# Add src to path for imports
current_dir = os.path.dirname(os.path.abspath(__file__))
src_path = os.path.join(current_dir, 'src')
if src_path not in sys.path:
sys.path.insert(0, src_path)
try:
from src.pipeline import TTSPipeline
except ImportError as e:
logging.error(f"Failed to import pipeline: {e}")
# Fallback import attempt
sys.path.append(os.path.join(os.path.dirname(__file__), 'src'))
from src.pipeline import TTSPipeline
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Global pipeline instance
tts_pipeline: Optional[TTSPipeline] = None
def initialize_pipeline():
"""Initialize the TTS pipeline with error handling."""
global tts_pipeline
try:
logger.info("Initializing TTS Pipeline...")
tts_pipeline = TTSPipeline(
model_checkpoint="Edmon02/TTS_NB_2",
max_chunk_length=200, # Optimal for 5-20s clips
crossfade_duration=0.1,
use_mixed_precision=True
)
# Apply production optimizations
tts_pipeline.optimize_for_production()
logger.info("TTS Pipeline initialized successfully")
return True
except Exception as e:
logger.error(f"Failed to initialize TTS pipeline: {e}")
return False
def predict(text: str, speaker: str,
enable_chunking: bool = True,
apply_processing: bool = True) -> Tuple[int, np.ndarray]:
"""
Main prediction function with optimization and error handling.
Args:
text: Input text to synthesize
speaker: Speaker selection
enable_chunking: Whether to enable intelligent chunking
apply_processing: Whether to apply audio post-processing
Returns:
Tuple of (sample_rate, audio_array)
"""
global tts_pipeline
start_time = time.time()
try:
# Validate inputs
if not text or not text.strip():
logger.warning("Empty text provided")
return 16000, np.zeros(0, dtype=np.int16)
if tts_pipeline is None:
logger.error("TTS pipeline not initialized")
return 16000, np.zeros(0, dtype=np.int16)
# Extract speaker code from selection
speaker_code = speaker.split("(")[0].strip()
# Log request
logger.info(f"Processing request: {len(text)} chars, speaker: {speaker_code}")
# Synthesize speech
sample_rate, audio = tts_pipeline.synthesize(
text=text,
speaker=speaker_code,
enable_chunking=enable_chunking,
apply_audio_processing=apply_processing
)
# Log performance
total_time = time.time() - start_time
audio_duration = len(audio) / sample_rate if len(audio) > 0 else 0
rtf = total_time / audio_duration if audio_duration > 0 else float('inf')
logger.info(f"Request completed in {total_time:.3f}s (RTF: {rtf:.2f})")
return sample_rate, audio
except Exception as e:
logger.error(f"Prediction failed: {e}")
return 16000, np.zeros(0, dtype=np.int16)
def get_performance_info() -> str:
"""Get performance statistics as formatted string."""
global tts_pipeline
if tts_pipeline is None:
return "Pipeline not initialized"
try:
stats = tts_pipeline.get_performance_stats()
info = f"""
**Performance Statistics:**
- Total Inferences: {stats['pipeline_stats']['total_inferences']}
- Average Processing Time: {stats['pipeline_stats']['avg_processing_time']:.3f}s
- Translation Cache Size: {stats['text_processor_stats']['translation_cache_size']}
- Model Inferences: {stats['model_stats']['total_inferences']}
- Average Model Time: {stats['model_stats'].get('avg_inference_time', 0):.3f}s
"""
return info.strip()
except Exception as e:
return f"Error getting performance info: {e}"
def health_check() -> str:
"""Perform system health check."""
global tts_pipeline
if tts_pipeline is None:
return "❌ Pipeline not initialized"
try:
health = tts_pipeline.health_check()
if health["status"] == "healthy":
return "✅ All systems operational"
elif health["status"] == "degraded":
return "⚠️ Some components have issues"
else:
return f"❌ System error: {health.get('error', 'Unknown error')}"
except Exception as e:
return f"❌ Health check failed: {e}"
# Application metadata
TITLE = "🎤 SpeechT5 Armenian TTS - Optimized"
DESCRIPTION = """
# High-Performance Armenian Text-to-Speech
This is an **optimized version** of SpeechT5 for Armenian language synthesis, featuring:
### 🚀 **Performance Optimizations**
- **Intelligent Text Chunking**: Handles long texts by splitting them intelligently at sentence boundaries
- **Caching**: Translation and embedding caching for faster repeated requests
- **Mixed Precision**: GPU optimization with FP16 inference when available
- **Crossfading**: Smooth audio transitions between chunks for natural-sounding longer texts
### 🎯 **Advanced Features**
- **Smart Text Processing**: Automatic number-to-word conversion with Armenian translation
- **Audio Post-Processing**: Noise gating, normalization, and dynamic range optimization
- **Robust Error Handling**: Graceful fallbacks and comprehensive logging
- **Real-time Performance Monitoring**: Track processing times and system health
### 📝 **Usage Tips**
- **Short texts** (< 200 chars): Processed directly for maximum speed
- **Long texts**: Automatically chunked with overlap for seamless audio
- **Numbers**: Automatically converted to Armenian words
- **Performance**: Enable chunking for texts longer than a few sentences
### 🎵 **Audio Quality**
- Sample Rate: 16 kHz
- Optimized for natural prosody and clear pronunciation
- Cross-fade transitions for multi-chunk synthesis
The model was trained on short clips (5-20s) but uses advanced algorithms to handle longer texts effectively.
"""
EXAMPLES = [
# Short examples for quick testing
["Բարև ձեզ, ինչպե՞ս եք:", "BDL (male)", True, True],
["Այսօր գեղեցիկ օր է:", "BDL (male)", False, True],
# Medium examples demonstrating chunking
["Հայաստանն ունի հարուստ պատմություն և մշակույթ: Երևանը մայրաքաղաքն է, որն ունի 2800 տարվա պատմություն:", "BDL (male)", True, True],
# Long example with numbers
["Արարատ լեռը բարձրությունը 5165 մետր է: Այն Հայաստանի խորհրդանիշն է և գտնվում է Թուրքիայի տարածքում: Լեռան վրա ըստ Աստվածաշնչի՝ կանգնել է Նոյի տապանը 40 օրվա ջրհեղեղից հետո:", "BDL (male)", True, True],
# Technical example
["Մեքենայի շարժիչը 150 ձիուժ է և 2.0 լիտր ծավալ ունի: Այն կարող է արագացնել 0-ից 100 կմ/ժ 8.5 վայրկյանում:", "BDL (male)", True, True],
]
# Custom CSS for better styling
CUSTOM_CSS = """
.gradio-container {
max-width: 1200px !important;
margin: auto !important;
}
.performance-info {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 15px;
border-radius: 10px;
color: white;
margin: 10px 0;
}
.health-status {
padding: 10px;
border-radius: 8px;
margin: 10px 0;
font-weight: bold;
}
.status-healthy { background-color: #d4edda; color: #155724; }
.status-warning { background-color: #fff3cd; color: #856404; }
.status-error { background-color: #f8d7da; color: #721c24; }
"""
def create_interface():
"""Create and configure the Gradio interface."""
with gr.Blocks(
theme=gr.themes.Soft(),
css=CUSTOM_CSS,
title="SpeechT5 Armenian TTS"
) as interface:
# Header
gr.Markdown(f"# {TITLE}")
gr.Markdown(DESCRIPTION)
with gr.Row():
with gr.Column(scale=2):
# Main input controls
text_input = gr.Textbox(
label="📝 Input Text (Armenian)",
placeholder="Մուտքագրեք ձեր տեքստը այստեղ...",
lines=3,
max_lines=10
)
with gr.Row():
speaker_input = gr.Radio(
label="🎭 Speaker",
choices=["BDL (male)"],
value="BDL (male)"
)
with gr.Row():
chunking_checkbox = gr.Checkbox(
label="🧩 Enable Intelligent Chunking",
value=True,
info="Automatically split long texts for better quality"
)
processing_checkbox = gr.Checkbox(
label="🎚️ Apply Audio Processing",
value=True,
info="Apply noise gating, normalization, and crossfading"
)
# Generate button
generate_btn = gr.Button(
"🎤 Generate Speech",
variant="primary",
size="lg"
)
with gr.Column(scale=1):
# System information panel
gr.Markdown("### 📊 System Status")
health_display = gr.Textbox(
label="Health Status",
value="Initializing...",
interactive=False,
max_lines=1
)
performance_display = gr.Textbox(
label="Performance Stats",
value="No data yet",
interactive=False,
max_lines=8
)
refresh_btn = gr.Button("🔄 Refresh Stats", size="sm")
# Output
audio_output = gr.Audio(
label="🔊 Generated Speech",
type="numpy",
interactive=False
)
# Examples section
gr.Markdown("### 💡 Example Texts")
# Use simpler Examples component to avoid schema issues
examples = gr.Examples(
examples=EXAMPLES,
inputs=[text_input, speaker_input, chunking_checkbox, processing_checkbox],
outputs=audio_output,
fn=predict,
cache_examples=False, # Disable caching to avoid schema issues
label="Click any example to try it:"
)
# Event handlers
generate_btn.click(
fn=predict,
inputs=[text_input, speaker_input, chunking_checkbox, processing_checkbox],
outputs=[audio_output],
show_progress="full"
)
refresh_btn.click(
fn=lambda: (health_check(), get_performance_info()),
outputs=[health_display, performance_display],
show_progress="minimal"
)
# Auto-refresh health status on load
interface.load(
fn=lambda: (health_check(), get_performance_info()),
outputs=[health_display, performance_display]
)
return interface
def main():
"""Main application entry point."""
logger.info("Starting SpeechT5 Armenian TTS Application")
# Initialize pipeline
if not initialize_pipeline():
logger.error("Failed to initialize TTS pipeline - exiting")
sys.exit(1)
# Create and launch interface
interface = create_interface()
# Launch with optimized settings
interface.launch(
share=False, # Disable share for HF Spaces
inbrowser=False,
show_error=True,
quiet=False,
server_name="0.0.0.0", # Allow external connections
server_port=7860, # Standard Gradio port
max_threads=4, # Limit concurrent requests
)
if __name__ == "__main__":
main()
|