Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,29 @@
|
|
| 1 |
-
import whisper
|
| 2 |
-
from transformers import MarianMTModel, MarianTokenizer
|
| 3 |
-
from gtts import gTTS
|
| 4 |
-
import tempfile
|
| 5 |
-
import os
|
| 6 |
-
import certifi
|
| 7 |
-
import gradio as gr
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
# Save audio to temp file
|
| 13 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 14 |
tmp.write(audio_file.read())
|
| 15 |
tmp_path = tmp.name
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
result = whisper_model.transcribe(tmp_path)
|
| 20 |
-
text = result["text"]
|
| 21 |
-
|
| 22 |
-
# 2. Translate text
|
| 23 |
-
lang_map = {
|
| 24 |
-
'hi': "Helsinki-NLP/opus-mt-en-hi",
|
| 25 |
-
'es': "Helsinki-NLP/opus-mt-en-es",
|
| 26 |
-
'fr': "Helsinki-NLP/opus-mt-en-fr",
|
| 27 |
-
'bn': "shhossain/opus-mt-en-to-bn"
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
if target_language not in lang_map:
|
| 31 |
-
return "Unsupported language selected", None
|
| 32 |
-
|
| 33 |
-
model_name = lang_map[target_language]
|
| 34 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 35 |
-
translation_model = MarianMTModel.from_pretrained(model_name)
|
| 36 |
-
|
| 37 |
-
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 38 |
-
outputs = translation_model.generate(**inputs)
|
| 39 |
-
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 40 |
-
|
| 41 |
-
# 3. Convert to speech with gTTS
|
| 42 |
-
tts = gTTS(translated_text, lang=target_language)
|
| 43 |
-
output_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
|
| 44 |
-
tts.save(output_path)
|
| 45 |
-
|
| 46 |
-
return translated_text, output_path
|
| 47 |
|
| 48 |
-
# Gradio Interface
|
| 49 |
iface = gr.Interface(
|
| 50 |
-
fn=
|
| 51 |
inputs=[
|
| 52 |
-
gr.Audio(source="
|
| 53 |
-
gr.Dropdown(["hi", "es", "fr", "bn"], label="
|
| 54 |
],
|
| 55 |
outputs=[
|
|
|
|
| 56 |
gr.Textbox(label="Translated Text"),
|
| 57 |
-
gr.Audio(label="
|
| 58 |
],
|
| 59 |
-
title="
|
| 60 |
-
description="
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from processing import transcribe_translate_speak
|
| 4 |
+
import tempfile
|
| 5 |
|
| 6 |
+
def process(audio_file, language):
|
|
|
|
| 7 |
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 8 |
tmp.write(audio_file.read())
|
| 9 |
tmp_path = tmp.name
|
| 10 |
|
| 11 |
+
transcription, translation, audio_output = transcribe_translate_speak(tmp_path, language)
|
| 12 |
+
return transcription, translation, audio_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
iface = gr.Interface(
|
| 15 |
+
fn=process,
|
| 16 |
inputs=[
|
| 17 |
+
gr.Audio(source="microphone", type="file", label="Speak or Upload Audio"),
|
| 18 |
+
gr.Dropdown(["hi", "es", "fr", "bn"], label="Translate To")
|
| 19 |
],
|
| 20 |
outputs=[
|
| 21 |
+
gr.Textbox(label="Transcribed Text"),
|
| 22 |
gr.Textbox(label="Translated Text"),
|
| 23 |
+
gr.Audio(label="Translated Audio")
|
| 24 |
],
|
| 25 |
+
title="🎙️ SpeechSync - Python Edition",
|
| 26 |
+
description="Record your voice, and this app will transcribe, translate, and speak it in your chosen language."
|
| 27 |
)
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|