Timemaster commited on
Commit
5981e07
·
verified ·
1 Parent(s): d383dd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -38
app.py CHANGED
@@ -1,72 +1,73 @@
1
  import gradio as gr
2
- from gtts import gTTS
3
  import tempfile
4
  import os
5
 
6
- # Define a simple dictionary of available languages for gTTS
7
- # We can't use the massive list from tts_voice.py because gTTS uses simple language codes.
8
- gtts_languages = {
9
- "English": "en",
10
- "Mandarin Chinese": "zh-cn",
11
- "French": "fr",
12
- "German": "de",
13
- "Spanish": "es",
14
- "Japanese": "ja",
15
- "Korean": "ko",
16
- "Russian": "ru"
17
- }
 
18
 
19
- # The main function is now SYNCHRONOUS (no 'async')
20
- def text_to_speech_gtts(text, language_name):
 
21
  # 1. Input Validation
 
 
22
  if not text or not text.strip():
23
  return "ERROR: Input text cannot be empty.", None
24
 
25
  try:
26
- # Get the language code from the name
27
- lang_code = gtts_languages[language_name]
28
-
29
  # Create a temporary file path
30
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
31
  tmp_path = tmp_file.name
32
 
33
- # Initialize gTTS object
34
- tts = gTTS(text=text, lang=lang_code, slow=False)
35
-
36
- # Save the audio file (synchronous operation)
37
- tts.save(tmp_path)
 
 
38
 
39
  return "Speech synthesis complete: {}".format(text), tmp_path
40
-
41
  except Exception as e:
42
- # Handle all gTTS-related errors (e.g., language not supported, network failure)
43
  return f"ERROR: Failed to generate audio. Details: {str(e)}", None
44
 
45
 
46
- # --- Modern Gradio Component Syntax (Translated UI) ---
47
  input_text = gr.Textbox(lines=5, label="Input Text")
48
  output_text = gr.Textbox(label="Output Text")
49
  output_audio = gr.Audio(type="filepath", label="Generated Audio File")
50
 
51
- default_language = "English" # Use English as the default language
52
  language = gr.Dropdown(
53
- choices=list(gtts_languages.keys()),
54
- value=default_language,
55
- label="Language" # Dropped 'Voice' because gTTS has no specific voice selection
56
  )
57
 
58
 
59
  # --- Gradio Interface Definition ---
60
  interface = gr.Interface(
61
- fn=text_to_speech_gtts, # Use the new synchronous function
62
- inputs=[input_text, language],
63
- outputs=[output_text, output_audio],
64
- title="Google TTS Text-to-Speech (Robust Version)",
65
- description="Convert text into audio using the reliable Google Text-to-Speech service. (Max 100 characters for optimal stability)"
66
  )
67
 
68
 
69
- # --- Standard Synchronous Launch Command ---
70
  if __name__ == "__main__":
71
- # Gradio runs synchronous functions reliably without extra configuration.
72
  interface.launch()
 
1
  import gradio as gr
2
+ from TTS.api import TTS
3
  import tempfile
4
  import os
5
 
6
+ # --- Model Loading (Runs only once at startup) ---
7
+ # NOTE: This model is chosen for its balance of quality and CPU compatibility.
8
+ # You can try other models from the Coqui TTS documentation if you need more languages.
9
+ try:
10
+ # Initialize TTS with the chosen model (VITS model for CPU efficiency)
11
+ tts_model = TTS(model_name="tts_models/en/ljspeech/vits", progress_bar=False).to("cpu")
12
+ VOICES = ["ljspeech"] # VITS models often have a single trained speaker
13
+ DEFAULT_VOICE = VOICES[0]
14
+ except Exception as e:
15
+ tts_model = None
16
+ VOICES = ["Model Load Error"]
17
+ DEFAULT_VOICE = VOICES[0]
18
+ print(f"Error loading TTS model: {e}")
19
 
20
+
21
+ # --- Core TTS Function (Synchronous) ---
22
+ def text_to_speech_coqui(text, speaker_name):
23
  # 1. Input Validation
24
+ if not tts_model:
25
+ return "ERROR: TTS Model failed to load at startup. Check Space logs.", None
26
  if not text or not text.strip():
27
  return "ERROR: Input text cannot be empty.", None
28
 
29
  try:
 
 
 
30
  # Create a temporary file path
31
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
32
  tmp_path = tmp_file.name
33
 
34
+ # Generate audio file (Coqui TTS method)
35
+ # We pass the speaker_name even though this VITS model only has one.
36
+ tts_model.tts_to_file(
37
+ text=text,
38
+ speaker=speaker_name,
39
+ file_path=tmp_path
40
+ )
41
 
42
  return "Speech synthesis complete: {}".format(text), tmp_path
43
+
44
  except Exception as e:
 
45
  return f"ERROR: Failed to generate audio. Details: {str(e)}", None
46
 
47
 
48
+ # --- Gradio UI Definition ---
49
  input_text = gr.Textbox(lines=5, label="Input Text")
50
  output_text = gr.Textbox(label="Output Text")
51
  output_audio = gr.Audio(type="filepath", label="Generated Audio File")
52
 
53
+ # Dropdown uses the detected voices (or the error message)
54
  language = gr.Dropdown(
55
+ choices=VOICES,
56
+ value=DEFAULT_VOICE,
57
+ label="Speaker/Voice"
58
  )
59
 
60
 
61
  # --- Gradio Interface Definition ---
62
  interface = gr.Interface(
63
+ fn=text_to_speech_coqui,
64
+ inputs=[input_text, language],
65
+ outputs=[output_text, output_audio],
66
+ title="Coqui TTS (CPU Optimized)",
67
+ description="Customizable, high-quality Text-to-Speech running on CPU."
68
  )
69
 
70
 
71
+ # --- Standard Launch Command ---
72
  if __name__ == "__main__":
 
73
  interface.launch()