Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,98 +4,9 @@ from gtts import gTTS
|
|
4 |
from pydub import AudioSegment
|
5 |
from pydub.generators import Sine
|
6 |
import os
|
7 |
-
!pip install gradio
|
8 |
-
!pip install gtts
|
9 |
-
!pip install SpeechRecognition
|
10 |
-
|
11 |
-
import gradio as gr
|
12 |
-
from gtts import gTTS
|
13 |
-
import speech_recognition as sr
|
14 |
-
from difflib import SequenceMatcher
|
15 |
-
import tempfile
|
16 |
-
import os
|
17 |
-
|
18 |
-
# Define your sentences here
|
19 |
-
sents = [
|
20 |
-
"In the small mountain village of Echo Ridge, adventure was a part of everyday life. Nestled among towering peaks, the village was said to be protected by the 'Guardian of the Glen,' a massive eagle that supposedly watched over the villagers from its perch high in the mountains. The legend inspired many adventurous tales among the villagers, especially the children.",
|
21 |
-
"Among these children was a bright-eyed eighth grader named Alex. Alex was known for his daring spirit and his love for exploring the rugged landscapes around Echo Ridge. He had a particular fascination with old maps and tales of hidden treasures that had been lost in the mountains centuries ago.",
|
22 |
-
"One day, while exploring the local library, Alex stumbled upon an ancient map tucked inside a forgotten book on village lore. The map hinted at the location of a lost treasure, hidden deep within a cave known as Whispering Hollow. Excited by the prospect of a real adventure, Alex decided to seek out the treasure.",
|
23 |
-
"Knowing the journey would be risky, he enlisted the help of his best friends, Mia and Sam. Together, they prepared for the expedition, gathering supplies and studying the map extensively. They planned their route, took note of landmarks, and readied themselves for any challenges they might face.",
|
24 |
-
"Their journey began at dawn. They trekked through dense forests, crossed rushing streams, and climbed steep cliffs. Along the way, they encountered various wildlife and navigated through tricky terrain, their map guiding them every step of the way.",
|
25 |
-
"After hours of hiking, they finally reached Whispering Hollow. The cave was more magnificent than they had imagined, filled with intricate stalactites and echoes of dripping water. Using their flashlights, they ventured deeper into the cave, guided by the markings on the map.",
|
26 |
-
"As they reached the heart of the cave, they discovered an ancient chest hidden behind a fallen boulder. With hearts pounding, they moved the boulder and opened the chest. Inside, instead of gold or jewels, they found a collection of old artifacts: pottery, coins, and a beautifully carved statuette of an eagle — the Guardian of the Glen.",
|
27 |
-
"Realizing the historical significance of their find, they decided to donate the artifacts to the local museum. The village celebrated their discovery, and the children were hailed as heroes. Their adventure brought the community together, sparking a renewed interest in the history and legends of Echo Ridge. Alex, Mia, and Sam became local legends, known not only for their daring but also for their spirit of discovery and respect for heritage. They continued to explore the mountains, each adventure strengthening their friendship and deepening their connection to their village.",
|
28 |
-
"The legend of the Guardian of the Glen lived on, not just as a protector but as a symbol of adventure and discovery, inspiring future generations to explore the mysteries of Echo Ridge."
|
29 |
-
]
|
30 |
-
|
31 |
-
def text_to_speech(selected_sentence, language):
|
32 |
-
tld = 'co.uk' if language == "British English" else 'com'
|
33 |
-
|
34 |
-
sn = int(selected_sentence.split(".")[0]) # Extract the sentence number
|
35 |
-
mytext = sents[sn - 1] # Get the selected sentence
|
36 |
-
|
37 |
-
tts = gTTS(text=mytext, lang='en', tld=tld, slow=False)
|
38 |
-
filename = 'output.mp3'
|
39 |
-
tts.save(filename)
|
40 |
-
return filename
|
41 |
-
|
42 |
-
def recognize_speech_from_microphone(audio_path):
|
43 |
-
recognizer = sr.Recognizer()
|
44 |
-
try:
|
45 |
-
with sr.AudioFile(audio_path) as source:
|
46 |
-
audio_data = recognizer.record(source)
|
47 |
-
text = recognizer.recognize_google(audio_data)
|
48 |
-
return text
|
49 |
-
except sr.UnknownValueError:
|
50 |
-
return "Could not understand the audio"
|
51 |
-
except sr.RequestError as e:
|
52 |
-
return f"Could not request results from Google Speech Recognition service; {e}"
|
53 |
-
except Exception as e:
|
54 |
-
return str(e)
|
55 |
-
|
56 |
-
def calculate_similarity(original_text, recognized_text):
|
57 |
-
return SequenceMatcher(None, original_text.lower(), recognized_text.lower()).ratio() * 100
|
58 |
-
|
59 |
-
def process_audio(selected_sentence, audio_path):
|
60 |
-
sn = int(selected_sentence.split(".")[0]) # Extract the sentence number
|
61 |
-
original_text = sents[sn - 1] # Get the selected sentence
|
62 |
-
recognized_text = recognize_speech_from_microphone(audio_path)
|
63 |
-
if "Error" in recognized_text or "Could not" in recognized_text:
|
64 |
-
return recognized_text, 0.0
|
65 |
-
similarity = calculate_similarity(original_text, recognized_text)
|
66 |
-
return recognized_text, similarity
|
67 |
-
|
68 |
-
def display_sentence(selected_sentence):
|
69 |
-
sn = int(selected_sentence.split(".")[0])
|
70 |
-
return sents[sn - 1]
|
71 |
-
|
72 |
-
with gr.Blocks() as demo:
|
73 |
-
with gr.Row():
|
74 |
-
with gr.Column():
|
75 |
-
gr.Markdown("### Text-to-Speech Converter")
|
76 |
-
dropdown_sentences = gr.Dropdown(choices=[f"{i}. {sents[i-1]}" for i in range(1, len(sents) + 1)], label="Select Sentence")
|
77 |
-
radio_language = gr.Radio(choices=['English', 'British English'], label="Language")
|
78 |
-
generate_tts_button = gr.Button("Generate Speech")
|
79 |
-
tts_audio_output = gr.Audio(type="filepath", label="Output Audio")
|
80 |
-
generate_tts_button.click(text_to_speech, inputs=[dropdown_sentences, radio_language], outputs=tts_audio_output)
|
81 |
-
selected_sentence_display = gr.Textbox(label="Selected Sentence", interactive=False)
|
82 |
-
dropdown_sentences.change(display_sentence, inputs=dropdown_sentences, outputs=selected_sentence_display)
|
83 |
-
|
84 |
-
with gr.Row():
|
85 |
-
with gr.Column():
|
86 |
-
gr.Markdown("### Pronunciation Evaluator")
|
87 |
-
mic_input = gr.Audio(label="Your Pronunciation", type="filepath")
|
88 |
-
result_button = gr.Button("Evaluate Pronunciation")
|
89 |
-
recognized_text = gr.Textbox(label="Recognized Text")
|
90 |
-
similarity_score = gr.Number(label="Similarity (%)")
|
91 |
-
|
92 |
-
result_button.click(process_audio, inputs=[dropdown_sentences, mic_input], outputs=[recognized_text, similarity_score])
|
93 |
-
|
94 |
-
demo.launch()
|
95 |
|
96 |
# Cloze questions
|
97 |
cloze_questions = [
|
98 |
-
|
99 |
{
|
100 |
"question": "Alex [BLANK] for his daring spirit and his love for exploring the rugged landscapes around Echo Ridge.",
|
101 |
"answer": "was known",
|
@@ -172,10 +83,18 @@ def text_to_speech_with_bell(text, filename):
|
|
172 |
os.remove("part2.mp3")
|
173 |
return filename
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
# Generate audio files for questions
|
176 |
for i, question in enumerate(cloze_questions):
|
177 |
-
|
178 |
-
|
|
|
|
|
179 |
|
180 |
# Function to handle the cloze quiz
|
181 |
def cloze_quiz(state, answer):
|
@@ -193,32 +112,38 @@ def cloze_quiz(state, answer):
|
|
193 |
question_index += 1
|
194 |
|
195 |
if question_index < len(cloze_questions):
|
196 |
-
|
|
|
197 |
next_hint = f"Hint: {cloze_questions[question_index]['hint']} ◁◁◁ Check out this verb!"
|
198 |
-
return (name, question_index, score, results),
|
199 |
else:
|
200 |
result_text = f"* Name: {name}\n* Score: {score} out of {len(cloze_questions)}\n" + "\n".join(results)
|
201 |
-
return (name, question_index, score, results), None, "", gr.update(visible=True, value=result_text), gr.update(visible=False)
|
202 |
|
203 |
# Function to start the quiz
|
204 |
def start_quiz(name):
|
205 |
hint = f"Hint: {cloze_questions[0]['hint']} ◁◁◁ Check out this verb!"
|
206 |
-
return (name, 0, 0, []), cloze_questions[0]["
|
207 |
|
208 |
# Create the Gradio interface
|
209 |
with gr.Blocks() as iface:
|
210 |
gr.Markdown("# Listening Cloze Test Instructions")
|
211 |
gr.Markdown("""
|
212 |
-
**
|
213 |
|
214 |
-
|
|
|
|
|
|
|
|
|
215 |
|
216 |
**Are you ready? Let's begin!**
|
217 |
""")
|
218 |
-
|
219 |
name_input = gr.Textbox(label="Enter your name")
|
220 |
start_button = gr.Button("Start Quiz")
|
221 |
-
|
|
|
222 |
hint_output = gr.Markdown()
|
223 |
answer_input = gr.Textbox(label="Your Answer")
|
224 |
next_button = gr.Button("Next")
|
@@ -227,7 +152,7 @@ with gr.Blocks() as iface:
|
|
227 |
# Initialize the state
|
228 |
state = gr.State()
|
229 |
|
230 |
-
start_button.click(start_quiz, inputs=name_input, outputs=[state,
|
231 |
-
next_button.click(cloze_quiz, inputs=[state, answer_input], outputs=[state,
|
232 |
|
233 |
-
iface.launch(share=True)
|
|
|
4 |
from pydub import AudioSegment
|
5 |
from pydub.generators import Sine
|
6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Cloze questions
|
9 |
cloze_questions = [
|
|
|
10 |
{
|
11 |
"question": "Alex [BLANK] for his daring spirit and his love for exploring the rugged landscapes around Echo Ridge.",
|
12 |
"answer": "was known",
|
|
|
83 |
os.remove("part2.mp3")
|
84 |
return filename
|
85 |
|
86 |
+
# Function to convert text to speech without any modifications
|
87 |
+
def text_to_speech(text, filename):
|
88 |
+
tts = gTTS(text)
|
89 |
+
tts.save(filename)
|
90 |
+
return filename
|
91 |
+
|
92 |
# Generate audio files for questions
|
93 |
for i, question in enumerate(cloze_questions):
|
94 |
+
full_audio_filename = f"full_question_{i+1}.mp3"
|
95 |
+
cloze_audio_filename = f"cloze_question_{i+1}.mp3"
|
96 |
+
question["full_audio"] = text_to_speech(question["question"].replace("[BLANK]", question["answer"]), full_audio_filename)
|
97 |
+
question["cloze_audio"] = text_to_speech_with_bell(question["question"], cloze_audio_filename)
|
98 |
|
99 |
# Function to handle the cloze quiz
|
100 |
def cloze_quiz(state, answer):
|
|
|
112 |
question_index += 1
|
113 |
|
114 |
if question_index < len(cloze_questions):
|
115 |
+
next_full_audio = cloze_questions[question_index]["full_audio"]
|
116 |
+
next_cloze_audio = cloze_questions[question_index]["cloze_audio"]
|
117 |
next_hint = f"Hint: {cloze_questions[question_index]['hint']} ◁◁◁ Check out this verb!"
|
118 |
+
return (name, question_index, score, results), next_full_audio, next_cloze_audio, next_hint, gr.update(visible=False), gr.update(value="", interactive=True, visible=True)
|
119 |
else:
|
120 |
result_text = f"* Name: {name}\n* Score: {score} out of {len(cloze_questions)}\n" + "\n".join(results)
|
121 |
+
return (name, question_index, score, results), None, None, "", gr.update(visible=True, value=result_text), gr.update(visible=False)
|
122 |
|
123 |
# Function to start the quiz
|
124 |
def start_quiz(name):
|
125 |
hint = f"Hint: {cloze_questions[0]['hint']} ◁◁◁ Check out this verb!"
|
126 |
+
return (name, 0, 0, []), cloze_questions[0]["full_audio"], cloze_questions[0]["cloze_audio"], hint, gr.update(visible=False), gr.update(visible=True)
|
127 |
|
128 |
# Create the Gradio interface
|
129 |
with gr.Blocks() as iface:
|
130 |
gr.Markdown("# Listening Cloze Test Instructions")
|
131 |
gr.Markdown("""
|
132 |
+
**Instructions:**
|
133 |
|
134 |
+
1. Listen to the full sentence.
|
135 |
+
2. Listen to the cloze question with the beep sound.
|
136 |
+
3. Write the correct form of the verb you hear in the blank space. Remember, you do not need to write the entire sentence, just the verb.
|
137 |
+
|
138 |
+
**For example, if you hear "Yesterday, Alex went to the store," and then you hear "Yesterday, Alex ___ to the store" with a beep sound, you should write "went" if that is the verb you heard.**
|
139 |
|
140 |
**Are you ready? Let's begin!**
|
141 |
""")
|
142 |
+
|
143 |
name_input = gr.Textbox(label="Enter your name")
|
144 |
start_button = gr.Button("Start Quiz")
|
145 |
+
full_question_audio = gr.Audio(interactive=True, autoplay=False, label="Full Sentence Listening")
|
146 |
+
cloze_question_audio = gr.Audio(interactive=True, autoplay=False, label="Cloze Question")
|
147 |
hint_output = gr.Markdown()
|
148 |
answer_input = gr.Textbox(label="Your Answer")
|
149 |
next_button = gr.Button("Next")
|
|
|
152 |
# Initialize the state
|
153 |
state = gr.State()
|
154 |
|
155 |
+
start_button.click(start_quiz, inputs=name_input, outputs=[state, full_question_audio, cloze_question_audio, hint_output, result_output, answer_input])
|
156 |
+
next_button.click(cloze_quiz, inputs=[state, answer_input], outputs=[state, full_question_audio, cloze_question_audio, hint_output, result_output, answer_input])
|
157 |
|
158 |
+
iface.launch(share=True)
|