|
import gradio as gr |
|
import requests |
|
import soundfile as sf |
|
import numpy as np |
|
import tempfile |
|
|
|
|
|
ASR_API_URL = "https://api-inference.huggingface.co/models/Baghdad99/saad-speech-recognition-hausa-audio-to-text" |
|
TTS_API_URL = "https://api-inference.huggingface.co/models/Baghdad99/english_voice_tts" |
|
TRANSLATION_API_URL = "https://api-inference.huggingface.co/models/Baghdad99/saad-hausa-text-to-english-text" |
|
headers = {"Authorization": "Bearer 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"} |
|
|
|
|
|
def query(api_url, payload): |
|
response = requests.post(api_url, headers=headers, json=payload) |
|
return response.json() |
|
|
|
|
|
def translate_speech(audio): |
|
|
|
audio_data, sample_rate = audio |
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f: |
|
sf.write(f, audio_data, sample_rate) |
|
audio_file = f.name |
|
|
|
|
|
with open(audio_file, "rb") as f: |
|
data = f.read() |
|
response = requests.post(ASR_API_URL, headers=headers, data=data) |
|
output = response.json() |
|
|
|
|
|
if 'text' in output: |
|
transcription = output["text"] |
|
else: |
|
print("The output does not contain 'text'") |
|
return |
|
|
|
|
|
translated_text = query(TRANSLATION_API_URL, {"inputs": transcription}) |
|
|
|
|
|
response = requests.post(TTS_API_URL, headers=headers, json={"inputs": translated_text}) |
|
audio_bytes = response.content |
|
|
|
|
|
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f: |
|
f.write(audio_bytes) |
|
audio_file = f.name |
|
audio_data, _ = sf.read(audio_file) |
|
|
|
return audio_data |
|
|
|
|
|
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() |
|
|