Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
from transformers import MarianMTModel, MarianTokenizer
|
4 |
+
from gtts import gTTS
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
# Load Whisper ASR model
|
8 |
+
whisper_model = whisper.load_model("small") # You can choose 'base', 'small', 'medium', 'large'
|
9 |
+
|
10 |
+
# Load translation models for Hausa-English and English-Hausa
|
11 |
+
model_name_he = 'Helsinki-NLP/opus-mt-ha-en' # Hausa to English
|
12 |
+
model_name_eh = 'Helsinki-NLP/opus-mt-en-ha' # English to Hausa
|
13 |
+
|
14 |
+
tokenizer_he = MarianTokenizer.from_pretrained(model_name_he)
|
15 |
+
model_he = MarianMTModel.from_pretrained(model_name_he)
|
16 |
+
|
17 |
+
tokenizer_eh = MarianTokenizer.from_pretrained(model_name_eh)
|
18 |
+
model_eh = MarianMTModel.from_pretrained(model_name_eh)
|
19 |
+
|
20 |
+
# Function to punctuate (simple punctuation for demo)
|
21 |
+
def punctuate(text):
|
22 |
+
if text[-1] not in '.!?':
|
23 |
+
text += '.'
|
24 |
+
return text.capitalize()
|
25 |
+
|
26 |
+
# Function to translate and punctuate
|
27 |
+
def translate_and_punctuate(text, direction):
|
28 |
+
if direction == "Hausa to English":
|
29 |
+
translated = model_he.generate(**tokenizer_he(text, return_tensors="pt", padding=True))
|
30 |
+
result = tokenizer_he.decode(translated[0], skip_special_tokens=True)
|
31 |
+
else:
|
32 |
+
translated = model_eh.generate(**tokenizer_eh(text, return_tensors="pt", padding=True))
|
33 |
+
result = tokenizer_eh.decode(translated[0], skip_special_tokens=True)
|
34 |
+
|
35 |
+
return punctuate(result)
|
36 |
+
|
37 |
+
# Text-to-speech function
|
38 |
+
def text_to_speech(text, language):
|
39 |
+
tts = gTTS(text=text, lang=language)
|
40 |
+
audio_fp = BytesIO()
|
41 |
+
tts.save(audio_fp)
|
42 |
+
audio_fp.seek(0)
|
43 |
+
return audio_fp
|
44 |
+
|
45 |
+
# Real-time translation function
|
46 |
+
def real_time_translation(audio, direction):
|
47 |
+
# Use Whisper model to transcribe the audio (speech to text)
|
48 |
+
result = whisper_model.transcribe(audio)
|
49 |
+
spoken_text = result['text']
|
50 |
+
|
51 |
+
# Translate and punctuate the transcribed text
|
52 |
+
translated_text = translate_and_punctuate(spoken_text, direction)
|
53 |
+
|
54 |
+
# Generate speech output from the translated text
|
55 |
+
if direction == "Hausa to English":
|
56 |
+
speech_output = text_to_speech(translated_text, "en")
|
57 |
+
else:
|
58 |
+
speech_output = text_to_speech(translated_text, "ha")
|
59 |
+
|
60 |
+
return translated_text, speech_output
|
61 |
+
|
62 |
+
# Gradio interface
|
63 |
+
def translation_app(audio, direction):
|
64 |
+
# Handle real-time translation from audio input
|
65 |
+
translated_text, speech_output = real_time_translation(audio, direction)
|
66 |
+
|
67 |
+
return translated_text, speech_output
|
68 |
+
|
69 |
+
# Define Gradio inputs and outputs
|
70 |
+
inputs = [
|
71 |
+
gr.Audio(type="filepath", label="Speak Now"),
|
72 |
+
gr.Radio(choices=["Hausa to English", "English to Hausa"], label="Translation Direction")
|
73 |
+
]
|
74 |
+
|
75 |
+
outputs = [
|
76 |
+
gr.Textbox(label="Translated and Punctuated Text"),
|
77 |
+
gr.Audio(label="Translated Speech")
|
78 |
+
]
|
79 |
+
|
80 |
+
# Launch Gradio app
|
81 |
+
gr.Interface(fn=translation_app, inputs=inputs, outputs=outputs, title="Real-Time Hausa-English Speech Translator with Whisper").launch()
|