Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,24 +36,28 @@ def generate_question(text):
|
|
36 |
return question, audio_path
|
37 |
|
38 |
def transcribe_and_check(audio_path):
|
39 |
-
global
|
40 |
recognizer = sr.Recognizer()
|
41 |
with sr.AudioFile(audio_path) as source:
|
42 |
audio_data = recognizer.record(source)
|
43 |
try:
|
44 |
-
|
45 |
except sr.UnknownValueError:
|
46 |
-
return "Sorry, I
|
47 |
except sr.RequestError:
|
48 |
-
return "Speech recognition
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
|
53 |
-
else:
|
54 |
-
feedback = "β Try again."
|
55 |
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
with gr.Blocks() as app:
|
59 |
gr.Markdown("### π Interactive Coursebook Q&A")
|
|
|
36 |
return question, audio_path
|
37 |
|
38 |
def transcribe_and_check(audio_path):
|
39 |
+
global expected_answer
|
40 |
recognizer = sr.Recognizer()
|
41 |
with sr.AudioFile(audio_path) as source:
|
42 |
audio_data = recognizer.record(source)
|
43 |
try:
|
44 |
+
user_answer = recognizer.recognize_google(audio_data).lower()
|
45 |
except sr.UnknownValueError:
|
46 |
+
return "Sorry, I couldn't understand your answer."
|
47 |
except sr.RequestError:
|
48 |
+
return "Speech recognition error."
|
49 |
|
50 |
+
# Simple keyword-based matching
|
51 |
+
user_words = set(user_answer.split())
|
52 |
+
expected_words = set(expected_answer.lower().split())
|
|
|
|
|
53 |
|
54 |
+
common = user_words.intersection(expected_words)
|
55 |
+
score = len(common) / max(1, len(expected_words))
|
56 |
+
|
57 |
+
if score > 0.3 or any(word in user_answer for word in ["special", "lamp"]):
|
58 |
+
return f"β
Good answer: '{user_answer}'"
|
59 |
+
else:
|
60 |
+
return f"β Try again. You said: '{user_answer}'"
|
61 |
|
62 |
with gr.Blocks() as app:
|
63 |
gr.Markdown("### π Interactive Coursebook Q&A")
|