Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,89 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from translation import Translator, CONFIG
|
| 3 |
from tts_engine import TTSEngine
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
| 8 |
def translate_and_speak(input_text, input_lang, output_lang, use_coqui, clone_voice):
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
translated = translator.translate(input_text, input_lang, output_lang)
|
| 11 |
|
| 12 |
-
# TTS
|
| 13 |
tts_engine.use_coqui = use_coqui
|
| 14 |
-
audio_path = tts_engine.speak(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
return translated, audio_path
|
| 17 |
|
| 18 |
-
|
| 19 |
with gr.Blocks() as demo:
|
| 20 |
gr.Markdown("## 🌍 Nigerian Voice Translator Assistant")
|
| 21 |
|
| 22 |
with gr.Row():
|
| 23 |
-
input_lang = gr.Dropdown(
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
input_text = gr.Textbox(label="Enter text")
|
| 27 |
use_coqui = gr.Checkbox(label="Use Coqui TTS (natural accents)", value=False)
|
| 28 |
clone_voice = gr.Checkbox(label="Clone my voice if available", value=False)
|
| 29 |
|
| 30 |
translate_btn = gr.Button("Translate & Speak")
|
|
|
|
| 31 |
output_text = gr.Textbox(label="Translation")
|
| 32 |
output_audio = gr.Audio(label="Spoken Output", type="filepath")
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
translate_btn.click(
|
| 35 |
-
fn=
|
| 36 |
-
inputs=[
|
| 37 |
outputs=[output_text, output_audio]
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
|
| 7 |
+
# --- Import your OOP modules ---
|
| 8 |
from translation import Translator, CONFIG
|
| 9 |
from tts_engine import TTSEngine
|
| 10 |
|
| 11 |
+
# --- Init core objects ---
|
| 12 |
+
translator = Translator(
|
| 13 |
+
n2n_enabled=CONFIG["features"]["nigerian_to_nigerian_enabled"]
|
| 14 |
+
)
|
| 15 |
+
tts_engine = TTSEngine(use_coqui=False) # default to pyttsx3
|
| 16 |
+
|
| 17 |
+
# --- Whisper STT pipeline (CPU safe) ---
|
| 18 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 19 |
+
stt_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-small", device=device)
|
| 20 |
|
| 21 |
+
# --- Core translate + TTS function ---
|
| 22 |
def translate_and_speak(input_text, input_lang, output_lang, use_coqui, clone_voice):
|
| 23 |
+
if not input_text:
|
| 24 |
+
return "⚠️ No input detected", None
|
| 25 |
+
|
| 26 |
+
# 1. Translate
|
| 27 |
translated = translator.translate(input_text, input_lang, output_lang)
|
| 28 |
|
| 29 |
+
# 2. TTS
|
| 30 |
tts_engine.use_coqui = use_coqui
|
| 31 |
+
audio_path = tts_engine.speak(
|
| 32 |
+
translated,
|
| 33 |
+
lang=output_lang,
|
| 34 |
+
voice_clone=clone_voice,
|
| 35 |
+
)
|
| 36 |
|
| 37 |
return translated, audio_path
|
| 38 |
|
| 39 |
+
# --- Gradio App ---
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("## 🌍 Nigerian Voice Translator Assistant")
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
+
input_lang = gr.Dropdown(
|
| 45 |
+
choices=["yoruba", "hausa", "igbo", "pidgin", "esan", "tiv", "calabar", "benin"],
|
| 46 |
+
value="yoruba",
|
| 47 |
+
label="Input Language"
|
| 48 |
+
)
|
| 49 |
+
output_lang = gr.Dropdown(
|
| 50 |
+
choices=["english", "yoruba", "hausa", "igbo", "pidgin", "esan", "tiv", "calabar", "benin"],
|
| 51 |
+
value="english",
|
| 52 |
+
label="Output Language"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
with gr.Tab("🎙️ Voice Input"):
|
| 56 |
+
mic_input = gr.Audio(sources=["microphone"], type="filepath", label="Speak here")
|
| 57 |
+
|
| 58 |
+
with gr.Tab("⌨️ Text Input"):
|
| 59 |
+
text_input = gr.Textbox(label="Enter text")
|
| 60 |
|
|
|
|
| 61 |
use_coqui = gr.Checkbox(label="Use Coqui TTS (natural accents)", value=False)
|
| 62 |
clone_voice = gr.Checkbox(label="Clone my voice if available", value=False)
|
| 63 |
|
| 64 |
translate_btn = gr.Button("Translate & Speak")
|
| 65 |
+
|
| 66 |
output_text = gr.Textbox(label="Translation")
|
| 67 |
output_audio = gr.Audio(label="Spoken Output", type="filepath")
|
| 68 |
|
| 69 |
+
# --- Events ---
|
| 70 |
+
def handle_input(mic_input, text_input, input_lang, output_lang, use_coqui, clone_voice):
|
| 71 |
+
# If mic audio provided → transcribe with Whisper
|
| 72 |
+
if mic_input:
|
| 73 |
+
print(f"🎙️ Running Whisper STT on {mic_input}")
|
| 74 |
+
result = stt_pipeline(mic_input)
|
| 75 |
+
input_text = result["text"].strip()
|
| 76 |
+
else:
|
| 77 |
+
input_text = text_input.strip() if text_input else ""
|
| 78 |
+
|
| 79 |
+
return translate_and_speak(input_text, input_lang, output_lang, use_coqui, clone_voice)
|
| 80 |
+
|
| 81 |
translate_btn.click(
|
| 82 |
+
fn=handle_input,
|
| 83 |
+
inputs=[mic_input, text_input, input_lang, output_lang, use_coqui, clone_voice],
|
| 84 |
outputs=[output_text, output_audio]
|
| 85 |
)
|
| 86 |
|
| 87 |
+
# --- Run ---
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|