shadowing / app.py
MK-316's picture
Create app.py
04302fc verified
raw
history blame
2.15 kB
import streamlit as st
import pyttsx3 # For text-to-speech
import speech_recognition as sr # For speech recognition
from gtts import gTTS # Google Text-to-Speech for TTS (Alternative)
import tempfile
import os
# Initialize text-to-speech engine (pyttsx3 for offline use)
engine = pyttsx3.init()
# Function to generate speech from text
def speak_text(text):
# Using gTTS for an easy-to-use Google Text-to-Speech implementation
tts = gTTS(text=text, lang='en')
tts.save("sentence.mp3")
# Play the sentence using the default media player
os.system("start sentence.mp3") # For Windows; use 'afplay sentence.mp3' for macOS, 'mpg321 sentence.mp3' for Linux
# Function to record user's speech
def record_speech():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
st.write("Listening... Speak now!")
audio = recognizer.listen(source)
try:
# Convert speech to text
recognized_text = recognizer.recognize_google(audio)
st.write(f"Recognized: {recognized_text}")
return recognized_text
except sr.UnknownValueError:
st.write("Sorry, I could not understand your speech.")
return None
except sr.RequestError:
st.write("Could not request results from Google Speech Recognition service.")
return None
# Tab 2: Practice Shadowing (User reads/speaks English)
with tab2:
st.header("2: English Shadowing Practice")
st.write("In this section, you can practice your English speaking and reading skills.")
sentence = st.text_area("Enter the sentence you want to practice", "Hello, how are you doing today?")
# Button to generate speech for the sentence
if st.button("Listen to the Sentence"):
speak_text(sentence)
# Button to record user speech
if st.button("Record Your Speech"):
recognized_text = record_speech()
if recognized_text:
if recognized_text.lower() == sentence.lower():
st.success("Great job! Your pronunciation is correct.")
else:
st.warning("Oops! Your pronunciation is a bit off. Try again!")