File size: 4,608 Bytes
aac8fa1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import gradio as gr
import matplotlib as mpl
from quiz import BenchmarkQuiz, BENCHMARKS, QuestionData
mpl.rcParams["figure.dpi"] = 300
quiz = BenchmarkQuiz()
def update_quiz_screen(question_data: QuestionData):
quiz_state = quiz.state
return {
quiz_screen: gr.update(visible=True),
question_number: gr.update(value=question_data.question_num),
question_text: gr.update(value=question_data.question),
answer_input: gr.update(
value=quiz_state.user_answers[quiz_state.current_question],
choices=question_data.options,
visible=BENCHMARKS[quiz_state.benchmark_name]["type"] == "multiple_choice",
label=question_data.instruction,
),
free_text_input: gr.update(
value=quiz_state.user_answers[quiz_state.current_question],
visible=BENCHMARKS[quiz_state.benchmark_name]["type"] == "free_text",
label=question_data.instruction,
),
next_button: gr.update(
value=question_data.next_button_text,
),
previous_button: gr.update(visible=question_data.previous_button_visibility),
}
def update_score_screen(plot):
return {
score_screen: gr.update(visible=True),
score_plot: gr.update(value=plot),
}
def start_quiz_handler(benchmark_name):
quiz.start_quiz(benchmark_name)
question_data = quiz.update_question()
return {
start_screen: gr.update(visible=False),
score_screen: gr.update(visible=False),
**update_quiz_screen(question_data),
}
def next_question_handler(answer_input, free_text_input):
answer = (
answer_input
if BENCHMARKS[quiz.state.benchmark_name]["type"] == "multiple_choice"
else free_text_input
)
result = quiz.next_question(answer)
if result["completed"]:
return {
quiz_screen: gr.update(visible=False),
**update_score_screen(result["plot"]),
}
else:
return update_quiz_screen(result["question_data"])
def previous_question_handler():
question_data = quiz.previous_question()
return update_quiz_screen(question_data)
def reset_quiz_handler():
return {
start_screen: gr.update(visible=True),
quiz_screen: gr.update(visible=False),
score_screen: gr.update(visible=False),
next_button: gr.update(visible=True),
}
with gr.Blocks(theme=gr.themes.Soft()) as demo:
start_screen = gr.Column(visible=True)
with start_screen:
gr.Markdown("# Veldu mælipróf")
benchmark_buttons = {
name: gr.Button(info["name"]) for name, info in BENCHMARKS.items()
}
quiz_screen = gr.Column(visible=False)
with quiz_screen:
question_number = gr.Markdown()
question_text = gr.Markdown()
answer_input = gr.Radio(choices=[], visible=False)
free_text_input = gr.Textbox(visible=False)
with gr.Row():
previous_button = gr.Button("Fyrri")
next_button = gr.Button("Næsta")
score_screen = gr.Column(visible=False)
with score_screen:
gr.Markdown(f"## Niðurstöður")
score_plot = gr.Plot()
reset_btn = gr.Button("Byrja upp á nýtt")
for benchmark_name, button in benchmark_buttons.items():
button.click(
fn=start_quiz_handler,
inputs=[gr.State(benchmark_name)],
outputs=[
start_screen,
quiz_screen,
score_screen,
question_number,
question_text,
answer_input,
free_text_input,
next_button,
previous_button,
],
)
next_button.click(
fn=next_question_handler,
inputs=[answer_input, free_text_input],
outputs=[
quiz_screen,
score_screen,
question_number,
question_text,
answer_input,
free_text_input,
next_button,
previous_button,
score_plot,
],
)
previous_button.click(
fn=previous_question_handler,
inputs=[],
outputs=[
quiz_screen,
question_number,
question_text,
answer_input,
free_text_input,
next_button,
previous_button,
],
)
reset_btn.click(
fn=reset_quiz_handler,
inputs=[],
outputs=[start_screen, quiz_screen, score_screen, next_button],
)
demo.launch()
|