MK-316 commited on
Commit
1d338e1
·
verified ·
1 Parent(s): 292fb66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -11
app.py CHANGED
@@ -3,14 +3,27 @@ from gtts import gTTS # Google Text-to-Speech (Online)
3
  import speech_recognition as sr # For speech recognition
4
  import tempfile
5
  import os
 
6
 
7
- # Function to generate speech from text (using gTTS for online TTS)
8
- def speak_text(text):
9
- tts = gTTS(text=text, lang='en')
10
- # Save to temporary file
 
 
 
 
 
 
 
 
 
 
 
 
11
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmpfile:
12
  tts.save(tmpfile.name)
13
- os.system(f"start {tmpfile.name}") # For Windows; use 'afplay' for macOS, 'mpg321' for Linux
14
 
15
  # Function to transcribe speech using SpeechRecognition from uploaded file
16
  def transcribe_audio(uploaded_file):
@@ -29,16 +42,36 @@ def transcribe_audio(uploaded_file):
29
  except sr.RequestError as e:
30
  return f"Could not request results from Google Speech Recognition service; {e}"
31
 
 
 
 
 
 
 
 
 
 
 
32
  # Streamlit App UI
33
- st.title("English Shadowing App")
34
- st.write("Practice speaking English by shadowing.")
35
 
36
- # Get user input sentence
37
- sentence = st.text_input("Enter a sentence to practice:")
38
 
39
  if sentence:
40
- # Play the sentence using TTS (gTTS)
41
- speak_text(sentence)
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Upload Audio File
44
  uploaded_file = st.file_uploader("Upload your recorded audio", type=["wav", "mp3"])
 
3
  import speech_recognition as sr # For speech recognition
4
  import tempfile
5
  import os
6
+ import time
7
 
8
+ # Function to generate beep sound (3 seconds) + sentence
9
+ def generate_beep_and_sentence_audio(sentence):
10
+ beep_duration = 0.5 # Each beep lasts 0.5 seconds
11
+ beep_count = 6 # "do-do-mi" sound pattern
12
+
13
+ # Generate a beep sound pattern using text-to-speech
14
+ beep_audio = "do " * beep_count # "do-do-mi" pattern
15
+ beep_audio = beep_audio.strip() # Remove extra space at the end
16
+
17
+ # Concatenate beep sound and the actual sentence
18
+ full_text = beep_audio + " " + sentence
19
+
20
+ # Generate the audio with gTTS
21
+ tts = gTTS(text=full_text, lang='en')
22
+
23
+ # Save audio to a temporary file
24
  with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmpfile:
25
  tts.save(tmpfile.name)
26
+ return tmpfile.name
27
 
28
  # Function to transcribe speech using SpeechRecognition from uploaded file
29
  def transcribe_audio(uploaded_file):
 
42
  except sr.RequestError as e:
43
  return f"Could not request results from Google Speech Recognition service; {e}"
44
 
45
+ # Predefined list of sample sentences
46
+ sample_sentences = [
47
+ "Hello, how are you?",
48
+ "I love listening to music.",
49
+ "This is a beautiful day.",
50
+ "Can you help me with this task?",
51
+ "The weather is really nice today.",
52
+ "She is reading a book right now."
53
+ ]
54
+
55
  # Streamlit App UI
56
+ st.title("English Shadowing Practice")
57
+ st.write("Practice speaking English by shadowing. Listen to the sentence, and repeat it!")
58
 
59
+ # Select a random sentence from the predefined list
60
+ sentence = st.selectbox("Choose a sentence to practice:", sample_sentences)
61
 
62
  if sentence:
63
+ st.write(f"**Sentence to practice**: {sentence}")
64
+
65
+ # Generate the audio with 3 seconds of beeps followed by the sentence
66
+ audio_file = generate_beep_and_sentence_audio(sentence)
67
+
68
+ # Display a message to the user to play the audio and practice
69
+ st.write("The audio will be played with beeps. Wait for the beep sound before speaking.")
70
+
71
+ # Add a button to play the audio
72
+ if st.button("Play Audio"):
73
+ st.audio(audio_file, format="audio/mp3")
74
+ time.sleep(1) # Give it a short time before user plays their own recording
75
 
76
  # Upload Audio File
77
  uploaded_file = st.file_uploader("Upload your recorded audio", type=["wav", "mp3"])