Spaces:
Runtime error
Runtime error
Upload 4 files
Browse files- README.md +3 -0
- gradio_app.py +21 -0
- main.py +42 -0
- requirements.txt +6 -0
README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Multilingual Realtime Translator
|
| 2 |
+
|
| 3 |
+
Translate between English and Nigerian languages using speech and text.
|
gradio_app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline, MarianMTModel, MarianTokenizer
|
| 4 |
+
|
| 5 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 6 |
+
mt_model_name = "Helsinki-NLP/opus-mt-yo-en"
|
| 7 |
+
tokenizer = MarianTokenizer.from_pretrained(mt_model_name)
|
| 8 |
+
model = MarianMTModel.from_pretrained(mt_model_name)
|
| 9 |
+
|
| 10 |
+
def translate_speech(audio):
|
| 11 |
+
transcription = asr(audio)["text"]
|
| 12 |
+
inputs = tokenizer(transcription, return_tensors="pt", padding=True)
|
| 13 |
+
translated = model.generate(**inputs)
|
| 14 |
+
translation = tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 15 |
+
return transcription, translation
|
| 16 |
+
|
| 17 |
+
iface = gr.Interface(fn=translate_speech,
|
| 18 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
| 19 |
+
outputs=["text", "text"],
|
| 20 |
+
title="Yoruba to English Speech Translator")
|
| 21 |
+
iface.launch()
|
main.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import edge_tts
|
| 5 |
+
import asyncio
|
| 6 |
+
|
| 7 |
+
# Load Whisper model
|
| 8 |
+
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 9 |
+
|
| 10 |
+
# Load translation model (Yoruba β English)
|
| 11 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 12 |
+
mt_model_name = "Helsinki-NLP/opus-mt-yo-en"
|
| 13 |
+
tokenizer = MarianTokenizer.from_pretrained(mt_model_name)
|
| 14 |
+
model = MarianMTModel.from_pretrained(mt_model_name)
|
| 15 |
+
|
| 16 |
+
# TTS
|
| 17 |
+
async def speak(text):
|
| 18 |
+
communicate = edge_tts.Communicate(text, "en-US-GuyNeural")
|
| 19 |
+
await communicate.save("output.mp3")
|
| 20 |
+
|
| 21 |
+
def translate_text(text):
|
| 22 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
| 23 |
+
translated = model.generate(**inputs)
|
| 24 |
+
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 25 |
+
|
| 26 |
+
# Real-time mic input
|
| 27 |
+
recognizer = sr.Recognizer()
|
| 28 |
+
with sr.Microphone() as source:
|
| 29 |
+
print("Speak now...")
|
| 30 |
+
audio = recognizer.listen(source)
|
| 31 |
+
print("Processing...")
|
| 32 |
+
|
| 33 |
+
# Speech to Text
|
| 34 |
+
result = asr(audio.get_wav_data())["text"]
|
| 35 |
+
print("Transcribed:", result)
|
| 36 |
+
|
| 37 |
+
# Translate
|
| 38 |
+
translation = translate_text(result)
|
| 39 |
+
print("Translated:", translation)
|
| 40 |
+
|
| 41 |
+
# Speak
|
| 42 |
+
asyncio.run(speak(translation))
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
openai-whisper
|
| 4 |
+
gradio
|
| 5 |
+
edge-tts
|
| 6 |
+
speechrecognition
|