Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,6 @@ 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 |
|
|
@@ -24,20 +22,13 @@ def speak_text(text, use_gtts=False):
|
|
| 24 |
engine.say(text)
|
| 25 |
engine.runAndWait()
|
| 26 |
|
| 27 |
-
# Function to
|
| 28 |
-
def
|
| 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 |
-
|
| 40 |
-
|
|
|
|
| 41 |
with sr.AudioFile(tmpfile.name) as source:
|
| 42 |
audio = recognizer.record(source) # Read entire audio file
|
| 43 |
try:
|
|
@@ -59,17 +50,15 @@ if sentence:
|
|
| 59 |
# Play the sentence using TTS
|
| 60 |
speak_text(sentence, use_gtts=True)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
st.
|
| 64 |
-
if st.button("Start Recording"):
|
| 65 |
-
audio_data = record_speech(duration=5)
|
| 66 |
-
st.write("Recording complete.")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
| 70 |
st.write(f"Your transcription: {transcription}")
|
| 71 |
|
| 72 |
-
#
|
| 73 |
if transcription.lower() == sentence.lower():
|
| 74 |
st.success("Great job! Your pronunciation is correct.")
|
| 75 |
else:
|
|
|
|
| 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 tempfile
|
| 6 |
import os
|
| 7 |
|
|
|
|
| 22 |
engine.say(text)
|
| 23 |
engine.runAndWait()
|
| 24 |
|
| 25 |
+
# Function to transcribe speech using SpeechRecognition from uploaded file
|
| 26 |
+
def transcribe_audio(uploaded_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
recognizer = sr.Recognizer()
|
| 28 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmpfile:
|
| 29 |
+
tmpfile.write(uploaded_file.getbuffer())
|
| 30 |
+
tmpfile.close()
|
| 31 |
+
|
| 32 |
with sr.AudioFile(tmpfile.name) as source:
|
| 33 |
audio = recognizer.record(source) # Read entire audio file
|
| 34 |
try:
|
|
|
|
| 50 |
# Play the sentence using TTS
|
| 51 |
speak_text(sentence, use_gtts=True)
|
| 52 |
|
| 53 |
+
# Upload Audio File
|
| 54 |
+
uploaded_file = st.file_uploader("Upload your recorded audio", type=["wav", "mp3"])
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
if uploaded_file is not None:
|
| 57 |
+
# Transcribe the uploaded audio
|
| 58 |
+
transcription = transcribe_audio(uploaded_file)
|
| 59 |
st.write(f"Your transcription: {transcription}")
|
| 60 |
|
| 61 |
+
# Compare transcription with user input
|
| 62 |
if transcription.lower() == sentence.lower():
|
| 63 |
st.success("Great job! Your pronunciation is correct.")
|
| 64 |
else:
|