Update app.py
Browse files
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 (
|
|
|
|
|
|
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Function to
|
| 20 |
-
def
|
| 21 |
recognizer = sr.Recognizer()
|
| 22 |
-
with
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
if sentence:
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
st.write("
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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!")
|