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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -35
app.py CHANGED
@@ -1,70 +1,74 @@
1
- # Text-β†’Translation-β†’Speech | CPU-only Hugging-Face Space
2
 
3
  import tempfile
4
  from pathlib import Path
5
 
6
- import gradio as gr
7
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
 
8
  from TTS.api import TTS
 
9
 
10
  # ─────────────────────────────
11
- # Translation pipelines
12
  # ─────────────────────────────
13
- PIPE_EN_ES = pipeline(
14
- "translation", model="Helsinki-NLP/opus-mt-en-es", device=-1
15
- )
16
- PIPE_ES_EN = pipeline(
17
- "translation", model="Helsinki-NLP/opus-mt-es-en", device=-1
18
- )
19
 
20
  # ─────────────────────────────
21
- # TTS models (Coqui TTS)
 
 
 
 
 
 
22
  # ─────────────────────────────
23
  TTS_EN = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
24
  TTS_ES = TTS(model_name="tts_models/es/mai/tacotron2-DDC", progress_bar=False)
25
 
26
  # ─────────────────────────────
27
- # Core function
 
 
 
 
 
 
 
 
 
28
  # ─────────────────────────────
29
- def translate_and_speak(text: str, target_lang: str):
30
- if not text.strip():
31
  return "", None
32
 
33
  if target_lang == "Spanish":
34
- translated = PIPE_EN_ES(text)[0]["translation_text"]
35
- wav_path = synthesize(TTS_ES, translated)
36
  else: # English
37
- translated = PIPE_ES_EN(text)[0]["translation_text"]
38
- wav_path = synthesize(TTS_EN, translated)
39
-
40
- return translated, wav_path
41
 
42
-
43
- def synthesize(tts_model: TTS, text: str) -> str:
44
- """Generate WAV and return file path."""
45
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
46
- wav_file = Path(tmp.name)
47
- tts_model.tts_to_file(text=text, file_path=wav_file)
48
- return str(wav_file)
49
 
50
  # ─────────────────────────────
51
- # Gradio UI
52
  # ─────────────────────────────
53
  with gr.Blocks(title="Translator & TTS") as demo:
54
  gr.Markdown(
55
  "# πŸŒπŸ’¬ Text β†’ Translate β†’ Speech\n"
56
- "Type a sentence, choose target language, hear it spoken!"
57
  )
58
 
59
- inp = gr.Textbox(label="Your sentence (English or Spanish)", lines=2)
60
- tgt = gr.Radio(["Spanish", "English"], value="Spanish", label="Translate to")
61
 
62
- btn = gr.Button("Translate & Speak", variant="primary")
63
 
64
- out_text = gr.Textbox(label="Translated text", interactive=False)
65
- out_audio = gr.Audio(label="TTS output", type="filepath")
66
 
67
- btn.click(translate_and_speak, [inp, tgt], [out_text, out_audio])
68
 
69
  if __name__ == "__main__":
70
  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
5
 
6
+ import torch
7
+ import TTS.utils.radam as tts_radam
8
+ from transformers import pipeline
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
19
+ # ─────────────────────────────
20
+ PIPE_EN_ES = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es", device=-1)
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:
34
+ wav_path = Path(tmp.name)
35
+ tts_model.tts_to_file(text=text, file_path=wav_path)
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():
43
  return "", None
44
 
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
 
52
+ return translated, audio_file
 
 
 
 
 
 
53
 
54
  # ─────────────────────────────
55
+ # 6. Gradio UI
56
  # ─────────────────────────────
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")