Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
import pyttsx3
|
|
|
|
| 4 |
|
| 5 |
-
# Initialize
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model = MarianMTModel.from_pretrained(model_name)
|
| 9 |
-
translator = pipeline('translation', model=model, tokenizer=tokenizer)
|
| 10 |
|
| 11 |
-
# Initialize
|
| 12 |
engine = pyttsx3.init()
|
| 13 |
|
| 14 |
-
# Function to translate text
|
| 15 |
-
def
|
| 16 |
-
if
|
| 17 |
-
|
| 18 |
else:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def provide_pronunciation_feedback(text, lang):
|
| 24 |
-
engine.say(text)
|
| 25 |
engine.runAndWait()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
# Gradio
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
# UI with Gradio
|
| 34 |
-
iface = gr.Interface(fn=translate_and_practice,
|
| 35 |
-
inputs=[gr.Textbox(label="Enter Sentence"),
|
| 36 |
-
gr.Radio(choices=["English to Urdu", "Urdu to English"], label="Language Direction")],
|
| 37 |
-
outputs="text",
|
| 38 |
-
live=True, # To enable real-time updates
|
| 39 |
-
title="English-Urdu Language Tutor",
|
| 40 |
-
description="Enter a sentence and get the translation with pronunciation feedback.")
|
| 41 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import pyttsx3
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
|
| 6 |
+
# Initialize translation model (English <-> Urdu)
|
| 7 |
+
translation_model = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
|
| 8 |
+
reverse_translation_model = pipeline("translation_ur_to_en", model="Helsinki-NLP/opus-mt-ur-en")
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Initialize text-to-speech engine
|
| 11 |
engine = pyttsx3.init()
|
| 12 |
|
| 13 |
+
# Function to translate text and provide feedback
|
| 14 |
+
def translate_and_speak(text, direction):
|
| 15 |
+
if direction == "English to Urdu":
|
| 16 |
+
translated_text = translation_model(text)[0]['translation_text']
|
| 17 |
else:
|
| 18 |
+
translated_text = reverse_translation_model(text)[0]['translation_text']
|
| 19 |
+
|
| 20 |
+
# Use TTS to speak the translated text
|
| 21 |
+
engine.say(translated_text)
|
|
|
|
|
|
|
| 22 |
engine.runAndWait()
|
| 23 |
+
|
| 24 |
+
return translated_text
|
| 25 |
+
|
| 26 |
+
# Function for Speech-to-Text
|
| 27 |
+
def speech_to_text(audio_file, direction):
|
| 28 |
+
# Recognize speech from audio
|
| 29 |
+
recognizer = sr.Recognizer()
|
| 30 |
+
audio = sr.AudioFile(audio_file.name)
|
| 31 |
+
with audio as source:
|
| 32 |
+
audio_data = recognizer.record(source)
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
text = recognizer.recognize_google(audio_data)
|
| 36 |
+
except sr.UnknownValueError:
|
| 37 |
+
text = "Sorry, could not understand the audio."
|
| 38 |
+
except sr.RequestError:
|
| 39 |
+
text = "Could not request results from Google Speech Recognition service."
|
| 40 |
+
|
| 41 |
+
# Translate text
|
| 42 |
+
return translate_and_speak(text, direction)
|
| 43 |
|
| 44 |
+
# Gradio interface
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=translate_and_speak,
|
| 47 |
+
inputs=[gr.Textbox(label="Enter Text"),
|
| 48 |
+
gr.Radio(choices=["English to Urdu", "Urdu to English"], label="Translation Direction")],
|
| 49 |
+
outputs="text",
|
| 50 |
+
live=True,
|
| 51 |
+
title="AI-Powered Language Tutor",
|
| 52 |
+
description="An interactive tutor to help you practice English-Urdu translations with speech feedback!"
|
| 53 |
+
)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
iface.launch()
|