akazmi commited on
Commit
51d8785
·
verified ·
1 Parent(s): be869f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -31
app.py CHANGED
@@ -1,41 +1,55 @@
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()
 
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()