MK-316 commited on
Commit
23f3f9c
·
verified ·
1 Parent(s): 5bf4ef9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -42
app.py CHANGED
@@ -1,54 +1,76 @@
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...")
24
- audio = recognizer.listen(source)
25
- try:
26
- st.write("Recognizing speech...")
27
- text = recognizer.recognize_google(audio)
28
- return text
29
- except sr.UnknownValueError:
30
- return "Sorry, I couldn't understand the speech."
31
- except sr.RequestError:
32
- return "Sorry, there was an error with the speech recognition service."
33
-
34
- # Streamlit UI
35
- st.title("English Shadowing Practice")
36
-
37
- # Input box for the user to type a sentence
38
- sentence = st.text_input("Enter a sentence to practice")
 
 
39
 
40
  if sentence:
41
- # Display the input sentence
42
- st.write(f"Your sentence: {sentence}")
43
- # Play the sentence
44
- speak_text(sentence)
45
-
46
- # Button to record user's speech
47
- if st.button("Record Your Speech"):
48
- speech = record_speech()
49
- st.write(f"You said: {speech}")
50
- # Provide feedback or allow comparison
51
- if speech.lower() == sentence.lower():
52
- st.write("Good job! Your speech matches the sentence.")
53
- else:
54
- st.write("Try again!")
 
 
 
 
 
1
  import streamlit as st
2
+ import pyttsx3 # For text-to-speech (offline)
3
  import speech_recognition as sr # For speech recognition
4
+ from gtts import gTTS # Google Text-to-Speech for TTS (alternative)
5
+ import sounddevice as sd # For audio recording
6
+ import numpy as np # Required for sounddevice
7
  import tempfile
8
  import os
9
 
10
  # Initialize text-to-speech engine (pyttsx3 for offline use)
11
  engine = pyttsx3.init()
12
 
13
+ # Function to generate speech from text (using pyttsx3 or gTTS)
14
+ def speak_text(text, use_gtts=False):
15
+ if use_gtts:
16
+ # Use Google TTS (Online)
17
+ tts = gTTS(text=text, lang='en')
18
+ # Save to temporary file
19
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmpfile:
20
+ tts.save(tmpfile.name)
21
+ os.system(f"start {tmpfile.name}") # For Windows; use 'afplay' for macOS, 'mpg321' for Linux
22
+ else:
23
+ # Use pyttsx3 (Offline)
24
+ engine.say(text)
25
+ engine.runAndWait()
26
+
27
+ # Function to record user's speech using sounddevice
28
+ def record_speech(duration=5, fs=16000):
29
+ # Record audio for a given duration (seconds) and sampling rate (fs)
30
+ st.write("Recording...")
31
+ audio_data = sd.rec(int(duration * fs), samplerate=fs, channels=1, dtype='int16')
32
+ sd.wait() # Wait until recording is finished
33
+ return audio_data
34
 
35
+ # Function to transcribe speech using SpeechRecognition
36
+ def transcribe_speech(audio_data, fs=16000):
37
  recognizer = sr.Recognizer()
38
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmpfile:
39
+ # Save recorded audio to temporary file
40
+ sd.write(tmpfile.name, audio_data, fs)
41
+ with sr.AudioFile(tmpfile.name) as source:
42
+ audio = recognizer.record(source) # Read entire audio file
43
+ try:
44
+ # Perform speech recognition
45
+ return recognizer.recognize_google(audio)
46
+ except sr.UnknownValueError:
47
+ return "Sorry, I could not understand the audio."
48
+ except sr.RequestError as e:
49
+ return f"Could not request results from Google Speech Recognition service; {e}"
50
+
51
+ # Streamlit App UI
52
+ st.title("English Shadowing App")
53
+ st.write("Practice speaking English by shadowing.")
54
+
55
+ # Get user input sentence
56
+ sentence = st.text_input("Enter a sentence to practice:")
57
 
58
  if sentence:
59
+ # Play the sentence using TTS
60
+ speak_text(sentence, use_gtts=True)
61
+
62
+ # Record user's speech
63
+ st.write("Now, please say the sentence.")
64
+ if st.button("Start Recording"):
65
+ audio_data = record_speech(duration=5)
66
+ st.write("Recording complete.")
67
+
68
+ # Transcribe and compare
69
+ transcription = transcribe_speech(audio_data)
70
+ st.write(f"Your transcription: {transcription}")
71
+
72
+ # Show comparison
73
+ if transcription.lower() == sentence.lower():
74
+ st.success("Great job! Your pronunciation is correct.")
75
+ else:
76
+ st.warning("Try again. Keep practicing!")