File size: 3,333 Bytes
04302fc
9e71155
04302fc
 
 
1d338e1
04302fc
1d338e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e71155
 
1d338e1
23f3f9c
09bc63b
 
04302fc
23f3f9c
09bc63b
 
 
23f3f9c
 
 
 
 
 
 
 
 
 
1d338e1
 
 
 
 
 
 
 
 
 
23f3f9c
1d338e1
 
23f3f9c
1d338e1
 
06b0b92
 
1d338e1
 
 
 
 
 
 
 
 
 
 
 
23f3f9c
09bc63b
 
23f3f9c
09bc63b
 
 
23f3f9c
 
09bc63b
23f3f9c
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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!")