hivecorp commited on
Commit
6c4df3d
·
verified ·
1 Parent(s): 3fd60fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -570
app.py CHANGED
@@ -1,604 +1,189 @@
1
  import gradio as gr
2
- from pydub import AudioSegment
3
  import edge_tts
4
- import os
5
  import asyncio
6
- import uuid
7
- import re
8
- import time
9
  import tempfile
10
- from concurrent.futures import ThreadPoolExecutor
11
- from typing import List, Tuple, Optional, Dict, Any
12
- import math
13
- from dataclasses import dataclass
14
-
15
- class TimingManager:
16
- def __init__(self):
17
- self.current_time = 0
18
- self.segment_gap = 100 # ms gap between segments
19
-
20
- def get_timing(self, duration):
21
- start_time = self.current_time
22
- end_time = start_time + duration
23
- self.current_time = end_time + self.segment_gap
24
- return start_time, end_time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- def get_audio_length(audio_file):
27
- audio = AudioSegment.from_file(audio_file)
28
- return len(audio) / 1000
 
 
 
 
 
29
 
30
- def format_time_ms(milliseconds):
31
- seconds, ms = divmod(int(milliseconds), 1000)
32
- mins, secs = divmod(seconds, 60)
33
- hrs, mins = divmod(mins, 60)
34
- return f"{hrs:02}:{mins:02}:{secs:02},{ms:03}"
35
 
36
- @dataclass
37
- class Segment:
38
- id: int
39
- text: str
40
- start_time: int = 0
41
- end_time: int = 0
42
- duration: int = 0
43
- audio: Optional[AudioSegment] = None
44
- lines: List[str] = None # Add lines field for display purposes only
45
 
46
- class TextProcessor:
47
- def __init__(self, words_per_line: int, lines_per_segment: int):
48
- self.words_per_line = words_per_line
49
- self.lines_per_segment = lines_per_segment
50
- self.min_segment_words = 3
51
- self.max_segment_words = words_per_line * lines_per_segment * 1.5 # Allow 50% more for natural breaks
52
- self.punctuation_weights = {
53
- '.': 1.0, # Strong break
54
- '!': 1.0,
55
- '?': 1.0,
56
- ';': 0.8, # Medium-strong break
57
- ':': 0.7,
58
- ',': 0.5, # Medium break
59
- '-': 0.3, # Weak break
60
- '(': 0.2,
61
- ')': 0.2
62
- }
63
 
64
- def analyze_sentence_complexity(self, text: str) -> float:
65
- """Analyze sentence complexity to determine optimal segment length"""
66
- words = text.split()
67
- complexity = 1.0
68
-
69
- # Adjust for sentence length
70
- if len(words) > self.words_per_line * 2:
71
- complexity *= 1.2
72
-
73
- # Adjust for punctuation density
74
- punct_count = sum(text.count(p) for p in self.punctuation_weights.keys())
75
- complexity *= (1 + (punct_count / len(words)) * 0.5)
76
-
77
- return complexity
78
 
79
- def find_natural_breaks(self, text: str) -> List[Tuple[int, float]]:
80
- """Find natural break points with their weights"""
81
- breaks = []
82
- words = text.split()
83
-
84
- for i, word in enumerate(words):
85
- weight = 0
86
-
87
- # Check for punctuation
88
- for punct, punct_weight in self.punctuation_weights.items():
89
- if word.endswith(punct):
90
- weight = max(weight, punct_weight)
91
-
92
- # Check for natural phrase boundaries
93
- phrase_starters = {'however', 'therefore', 'moreover', 'furthermore', 'meanwhile', 'although', 'because'}
94
- if i < len(words) - 1 and words[i+1].lower() in phrase_starters:
95
- weight = max(weight, 0.6)
96
-
97
- # Check for conjunctions at natural points
98
- if i > self.min_segment_words:
99
- conjunctions = {'and', 'but', 'or', 'nor', 'for', 'yet', 'so'}
100
- if word.lower() in conjunctions:
101
- weight = max(weight, 0.4)
102
-
103
- if weight > 0:
104
- breaks.append((i, weight))
105
-
106
- return breaks
107
 
108
- def split_into_segments(self, text: str) -> List[Segment]:
109
- # Normalize text and add proper spacing around punctuation
110
- text = re.sub(r'\s+', ' ', text.strip())
111
- text = re.sub(r'([.!?,;:])\s*', r'\1 ', text)
112
- text = re.sub(r'\s+([.!?,;:])', r'\1', text)
113
-
114
- # First, split into major segments by strong punctuation
115
- segments = []
116
- current_segment = []
117
- current_text = ""
118
- words = text.split()
119
-
120
- i = 0
121
- while i < len(words):
122
- complexity = self.analyze_sentence_complexity(' '.join(words[i:i + self.words_per_line * 2]))
123
- breaks = self.find_natural_breaks(' '.join(words[i:i + int(self.max_segment_words * complexity)]))
124
-
125
- # Find best break point
126
- best_break = None
127
- best_weight = 0
128
-
129
- for break_idx, weight in breaks:
130
- actual_idx = i + break_idx
131
- if (actual_idx - i >= self.min_segment_words and
132
- actual_idx - i <= self.max_segment_words):
133
- if weight > best_weight:
134
- best_break = break_idx
135
- best_weight = weight
136
-
137
- if best_break is None:
138
- # If no good break found, use maximum length
139
- best_break = min(self.words_per_line * self.lines_per_segment, len(words) - i)
140
-
141
- # Create segment
142
- segment_words = words[i:i + best_break + 1]
143
- segment_text = ' '.join(segment_words)
144
-
145
- # Split segment into lines
146
- lines = self.split_into_lines(segment_text)
147
- final_segment_text = '\n'.join(lines)
148
-
149
- segments.append(Segment(
150
- id=len(segments) + 1,
151
- text=final_segment_text
152
- ))
153
-
154
- i += best_break + 1
155
-
156
- return segments
157
 
158
- def split_into_lines(self, text: str) -> List[str]:
159
- """Split segment text into natural lines"""
160
- words = text.split()
161
- lines = []
162
- current_line = []
163
- word_count = 0
164
-
165
- for word in words:
166
- current_line.append(word)
167
- word_count += 1
168
-
169
- # Check for natural line breaks
170
- is_break = (
171
- word_count >= self.words_per_line or
172
- any(word.endswith(p) for p in '.!?') or
173
- (word_count >= self.words_per_line * 0.7 and
174
- any(word.endswith(p) for p in ',;:'))
175
- )
176
-
177
- if is_break:
178
- lines.append(' '.join(current_line))
179
- current_line = []
180
- word_count = 0
181
-
182
- if current_line:
183
- lines.append(' '.join(current_line))
184
-
185
- return lines
186
 
187
- # IMPROVEMENT 1: Enhanced Error Handling
188
- class TTSError(Exception):
189
- """Custom exception for TTS processing errors"""
190
- pass
 
191
 
192
- async def process_segment_with_timing(segment: Segment, voice: str, rate: str, pitch: str) -> Segment:
193
- """Process a complete segment as a single TTS unit with improved error handling"""
194
- audio_file = os.path.join(tempfile.gettempdir(), f"temp_segment_{segment.id}_{uuid.uuid4()}.wav")
195
- try:
196
- # Process the entire segment text as one unit, replacing newlines with spaces
197
- segment_text = ' '.join(segment.text.split('\n'))
198
- tts = edge_tts.Communicate(segment_text, voice, rate=rate, pitch=pitch)
199
-
200
- try:
201
- await tts.save(audio_file)
202
- except Exception as e:
203
- raise TTSError(f"Failed to generate audio for segment {segment.id}: {str(e)}")
204
-
205
- if not os.path.exists(audio_file) or os.path.getsize(audio_file) == 0:
206
- raise TTSError(f"Generated audio file is empty or missing for segment {segment.id}")
207
-
208
- try:
209
- segment.audio = AudioSegment.from_file(audio_file)
210
- # Reduced silence to 30ms for more natural flow
211
- silence = AudioSegment.silent(duration=30)
212
- segment.audio = silence + segment.audio + silence
213
- segment.duration = len(segment.audio)
214
- except Exception as e:
215
- raise TTSError(f"Failed to process audio file for segment {segment.id}: {str(e)}")
216
-
217
- return segment
218
- except Exception as e:
219
- if not isinstance(e, TTSError):
220
- raise TTSError(f"Unexpected error processing segment {segment.id}: {str(e)}")
221
- raise
222
- finally:
223
- if os.path.exists(audio_file):
224
- try:
225
- os.remove(audio_file)
226
- except Exception:
227
- pass # Ignore deletion errors
228
-
229
- # IMPROVEMENT 2: Better File Management with cleanup
230
- class FileManager:
231
- """Manages temporary and output files with cleanup capabilities"""
232
- def __init__(self):
233
- self.temp_dir = tempfile.mkdtemp(prefix="tts_app_")
234
- self.output_files = []
235
- self.max_files_to_keep = 5 # Keep only the 5 most recent output pairs
236
-
237
- def get_temp_path(self, prefix):
238
- """Get a path for a temporary file"""
239
- return os.path.join(self.temp_dir, f"{prefix}_{uuid.uuid4()}")
240
-
241
- def create_output_paths(self):
242
- """Create paths for output files"""
243
- unique_id = str(uuid.uuid4())
244
- audio_path = os.path.join(self.temp_dir, f"final_audio_{unique_id}.mp3")
245
- srt_path = os.path.join(self.temp_dir, f"final_subtitles_{unique_id}.srt")
246
-
247
- self.output_files.append((srt_path, audio_path))
248
- self.cleanup_old_files()
249
-
250
- return srt_path, audio_path
251
-
252
- def cleanup_old_files(self):
253
- """Clean up old output files, keeping only the most recent ones"""
254
- if len(self.output_files) > self.max_files_to_keep:
255
- old_files = self.output_files[:-self.max_files_to_keep]
256
- for srt_path, audio_path in old_files:
257
- try:
258
- if os.path.exists(srt_path):
259
- os.remove(srt_path)
260
- if os.path.exists(audio_path):
261
- os.remove(audio_path)
262
- except Exception:
263
- pass # Ignore deletion errors
264
 
265
- # Update the list to only include files we're keeping
266
- self.output_files = self.output_files[-self.max_files_to_keep:]
267
-
268
- def cleanup_all(self):
269
- """Clean up all managed files"""
270
- for srt_path, audio_path in self.output_files:
271
- try:
272
- if os.path.exists(srt_path):
273
- os.remove(srt_path)
274
- if os.path.exists(audio_path):
275
- os.remove(audio_path)
276
- except Exception:
277
- pass # Ignore deletion errors
278
-
279
- try:
280
- os.rmdir(self.temp_dir)
281
- except Exception:
282
- pass # Ignore if directory isn't empty or can't be removed
283
-
284
- # Create global file manager
285
- file_manager = FileManager()
286
 
287
- # IMPROVEMENT 3: Parallel Processing for Segments
288
- async def generate_accurate_srt(
289
- text: str,
290
- voice: str,
291
- rate: str,
292
- pitch: str,
293
- words_per_line: int,
294
- lines_per_segment: int,
295
- progress_callback=None,
296
- parallel: bool = True,
297
- max_workers: int = 4
298
- ) -> Tuple[str, str]:
299
- """Generate accurate SRT with parallel processing option"""
300
- processor = TextProcessor(words_per_line, lines_per_segment)
301
- segments = processor.split_into_segments(text)
302
-
303
- total_segments = len(segments)
304
- processed_segments = []
305
-
306
- # Update progress to show segmentation is complete
307
- if progress_callback:
308
- progress_callback(0.1, "Text segmentation complete")
309
-
310
- if parallel and total_segments > 1:
311
- # Process segments in parallel
312
- processed_count = 0
313
- segment_tasks = []
314
-
315
- # Create a semaphore to limit concurrent tasks
316
- semaphore = asyncio.Semaphore(max_workers)
317
-
318
- async def process_with_semaphore(segment):
319
- async with semaphore:
320
- nonlocal processed_count
321
- try:
322
- result = await process_segment_with_timing(segment, voice, rate, pitch)
323
- processed_count += 1
324
- if progress_callback:
325
- progress = 0.1 + (0.8 * processed_count / total_segments)
326
- progress_callback(progress, f"Processed {processed_count}/{total_segments} segments")
327
- return result
328
- except Exception as e:
329
- # Handle errors in individual segments
330
- processed_count += 1
331
- if progress_callback:
332
- progress = 0.1 + (0.8 * processed_count / total_segments)
333
- progress_callback(progress, f"Error in segment {segment.id}: {str(e)}")
334
- raise
335
-
336
- # Create tasks for all segments
337
- for segment in segments:
338
- segment_tasks.append(process_with_semaphore(segment))
339
-
340
- # Run all tasks and collect results
341
- try:
342
- processed_segments = await asyncio.gather(*segment_tasks)
343
- except Exception as e:
344
- if progress_callback:
345
- progress_callback(0.9, f"Error during parallel processing: {str(e)}")
346
- raise TTSError(f"Failed during parallel processing: {str(e)}")
347
- else:
348
- # Process segments sequentially (original method)
349
- for i, segment in enumerate(segments):
350
- try:
351
- processed_segment = await process_segment_with_timing(segment, voice, rate, pitch)
352
- processed_segments.append(processed_segment)
353
-
354
- if progress_callback:
355
- progress = 0.1 + (0.8 * (i + 1) / total_segments)
356
- progress_callback(progress, f"Processed {i + 1}/{total_segments} segments")
357
- except Exception as e:
358
- if progress_callback:
359
- progress_callback(0.9, f"Error processing segment {segment.id}: {str(e)}")
360
- raise TTSError(f"Failed to process segment {segment.id}: {str(e)}")
361
-
362
- # Sort segments by ID to ensure correct order
363
- processed_segments.sort(key=lambda s: s.id)
364
-
365
- if progress_callback:
366
- progress_callback(0.9, "Finalizing audio and subtitles")
367
 
368
- # Now combine the segments in the correct order
369
- current_time = 0
370
- final_audio = AudioSegment.empty()
371
- srt_content = ""
372
 
373
- for segment in processed_segments:
374
- # Calculate precise timing
375
- segment.start_time = current_time
376
- segment.end_time = current_time + segment.duration
377
-
378
- # Add to SRT with precise timing
379
- srt_content += (
380
- f"{segment.id}\n"
381
- f"{format_time_ms(segment.start_time)} --> {format_time_ms(segment.end_time)}\n"
382
- f"{segment.text}\n\n"
383
- )
384
-
385
- # Add to final audio with precise positioning
386
- final_audio = final_audio.append(segment.audio, crossfade=0)
387
-
388
- # Update timing with precise gap
389
- current_time = segment.end_time
390
-
391
- # Export with high precision
392
- srt_path, audio_path = file_manager.create_output_paths()
393
-
394
- try:
395
- # Export with optimized quality settings and compression
396
- export_params = {
397
- 'format': 'mp3',
398
- 'bitrate': '192k', # Reduced from 320k but still high quality
399
- 'parameters': [
400
- '-ar', '44100', # Standard sample rate
401
- '-ac', '2', # Stereo
402
- '-compression_level', '0', # Best compression
403
- '-qscale:a', '2' # High quality VBR encoding
404
- ]
405
- }
406
- final_audio.export(audio_path, **export_params)
407
-
408
- with open(srt_path, "w", encoding='utf-8') as f:
409
- f.write(srt_content)
410
- except Exception as e:
411
- if progress_callback:
412
- progress_callback(1.0, f"Error exporting final files: {str(e)}")
413
- raise TTSError(f"Failed to export final files: {str(e)}")
414
-
415
- if progress_callback:
416
- progress_callback(1.0, "Complete!")
417
-
418
- return srt_path, audio_path
419
 
420
- # IMPROVEMENT 4: Progress Reporting with proper error handling for older Gradio versions
421
- async def process_text_with_progress(
422
- text,
423
- pitch,
424
- rate,
425
- voice,
426
- words_per_line,
427
- lines_per_segment,
428
- parallel_processing,
429
- progress=gr.Progress()
430
- ):
431
- # Input validation
432
- if not text or text.strip() == "":
433
- return None, None, None, True, "Please enter some text to convert to speech."
434
 
435
- # Format pitch and rate strings
436
- pitch_str = f"{pitch:+d}Hz" if pitch != 0 else "+0Hz"
437
- rate_str = f"{rate:+d}%" if rate != 0 else "+0%"
438
 
439
- try:
440
- # Start progress tracking
441
- progress(0, "Preparing text...")
442
-
443
- def update_progress(value, status):
444
- progress(value, status)
445
-
446
- srt_path, audio_path = await generate_accurate_srt(
447
- text,
448
- voice_options[voice],
449
- rate_str,
450
- pitch_str,
451
- words_per_line,
452
- lines_per_segment,
453
- progress_callback=update_progress,
454
- parallel=parallel_processing
455
- )
456
-
457
- # If successful, return results and hide error
458
- return srt_path, audio_path, audio_path, False, ""
459
- except TTSError as e:
460
- # Return specific TTS error
461
- return None, None, None, True, f"TTS Error: {str(e)}"
462
- except Exception as e:
463
- # Return any other error
464
- return None, None, None, True, f"Unexpected error: {str(e)}"
465
-
466
- # Voice options dictionary
467
- voice_options = {
468
- "Andrew Male": "en-US-AndrewNeural",
469
- "Jenny Female": "en-US-JennyNeural",
470
- "Guy Male": "en-US-GuyNeural",
471
- "Ana Female": "en-US-AnaNeural",
472
- "Aria Female": "en-US-AriaNeural",
473
- "Brian Male": "en-US-BrianNeural",
474
- "Christopher Male": "en-US-ChristopherNeural",
475
- "Eric Male": "en-US-EricNeural",
476
- "Michelle Male": "en-US-MichelleNeural",
477
- "Roger Male": "en-US-RogerNeural",
478
- "Natasha Female": "en-AU-NatashaNeural",
479
- "William Male": "en-AU-WilliamNeural",
480
- "Clara Female": "en-CA-ClaraNeural",
481
- "Liam Female ": "en-CA-LiamNeural",
482
- "Libby Female": "en-GB-LibbyNeural",
483
- "Maisie": "en-GB-MaisieNeural",
484
- "Ryan": "en-GB-RyanNeural",
485
- "Sonia": "en-GB-SoniaNeural",
486
- "Thomas": "en-GB-ThomasNeural",
487
- "Sam": "en-HK-SamNeural",
488
- "Yan": "en-HK-YanNeural",
489
- "Connor": "en-IE-ConnorNeural",
490
- "Emily": "en-IE-EmilyNeural",
491
- "Neerja": "en-IN-NeerjaNeural",
492
- "Prabhat": "en-IN-PrabhatNeural",
493
- "Asilia": "en-KE-AsiliaNeural",
494
- "Chilemba": "en-KE-ChilembaNeural",
495
- "Abeo": "en-NG-AbeoNeural",
496
- "Ezinne": "en-NG-EzinneNeural",
497
- "Mitchell": "en-NZ-MitchellNeural",
498
- "James": "en-PH-JamesNeural",
499
- "Rosa": "en-PH-RosaNeural",
500
- "Luna": "en-SG-LunaNeural",
501
- "Wayne": "en-SG-WayneNeural",
502
- "Elimu": "en-TZ-ElimuNeural",
503
- "Imani": "en-TZ-ImaniNeural",
504
- "Leah": "en-ZA-LeahNeural",
505
- "Luke": "en-ZA-LukeNeural"
506
- # Add other voices as needed
507
- }
508
-
509
- # Register cleanup on exit
510
- import atexit
511
- atexit.register(file_manager.cleanup_all)
512
-
513
- # Create Gradio interface
514
- with gr.Blocks(title="Advanced TTS with Configurable SRT Generation") as app:
515
- gr.Markdown("# Advanced TTS with Configurable SRT Generation")
516
- gr.Markdown("Generate perfectly synchronized audio and subtitles with natural speech patterns.")
517
 
518
- with gr.Row():
519
- with gr.Column(scale=3):
520
- text_input = gr.Textbox(label="Enter Text", lines=10, placeholder="Enter your text here...")
521
-
522
- with gr.Column(scale=2):
523
- voice_dropdown = gr.Dropdown(
524
- label="Select Voice",
525
- choices=list(voice_options.keys()),
526
- value="Jenny Female"
527
- )
528
- pitch_slider = gr.Slider(
529
- label="Pitch Adjustment (Hz)",
530
- minimum=-10,
531
- maximum=10,
532
- value=0,
533
- step=1
534
- )
535
- rate_slider = gr.Slider(
536
- label="Rate Adjustment (%)",
537
- minimum=-25,
538
- maximum=25,
539
- value=0,
540
- step=1
541
- )
542
 
543
- with gr.Row():
544
- with gr.Column():
545
- words_per_line = gr.Slider(
546
- label="Words per Line",
547
- minimum=3,
548
- maximum=12,
549
- value=6,
550
- step=1,
551
- info="Controls how many words appear on each line of the subtitle"
552
- )
553
- with gr.Column():
554
- lines_per_segment = gr.Slider(
555
- label="Lines per Segment",
556
- minimum=1,
557
- maximum=4,
558
- value=2,
559
- step=1,
560
- info="Controls how many lines appear in each subtitle segment"
561
- )
562
- with gr.Column():
563
- parallel_processing = gr.Checkbox(
564
- label="Enable Parallel Processing",
565
- value=True,
566
- info="Process multiple segments simultaneously for faster conversion (recommended for longer texts)"
567
- )
568
 
569
- submit_btn = gr.Button("Generate Audio & Subtitles")
 
570
 
571
- # Add error message component
572
- error_output = gr.Textbox(label="Status", visible=False)
 
 
 
573
 
574
- with gr.Row():
575
- with gr.Column():
576
- audio_output = gr.Audio(label="Preview Audio")
577
- with gr.Column():
578
- srt_file = gr.File(label="Download SRT")
579
- audio_file = gr.File(label="Download Audio")
580
 
581
- # Handle button click with manual error handling instead of .catch()
582
- submit_btn.click(
583
- fn=process_text_with_progress,
584
  inputs=[
585
- text_input,
586
- pitch_slider,
587
- rate_slider,
588
- voice_dropdown,
589
- words_per_line,
590
- lines_per_segment,
591
- parallel_processing
592
  ],
593
  outputs=[
594
- srt_file,
595
- audio_file,
596
- audio_output,
597
- error_output,
598
- error_output
599
  ],
600
- api_name="generate"
 
 
 
 
601
  )
 
602
 
 
603
  if __name__ == "__main__":
604
- app.launch()
 
 
1
  import gradio as gr
 
2
  import edge_tts
 
3
  import asyncio
 
 
 
4
  import tempfile
5
+ import os
6
+ import re
7
+ from pydub import AudioSegment # Required for audio duration, needs ffmpeg installed
8
+
9
+ # Get all available voices
10
+ async def get_voices():
11
+ """Fetches all available voices from the Edge TTS service."""
12
+ voices = await edge_tts.list_voices()
13
+ # Format voice names for display in the dropdown
14
+ return {f"{v['ShortName']} - {v['Locale']} ({v['Gender']})": v['ShortName'] for v in voices}
15
+
16
+ # Text-to-speech function
17
+ async def text_to_speech(text, voice, rate, pitch):
18
+ """
19
+ Converts text to speech using Edge TTS and saves it to a temporary file.
20
+ Returns the path to the generated audio file and the original text for SRT generation.
21
+ """
22
+ if not text.strip():
23
+ # Return a string for the warning, instead of a gr.Warning object directly
24
+ return None, None, "Please enter text to convert."
25
+ if not voice:
26
+ # Return a string for the warning
27
+ return None, None, "Please select a voice."
28
+
29
+ # Extract the short name from the selected voice string
30
+ voice_short_name = voice.split(" - ")[0]
31
+
32
+ # Format rate and pitch for the Edge TTS API
33
+ rate_str = f"{rate:+d}%"
34
+ pitch_str = f"{pitch:+d}Hz"
35
+
36
+ # Initialize the Edge TTS communicator
37
+ communicate = edge_tts.Communicate(text, voice_short_name, rate=rate_str, pitch=pitch_str)
38
+
39
+ # Create a temporary file to save the audio
40
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
41
+ tmp_path = tmp_file.name
42
+ await communicate.save(tmp_path)
43
+
44
+ return tmp_path, text, "" # Return audio path, original text, and an empty string for no warning
45
+
46
+ def format_time(ms):
47
+ """
48
+ Formats milliseconds into SRT time format (HH:MM:SS,mmm).
49
+ """
50
+ hours = int(ms / 3_600_000)
51
+ ms %= 3_600_000
52
+ minutes = int(ms / 60_000)
53
+ ms %= 60_000
54
+ seconds = int(ms / 1_000)
55
+ milliseconds = int(ms % 1_000)
56
+ return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
57
+
58
+ def generate_srt(text_input, audio_filepath):
59
+ """
60
+ Generates a basic SRT file based on text input and estimated timings
61
+ from audio duration. Timings are proportional to segment text length.
62
+
63
+ Note: This does not use advanced audio analysis for precise timing of pauses.
64
+ It's an estimation based on character count per segment.
65
+ Requires ffmpeg installed for pydub to read audio duration.
66
+ """
67
+ if not text_input or not audio_filepath:
68
+ return None
69
 
70
+ try:
71
+ # Load audio to get its total duration using pydub
72
+ audio = AudioSegment.from_file(audio_filepath)
73
+ audio_duration_ms = len(audio)
74
+ except Exception as e:
75
+ print(f"Error getting audio duration with pydub: {e}. SRT generation requires ffmpeg.")
76
+ # If pydub fails (e.g., ffmpeg not found), return None for SRT
77
+ return None
78
 
79
+ # Split text into segments. This regex splits on common sentence-ending
80
+ # punctuation, keeping the punctuation with the segment, and also handles newlines.
81
+ segments = re.findall(r'[^.!?,\n]+[.!?,\n]*', text_input)
82
+ segments = [s.strip() for s in segments if s.strip()] # Clean up empty strings
 
83
 
84
+ if not segments:
85
+ return None
 
 
 
 
 
 
 
86
 
87
+ srt_content = []
88
+ current_time_ms = 0
89
+ total_chars = sum(len(s) for s in segments)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
+ if total_chars == 0: # Prevent division by zero if text is somehow empty after stripping
92
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
+ for i, segment in enumerate(segments):
95
+ # Estimate duration for the segment based on its character count
96
+ # This assumes a roughly constant speech rate throughout the audio.
97
+ estimated_segment_duration_ms = (len(segment) / total_chars) * audio_duration_ms
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
+ start_time = current_time_ms
100
+ end_time = current_time_ms + estimated_segment_duration_ms
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ # Ensure the last segment's end time matches the total audio duration
103
+ if i == len(segments) - 1:
104
+ end_time = audio_duration_ms
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
+ # Add SRT entry
107
+ srt_content.append(str(i + 1))
108
+ srt_content.append(f"{format_time(start_time)} --> {format_time(end_time)}")
109
+ srt_content.append(segment)
110
+ srt_content.append("") # Empty line separates SRT blocks
111
 
112
+ current_time_ms = end_time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113
 
114
+ # Save the SRT content to a temporary file
115
+ srt_filename = f"{os.path.splitext(audio_filepath)[0]}.srt"
116
+ with open(srt_filename, "w", encoding="utf-8") as f:
117
+ f.write("\n".join(srt_content))
118
+
119
+ return srt_filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
+ # Gradio interface function (wraps async functions and handles SRT generation)
122
+ def tts_interface(text, voice, rate, pitch):
123
+ """
124
+ The main interface function for Gradio. It calls text_to_speech and then generate_srt.
125
+ """
126
+ # Run the async text_to_speech function
127
+ audio_path, original_text, warning = asyncio.run(text_to_speech(text, voice, rate, pitch))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
+ srt_path = None
130
+ if audio_path: # Only attempt SRT generation if audio was successfully created
131
+ srt_path = generate_srt(original_text, audio_path)
 
132
 
133
+ # Return the generated audio, SRT file, and any warnings
134
+ return audio_path, srt_path, warning
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
+ # Create Gradio application
137
+ async def create_demo():
138
+ """
139
+ Asynchronously creates and configures the Gradio interface.
140
+ """
141
+ voices = await get_voices() # Fetch voices when the app starts
 
 
 
 
 
 
 
 
142
 
143
+ description = """
144
+ Convert text to speech using Microsoft Edge TTS. Adjust speech rate and pitch: 0 is default, positive values increase, negative values decrease.
 
145
 
146
+ ✨ **New Feature: Generate SRT Subtitles (Estimated Timings)!** ✨
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
+ Automatically generates an SRT (SubRip Subtitle) file from your input text.
149
+ **Important Note on Timings:** The SRT timings are *estimated* based on the length of each text segment relative to the total audio duration. This feature *does not* perform advanced audio waveform analysis for precise pause detection or word-level synchronization. For perfectly synchronized subtitles, dedicated forced-alignment tools are typically required.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
+ 🎥 **Exciting News: Introducing our Text-to-Video Converter!** 🎥
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
+ Take your content creation to the next level with our cutting-edge Text-to-Video Converter!
154
+ Transform your words into stunning, professional-quality videos in just a few clicks.
155
 
156
+ Features:
157
+ Convert text to engaging videos with customizable visuals
158
+ • Choose from 40+ languages and 300+ voices
159
+ • Perfect for creating audiobooks, storytelling, and language learning materials
160
+ • Ideal for educators, content creators, and language enthusiasts
161
 
162
+ Ready to revolutionize your content? [Click here to try our Text-to-Video Converter now!](https://text2video.wingetgui.com/)
163
+ """
 
 
 
 
164
 
165
+ demo = gr.Interface(
166
+ fn=tts_interface, # The function that processes inputs and returns outputs
 
167
  inputs=[
168
+ gr.Textbox(label="Input Text", lines=5, placeholder="Enter your text here to convert to speech and generate SRT..."),
169
+ gr.Dropdown(choices=[""] + list(voices.keys()), label="Select Voice", value="", type="value"),
170
+ gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1),
171
+ gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
 
 
 
172
  ],
173
  outputs=[
174
+ gr.Audio(label="Generated Audio", type="filepath"),
175
+ gr.File(label="Generated SRT Subtitle", type="filepath", file_count="single", visible=True), # Output for the SRT file
176
+ gr.Markdown(label="Warning") # Now expects a string output
 
 
177
  ],
178
+ title="Edge TTS Text-to-Speech with SRT Generator",
179
+ description=description,
180
+ article="Experience the power of Edge TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for even more creative possibilities!",
181
+ analytics_enabled=False,
182
+ allow_flagging=False
183
  )
184
+ return demo
185
 
186
+ # Run the application
187
  if __name__ == "__main__":
188
+ demo = asyncio.run(create_demo())
189
+ demo.launch()