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 
 
c4f9f48
 
 
 
b2c4ab7
 
c4f9f48
 
b2c4ab7
 
 
 
 
 
 
 
 
 
 
c4f9f48
 
b2c4ab7
c4f9f48
 
 
b2c4ab7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4f9f48
 
 
 
 
 
 
 
 
 
 
 
b2c4ab7
 
 
 
 
 
 
 
 
 
c4f9f48
 
 
 
b2c4ab7
 
 
 
 
 
 
 
c4f9f48
 
 
 
b2c4ab7
c4f9f48
b2c4ab7
 
 
 
 
 
 
 
 
 
 
 
 
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import gradio as gr
import torch
import numpy as np
import re
from transformers import pipeline
import soundfile as sf
import io
import tempfile
import os
from pydub import AudioSegment
import nltk
from nltk.tokenize import sent_tokenize
import warnings
import time
warnings.filterwarnings("ignore")

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

class LongFormTTS:
    def __init__(self):
        print("Loading TTS models...")
        
        # Try multiple TTS approaches for better compatibility
        self.tts_pipeline = None
        self.backup_tts = None
        
        # Primary: Try Bark (works well on HF Spaces)
        try:
            print("Loading Bark TTS...")
            self.tts_pipeline = pipeline(
                "text-to-speech",
                model="suno/bark-small",
                torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
            )
            self.tts_method = "bark"
            print("βœ… Bark TTS loaded successfully!")
        except Exception as e:
            print(f"❌ Bark TTS failed: {e}")
            
            # Backup: Try Parler TTS
            try:
                print("Loading Parler TTS...")
                self.tts_pipeline = pipeline(
                    "text-to-speech",
                    model="parler-tts/parler_tts_mini_v0.1",
                    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
                )
                self.tts_method = "parler"
                print("βœ… Parler TTS loaded successfully!")
            except Exception as e:
                print(f"❌ Parler TTS failed: {e}")
                
                # Final backup: Try FastSpeech2
                try:
                    print("Loading FastSpeech2...")
                    from TTS.api import TTS
                    self.backup_tts = TTS(model_name="tts_models/en/ljspeech/fastspeech2")
                    self.tts_method = "fastspeech2"
                    print("βœ… FastSpeech2 loaded successfully!")
                except Exception as e:
                    print(f"❌ All TTS models failed: {e}")
                    raise Exception("No TTS model could be loaded. Please check the requirements.")
    
    def preprocess_text(self, text):
        """Clean and prepare text for TTS"""
        # Remove extra whitespace
        text = re.sub(r'\s+', ' ', text.strip())
        
        # Handle common abbreviations
        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',
        }
        
        for abbr, full in abbreviations.items():
            text = text.replace(abbr, full)
        
        # Handle numbers (basic)
        text = re.sub(r'\b(\d+)\b', lambda m: self.number_to_words(int(m.group())), text)
        
        # Clean up any problematic characters
        text = re.sub(r'[^\w\s\.,!?;:\-\(\)]', '', text)
        
        return text
    
    def number_to_words(self, num):
        """Convert numbers to words (basic implementation)"""
        if num == 0:
            return "zero"
        
        if num > 9999:
            return str(num)  # Keep large numbers as digits
        
        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 < 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" + ("" if num % 100 == 0 else " " + self.number_to_words(num % 100))
        else:
            return str(num)
    
    def chunk_text(self, text, max_length=200):
        """Split text into manageable chunks while preserving sentence boundaries"""
        sentences = sent_tokenize(text)
        chunks = []
        current_chunk = ""
        
        for sentence in sentences:
            # If single sentence is too long, split by clauses
            if len(sentence) > max_length:
                clauses = re.split(r'[,;:]', sentence)
                for clause in clauses:
                    clause = clause.strip()
                    if len(current_chunk + clause) > max_length:
                        if current_chunk:
                            chunks.append(current_chunk.strip())
                            current_chunk = clause
                        else:
                            # Even single clause is too long, force split
                            words = clause.split()
                            temp_chunk = ""
                            for word in words:
                                if len(temp_chunk + word) > max_length:
                                    if temp_chunk:
                                        chunks.append(temp_chunk.strip())
                                        temp_chunk = word
                                    else:
                                        chunks.append(word)
                                else:
                                    temp_chunk += " " + word if temp_chunk else word
                            if temp_chunk:
                                current_chunk = temp_chunk
                    else:
                        current_chunk += " " + clause if current_chunk else clause
            else:
                if len(current_chunk + sentence) > max_length:
                    if current_chunk:
                        chunks.append(current_chunk.strip())
                        current_chunk = sentence
                    else:
                        chunks.append(sentence)
                else:
                    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):
        """Generate speech for a single text chunk"""
        try:
            if self.tts_method == "bark":
                # Bark TTS
                speech = self.tts_pipeline(text_chunk)
                audio = speech["audio"]
                sampling_rate = speech["sampling_rate"]
                return audio, sampling_rate
                
            elif self.tts_method == "parler":
                # Parler TTS
                speech = self.tts_pipeline(text_chunk)
                audio = speech["audio"]
                sampling_rate = speech["sampling_rate"]
                return audio, sampling_rate
                
            elif self.tts_method == "fastspeech2":
                # FastSpeech2 via TTS library
                with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
                    self.backup_tts.tts_to_file(text=text_chunk, file_path=tmp_file.name)
                    audio, sr = sf.read(tmp_file.name)
                    os.unlink(tmp_file.name)
                    return audio, sr
            else:
                raise Exception("No TTS method available")
                
        except Exception as e:
            print(f"Error generating speech for chunk: {e}")
            return None, None
    
    def generate_long_speech(self, text, progress_callback=None):
        """Generate speech for long text by processing in chunks"""
        # Preprocess text
        text = self.preprocess_text(text)
        
        # Split into chunks
        chunks = self.chunk_text(text, max_length=150)  # Smaller chunks for better compatibility
        print(f"Split text into {len(chunks)} chunks")
        
        if not chunks:
            return None, None
        
        # Generate speech for each chunk
        audio_segments = []
        sampling_rate = None
        total_chunks = len(chunks)
        
        for i, chunk in enumerate(chunks):
            if progress_callback:
                progress_callback(f"Processing chunk {i+1}/{total_chunks}: {chunk[:30]}...")
            
            audio_chunk, sr = self.generate_speech_chunk(chunk)
            
            if audio_chunk is not None and len(audio_chunk) > 0:
                if sampling_rate is None:
                    sampling_rate = sr
                
                # Ensure audio is 1D
                if len(audio_chunk.shape) > 1:
                    audio_chunk = audio_chunk.mean(axis=1)
                
                audio_segments.append(audio_chunk)
                
                # Add small pause between chunks (300ms of silence)
                pause_duration = int(0.3 * sampling_rate)
                silence = np.zeros(pause_duration)
                audio_segments.append(silence)
            
            # Small delay to prevent overwhelming the system
            time.sleep(0.1)
        
        if not audio_segments:
            return None, None
        
        # Concatenate all audio segments
        final_audio = np.concatenate(audio_segments)
        
        return final_audio, sampling_rate

# Initialize TTS system
print("Initializing TTS system...")
try:
    tts_system = LongFormTTS()
    print("βœ… TTS system initialized successfully!")
except Exception as e:
    print(f"❌ Failed to initialize TTS system: {e}")
    tts_system = None

def text_to_speech_interface(text, progress=gr.Progress()):
    """Main interface function for Gradio"""
    if tts_system is None:
        return None, "❌ TTS system not available. Please check the logs."
    
    if not text.strip():
        return None, "Please enter some text to convert to speech."
    
    if len(text) > 10000:
        return None, "Text is too long. Please keep it under 10,000 characters for optimal performance."
    
    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, progress_callback)
        
        if audio is None or len(audio) == 0:
            return None, "Failed to generate audio. Please try with shorter text or check your input."
        
        progress(0.9, desc="Finalizing audio...")
        
        # Save to temporary 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"βœ… Successfully generated {duration:.1f} seconds of audio using {tts_system.tts_method.upper()}!"
        
    except Exception as e:
        error_msg = f"❌ Error: {str(e)}"
        print(error_msg)
        return None, error_msg

# Create Gradio interface
def create_interface():
    with gr.Blocks(
        title="🎀 Long-Form Text-to-Speech Generator",
        theme=gr.themes.Soft(),
        css="""
        .main-header {
            text-align: center;
            margin-bottom: 2rem;
        }
        .feature-box {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem;
            border-radius: 10px;
            margin: 1rem 0;
        }
        .status-box {
            background: #f8f9fa;
            border-left: 4px solid #007bff;
            padding: 1rem;
            margin: 1rem 0;
        }
        """
    ) as demo:
        
        gr.HTML("""
        <div class="main-header">
            <h1>🎀 Long-Form Text-to-Speech Generator</h1>
            <p>Convert any length of text to natural human-like speech using free AI models</p>
        </div>
        """)
        
        # Show TTS system status
        if tts_system is not None:
            status_html = f"""
            <div class="status-box">
                <h4>🟒 System Status: Ready</h4>
                <p>Using <strong>{tts_system.tts_method.upper()}</strong> TTS engine</p>
            </div>
            """
        else:
            status_html = """
            <div class="status-box" style="border-left-color: #dc3545;">
                <h4>πŸ”΄ System Status: Error</h4>
                <p>TTS system failed to initialize. Please check the logs.</p>
            </div>
            """
        
        gr.HTML(status_html)
        
        with gr.Row():
            with gr.Column(scale=2):
                text_input = gr.Textbox(
                    label="πŸ“ Enter your text (max 10,000 characters)",
                    placeholder="Type or paste your text here...",
                    lines=8,
                    max_lines=15
                )
                
                char_count = gr.HTML("Character count: 0")
                
                generate_btn = gr.Button(
                    "🎯 Generate Speech",
                    variant="primary",
                    size="lg"
                )
            
            with gr.Column(scale=1):
                gr.HTML("""
                <div class="feature-box">
                    <h3>✨ Features</h3>
                    <ul>
                        <li>πŸš€ Long text support</li>
                        <li>πŸ€– Multiple TTS engines</li>
                        <li>⚑ Smart text chunking</li>
                        <li>πŸ†“ Completely free</li>
                        <li>πŸ”§ Auto preprocessing</li>
                        <li>πŸ“± Mobile friendly</li>
                    </ul>
                </div>
                """)
        
        status_text = gr.Textbox(
            label="πŸ“Š Status",
            interactive=False,
            value="Ready to generate speech!"
        )
        
        audio_output = gr.Audio(
            label="πŸ”Š Generated Speech",
            type="filepath"
        )
        
        # Character counter
        def update_char_count(text):
            count = len(text)
            color = "green" if count <= 10000 else "red"
            return f'<span style="color: {color};">Character count: {count}/10,000</span>'
        
        text_input.change(
            fn=update_char_count,
            inputs=[text_input],
            outputs=[char_count]
        )
        
        # Event handlers
        generate_btn.click(
            fn=text_to_speech_interface,
            inputs=[text_input],
            outputs=[audio_output, status_text]
        )
        
        # Example texts
        gr.Examples(
            examples=[
                ["Hello! This is a test of the text-to-speech system. It can handle longer texts by splitting them into smaller chunks."],
                ["The quick brown fox jumps over the lazy dog. This sentence contains every letter of the alphabet."],
                ["In a hole in the ground there lived a hobbit. Not a nasty, dirty, wet hole, but a comfortable hobbit-hole."],
                ["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."]
            ],
            inputs=[text_input]
        )
        
        gr.HTML("""
        <div style="margin-top: 2rem; padding: 1rem; background: #f0f0f0; border-radius: 5px;">
            <h4>πŸ”§ How it works:</h4>
            <ol>
                <li><strong>Multiple Engines:</strong> Tries Bark, Parler, or FastSpeech2 TTS models</li>
                <li><strong>Smart Chunking:</strong> Splits long text at natural boundaries</li>
                <li><strong>Audio Processing:</strong> Combines chunks with natural pauses</li>
                <li><strong>Quality Output:</strong> Generates high-quality WAV audio</li>
            </ol>
            <p><em>πŸ’‘ Tip: For best results, use well-formatted text with proper punctuation!</em></p>
        </div>
        """)
    
    return demo

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