Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline,
|
3 |
-
import torch
|
4 |
import numpy as np
|
5 |
|
6 |
# Load the pipeline for speech recognition and translation
|
@@ -10,10 +9,7 @@ pipe = pipeline(
|
|
10 |
tokenizer="Baghdad99/saad-speech-recognition-hausa-audio-to-text"
|
11 |
)
|
12 |
translator = pipeline("text2text-generation", model="Baghdad99/saad-hausa-text-to-english-text")
|
13 |
-
|
14 |
-
# Load the VITS model for text-to-speech synthesis
|
15 |
-
tts_model = VitsModel.from_pretrained("Baghdad99/english_voice_tts")
|
16 |
-
tts_tokenizer = AutoTokenizer.from_pretrained("Baghdad99/english_voice_tts")
|
17 |
|
18 |
# Define the function to translate speech
|
19 |
def translate_speech(audio):
|
@@ -43,15 +39,19 @@ def translate_speech(audio):
|
|
43 |
print("The translated text does not contain 'generated_token_ids'")
|
44 |
return
|
45 |
|
46 |
-
# Use the
|
47 |
-
|
48 |
-
with torch.no_grad():
|
49 |
-
synthesised_speech = tts_model(**tts_inputs).waveform
|
50 |
print(f"Synthesised speech: {synthesised_speech}") # Print the synthesised speech to see what it contains
|
51 |
|
52 |
-
#
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
return 16000, synthesised_speech
|
57 |
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoTokenizer
|
|
|
3 |
import numpy as np
|
4 |
|
5 |
# Load the pipeline for speech recognition and translation
|
|
|
9 |
tokenizer="Baghdad99/saad-speech-recognition-hausa-audio-to-text"
|
10 |
)
|
11 |
translator = pipeline("text2text-generation", model="Baghdad99/saad-hausa-text-to-english-text")
|
12 |
+
tts = pipeline("text-to-speech", model="Baghdad99/english_voice_tts")
|
|
|
|
|
|
|
13 |
|
14 |
# Define the function to translate speech
|
15 |
def translate_speech(audio):
|
|
|
39 |
print("The translated text does not contain 'generated_token_ids'")
|
40 |
return
|
41 |
|
42 |
+
# Use the text-to-speech pipeline to synthesize the translated text
|
43 |
+
synthesised_speech = tts(translated_text_str)
|
|
|
|
|
44 |
print(f"Synthesised speech: {synthesised_speech}") # Print the synthesised speech to see what it contains
|
45 |
|
46 |
+
# Check if the synthesised speech contains 'audio'
|
47 |
+
if 'audio' in synthesised_speech:
|
48 |
+
synthesised_speech_data = synthesised_speech['audio']
|
49 |
+
else:
|
50 |
+
print("The synthesised speech does not contain 'audio'")
|
51 |
+
return
|
52 |
+
|
53 |
+
# Scale the audio data to the range of int16 format
|
54 |
+
synthesised_speech = (synthesised_speech_data * 32767).astype(np.int16)
|
55 |
|
56 |
return 16000, synthesised_speech
|
57 |
|