ghostai1 commited on
Commit
57fc41b
Β·
verified Β·
1 Parent(s): c860340

Update app.py

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