Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import
|
|
|
4 |
|
5 |
# Load models
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Gradio Interface
|
23 |
-
|
24 |
-
fn=
|
25 |
-
inputs=gr.Audio(
|
26 |
outputs=[
|
27 |
-
gr.Textbox(label="
|
28 |
-
gr.
|
29 |
-
gr.Audio(label="English Audio")
|
30 |
],
|
31 |
-
title="Hindi to English
|
32 |
-
description="
|
33 |
)
|
34 |
|
35 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import whisper
|
3 |
+
from transformers import MarianMTModel, MarianTokenizer
|
4 |
+
from TTS.api import TTS
|
5 |
|
6 |
# Load models
|
7 |
+
print("Loading Whisper (for Hindi STT)...")
|
8 |
+
whisper_model = whisper.load_model("small")
|
9 |
+
|
10 |
+
print("Loading MarianMT (for Hindi to English)...")
|
11 |
+
translator_tokenizer = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-hi-en")
|
12 |
+
translator_model = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-hi-en")
|
13 |
+
|
14 |
+
print("Loading TTS model (Tacotron2 + HiFi-GAN)...")
|
15 |
+
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
16 |
+
|
17 |
+
# Pipeline function
|
18 |
+
def hindi_speech_to_english_audio(audio):
|
19 |
+
# Step 1: Hindi Speech to Text
|
20 |
+
result = whisper_model.transcribe(audio, language="hi")
|
21 |
+
hindi_text = result["text"]
|
22 |
+
|
23 |
+
# Step 2: Hindi to English Translation
|
24 |
+
inputs = translator_tokenizer(hindi_text, return_tensors="pt", padding=True)
|
25 |
+
translated_tokens = translator_model.generate(**inputs)
|
26 |
+
english_text = translator_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
# Step 3: English Text to Speech
|
29 |
+
tts.tts_to_file(text=english_text, file_path="output.wav")
|
30 |
+
|
31 |
+
return english_text, "output.wav"
|
32 |
|
33 |
# Gradio Interface
|
34 |
+
interface = gr.Interface(
|
35 |
+
fn=hindi_speech_to_english_audio,
|
36 |
+
inputs=gr.Audio(sources=["microphone"], type="filepath", label="🎙️ Speak in Hindi"),
|
37 |
outputs=[
|
38 |
+
gr.Textbox(label="🔤 Translated English Text"),
|
39 |
+
gr.Audio(type="filepath", label="🗣️ English Audio Output")
|
|
|
40 |
],
|
41 |
+
title="Hindi Speech to English Audio Translator",
|
42 |
+
description="🎧 Speak in Hindi and hear it back in English!",
|
43 |
)
|
44 |
|
45 |
+
# Run app
|
46 |
+
interface.launch()
|