Spaces:
Running
Running
import gradio as gr | |
from vocab import get_sources, get_words_from_source | |
from quiz import generate_fill_in_blank_exam, check_exam | |
# 初始化全局變數保存生成的題目 | |
generated_questions = [] | |
# 生成題目 | |
def generate_quiz(source, num): | |
global generated_questions | |
generated_questions = generate_fill_in_blank_exam(source, num) | |
quiz_display = "" | |
for i, q in enumerate(generated_questions): | |
options_display = "".join([f"<label><input type='radio' name='q{i}' value='{option}'> {option}</label> " for option in q['options']]) | |
quiz_display += f"<p>{i + 1}. {q['sentence']}</p><div>{options_display}</div>" | |
quiz_display += "<p><strong>請回答後點擊提交</strong></p>" | |
return quiz_display | |
# 提交答案並計分 | |
def submit_quiz(user_answers): | |
global generated_questions | |
if not generated_questions: | |
return "<p style='color:red;'>❌ 尚未生成題目</p>", "無法計分" | |
if len(user_answers) != len(generated_questions): | |
return "<p style='color:red;'>❌ 所有題目都需作答</p>", "作答不完整" | |
score, results = check_exam(user_answers, generated_questions) | |
result_display = f"<h3>成績:{score}</h3>" | |
for i, result in enumerate(results): | |
color = 'green' if result['is_correct'] else 'red' | |
result_display += f"<p style='color:{color};'>{i + 1}. {result['question']}<br>你的答案:{result['user_answer']}<br>正確答案:{result['correct_answer']}</p>" | |
return result_display, f"得分:{score}" | |
with gr.Blocks() as demo: | |
gr.Markdown(""" | |
# 📝 英文單字小考 | |
選擇單字庫,生成填空選擇題。作答後自動計分。 | |
""") | |
source_dropdown = gr.Dropdown(choices=get_sources(), value="common3000", label="選擇單字庫") | |
num_slider = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="題目數量") | |
generate_button = gr.Button("✨ 生成小考") | |
quiz_output = gr.HTML() | |
user_answers = [gr.Radio(choices=[], label=f"題目{i + 1}") for i in range(10)] | |
submit_button = gr.Button("✅ 提交作答") | |
result_output = gr.HTML() | |
score_output = gr.Textbox(label="分數", interactive=False) | |
# 生成題目按鈕 | |
def update_radios(source, num): | |
quiz_html = generate_quiz(source, num) | |
for i, q in enumerate(generated_questions): | |
user_answers[i].choices = q['options'] | |
user_answers[i].visible = True if i < num else False | |
return quiz_html, [gr.update(visible=(i < num)) for i in range(10)] | |
generate_button.click(update_radios, inputs=[source_dropdown, num_slider], outputs=[quiz_output] + user_answers) | |
submit_button.click(submit_quiz, inputs=user_answers, outputs=[result_output, score_output]) | |
# 執行程式 | |
demo.launch() | |