|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
pipe = pipeline( |
|
"automatic-speech-recognition", |
|
model="Baghdad99/saad-speech-recognition-hausa-audio-to-text", |
|
tokenizer="Baghdad99/saad-speech-recognition-hausa-audio-to-text" |
|
) |
|
translator = pipeline("text2text-generation", model="Baghdad99/saad-hausa-text-to-english-text") |
|
tts = pipeline("text-to-speech", model="Baghdad99/english_voice_tts") |
|
|
|
|
|
def translate_speech(audio): |
|
|
|
audio_data = audio[0] |
|
|
|
|
|
transcription = pipe(audio_data)["transcription"] |
|
|
|
|
|
translated_text = translator(transcription, return_tensors="pt", padding=True) |
|
|
|
|
|
synthesised_speech = tts(translated_text, return_tensors='pt') |
|
|
|
|
|
max_range = 32767 |
|
synthesised_speech = (synthesised_speech.numpy() * max_range).astype(np.int16) |
|
|
|
return 16000, synthesised_speech |
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=translate_speech, |
|
inputs=gr.inputs.Audio(source="microphone", type="numpy"), |
|
outputs=gr.outputs.Audio(type="numpy"), |
|
title="Hausa to English Translation", |
|
description="Realtime demo for Hausa to English translation using speech recognition and text-to-speech synthesis." |
|
) |
|
|
|
iface.launch() |
|
|