Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,13 @@
|
|
| 1 |
-
import
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import
|
| 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
|
| 11 |
-
|
| 12 |
|
| 13 |
# Function to translate text and provide feedback
|
| 14 |
def translate_and_speak(text, direction):
|
|
@@ -17,39 +16,27 @@ def translate_and_speak(text, direction):
|
|
| 17 |
else:
|
| 18 |
translated_text = reverse_translation_model(text)[0]['translation_text']
|
| 19 |
|
| 20 |
-
# Use TTS to
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
return translated_text
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 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 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import soundfile as sf
|
|
|
|
| 4 |
|
| 5 |
# Initialize translation model (English <-> Urdu)
|
| 6 |
translation_model = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
|
| 7 |
reverse_translation_model = pipeline("translation_ur_to_en", model="Helsinki-NLP/opus-mt-ur-en")
|
| 8 |
|
| 9 |
+
# Initialize text-to-speech model from Hugging Face
|
| 10 |
+
tts_model = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
|
| 11 |
|
| 12 |
# Function to translate text and provide feedback
|
| 13 |
def translate_and_speak(text, direction):
|
|
|
|
| 16 |
else:
|
| 17 |
translated_text = reverse_translation_model(text)[0]['translation_text']
|
| 18 |
|
| 19 |
+
# Use TTS to synthesize speech from translated text
|
| 20 |
+
audio = tts_model(translated_text)
|
| 21 |
+
audio_path = "output.wav"
|
| 22 |
+
sf.write(audio_path, audio["array"], 22050) # Save audio to file
|
| 23 |
+
return translated_text, audio_path
|
| 24 |
|
| 25 |
+
# Streamlit app UI
|
| 26 |
+
st.title("AI-Powered Language Tutor")
|
| 27 |
+
st.write("An interactive tutor to help you practice English-Urdu translations with speech feedback!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# User input for translation
|
| 30 |
+
text_input = st.text_area("Enter Text", "Hello, how are you?")
|
| 31 |
+
direction = st.radio("Choose Translation Direction", ["English to Urdu", "Urdu to English"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Button to process the text and play audio
|
| 34 |
+
if st.button("Translate and Speak"):
|
| 35 |
+
translated_text, audio_path = translate_and_speak(text_input, direction)
|
| 36 |
+
|
| 37 |
+
# Display translated text and audio
|
| 38 |
+
st.subheader("Translated Text:")
|
| 39 |
+
st.write(translated_text)
|
| 40 |
+
|
| 41 |
+
st.subheader("Generated Speech:")
|
| 42 |
+
st.audio(audio_path, format="audio/wav")
|