Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
| 3 |
+
import pyttsx3 # Text-to-Speech for feedback
|
| 4 |
+
|
| 5 |
+
# Initialize Translation Model
|
| 6 |
+
model_name = 'Helsinki-NLP/opus-mt-en-ur'
|
| 7 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 9 |
+
translator = pipeline('translation', model=model, tokenizer=tokenizer)
|
| 10 |
+
|
| 11 |
+
# Initialize Text-to-Speech Engine
|
| 12 |
+
engine = pyttsx3.init()
|
| 13 |
+
|
| 14 |
+
# Function to translate text
|
| 15 |
+
def translate_text(input_text, lang_direction):
|
| 16 |
+
if lang_direction == 'English to Urdu':
|
| 17 |
+
translation = translator(input_text, max_length=100)[0]['translation_text']
|
| 18 |
+
else:
|
| 19 |
+
translation = translator(input_text, max_length=100)[0]['translation_text']
|
| 20 |
+
return translation
|
| 21 |
+
|
| 22 |
+
# Function to provide pronunciation feedback (using TTS)
|
| 23 |
+
def provide_pronunciation_feedback(text, lang):
|
| 24 |
+
engine.say(text)
|
| 25 |
+
engine.runAndWait()
|
| 26 |
+
|
| 27 |
+
# Gradio Interface
|
| 28 |
+
def translate_and_practice(sentence, lang_direction):
|
| 29 |
+
translation = translate_text(sentence, lang_direction)
|
| 30 |
+
provide_pronunciation_feedback(translation, 'ur' if lang_direction == 'English to Urdu' else 'en')
|
| 31 |
+
return translation
|
| 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()
|