Spaces:
Sleeping
Sleeping
Updated
Browse files
app.py
CHANGED
|
@@ -1,39 +1,39 @@
|
|
| 1 |
-
import gradio
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
transcription =
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def synthesise(translated_text):
|
| 25 |
-
inputs = tts_tokenizer(translated_text, return_tensors='pt')
|
| 26 |
-
audio = tts_model.generate(inputs['input_ids'])
|
| 27 |
-
return audio
|
| 28 |
-
|
| 29 |
-
def translate_speech(audio, sampling_rate):
|
| 30 |
-
translated_text = translate(audio, sampling_rate=sampling_rate)
|
| 31 |
-
synthesised_speech = synthesise(translated_text)
|
| 32 |
# Define the max_range variable
|
| 33 |
max_range = 32767 # You can adjust this value based on your requirements
|
| 34 |
synthesised_speech = (synthesised_speech.numpy() * max_range).astype(np.int16)
|
|
|
|
| 35 |
return 16000, synthesised_speech
|
| 36 |
|
| 37 |
# Define the Gradio interface
|
| 38 |
-
iface =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the pipeline for speech recognition and translation
|
| 5 |
+
pipe = pipeline(
|
| 6 |
+
"automatic-speech-recognition",
|
| 7 |
+
model="Baghdad99/saad-speech-recognition-hausa-audio-to-text",
|
| 8 |
+
tokenizer="Baghdad99/saad-speech-recognition-hausa-audio-to-text"
|
| 9 |
+
)
|
| 10 |
+
translator = pipeline("text2text-generation", model="Baghdad99/saad-hausa-text-to-english-text")
|
| 11 |
+
tts = pipeline("text-to-speech", model="Baghdad99/english_voice_tts")
|
| 12 |
+
|
| 13 |
+
# Define the function to translate speech
|
| 14 |
+
def translate_speech(audio):
|
| 15 |
+
# Use the speech recognition pipeline to transcribe the audio
|
| 16 |
+
transcription = pipe(audio, sampling_rate=16000)[0]["transcription"]
|
| 17 |
+
|
| 18 |
+
# Use the translation pipeline to translate the transcription
|
| 19 |
+
translated_text = translator(transcription, return_tensors="pt", padding=True)
|
| 20 |
+
|
| 21 |
+
# Use the text-to-speech pipeline to synthesize the translated text
|
| 22 |
+
synthesised_speech = tts(translated_text, return_tensors='pt')
|
| 23 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Define the max_range variable
|
| 25 |
max_range = 32767 # You can adjust this value based on your requirements
|
| 26 |
synthesised_speech = (synthesised_speech.numpy() * max_range).astype(np.int16)
|
| 27 |
+
|
| 28 |
return 16000, synthesised_speech
|
| 29 |
|
| 30 |
# Define the Gradio interface
|
| 31 |
+
iface = gr.Interface(
|
| 32 |
+
fn=translate_speech,
|
| 33 |
+
inputs=gr.inputs.Audio(source="microphone", type="numpy"),
|
| 34 |
+
outputs=gr.outputs.Audio(type="numpy"),
|
| 35 |
+
title="Hausa to English Translation",
|
| 36 |
+
description="Realtime demo for Hausa to English translation using speech recognition and text-to-speech synthesis."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
iface.launch()
|