File size: 3,046 Bytes
3f1840e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
SpeechT5 Armenian TTS - Minimal HF Spaces Version
================================================

Ultra-minimal version to avoid Gradio schema issues.
"""

import gradio as gr
import numpy as np
import logging
import os
import sys

# Setup basic logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Global pipeline
pipeline = None

def init_pipeline():
    """Initialize pipeline with maximum error protection."""
    global pipeline
    try:
        # Add src path
        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)
        
        from src.pipeline import TTSPipeline
        
        pipeline = TTSPipeline(
            model_checkpoint="Edmon02/TTS_NB_2",
            max_chunk_length=200,
            use_mixed_precision=True
        )
        pipeline.optimize_for_production()
        logger.info("Pipeline initialized")
        return True
    except Exception as e:
        logger.error(f"Pipeline init failed: {e}")
        # Create minimal fallback
        class DummyPipeline:
            def synthesize(self, text, **kwargs):
                # Generate a simple tone
                duration = min(len(text) * 0.1, 2.0)
                sr = 16000
                samples = int(duration * sr)
                t = np.linspace(0, duration, samples)
                audio = np.sin(2 * np.pi * 440 * t) * 0.1
                return sr, (audio * 32767).astype(np.int16)
        
        pipeline = DummyPipeline()
        return False

def tts_function(text):
    """Main TTS function with minimal interface."""
    if not text or not text.strip():
        # Return short silence
        return 16000, np.zeros(8000, dtype=np.int16)
    
    try:
        logger.info(f"Processing: {text[:30]}...")
        sr, audio = pipeline.synthesize(
            text=text,
            speaker="BDL",
            enable_chunking=True,
            apply_audio_processing=True
        )
        logger.info(f"Generated {len(audio)} samples")
        return sr, audio
    except Exception as e:
        logger.error(f"TTS error: {e}")
        # Return silence on error
        return 16000, np.zeros(8000, dtype=np.int16)

# Initialize pipeline
logger.info("Starting TTS app...")
success = init_pipeline()
status = "✅ Ready" if success else "⚠️ Test Mode"

# Create minimal interface
iface = gr.Interface(
    fn=tts_function,
    inputs=gr.inputs.Textbox(
        lines=2, 
        placeholder="Enter Armenian text...",
        label="Armenian Text"
    ),
    outputs=gr.outputs.Audio(label="Speech"),
    title=f"🎤 Armenian TTS {status}",
    description="Convert Armenian text to speech.",
    examples=[
        "Բարև ձեզ:",
        "Ինչպե՞ս եք:",
        "Շնորհակալություն:",
    ]
)

# Launch with minimal config
if __name__ == "__main__":
    iface.launch(
        server_name="0.0.0.0",
        server_port=7860
    )