innocentpeter commited on
Commit
00a2929
Β·
verified Β·
1 Parent(s): 880f9cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -99
app.py CHANGED
@@ -1,115 +1,68 @@
1
- import os
2
  import gradio as gr
3
- from pathlib import Path
4
- import torch
5
- from transformers import pipeline
6
- from langdetect import detect
7
-
8
- # --- Import OOP modules ---
9
- from translation import Translator, CONFIG
10
  from tts_engine import TTSEngine
 
11
 
12
- # --- Init core ---
13
- translator = Translator(
14
- n2n_enabled=CONFIG["features"]["nigerian_to_nigerian_enabled"]
15
- )
16
-
17
- # On Hugging Face: disable pyttsx3 (no espeak). Default = Coqui
18
- USE_PYTTSX3 = os.environ.get("USE_PYTTSX3", "false").lower() == "true"
19
- tts_engine = TTSEngine(use_coqui=not USE_PYTTSX3)
20
 
21
- # --- Whisper STT ---
22
- device = 0 if torch.cuda.is_available() else -1
23
- stt_pipeline = pipeline(
24
- "automatic-speech-recognition",
25
- model="openai/whisper-small",
26
- device=device
27
- )
28
 
29
- def translate_and_speak(input_text, input_lang, output_lang, use_coqui, clone_voice):
30
- if not input_text:
31
- return "⚠️ No input detected", None
 
 
 
 
32
 
33
- # 1. Translate
34
- translated = translator.translate(input_text, input_lang, output_lang)
35
 
36
- # 2. TTS
37
- tts_engine.use_coqui = use_coqui
38
- audio_path = tts_engine.speak(
39
- translated,
40
- lang=output_lang,
41
- voice_clone=clone_voice,
42
- )
43
 
44
  return translated, audio_path
45
 
46
 
47
- def handle_input(mic_input, text_input, input_lang, output_lang, use_coqui, clone_voice):
48
- input_text = ""
49
-
50
- # Mic input β†’ Whisper
51
- if mic_input:
52
- result = stt_pipeline(mic_input)
53
- input_text = result["text"].strip()
54
-
55
- # Auto language detect
56
- try:
57
- detected = detect(input_text)
58
- print(f"🌍 Auto-detected: {detected}")
59
- # Map detection to supported langs
60
- if detected.startswith("yo"):
61
- input_lang = "yoruba"
62
- elif detected.startswith("ha"):
63
- input_lang = "hausa"
64
- elif detected.startswith("ig"):
65
- input_lang = "igbo"
66
- elif detected.startswith("en"):
67
- input_lang = "english"
68
- # else leave user selection
69
- except Exception as e:
70
- print("⚠️ Language detection failed:", e)
71
-
72
- elif text_input:
73
- input_text = text_input.strip()
74
-
75
- return translate_and_speak(input_text, input_lang, output_lang, use_coqui, clone_voice)
76
-
77
-
78
- # --- Gradio App ---
79
- with gr.Blocks() as demo:
80
- gr.Markdown("## 🌍 Nigerian Voice Translator Assistant")
81
 
82
  with gr.Row():
83
- input_lang = gr.Dropdown(
84
- choices=["auto", "yoruba", "hausa", "igbo", "pidgin", "esan", "tiv", "calabar", "benin", "english"],
85
- value="auto",
86
- label="Input Language (auto-detect by default)"
87
- )
88
- output_lang = gr.Dropdown(
89
- choices=["english","yoruba","hausa","igbo","pidgin","esan","tiv","calabar","benin"],
90
- value="english",
91
- label="Output Language"
92
- )
93
-
94
- with gr.Tab("πŸŽ™οΈ Voice Input"):
95
- mic_input = gr.Audio(sources=["microphone"], type="filepath", label="Speak here")
96
-
97
- with gr.Tab("⌨️ Text Input"):
98
- text_input = gr.Textbox(label="Enter text")
99
-
100
- use_coqui = gr.Checkbox(label="Use Coqui TTS (natural accents)", value=True)
101
- clone_voice = gr.Checkbox(label="Clone my voice if available", value=False)
102
-
103
- translate_btn = gr.Button("Translate & Speak")
104
-
105
- output_text = gr.Textbox(label="Translation")
106
- output_audio = gr.Audio(label="Spoken Output", type="filepath")
107
 
108
- translate_btn.click(
109
- fn=handle_input,
110
- inputs=[mic_input, text_input, input_lang, output_lang, use_coqui, clone_voice],
111
- outputs=[output_text, output_audio]
 
112
  )
113
 
114
- if __name__ == "__main__":
115
- demo.launch()
 
 
1
  import gradio as gr
2
+ from stt_engine import STTEngine
 
 
 
 
 
 
3
  from tts_engine import TTSEngine
4
+ from translator import TranslatorEngine
5
 
6
+ # Init engines
7
+ stt_engine = STTEngine()
8
+ tts_engine = TTSEngine(use_coqui=True)
9
+ translator = TranslatorEngine()
 
 
 
 
10
 
11
+ LANGUAGES = [
12
+ "english", "yoruba", "igbo", "hausa", "pidgin",
13
+ "esan", "tiv", "calabar", "benin"
14
+ ]
 
 
 
15
 
16
+ def handle_conversation(audio, src_lang, tgt_lang, clone_voice):
17
+ """One side speaks -> STT -> Translate -> TTS"""
18
+ if audio is None:
19
+ return "", None
20
+
21
+ # Speech to text
22
+ text = stt_engine.transcribe(audio, language=src_lang)
23
 
24
+ # Translate
25
+ translated = translator.translate(text, src_lang, tgt_lang)
26
 
27
+ # TTS (with cloned voice if available)
28
+ audio_path = tts_engine.speak(translated, lang=tgt_lang, voice_clone=clone_voice)
 
 
 
 
 
29
 
30
  return translated, audio_path
31
 
32
 
33
+ with gr.Blocks(title="🌍 Two-Way Translation Assistant") as demo:
34
+ gr.Markdown("# 🌍 Nigerian Two-Way Voice Translator")
35
+ gr.Markdown("Speak in your language, hear it in theirs. Supports English ↔ Nigerian languages.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  with gr.Row():
38
+ with gr.Column():
39
+ gr.Markdown("### πŸ§‘ Speaker A")
40
+ src_lang = gr.Dropdown(LANGUAGES, value="english", label="Speaker A Language")
41
+ audio_in_a = gr.Audio(sources=["microphone"], type="filepath", label="🎀 Speak here")
42
+ translated_a = gr.Textbox(label="Translated Text", interactive=False)
43
+ audio_out_a = gr.Audio(label="πŸ”Š Translation Audio")
44
+
45
+ with gr.Column():
46
+ gr.Markdown("### πŸ‘© Speaker B")
47
+ tgt_lang = gr.Dropdown(LANGUAGES, value="yoruba", label="Speaker B Language")
48
+ audio_in_b = gr.Audio(sources=["microphone"], type="filepath", label="🎀 Reply here")
49
+ translated_b = gr.Textbox(label="Translated Text", interactive=False)
50
+ audio_out_b = gr.Audio(label="πŸ”Š Translation Audio")
51
+
52
+ clone_voice = gr.Checkbox(value=False, label="πŸŽ™οΈ Use my cloned voice (if my_voice.wav exists)")
53
+
54
+ # Wire up A -> B
55
+ audio_in_a.change(
56
+ handle_conversation,
57
+ inputs=[audio_in_a, src_lang, tgt_lang, clone_voice],
58
+ outputs=[translated_a, audio_out_a]
59
+ )
 
 
60
 
61
+ # Wire up B -> A
62
+ audio_in_b.change(
63
+ handle_conversation,
64
+ inputs=[audio_in_b, tgt_lang, src_lang, clone_voice],
65
+ outputs=[translated_b, audio_out_b]
66
  )
67
 
68
+ demo.launch()