Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,115 +1,68 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 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 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
model="openai/whisper-small",
|
| 26 |
-
device=device
|
| 27 |
-
)
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
translated = translator.translate(
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
tts_engine.
|
| 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 |
-
|
| 48 |
-
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
value="
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
output_text = gr.Textbox(label="Translation")
|
| 106 |
-
output_audio = gr.Audio(label="Spoken Output", type="filepath")
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 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()
|
|
|