Update app.py
Browse files
app.py
CHANGED
|
@@ -5,28 +5,25 @@ from pydub import AudioSegment
|
|
| 5 |
import numpy as np
|
| 6 |
import io
|
| 7 |
|
| 8 |
-
#
|
| 9 |
asr = pipeline(
|
| 10 |
"automatic-speech-recognition",
|
| 11 |
model="openai/whisper-tiny",
|
| 12 |
device="cpu"
|
| 13 |
)
|
| 14 |
|
| 15 |
-
def speech_to_speech_translation(
|
| 16 |
-
if
|
| 17 |
return None
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# Speech → French text (NON-ENGLISH)
|
| 22 |
result = asr(
|
| 23 |
-
|
| 24 |
-
sampling_rate=sr,
|
| 25 |
generate_kwargs={"task": "translate", "language": "french"}
|
| 26 |
)
|
| 27 |
french_text = result["text"]
|
| 28 |
|
| 29 |
-
# French text → French speech (free
|
| 30 |
tts = gTTS(text=french_text, lang="fr")
|
| 31 |
mp3_fp = io.BytesIO()
|
| 32 |
tts.write_to_fp(mp3_fp)
|
|
@@ -40,8 +37,8 @@ def speech_to_speech_translation(audio):
|
|
| 40 |
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=speech_to_speech_translation,
|
| 43 |
-
inputs=gr.Audio(type="
|
| 44 |
-
outputs=gr.Audio(type="numpy"),
|
| 45 |
title="Speech-to-Speech Translation (French)",
|
| 46 |
allow_flagging="never"
|
| 47 |
)
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import io
|
| 7 |
|
| 8 |
+
# Load Whisper TINY (multilingual) – CPU safe
|
| 9 |
asr = pipeline(
|
| 10 |
"automatic-speech-recognition",
|
| 11 |
model="openai/whisper-tiny",
|
| 12 |
device="cpu"
|
| 13 |
)
|
| 14 |
|
| 15 |
+
def speech_to_speech_translation(audio_path):
|
| 16 |
+
if audio_path is None:
|
| 17 |
return None
|
| 18 |
|
| 19 |
+
# 1. Speech file → French text
|
|
|
|
|
|
|
| 20 |
result = asr(
|
| 21 |
+
audio_path,
|
|
|
|
| 22 |
generate_kwargs={"task": "translate", "language": "french"}
|
| 23 |
)
|
| 24 |
french_text = result["text"]
|
| 25 |
|
| 26 |
+
# 2. French text → French speech (free, lightweight)
|
| 27 |
tts = gTTS(text=french_text, lang="fr")
|
| 28 |
mp3_fp = io.BytesIO()
|
| 29 |
tts.write_to_fp(mp3_fp)
|
|
|
|
| 37 |
|
| 38 |
demo = gr.Interface(
|
| 39 |
fn=speech_to_speech_translation,
|
| 40 |
+
inputs=gr.Audio(type="filepath", label="Upload speech"),
|
| 41 |
+
outputs=gr.Audio(type="numpy", label="French speech output"),
|
| 42 |
title="Speech-to-Speech Translation (French)",
|
| 43 |
allow_flagging="never"
|
| 44 |
)
|