File size: 1,932 Bytes
df27a26
382ed84
df27a26
 
d347764
b2c7d3a
 
 
 
c714a80
b2c7d3a
 
d347764
df27a26
 
17cfe18
b2c7d3a
382ed84
b2c7d3a
 
 
df27a26
d347764
df27a26
b2c7d3a
 
 
d347764
df27a26
 
 
 
 
17cfe18
b2c7d3a
df27a26
b2c7d3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import gradio
import torch
import numpy as np
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForTextToWaveform

# Load your pretrained models
asr_model = Wav2Vec2ForCTC.from_pretrained("Baghdad99/saad-speech-recognition-hausa-audio-to-text")
asr_processor = Wav2Vec2Processor.from_pretrained("Baghdad99/saad-speech-recognition-hausa-audio-to-text")
translation_tokenizer = AutoTokenizer.from_pretrained("Baghdad99/saad-hausa-text-to-english-text")
translation_model = AutoModelForSeq2SeqLM.from_pretrained("Baghdad99/saad-hausa-text-to-english-text", from_tf=True)
tts_tokenizer = AutoTokenizer.from_pretrained("Baghdad99/english_voice_tts")
tts_model = AutoModelForTextToWaveform.from_pretrained("Baghdad99/english_voice_tts")

# Define the translation and synthesis functions
def translate(audio_signal):
    inputs = asr_processor(audio_signal, return_tensors="pt", padding=True)
    logits = asr_model(inputs.input_values).logits
    predicted_ids = torch.argmax(logits, dim=-1)
    transcription = asr_processor.decode(predicted_ids[0])
    translated = translation_model.generate(**translation_tokenizer(transcription, return_tensors="pt", padding=True))
    translated_text = [translation_tokenizer.decode(t, skip_special_tokens=True) for t in translated]
    return translated_text

def synthesise(translated_text):
    inputs = tts_tokenizer(translated_text, return_tensors='pt')
    audio = tts_model.generate(inputs['input_ids'])
    return audio

def translate_speech(audio):
    translated_text = translate(audio)
    synthesised_speech = synthesise(translated_text)
    synthesised_speech = (synthesised_speech.numpy() * max_range).astype(np.int16)
    return 16000, synthesised_speech

# Define the Gradio interface
iface = gradio.Interface(fn=translate_speech, inputs=gradio.inputs.Audio(source="microphone", type="numpy"), outputs="audio")
iface.launch()