|
import torch |
|
import gradio as gr |
|
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor, pipeline, AutoTokenizer |
|
import numpy as np |
|
import librosa |
|
|
|
|
|
asr_model = Wav2Vec2ForCTC.from_pretrained("Akashpb13/Hausa_xlsr") |
|
asr_processor = Wav2Vec2Processor.from_pretrained("Akashpb13/Hausa_xlsr") |
|
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_input): |
|
|
|
audio_data, sample_rate = librosa.load(audio_input, sr=None) |
|
|
|
|
|
input_dict = asr_processor(audio_data, sampling_rate=sample_rate, return_tensors="pt", padding=True) |
|
|
|
|
|
logits = asr_model(input_dict.input_values.to("cpu")).logits |
|
|
|
|
|
pred_ids = torch.argmax(logits, dim=-1)[0] |
|
|
|
|
|
transcription = asr_processor.decode(pred_ids) |
|
print(f"Transcription: {transcription}") |
|
|
|
|
|
translated_text = translator(transcription, return_tensors="pt") |
|
print(f"Translated text: {translated_text}") |
|
|
|
|
|
if 'generated_token_ids' in translated_text[0]: |
|
|
|
translated_text_str = translator.tokenizer.decode(translated_text[0]['generated_token_ids']) |
|
print(f"Translated text string: {translated_text_str}") |
|
else: |
|
print("The translated text does not contain 'generated_token_ids'") |
|
return |
|
|
|
|
|
synthesised_speech = tts(translated_text_str) |
|
|
|
|
|
if 'audio' in synthesised_speech: |
|
synthesised_speech_data = synthesised_speech['audio'] |
|
else: |
|
print("The synthesised speech does not contain 'audio'") |
|
return |
|
|
|
|
|
synthesised_speech_data = synthesised_speech_data.flatten() |
|
|
|
|
|
synthesised_speech = (synthesised_speech_data * 32767).astype(np.int16) |
|
|
|
return 16000, synthesised_speech |
|
|
|
|
|
iface = gr.Interface( |
|
fn=translate_speech, |
|
inputs=gr.inputs.Audio(type="filepath"), |
|
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() |
|
|