Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,64 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
from tts_engine import TTSEngine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# Translation: MarianMT (generic English <-> multilingual)
|
| 10 |
-
translator_pipeline = pipeline("translation", model="Helsinki-NLP/opus-mt-mul-en")
|
| 11 |
-
|
| 12 |
tts_engine = TTSEngine(use_coqui=True)
|
|
|
|
| 13 |
|
| 14 |
-
LANGUAGES = [
|
| 15 |
-
"english", "yoruba", "igbo", "hausa", "pidgin",
|
| 16 |
-
"esan", "tiv", "calabar", "benin"
|
| 17 |
-
]
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
"""Speech to text using Whisper."""
|
| 21 |
if audio is None:
|
| 22 |
-
return ""
|
| 23 |
-
result = stt_pipeline(audio, generate_kwargs={"language": language})
|
| 24 |
-
return result["text"]
|
| 25 |
-
|
| 26 |
-
def translate(text, src_lang, tgt_lang):
|
| 27 |
-
"""Dummy translation with Marian (you can extend with Nigerian mappings)."""
|
| 28 |
-
if not text:
|
| 29 |
-
return ""
|
| 30 |
-
# For Nigerian languages not covered by Marian, just return the same text
|
| 31 |
-
if src_lang not in ["english", "en"] or tgt_lang not in ["english", "en"]:
|
| 32 |
-
return f"[{src_lang}->{tgt_lang}] {text}"
|
| 33 |
-
translated = translator_pipeline(text)[0]["translation_text"]
|
| 34 |
-
return translated
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# Step 1: STT
|
| 39 |
-
text = transcribe(audio, language="en" if src_lang == "english" else None)
|
| 40 |
|
| 41 |
# Step 2: Translate
|
| 42 |
-
translated = translate(text, src_lang, tgt_lang)
|
| 43 |
|
| 44 |
-
# Step 3:
|
| 45 |
audio_path = tts_engine.speak(translated, lang=tgt_lang, voice_clone=clone_voice)
|
| 46 |
|
| 47 |
return translated, audio_path
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
with gr.Blocks(title="π Two-Way
|
| 51 |
gr.Markdown("# π Nigerian Two-Way Voice Translator")
|
| 52 |
-
gr.
|
| 53 |
-
|
| 54 |
-
with gr.Row():
|
| 55 |
-
with gr.Column():
|
| 56 |
-
gr.Markdown("### π§ Speaker A")
|
| 57 |
src_lang = gr.Dropdown(LANGUAGES, value="english", label="Speaker A Language")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
gr.
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
audio_in_b.change(
|
| 80 |
-
handle_conversation,
|
| 81 |
-
inputs=[audio_in_b, tgt_lang, src_lang, clone_voice],
|
| 82 |
-
outputs=[translated_b, audio_out_b]
|
| 83 |
-
)
|
| 84 |
|
| 85 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from tts_engine import TTSEngine
|
| 3 |
+
from translation import Translator, CustomTranslator
|
| 4 |
+
from data_manager import save_uploaded_file, convert_to_jsonl
|
| 5 |
+
from training.train_translation import train_from_jsonl
|
| 6 |
+
from stt_engine import STTEngine
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# Init engines
|
| 10 |
+
stt_engine = STTEngine()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
tts_engine = TTSEngine(use_coqui=True)
|
| 12 |
+
translator = CustomTranslator() if os.path.exists("./training/outputs/model") else Translator()
|
| 13 |
|
| 14 |
+
LANGUAGES = ["english", "yoruba", "igbo", "hausa", "pidgin", "esan", "tiv", "calabar", "benin"]
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
def handle_conversation(audio, src_lang, tgt_lang, clone_voice):
|
|
|
|
| 17 |
if audio is None:
|
| 18 |
+
return "", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Step 1: Speech to Text
|
| 21 |
+
text = stt_engine.transcribe(audio, language=src_lang)
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Step 2: Translate
|
| 24 |
+
translated = translator.translate(text, src_lang, tgt_lang)
|
| 25 |
|
| 26 |
+
# Step 3: Text to Speech
|
| 27 |
audio_path = tts_engine.speak(translated, lang=tgt_lang, voice_clone=clone_voice)
|
| 28 |
|
| 29 |
return translated, audio_path
|
| 30 |
|
| 31 |
+
def admin_upload(file):
|
| 32 |
+
file_path = save_uploaded_file(file, file.name)
|
| 33 |
+
jsonl_path = convert_to_jsonl(file_path)
|
| 34 |
+
train_from_jsonl(jsonl_path)
|
| 35 |
+
return "β
Training done. Model updated!"
|
| 36 |
|
| 37 |
+
with gr.Blocks(title="π Two-Way Voice Translator") as demo:
|
| 38 |
gr.Markdown("# π Nigerian Two-Way Voice Translator")
|
| 39 |
+
with gr.Tab("Translator"):
|
| 40 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
| 41 |
src_lang = gr.Dropdown(LANGUAGES, value="english", label="Speaker A Language")
|
| 42 |
+
tgt_lang = gr.Dropdown(LANGUAGES, value="hausa", label="Speaker B Language")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
audio_in = gr.Audio(sources=["microphone"], type="filepath", label="π€ Speak")
|
| 46 |
+
translated = gr.Textbox(label="Translated Text", interactive=False)
|
| 47 |
+
audio_out = gr.Audio(label="π Translation Audio")
|
| 48 |
+
|
| 49 |
+
clone_voice = gr.Checkbox(value=False, label="ποΈ Use my cloned voice (if my_voice.wav exists)")
|
| 50 |
+
|
| 51 |
+
audio_in.change(
|
| 52 |
+
handle_conversation,
|
| 53 |
+
inputs=[audio_in, src_lang, tgt_lang, clone_voice],
|
| 54 |
+
outputs=[translated, audio_out]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
with gr.Tab("Admin (Training)"):
|
| 58 |
+
gr.Markdown("Upload Hausa β English data (.csv, .xlsx, .tsv, .jsonl)")
|
| 59 |
+
file_in = gr.File(label="Upload dataset")
|
| 60 |
+
train_btn = gr.Button("π Train Model")
|
| 61 |
+
output_box = gr.Textbox(label="Training Status")
|
| 62 |
+
train_btn.click(admin_upload, inputs=file_in, outputs=output_box)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
demo.launch()
|