ghostai1 commited on
Commit
77ab135
Β·
verified Β·
1 Parent(s): c0f9fa0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # Text β†’ Translate β†’ Speech | CPU-only Hugging-Face Space
2
 
3
  import tempfile
4
  from pathlib import Path
@@ -10,9 +10,9 @@ from TTS.api import TTS
10
  import gradio as gr
11
 
12
  # ─────────────────────────────
13
- # 1. Allow-list Coqui’s custom RAdam class (Torch β‰₯2.6)
14
  # ─────────────────────────────
15
- torch.serialization.add_safe_globals({"TTS.utils.radam.RAdam": tts_radam.RAdam})
16
 
17
  # ─────────────────────────────
18
  # 2. Translation pipelines
@@ -21,13 +21,13 @@ PIPE_EN_ES = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es", device=
21
  PIPE_ES_EN = pipeline("translation", model="Helsinki-NLP/opus-mt-es-en", device=-1)
22
 
23
  # ─────────────────────────────
24
- # 3. TTS models (Coqui TTS)
25
  # ─────────────────────────────
26
  TTS_EN = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
27
  TTS_ES = TTS(model_name="tts_models/es/mai/tacotron2-DDC", progress_bar=False)
28
 
29
  # ─────────────────────────────
30
- # 4. Helper: synthesize WAV to temp file
31
  # ─────────────────────────────
32
  def synthesize(tts_model: TTS, text: str) -> str:
33
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
@@ -36,7 +36,7 @@ def synthesize(tts_model: TTS, text: str) -> str:
36
  return str(wav_path)
37
 
38
  # ─────────────────────────────
39
- # 5. Core translate-and-speak function
40
  # ─────────────────────────────
41
  def translate_and_speak(txt: str, target_lang: str):
42
  if not txt.strip():
@@ -45,7 +45,7 @@ def translate_and_speak(txt: str, target_lang: str):
45
  if target_lang == "Spanish":
46
  translated = PIPE_EN_ES(txt)[0]["translation_text"]
47
  audio_file = synthesize(TTS_ES, translated)
48
- else: # English
49
  translated = PIPE_ES_EN(txt)[0]["translation_text"]
50
  audio_file = synthesize(TTS_EN, translated)
51
 
@@ -57,18 +57,18 @@ def translate_and_speak(txt: str, target_lang: str):
57
  with gr.Blocks(title="Translator & TTS") as demo:
58
  gr.Markdown(
59
  "# πŸŒπŸ’¬ Text β†’ Translate β†’ Speech\n"
60
- "Type a sentence, choose target language, and hear it spoken."
61
  )
62
 
63
- text_in = gr.Textbox(label="Sentence (English or Spanish)", lines=2)
64
- lang = gr.Radio(["Spanish", "English"], value="Spanish", label="Translate to")
65
 
66
- run_btn = gr.Button("Translate & Speak", variant="primary")
67
 
68
- text_out = gr.Textbox(label="Translated text", interactive=False)
69
- wav_out = gr.Audio(label="Speech output", type="filepath")
70
 
71
- run_btn.click(translate_and_speak, [text_in, lang], [text_out, wav_out])
72
 
73
  if __name__ == "__main__":
74
  demo.launch(server_name="0.0.0.0")
 
1
+ # Text β†’ Translate β†’ Speech | CPU-only Hugging-Face Space
2
 
3
  import tempfile
4
  from pathlib import Path
 
10
  import gradio as gr
11
 
12
  # ─────────────────────────────
13
+ # 1. Allow-list Coqui-TTS custom class (PyTorch β‰₯ 2.6)
14
  # ─────────────────────────────
15
+ torch.serialization.add_safe_globals({tts_radam.RAdam}) # ← must pass the class object
16
 
17
  # ─────────────────────────────
18
  # 2. Translation pipelines
 
21
  PIPE_ES_EN = pipeline("translation", model="Helsinki-NLP/opus-mt-es-en", device=-1)
22
 
23
  # ─────────────────────────────
24
+ # 3. TTS models
25
  # ─────────────────────────────
26
  TTS_EN = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
27
  TTS_ES = TTS(model_name="tts_models/es/mai/tacotron2-DDC", progress_bar=False)
28
 
29
  # ─────────────────────────────
30
+ # 4. Helper: synthesize WAV
31
  # ─────────────────────────────
32
  def synthesize(tts_model: TTS, text: str) -> str:
33
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
 
36
  return str(wav_path)
37
 
38
  # ─────────────────────────────
39
+ # 5. Core translate-and-speak
40
  # ─────────────────────────────
41
  def translate_and_speak(txt: str, target_lang: str):
42
  if not txt.strip():
 
45
  if target_lang == "Spanish":
46
  translated = PIPE_EN_ES(txt)[0]["translation_text"]
47
  audio_file = synthesize(TTS_ES, translated)
48
+ else:
49
  translated = PIPE_ES_EN(txt)[0]["translation_text"]
50
  audio_file = synthesize(TTS_EN, translated)
51
 
 
57
  with gr.Blocks(title="Translator & TTS") as demo:
58
  gr.Markdown(
59
  "# πŸŒπŸ’¬ Text β†’ Translate β†’ Speech\n"
60
+ "Type a sentence, choose the target language, and hear it spoken."
61
  )
62
 
63
+ sentence = gr.Textbox(label="Sentence (English or Spanish)", lines=2)
64
+ lang = gr.Radio(["Spanish", "English"], value="Spanish", label="Translate to")
65
 
66
+ run_btn = gr.Button("Translate & Speak", variant="primary")
67
 
68
+ translated_txt = gr.Textbox(label="Translated text", interactive=False)
69
+ audio_out = gr.Audio(label="Speech output", type="filepath")
70
 
71
+ run_btn.click(translate_and_speak, [sentence, lang], [translated_txt, audio_out])
72
 
73
  if __name__ == "__main__":
74
  demo.launch(server_name="0.0.0.0")