|
|
import streamlit as st |
|
|
from gtts import gTTS |
|
|
import speech_recognition as sr |
|
|
import tempfile |
|
|
import os |
|
|
import time |
|
|
|
|
|
|
|
|
def generate_beep_and_sentence_audio(sentence): |
|
|
beep_duration = 0.5 |
|
|
beep_count = 6 |
|
|
|
|
|
|
|
|
beep_audio = "do " * beep_count |
|
|
beep_audio = beep_audio.strip() |
|
|
|
|
|
|
|
|
full_text = beep_audio + " " + sentence |
|
|
|
|
|
|
|
|
tts = gTTS(text=full_text, lang='en') |
|
|
|
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmpfile: |
|
|
tts.save(tmpfile.name) |
|
|
return tmpfile.name |
|
|
|
|
|
|
|
|
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) |
|
|
try: |
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
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." |
|
|
] |
|
|
|
|
|
|
|
|
st.title("English Shadowing Practice") |
|
|
st.write("Practice speaking English by shadowing. Listen to the sentence, and repeat it!") |
|
|
|
|
|
|
|
|
sentence = st.selectbox("Choose a sentence to practice:", sample_sentences) |
|
|
|
|
|
if sentence: |
|
|
st.write(f"**Sentence to practice**: {sentence}") |
|
|
|
|
|
|
|
|
audio_file = generate_beep_and_sentence_audio(sentence) |
|
|
|
|
|
|
|
|
st.write("The audio will be played with beeps. Wait for the beep sound before speaking.") |
|
|
|
|
|
|
|
|
if st.button("Play Audio"): |
|
|
st.audio(audio_file, format="audio/mp3") |
|
|
time.sleep(1) |
|
|
|
|
|
|
|
|
uploaded_file = st.file_uploader("Upload your recorded audio", type=["wav", "mp3"]) |
|
|
|
|
|
if uploaded_file is not None: |
|
|
|
|
|
transcription = transcribe_audio(uploaded_file) |
|
|
st.write(f"Your transcription: {transcription}") |
|
|
|
|
|
|
|
|
if transcription.lower() == sentence.lower(): |
|
|
st.success("Great job! Your pronunciation is correct.") |
|
|
else: |
|
|
st.warning("Try again. Keep practicing!") |
|
|
|