MK-316 commited on
Commit
09bc63b
·
verified ·
1 Parent(s): 886a79d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -22
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 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:
@@ -59,17 +50,15 @@ 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:
 
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: