englissi commited on
Commit
37cf751
1 Parent(s): 2a12484

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +231 -0
app.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from gtts import gTTS
4
+ from pydub import AudioSegment
5
+ from pydub.generators import Sine
6
+ import os
7
+
8
+ # Cloze questions
9
+ cloze_questions = [
10
+ {
11
+ "question": "In the small mountain village of Echo Ridge, adventure [BLANK] a part of everyday life.",
12
+ "answer": "was",
13
+ "hint": "be"
14
+ },
15
+ {
16
+ "question": "Among these children [BLANK] a bright-eyed eighth grader named Alex.",
17
+ "answer": "was",
18
+ "hint": "be"
19
+ },
20
+ {
21
+ "question": "Alex [BLANK] for his daring spirit and his love for exploring the rugged landscapes around Echo Ridge.",
22
+ "answer": "was known",
23
+ "hint": "know"
24
+ },
25
+ {
26
+ "question": "He [BLANK] a particular fascination with the old maps and tales of hidden treasures that had been lost in the mountains centuries ago.",
27
+ "answer": "had",
28
+ "hint": "have"
29
+ },
30
+ {
31
+ "question": "One day, while exploring the local library, Alex [BLANK] upon an ancient map tucked inside a forgotten book on village lore.",
32
+ "answer": "stumbled",
33
+ "hint": "stumble"
34
+ },
35
+ {
36
+ "question": "The map [BLANK] at the location of a lost treasure, hidden deep within a cave known as Whispering Hollow.",
37
+ "answer": "hinted",
38
+ "hint": "hint"
39
+ },
40
+ {
41
+ "question": "Excited by the prospect of a real adventure, Alex [BLANK] to seek out the treasure.",
42
+ "answer": "decided",
43
+ "hint": "decide"
44
+ },
45
+ {
46
+ "question": "Knowing the journey would be risky, he [BLANK] the help of his best friends, Mia and Sam.",
47
+ "answer": "enlisted",
48
+ "hint": "enlist"
49
+ },
50
+ {
51
+ "question": "Together, they [BLANK] for the expedition, gathering supplies and studying the map extensively.",
52
+ "answer": "prepared",
53
+ "hint": "prepare"
54
+ },
55
+ {
56
+ "question": "They [BLANK] their route, took note of landmarks, and readied themselves for any challenges they might face.",
57
+ "answer": "planned",
58
+ "hint": "plan"
59
+ },
60
+ {
61
+ "question": "Their journey [BLANK] at dawn.",
62
+ "answer": "began",
63
+ "hint": "begin"
64
+ },
65
+ {
66
+ "question": "They [BLANK] through dense forests, crossed rushing streams, and climbed steep cliffs.",
67
+ "answer": "trekked",
68
+ "hint": "trek"
69
+ },
70
+ {
71
+ "question": "Along the way, they [BLANK] various wildlife and navigated through tricky terrains, their map guiding them every step of the way.",
72
+ "answer": "encountered",
73
+ "hint": "encounter"
74
+ },
75
+ {
76
+ "question": "After hours of hiking, they finally [BLANK] Whispering Hollow.",
77
+ "answer": "reached",
78
+ "hint": "reach"
79
+ },
80
+ {
81
+ "question": "The cave [BLANK] more magnificent than they had imagined, filled with intricate stalactites and echoes of dripping water.",
82
+ "answer": "was",
83
+ "hint": "be"
84
+ },
85
+ {
86
+ "question": "Using their flashlights, they [BLANK] deeper into the cave, guided by the markings on the map.",
87
+ "answer": "ventured",
88
+ "hint": "venture"
89
+ },
90
+ {
91
+ "question": "As they [BLANK] the heart of the cave, they discovered an ancient chest hidden behind a fallen boulder.",
92
+ "answer": "reached",
93
+ "hint": "reach"
94
+ },
95
+ {
96
+ "question": "With hearts pounding, they [BLANK] the boulder and opened the chest.",
97
+ "answer": "moved",
98
+ "hint": "move"
99
+ },
100
+ {
101
+ "question": "Inside, instead of gold or jewels, they [BLANK] a collection of old artifacts: pottery, coins, and a beautifully carved statuette of an eagle — the Guardian of the Glen.",
102
+ "answer": "found",
103
+ "hint": "find"
104
+ },
105
+ {
106
+ "question": "Realizing the historical significance of their find, they [BLANK] to donate the artifacts to the local museum.",
107
+ "answer": "decided",
108
+ "hint": "decide"
109
+ },
110
+ {
111
+ "question": "The village [BLANK] their discovery, and the children were hailed as heroes.",
112
+ "answer": "celebrated",
113
+ "hint": "celebrate"
114
+ },
115
+ {
116
+ "question": "Their adventure [BLANK] the community together, sparking a renewed interest in the history and legends of Echo Ridge.",
117
+ "answer": "brought",
118
+ "hint": "bring"
119
+ },
120
+ {
121
+ "question": "Alex, Mia, and Sam [BLANK] local legends, known not only for their daring but also for their spirit of discovery and respect for heritage.",
122
+ "answer": "became",
123
+ "hint": "become"
124
+ },
125
+ {
126
+ "question": "They [BLANK] to explore the mountains, each adventure strengthening their friendship and deepening their connection to their village.",
127
+ "answer": "continued",
128
+ "hint": "continue"
129
+ },
130
+ {
131
+ "question": "The legend of the Guardian of the Glen [BLANK] on, not just as a protector but as a symbol of adventure and discovery, inspiring future generations to explore the mysteries of Echo Ridge.",
132
+ "answer": "lived",
133
+ "hint": "live"
134
+ }
135
+ ]
136
+
137
+ # Function to create a bell sound
138
+ def create_bell_sound(filename="bell.wav"):
139
+ # Generate a bell sound (1000 Hz tone for 200 ms)
140
+ tone = Sine(1000).to_audio_segment(duration=200).apply_gain(-10).fade_in(50).fade_out(50)
141
+ tone.export(filename, format="wav")
142
+
143
+ # Create the bell sound file
144
+ create_bell_sound()
145
+
146
+ # Function to convert text to speech and add bell sound
147
+ def text_to_speech_with_bell(text, filename):
148
+ tts = gTTS(text.replace("[BLANK]", ""))
149
+ tts.save("temp.mp3")
150
+
151
+ # Load the generated speech and bell sound
152
+ speech = AudioSegment.from_mp3("temp.mp3")
153
+ bell = AudioSegment.from_wav("bell.wav")
154
+
155
+ # Find the position of the blank and insert the bell sound
156
+ blank_position = text.find("[BLANK]")
157
+ part1 = text[:blank_position]
158
+ part2 = text[blank_position + len("[BLANK]"):]
159
+ tts_part1 = gTTS(part1).save("part1.mp3")
160
+ tts_part2 = gTTS(part2).save("part2.mp3")
161
+ speech_part1 = AudioSegment.from_mp3("part1.mp3")
162
+ speech_part2 = AudioSegment.from_mp3("part2.mp3")
163
+
164
+ # Create a silent segment (2 seconds)
165
+ silent_segment = AudioSegment.silent(duration=2000)
166
+
167
+ combined = speech_part1 + bell + silent_segment + speech_part2
168
+
169
+ # Save the final audio with the bell sound inserted
170
+ combined.export(filename, format="mp3")
171
+ os.remove("temp.mp3")
172
+ os.remove("part1.mp3")
173
+ os.remove("part2.mp3")
174
+ return filename
175
+
176
+ # Generate audio files for questions
177
+ for i, question in enumerate(cloze_questions):
178
+ audio_filename = f"question_{i+1}.mp3"
179
+ question["audio"] = text_to_speech_with_bell(question["question"], audio_filename)
180
+
181
+ # Function to handle the cloze quiz
182
+ def cloze_quiz(state, answer):
183
+ name, question_index, score, results = state
184
+ question = cloze_questions[question_index]
185
+
186
+ correct = answer.strip().lower() == question["answer"].lower()
187
+
188
+ if correct:
189
+ score += 1
190
+ results.append(f"Question {question_index + 1}: Correct\n")
191
+ else:
192
+ results.append(f"Question {question_index + 1}: Incorrect, the correct answer is: {question['answer']}\n")
193
+
194
+ question_index += 1
195
+
196
+ if question_index < len(cloze_questions):
197
+ next_question_audio = cloze_questions[question_index]["audio"]
198
+ next_hint = f"Hint: {cloze_questions[question_index]['hint']} (동사 형태 변형 가능)"
199
+ return (name, question_index, score, results), next_question_audio, next_hint, gr.update(visible=False), gr.update(value="", interactive=True, visible=True)
200
+ else:
201
+ result_text = f"* Name: {name}\n* Score: {score} out of {len(cloze_questions)}\n" + "\n".join(results)
202
+ return (name, question_index, score, results), None, "", gr.update(visible=True, value=result_text), gr.update(visible=False)
203
+
204
+ # Function to start the quiz
205
+ def start_quiz(name):
206
+ hint = f"Hint: {cloze_questions[0]['hint']} (동사 형태 변형 가능)"
207
+ return (name, 0, 0, []), cloze_questions[0]["audio"], hint, gr.update(visible=False), gr.update(visible=True)
208
+
209
+ # Create the Gradio interface
210
+ with gr.Blocks() as iface:
211
+ gr.Markdown("# Listening Cloze Test (총문항수 27개)")
212
+ gr.Markdown("**신호음이 들리는 빈칸에 들어갈 동사의 형태를 써보세요. (문장 전체를 쓰지 않습니다.)**")
213
+ name_input = gr.Textbox(label="Enter your name")
214
+ start_button = gr.Button("Start Quiz")
215
+ question_audio = gr.Audio(interactive=False, autoplay=True)
216
+ hint_output = gr.Markdown()
217
+ answer_input = gr.Textbox(label="Your Answer")
218
+ next_button = gr.Button("Next")
219
+ result_output = gr.Textbox(label="Results", interactive=False, visible=False)
220
+
221
+ # Initialize the state
222
+ state = gr.State()
223
+
224
+ start_button.click(start_quiz, inputs=name_input, outputs=[state, question_audio, hint_output, result_output, answer_input])
225
+ next_button.click(cloze_quiz, inputs=[state, answer_input], outputs=[state, question_audio, hint_output, result_output, answer_input])
226
+
227
+ # Add the hyperlink and image
228
+ gr.Markdown("<img src='https://github.com/englissi/englissi/blob/4f8a7cc7c7194132422e05081fd7d9502c3c4c65/Sample/image%20matching.webp' alt='Image' />")
229
+ gr.Markdown("[Click here to visit the matching game](http://englissi-matching.hf.space)")
230
+
231
+ iface.launch(share=True)