shadowing / app.py
MK-316's picture
Update app.py
1d338e1 verified
import streamlit as st
from gtts import gTTS # Google Text-to-Speech (Online)
import speech_recognition as sr # For speech recognition
import tempfile
import os
import time
# Function to generate beep sound (3 seconds) + sentence
def generate_beep_and_sentence_audio(sentence):
beep_duration = 0.5 # Each beep lasts 0.5 seconds
beep_count = 6 # "do-do-mi" sound pattern
# Generate a beep sound pattern using text-to-speech
beep_audio = "do " * beep_count # "do-do-mi" pattern
beep_audio = beep_audio.strip() # Remove extra space at the end
# Concatenate beep sound and the actual sentence
full_text = beep_audio + " " + sentence
# Generate the audio with gTTS
tts = gTTS(text=full_text, lang='en')
# Save audio to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmpfile:
tts.save(tmpfile.name)
return tmpfile.name
# Function to transcribe speech using SpeechRecognition from uploaded file
def transcribe_audio(uploaded_file):
recognizer = sr.Recognizer()
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmpfile:
tmpfile.write(uploaded_file.getbuffer())
tmpfile.close()
with sr.AudioFile(tmpfile.name) as source:
audio = recognizer.record(source) # Read entire audio file
try:
# Perform speech recognition
return recognizer.recognize_google(audio)
except sr.UnknownValueError:
return "Sorry, I could not understand the audio."
except sr.RequestError as e:
return f"Could not request results from Google Speech Recognition service; {e}"
# Predefined list of sample sentences
sample_sentences = [
"Hello, how are you?",
"I love listening to music.",
"This is a beautiful day.",
"Can you help me with this task?",
"The weather is really nice today.",
"She is reading a book right now."
]
# Streamlit App UI
st.title("English Shadowing Practice")
st.write("Practice speaking English by shadowing. Listen to the sentence, and repeat it!")
# Select a random sentence from the predefined list
sentence = st.selectbox("Choose a sentence to practice:", sample_sentences)
if sentence:
st.write(f"**Sentence to practice**: {sentence}")
# Generate the audio with 3 seconds of beeps followed by the sentence
audio_file = generate_beep_and_sentence_audio(sentence)
# Display a message to the user to play the audio and practice
st.write("The audio will be played with beeps. Wait for the beep sound before speaking.")
# Add a button to play the audio
if st.button("Play Audio"):
st.audio(audio_file, format="audio/mp3")
time.sleep(1) # Give it a short time before user plays their own recording
# Upload Audio File
uploaded_file = st.file_uploader("Upload your recorded audio", type=["wav", "mp3"])
if uploaded_file is not None:
# Transcribe the uploaded audio
transcription = transcribe_audio(uploaded_file)
st.write(f"Your transcription: {transcription}")
# Compare transcription with user input
if transcription.lower() == sentence.lower():
st.success("Great job! Your pronunciation is correct.")
else:
st.warning("Try again. Keep practicing!")