Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -26,51 +26,47 @@ def translate_speech(audio_data_tuple):
|
|
26 |
output = model.transcribe([temp_audio_file.name])
|
27 |
print(f"Output: {output}") # Print the output to see what it contains
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
transcription = output["transcription"]
|
32 |
-
else:
|
33 |
-
print("The output does not contain 'transcription'")
|
34 |
-
return
|
35 |
-
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
|
60 |
-
|
61 |
-
|
62 |
|
63 |
-
|
64 |
-
|
65 |
|
66 |
-
|
67 |
|
68 |
# Define the Gradio interface
|
69 |
iface = gr.Interface(
|
70 |
fn=translate_speech,
|
71 |
inputs=gr.inputs.Audio(source="microphone"), # Change this line
|
72 |
outputs=gr.outputs.Audio(type="numpy"),
|
73 |
-
title="
|
74 |
description="Realtime demo for Hausa to English translation using speech recognition and text-to-speech synthesis."
|
75 |
)
|
76 |
|
|
|
26 |
output = model.transcribe([temp_audio_file.name])
|
27 |
print(f"Output: {output}") # Print the output to see what it contains
|
28 |
|
29 |
+
# Extract the transcriptions from the outputs
|
30 |
+
transcriptions = [output['transcription'] for output in outputs]
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
for transcription in transcriptions:
|
33 |
+
# Use the translation pipeline to translate the transcription
|
34 |
+
translated_text = translator(transcription, return_tensors="pt")
|
35 |
+
print(f"Translated text: {translated_text}") # Print the translated text to see what it contains
|
36 |
|
37 |
+
# Check if the translated text contains 'generated_token_ids'
|
38 |
+
if 'generated_token_ids' in translated_text[0]:
|
39 |
+
# Decode the tokens into text
|
40 |
+
translated_text_str = translator.tokenizer.decode(translated_text[0]['generated_token_ids'])
|
41 |
+
else:
|
42 |
+
print("The translated text does not contain 'generated_token_ids'")
|
43 |
+
return
|
44 |
|
45 |
+
# Use the text-to-speech pipeline to synthesize the translated text
|
46 |
+
synthesised_speech = tts(translated_text_str)
|
47 |
+
print(f"Synthesised speech: {synthesised_speech}") # Print the synthesised speech to see what it contains
|
48 |
|
49 |
+
# Check if the synthesised speech contains 'audio'
|
50 |
+
if 'audio' in synthesised_speech:
|
51 |
+
synthesised_speech_data = synthesised_speech['audio']
|
52 |
+
else:
|
53 |
+
print("The synthesised speech does not contain 'audio'")
|
54 |
+
return
|
55 |
|
56 |
+
# Flatten the audio data
|
57 |
+
synthesised_speech_data = synthesised_speech_data.flatten()
|
58 |
|
59 |
+
# Scale the audio data to the range of int16 format
|
60 |
+
synthesised_speech = (synthesised_speech_data * 32767).astype(np.int16)
|
61 |
|
62 |
+
return 16000, synthesised_speech
|
63 |
|
64 |
# Define the Gradio interface
|
65 |
iface = gr.Interface(
|
66 |
fn=translate_speech,
|
67 |
inputs=gr.inputs.Audio(source="microphone"), # Change this line
|
68 |
outputs=gr.outputs.Audio(type="numpy"),
|
69 |
+
title="English to Hausa Translation",
|
70 |
description="Realtime demo for Hausa to English translation using speech recognition and text-to-speech synthesis."
|
71 |
)
|
72 |
|