Spaces:
Sleeping
Sleeping
Update quiz.py
Browse files
quiz.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import random
|
2 |
from vocab import get_words_from_source
|
3 |
from sentences import get_sentence
|
|
|
4 |
|
5 |
# 生成單字選擇題
|
6 |
def generate_fill_in_blank_exam(source, num):
|
@@ -33,18 +34,7 @@ def generate_fill_in_blank_exam(source, num):
|
|
33 |
"phonetic": phonetic
|
34 |
})
|
35 |
|
36 |
-
|
37 |
-
if not questions:
|
38 |
-
return "<p style='color:red;'>❌ 無法生成試卷,可能單字缺少例句</p>"
|
39 |
-
|
40 |
-
exam_html = ""
|
41 |
-
for i, q in enumerate(questions):
|
42 |
-
exam_html += f"<p><strong>第 {i + 1} 題:</strong> {q['sentence']}</p>"
|
43 |
-
for option in q['options']:
|
44 |
-
exam_html += f"<input type='radio' name='q{i}' value='{option}'> {option} "
|
45 |
-
exam_html += "<br>"
|
46 |
-
|
47 |
-
return exam_html
|
48 |
|
49 |
|
50 |
# 自動對答案並計分
|
@@ -75,3 +65,17 @@ def check_exam(user_answers, questions):
|
|
75 |
f"<strong>正確答案:</strong> {res['correct_answer']}</p>"
|
76 |
|
77 |
return result_html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import random
|
2 |
from vocab import get_words_from_source
|
3 |
from sentences import get_sentence
|
4 |
+
import gradio as gr
|
5 |
|
6 |
# 生成單字選擇題
|
7 |
def generate_fill_in_blank_exam(source, num):
|
|
|
34 |
"phonetic": phonetic
|
35 |
})
|
36 |
|
37 |
+
return questions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
|
40 |
# 自動對答案並計分
|
|
|
65 |
f"<strong>正確答案:</strong> {res['correct_answer']}</p>"
|
66 |
|
67 |
return result_html
|
68 |
+
|
69 |
+
# 動態生成 Gradio Radio 元件
|
70 |
+
|
71 |
+
def render_exam_interface(questions):
|
72 |
+
radios = []
|
73 |
+
for i, q in enumerate(questions):
|
74 |
+
radios.append(
|
75 |
+
gr.Radio(
|
76 |
+
choices=q['options'],
|
77 |
+
label=f"第 {i + 1} 題:{q['sentence']}",
|
78 |
+
interactive=True
|
79 |
+
)
|
80 |
+
)
|
81 |
+
return radios
|