Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pyttsx3 # For text-to-speech
|
| 3 |
+
import speech_recognition as sr # For speech recognition
|
| 4 |
+
from gtts import gTTS # Google Text-to-Speech for TTS (Alternative)
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Initialize text-to-speech engine (pyttsx3 for offline use)
|
| 9 |
+
engine = pyttsx3.init()
|
| 10 |
+
|
| 11 |
+
# Function to generate speech from text
|
| 12 |
+
def speak_text(text):
|
| 13 |
+
# Using gTTS for an easy-to-use Google Text-to-Speech implementation
|
| 14 |
+
tts = gTTS(text=text, lang='en')
|
| 15 |
+
tts.save("sentence.mp3")
|
| 16 |
+
# Play the sentence using the default media player
|
| 17 |
+
os.system("start sentence.mp3") # For Windows; use 'afplay sentence.mp3' for macOS, 'mpg321 sentence.mp3' for Linux
|
| 18 |
+
|
| 19 |
+
# Function to record user's speech
|
| 20 |
+
def record_speech():
|
| 21 |
+
recognizer = sr.Recognizer()
|
| 22 |
+
with sr.Microphone() as source:
|
| 23 |
+
st.write("Listening... Speak now!")
|
| 24 |
+
audio = recognizer.listen(source)
|
| 25 |
+
try:
|
| 26 |
+
# Convert speech to text
|
| 27 |
+
recognized_text = recognizer.recognize_google(audio)
|
| 28 |
+
st.write(f"Recognized: {recognized_text}")
|
| 29 |
+
return recognized_text
|
| 30 |
+
except sr.UnknownValueError:
|
| 31 |
+
st.write("Sorry, I could not understand your speech.")
|
| 32 |
+
return None
|
| 33 |
+
except sr.RequestError:
|
| 34 |
+
st.write("Could not request results from Google Speech Recognition service.")
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# Tab 2: Practice Shadowing (User reads/speaks English)
|
| 38 |
+
with tab2:
|
| 39 |
+
st.header("2: English Shadowing Practice")
|
| 40 |
+
|
| 41 |
+
st.write("In this section, you can practice your English speaking and reading skills.")
|
| 42 |
+
|
| 43 |
+
sentence = st.text_area("Enter the sentence you want to practice", "Hello, how are you doing today?")
|
| 44 |
+
|
| 45 |
+
# Button to generate speech for the sentence
|
| 46 |
+
if st.button("Listen to the Sentence"):
|
| 47 |
+
speak_text(sentence)
|
| 48 |
+
|
| 49 |
+
# Button to record user speech
|
| 50 |
+
if st.button("Record Your Speech"):
|
| 51 |
+
recognized_text = record_speech()
|
| 52 |
+
if recognized_text:
|
| 53 |
+
if recognized_text.lower() == sentence.lower():
|
| 54 |
+
st.success("Great job! Your pronunciation is correct.")
|
| 55 |
+
else:
|
| 56 |
+
st.warning("Oops! Your pronunciation is a bit off. Try again!")
|