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ac2cb8
 
b2c4ab7
 
6ac2cb8
a7ffad5
 
 
6ac2cb8
a7ffad5
 
6ac2cb8
b2c4ab7
 
 
6ac2cb8
 
 
c4f9f48
a7ffad5
e392e54
 
6ac2cb8
 
 
b2c4ab7
 
6ac2cb8
a7ffad5
 
b2c4ab7
6ac2cb8
b2c4ab7
 
6ac2cb8
 
 
b2c4ab7
6ac2cb8
a7ffad5
b2c4ab7
6ac2cb8
b2c4ab7
 
e392e54
a7ffad5
b2c4ab7
 
6ac2cb8
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
import gradio as gr
import numpy as np
import re
import soundfile as sf
import tempfile
import os
import nltk
from nltk.tokenize import sent_tokenize
import warnings
import time
from TTS.api import TTS

warnings.filterwarnings("ignore")

# Download required NLTK data
try:
    nltk.data.find('tokenizers/punkt')
except LookupError:
    nltk.download('punkt')

# Load Coqui TTS model
print("πŸ”„ Loading Coqui TTS models...")
try:
    # This will download the model automatically if not found
    tts = TTS(model_name="tts_models/en/vctk/vits", progress_bar=False, gpu=False)
    print("βœ… Coqui TTS loaded successfully!")
    speakers = tts.speakers
    print(f"Available Speakers: {speakers}")
except Exception as e:
    print(f"❌ Failed to load Coqui TTS: {e}")
    tts = None
    speakers = []

class LongFormTTS:
    def __init__(self):
        self.tts = tts
        self.speakers = speakers
        self.sample_rate = 22050  # Coqui default sample rate

    def preprocess_text(self, text):
        """Clean and prepare text for TTS"""
        text = re.sub(r'\s+', ' ', text.strip())
        abbreviations = {
            'Dr.': 'Doctor', 'Mr.': 'Mister', 'Mrs.': 'Missus',
            'Ms.': 'Miss', 'Prof.': 'Professor', 'etc.': 'etcetera',
            'vs.': 'versus', 'e.g.': 'for example', 'i.e.': 'that is',
            'St.': 'Street', 'Ave.': 'Avenue', 'Blvd.': 'Boulevard',
            'Inc.': 'Incorporated', 'Corp.': 'Corporation', 'Ltd.': 'Limited',
            'U.S.': 'United States', 'U.K.': 'United Kingdom',
            'Ph.D.': 'PhD', 'M.D.': 'MD'
        }
        for abbr, full in abbreviations.items():
            text = text.replace(abbr, full)
        text = re.sub(r'\b(\d{1,4})\b', lambda m: self.number_to_words(int(m.group())), text)
        text = re.sub(r'\b(1[0-9]{3}|20[0-9]{2}|2100)\b', lambda m: m.group(), text)
        text = re.sub(r'[^\w\s\.,!?;:\-\(\)\'"]', ' ', text)
        return text.strip()

    def number_to_words(self, num):
        ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
        teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
                 "sixteen", "seventeen", "eighteen", "nineteen"]
        tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy",
                "eighty", "ninety"]
        if num == 0:
            return "zero"
        if num < 10:
            return ones[num]
        elif num < 20:
            return teens[num - 10]
        elif num < 100:
            return tens[num // 10] + ("" if num % 10 == 0 else " " + ones[num % 10])
        elif num < 1000:
            return ones[num // 100] + " hundred" + (" " + self.number_to_words(num % 100)).strip()
        else:
            thousands = num // 1000
            remainder = num % 1000
            result = self.number_to_words(thousands) + " thousand"
            if remainder > 0:
                result += " " + self.number_to_words(remainder)
            return result

    def chunk_text(self, text, max_length=200):
        sentences = sent_tokenize(text)
        chunks = []
        current_chunk = ""
        for sentence in sentences:
            if len(current_chunk + " " + sentence) > max_length:
                if current_chunk:
                    chunks.append(current_chunk.strip())
                current_chunk = sentence
            else:
                current_chunk = current_chunk + " " + sentence if current_chunk else sentence
        if current_chunk:
            chunks.append(current_chunk.strip())
        return [chunk for chunk in chunks if chunk.strip()]

    def generate_speech_chunk(self, text_chunk, speaker):
        try:
            audio = self.tts.tts(text=text_chunk, speaker=speaker)
            return audio
        except Exception as e:
            print(f"Error generating speech for chunk: {e}")
            return None

    def generate_long_speech(self, text, speaker=None, progress_callback=None):
        processed_text = self.preprocess_text(text)
        chunks = self.chunk_text(processed_text)
        print(f"Split into {len(chunks)} chunks")
        audio_segments = []
        silence = np.zeros(int(0.4 * self.sample_rate), dtype=np.float32)

        for i, chunk in enumerate(chunks):
            if progress_callback:
                progress_callback(f"Processing chunk {i+1}/{len(chunks)}: {chunk[:40]}{'...' if len(chunk) > 40 else ''}")
            print(f"Processing chunk {i+1}: {chunk}")
            audio_chunk = self.generate_speech_chunk(chunk, speaker)
            if audio_chunk is not None:
                audio_segments.append(np.array(audio_chunk))
                audio_segments.append(silence)
            time.sleep(0.1)

        if not audio_segments:
            return None, None

        final_audio = np.concatenate(audio_segments)
        max_val = np.max(np.abs(final_audio))
        if max_val > 0:
            final_audio = final_audio / max_val * 0.95
        return final_audio, self.sample_rate


# Global TTS system
print("πŸš€ Initializing TTS system...")
try:
    tts_system = LongFormTTS()
    print("βœ… TTS system ready!")
except Exception as e:
    print(f"❌ TTS initialization failed: {e}")
    tts_system = None


def text_to_speech_interface(text, speaker="p225", progress=gr.Progress()):
    if tts_system is None:
        return None, "❌ TTS system is not available. Please check the logs."
    if not text or not text.strip():
        return None, "⚠️ Please enter some text to convert to speech."
    if len(text) > 50000:
        return None, "⚠️ Text is too long. Please keep it under 50,000 characters."

    def progress_callback(message):
        progress(0.5, desc=message)

    try:
        progress(0.1, desc="πŸ”„ Starting text-to-speech conversion...")
        audio, sample_rate = tts_system.generate_long_speech(text, speaker, progress_callback)
        if audio is None:
            return None, "❌ Failed to generate audio."
        progress(0.9, desc="πŸ’Ύ Saving audio file...")
        with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
            sf.write(tmp_file.name, audio, sample_rate)
            audio_path = tmp_file.name
        progress(1.0, desc="βœ… Complete!")
        duration = len(audio) / sample_rate
        return audio_path, f"βœ… Generated {duration:.1f} seconds of audio successfully!"
    except Exception as e:
        return None, f"❌ Error: {str(e)}"


# Create Gradio interface
def create_interface():
    with gr.Blocks(
        title="🎀 Long-Form Text-to-Speech (Coqui)",
        theme=gr.themes.Soft(),
        css="""
        .main-header {
            text-align: center;
            margin-bottom: 2rem;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        """
    ) as demo:
        gr.HTML("""
        <div class="main-header">
            <h1>🎀 Long-Form Text-to-Speech Generator</h1>
            <p style="color: #666; font-size: 1.1em;">Choose a voice and transform any text into expressive human-like speech</p>
        </div>
        """)
        if tts_system:
            gr.HTML("""
            <div style="padding: 1rem; border-radius: 10px; margin: 1rem 0; border-left: 4px solid #28a745; background: #f8f9fa;">
                <h4>🟒 System Ready</h4>
                <p>Using <strong>Coqui TTS</strong> with multiple speaker support</p>
            </div>
            """)
        else:
            gr.HTML("""
            <div style="padding: 1rem; border-radius: 10px; margin: 1rem 0; border-left: 4px solid #dc3545; background: #f8d7da;">
                <h4>πŸ”΄ System Error</h4>
                <p>TTS system failed to initialize. Please refresh the page.</p>
            </div>
            """)
        with gr.Row():
            with gr.Column(scale=2):
                text_input = gr.Textbox(
                    label="πŸ“ Enter Your Text",
                    placeholder="Type or paste your text here... (Max 50,000 characters)",
                    lines=10,
                    max_lines=20,
                    info="Supports any length text with automatic chunking for optimal quality"
                )
                char_count = gr.HTML("<span style='color: #666;'>Character count: 0 / 50,000</span>")
                speaker_dropdown = gr.Dropdown(choices=tts_system.speakers if tts_system else [], value="p225", label="πŸ—£οΈ Choose Voice")
                generate_btn = gr.Button("🎯 Generate Speech", variant="primary", size="lg", scale=1)
            with gr.Column(scale=1):
                gr.HTML("""
                <div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 1.5rem; border-radius: 15px; margin: 1rem 0; box-shadow: 0 4px 15px rgba(0,0,0,0.1);">
                    <h3>✨ Key Features</h3>
                    <ul style="margin: 0; padding-left: 1.2em;">
                        <li>πŸš€ Handles long texts</li>
                        <li>🎭 Multiple human voices</li>
                        <li>⚑ Smart text processing</li>
                        <li>πŸ”§ Auto chunking</li>
                        <li>🎡 Natural-sounding speech</li>
                    </ul>
                </div>
                """)
        status_output = gr.Textbox(label="πŸ“Š Status", interactive=False, value="Ready to generate speech! Enter some text above.")
        audio_output = gr.Audio(label="πŸ”Š Generated Speech", type="filepath", show_download_button=True)

        def update_char_count(text):
            count = len(text) if text else 0
            color = "#28a745" if count <= 50000 else "#dc3545"
            return f'<span style="color: {color};">Character count: {count:,} / 50,000</span>'

        text_input.change(fn=update_char_count, inputs=[text_input], outputs=[char_count])

        generate_btn.click(
            fn=text_to_speech_interface,
            inputs=[text_input, speaker_dropdown],
            outputs=[audio_output, status_output],
            show_progress=True
        )

        gr.Examples(
            examples=[
                ["Hello! Welcome to our advanced text-to-speech system. This technology can convert any written text into natural-sounding human speech.", "p225"],
                ["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.", "p226"],
                ["Artificial intelligence has revolutionized many aspects of our daily lives.", "p227"],
            ],
            inputs=[text_input, speaker_dropdown],
            label="πŸ“š Try These Examples"
        )

    return demo


# Launch the application
if __name__ == "__main__":
    demo = create_interface()
    demo.launch(server_name="0.0.0.0", server_port=7860, share=True)