vladyslav
commited on
Commit
·
220d9f2
1
Parent(s):
125a144
Added states for users
Browse files
app.py
CHANGED
@@ -16,15 +16,8 @@ if os.getenv("ENV_TYPE") == "dev":
|
|
16 |
BOOKS["Test"] = "test.json"
|
17 |
MODEL_NAME_TO_CHOICE["test"] = "Test"
|
18 |
|
19 |
-
questions_data = []
|
20 |
-
current_question_index = 0
|
21 |
-
answers_log = [] # Log for saving answers
|
22 |
-
feedback_questions = ""
|
23 |
-
available_models = []
|
24 |
|
25 |
-
|
26 |
-
def get_question():
|
27 |
-
global questions_data, current_question_index
|
28 |
question = questions_data[current_question_index]
|
29 |
question_text = f"### Питання {current_question_index + 1}/{len(questions_data)}:\n#### {question['question']}"
|
30 |
answers = [answer['answer'] for answer in question['answers']]
|
@@ -33,34 +26,47 @@ def get_question():
|
|
33 |
|
34 |
|
35 |
def load_questions(model, book, student_name, class_name):
|
36 |
-
|
37 |
-
current_question_index = 0
|
38 |
-
answers_log.clear()
|
39 |
-
|
40 |
if not model or not book or student_name is None or class_name is None:
|
41 |
-
return
|
|
|
|
|
|
|
|
|
42 |
|
43 |
model_path = MODELS_PATH[model]
|
44 |
book_filename = BOOKS[book]
|
45 |
file_path = os.path.join("questions", model_path, book_filename)
|
46 |
|
47 |
if not os.path.exists(file_path):
|
48 |
-
return
|
|
|
|
|
|
|
|
|
49 |
|
50 |
with open(file_path, "r", encoding="utf-8") as file:
|
51 |
questions_data = json.load(file)
|
52 |
|
53 |
if questions_data:
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
55 |
else:
|
56 |
-
return
|
57 |
-
|
|
|
|
|
|
|
58 |
|
59 |
-
def get_next_question(selected_answer):
|
60 |
-
global current_question_index, answers_log, feedback_questions
|
61 |
|
|
|
|
|
62 |
if not selected_answer:
|
63 |
-
question_text, answers = get_question()
|
64 |
return (
|
65 |
question_text, # question_radio
|
66 |
gr.update(choices=answers, value=None, interactive=True, visible=True), # answer_radio
|
@@ -72,6 +78,9 @@ def get_next_question(selected_answer):
|
|
72 |
gr.update(visible=False), # answers_correct
|
73 |
gr.update(visible=False), # interesting_question
|
74 |
gr.update(visible=False, value=""), # feedback_questions_output
|
|
|
|
|
|
|
75 |
)
|
76 |
|
77 |
# Writing answer in log
|
@@ -80,11 +89,12 @@ def get_next_question(selected_answer):
|
|
80 |
"isCorrect": any(answer['answer'] == selected_answer and answer['isCorrect'] for answer in
|
81 |
questions_data[current_question_index]['answers'])
|
82 |
})
|
|
|
83 |
|
84 |
# Move to the next question
|
85 |
current_question_index += 1
|
86 |
if current_question_index < len(questions_data):
|
87 |
-
question_text, answers = get_question()
|
88 |
return (
|
89 |
question_text, # question_radio
|
90 |
gr.update(choices=answers, value=None, interactive=True, visible=True), # answer_radio
|
@@ -96,6 +106,9 @@ def get_next_question(selected_answer):
|
|
96 |
gr.update(visible=False), # answers_correct
|
97 |
gr.update(visible=False), # interesting_question
|
98 |
gr.update(visible=False, value=""), # feedback_questions_output
|
|
|
|
|
|
|
99 |
)
|
100 |
else:
|
101 |
# All questions are completed — ask for feedback
|
@@ -112,6 +125,9 @@ def get_next_question(selected_answer):
|
|
112 |
gr.update(visible=True), # answers_correct
|
113 |
gr.update(visible=True), # interesting_question
|
114 |
gr.update(visible=True, value=feedback_questions), # feedback_questions_output
|
|
|
|
|
|
|
115 |
)
|
116 |
|
117 |
|
@@ -122,8 +138,12 @@ def summarize_results(student_name,
|
|
122 |
feedback,
|
123 |
question_correct,
|
124 |
answers_correct,
|
125 |
-
interesting_question
|
126 |
-
|
|
|
|
|
|
|
|
|
127 |
questions = []
|
128 |
|
129 |
if question_correct is None or answers_correct is None or interesting_question is None:
|
@@ -137,8 +157,14 @@ def summarize_results(student_name,
|
|
137 |
gr.update(visible=True), # rating_text
|
138 |
gr.update(visible=True, value=feedback_questions), # feedback_questions_output
|
139 |
gr.update(visible=True), # feedback_not_provided
|
|
|
|
|
|
|
|
|
|
|
140 |
)
|
141 |
|
|
|
142 |
for question, answer in zip(questions_data, answers_log):
|
143 |
questions.append({
|
144 |
"question": question,
|
@@ -150,6 +176,7 @@ def summarize_results(student_name,
|
|
150 |
|
151 |
grade_12 = (correct_answers_count / total_questions) * 12
|
152 |
|
|
|
153 |
save_results(
|
154 |
student_name,
|
155 |
class_name,
|
@@ -170,8 +197,14 @@ def summarize_results(student_name,
|
|
170 |
f"#### Оцінка за 12-бальною шкалою: {grade_12:.2f}.\n\n"
|
171 |
)
|
172 |
|
173 |
-
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
return (
|
177 |
summary, # question_output
|
@@ -183,7 +216,12 @@ def summarize_results(student_name,
|
|
183 |
gr.update(visible=False), # rating_text
|
184 |
gr.update(visible=False, value=""), # feedback_questions_output
|
185 |
gr.update(visible=False), # feedback_not_provided
|
186 |
-
gr.update(choices=
|
|
|
|
|
|
|
|
|
|
|
187 |
)
|
188 |
|
189 |
|
@@ -206,13 +244,13 @@ def prepare_questions_for_feedback(questions, answer_log):
|
|
206 |
return "\n".join(feedback)
|
207 |
|
208 |
|
209 |
-
def update_students(class_name):
|
210 |
-
print('update_students')
|
211 |
students = STUDENTS.get(class_name, [])
|
212 |
return (
|
213 |
gr.update(choices=students, value=None, visible=True), # student_name_input
|
214 |
gr.update(choices=[], value=None, visible=False), # book_radio
|
215 |
-
gr.update(choices=
|
216 |
gr.update(value=""), # questions_output
|
217 |
gr.update(choices=[], value=None, visible=False), # answer_radio
|
218 |
gr.update(visible=False), # next_button
|
@@ -224,16 +262,20 @@ def update_students(class_name):
|
|
224 |
gr.update(value=None, visible=False), # interesting_question_radio
|
225 |
gr.update(value="", visible=False), # feedback_input
|
226 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
227 |
)
|
228 |
|
229 |
|
230 |
-
def handle_student_name_change(student_name):
|
231 |
-
|
232 |
-
|
233 |
if student_name == "Вчитель":
|
234 |
available_models = list(MODELS.keys())
|
235 |
|
236 |
-
print(
|
237 |
if not student_name:
|
238 |
return (
|
239 |
gr.update(choices=[], value=None, visible=False), # book_radio
|
@@ -249,6 +291,11 @@ def handle_student_name_change(student_name):
|
|
249 |
gr.update(value=None, visible=False), # interesting_question_radio
|
250 |
gr.update(value="", visible=False), # feedback_input
|
251 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
252 |
)
|
253 |
|
254 |
return (
|
@@ -265,16 +312,19 @@ def handle_student_name_change(student_name):
|
|
265 |
gr.update(value=None, visible=False), # interesting_question_radio
|
266 |
gr.update(value="", visible=False), # feedback_input
|
267 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
268 |
)
|
269 |
|
270 |
|
271 |
-
def filter_models(student_name, class_name, book):
|
272 |
-
|
273 |
-
|
274 |
-
print('filter_models')
|
275 |
if not book:
|
276 |
return (
|
277 |
-
gr.update(choices=
|
278 |
gr.update(value=""), # questions_output
|
279 |
gr.update(choices=[], value=None, visible=False), # answer_radio
|
280 |
gr.update(visible=False), # next_button
|
@@ -286,6 +336,11 @@ def filter_models(student_name, class_name, book):
|
|
286 |
gr.update(value=None, visible=False), # interesting_question_radio
|
287 |
gr.update(value="", visible=False), # feedback_input
|
288 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
289 |
)
|
290 |
|
291 |
if student_name == "Вчитель":
|
@@ -302,6 +357,11 @@ def filter_models(student_name, class_name, book):
|
|
302 |
gr.update(value=None, visible=False), # interesting_question_radio
|
303 |
gr.update(value="", visible=False), # feedback_input
|
304 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
305 |
)
|
306 |
|
307 |
tests = get_test_by_student_class_book(student_name, class_name, book)
|
@@ -321,8 +381,6 @@ def filter_models(student_name, class_name, book):
|
|
321 |
for model in list(available_models_names):
|
322 |
models_list.append(MODEL_NAME_TO_CHOICE.get(model))
|
323 |
|
324 |
-
available_models = models_list
|
325 |
-
|
326 |
return (
|
327 |
gr.update(choices=models_list, value=None, visible=True), # model_radio
|
328 |
gr.update(value=""), # questions_output
|
@@ -336,11 +394,16 @@ def filter_models(student_name, class_name, book):
|
|
336 |
gr.update(value=None, visible=False), # interesting_question_radio
|
337 |
gr.update(value="", visible=False), # feedback_input
|
338 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
339 |
)
|
340 |
|
341 |
|
342 |
-
def handle_model_change(model):
|
343 |
-
print(
|
344 |
print("handle_model_change")
|
345 |
if model is not None:
|
346 |
return (
|
@@ -355,6 +418,11 @@ def handle_model_change(model):
|
|
355 |
gr.update(value=None, visible=False), # interesting_question_radio
|
356 |
gr.update(value="", visible=False), # feedback_input
|
357 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
358 |
)
|
359 |
|
360 |
return (
|
@@ -369,6 +437,11 @@ def handle_model_change(model):
|
|
369 |
gr.update(value=None, visible=False), # interesting_question_radio
|
370 |
gr.update(value="", visible=False), # feedback_input
|
371 |
gr.update(visible=False), # submit_feedback_button
|
|
|
|
|
|
|
|
|
|
|
372 |
)
|
373 |
|
374 |
|
@@ -400,10 +473,17 @@ with gr.Blocks() as demo:
|
|
400 |
feedback_input = gr.Textbox(label="Будь-який коментар про тест (за бажанням)", visible=False)
|
401 |
submit_feedback_button = gr.Button("Завершити тест", visible=False)
|
402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
403 |
class_name_input.change(
|
404 |
update_students,
|
405 |
inputs=[
|
406 |
class_name_input,
|
|
|
407 |
],
|
408 |
outputs=[
|
409 |
student_name_input,
|
@@ -420,6 +500,11 @@ with gr.Blocks() as demo:
|
|
420 |
interesting_question_radio,
|
421 |
feedback_input,
|
422 |
submit_feedback_button,
|
|
|
|
|
|
|
|
|
|
|
423 |
]
|
424 |
)
|
425 |
|
@@ -427,6 +512,7 @@ with gr.Blocks() as demo:
|
|
427 |
handle_student_name_change,
|
428 |
inputs=[
|
429 |
student_name_input,
|
|
|
430 |
],
|
431 |
outputs=[
|
432 |
book_radio,
|
@@ -442,6 +528,11 @@ with gr.Blocks() as demo:
|
|
442 |
interesting_question_radio,
|
443 |
feedback_input,
|
444 |
submit_feedback_button,
|
|
|
|
|
|
|
|
|
|
|
445 |
]
|
446 |
)
|
447 |
|
@@ -451,6 +542,7 @@ with gr.Blocks() as demo:
|
|
451 |
student_name_input,
|
452 |
class_name_input,
|
453 |
book_radio,
|
|
|
454 |
],
|
455 |
outputs=[
|
456 |
model_radio,
|
@@ -465,13 +557,19 @@ with gr.Blocks() as demo:
|
|
465 |
interesting_question_radio,
|
466 |
feedback_input,
|
467 |
submit_feedback_button,
|
|
|
|
|
|
|
|
|
|
|
468 |
]
|
469 |
)
|
470 |
|
471 |
model_radio.change(
|
472 |
handle_model_change,
|
473 |
inputs=[
|
474 |
-
model_radio
|
|
|
475 |
],
|
476 |
outputs=[
|
477 |
question_output,
|
@@ -485,12 +583,18 @@ with gr.Blocks() as demo:
|
|
485 |
interesting_question_radio,
|
486 |
feedback_input,
|
487 |
submit_feedback_button,
|
|
|
|
|
|
|
|
|
|
|
488 |
]
|
489 |
)
|
490 |
|
491 |
|
492 |
-
def update_question(model, book, student_name, class_name):
|
493 |
-
question, answers = load_questions(model, book, student_name, class_name)
|
|
|
494 |
is_field_filled = len(answers) > 0
|
495 |
return (
|
496 |
question, # question_output
|
@@ -504,6 +608,11 @@ with gr.Blocks() as demo:
|
|
504 |
gr.update(visible=False, value=None), # interesting_question
|
505 |
gr.update(visible=False, value=""), # feedback_questions_output
|
506 |
gr.update(visible=False), # feedback_not_provided
|
|
|
|
|
|
|
|
|
|
|
507 |
)
|
508 |
|
509 |
|
@@ -514,6 +623,7 @@ with gr.Blocks() as demo:
|
|
514 |
book_radio,
|
515 |
student_name_input,
|
516 |
class_name_input,
|
|
|
517 |
],
|
518 |
outputs=[
|
519 |
question_output,
|
@@ -527,12 +637,22 @@ with gr.Blocks() as demo:
|
|
527 |
interesting_question_radio,
|
528 |
feedback_questions_output,
|
529 |
feedback_not_provided,
|
|
|
|
|
|
|
|
|
|
|
530 |
]
|
531 |
)
|
532 |
|
533 |
next_button.click(
|
534 |
get_next_question,
|
535 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
536 |
outputs=[
|
537 |
question_output,
|
538 |
answer_radio,
|
@@ -544,6 +664,9 @@ with gr.Blocks() as demo:
|
|
544 |
answers_correct_radio,
|
545 |
interesting_question_radio,
|
546 |
feedback_questions_output,
|
|
|
|
|
|
|
547 |
]
|
548 |
)
|
549 |
|
@@ -558,6 +681,10 @@ with gr.Blocks() as demo:
|
|
558 |
question_correct_radio,
|
559 |
answers_correct_radio,
|
560 |
interesting_question_radio,
|
|
|
|
|
|
|
|
|
561 |
],
|
562 |
outputs=[
|
563 |
question_output,
|
@@ -570,8 +697,13 @@ with gr.Blocks() as demo:
|
|
570 |
feedback_questions_output,
|
571 |
feedback_not_provided,
|
572 |
model_radio,
|
|
|
|
|
|
|
|
|
|
|
573 |
]
|
574 |
)
|
575 |
|
576 |
if __name__ == "__main__":
|
577 |
-
demo.launch()
|
|
|
16 |
BOOKS["Test"] = "test.json"
|
17 |
MODEL_NAME_TO_CHOICE["test"] = "Test"
|
18 |
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def get_question(questions_data, current_question_index):
|
|
|
|
|
21 |
question = questions_data[current_question_index]
|
22 |
question_text = f"### Питання {current_question_index + 1}/{len(questions_data)}:\n#### {question['question']}"
|
23 |
answers = [answer['answer'] for answer in question['answers']]
|
|
|
26 |
|
27 |
|
28 |
def load_questions(model, book, student_name, class_name):
|
29 |
+
print("load_questions")
|
|
|
|
|
|
|
30 |
if not model or not book or student_name is None or class_name is None:
|
31 |
+
return (
|
32 |
+
"# Будь ласка, заповніть усі поля!", # question_output
|
33 |
+
[], # answer_radio
|
34 |
+
[], # questions_data_state
|
35 |
+
)
|
36 |
|
37 |
model_path = MODELS_PATH[model]
|
38 |
book_filename = BOOKS[book]
|
39 |
file_path = os.path.join("questions", model_path, book_filename)
|
40 |
|
41 |
if not os.path.exists(file_path):
|
42 |
+
return (
|
43 |
+
f"Файл за шляхом {file_path} не знайдено.", # question_output
|
44 |
+
[], # answer_radio
|
45 |
+
[], # questions_data_state
|
46 |
+
)
|
47 |
|
48 |
with open(file_path, "r", encoding="utf-8") as file:
|
49 |
questions_data = json.load(file)
|
50 |
|
51 |
if questions_data:
|
52 |
+
question_text, answers = get_question(questions_data, 0)
|
53 |
+
return (
|
54 |
+
question_text, # question_output
|
55 |
+
answers, # answer_radio
|
56 |
+
questions_data, # questions_data_state
|
57 |
+
)
|
58 |
else:
|
59 |
+
return (
|
60 |
+
"У файлі немає питань.", # question_output
|
61 |
+
[], # answer_radio
|
62 |
+
questions_data, # questions_data_state
|
63 |
+
)
|
64 |
|
|
|
|
|
65 |
|
66 |
+
def get_next_question(selected_answer, questions_data, current_question_index, answers_log):
|
67 |
+
print("get_next_question answers_log", answers_log)
|
68 |
if not selected_answer:
|
69 |
+
question_text, answers = get_question(questions_data, current_question_index)
|
70 |
return (
|
71 |
question_text, # question_radio
|
72 |
gr.update(choices=answers, value=None, interactive=True, visible=True), # answer_radio
|
|
|
78 |
gr.update(visible=False), # answers_correct
|
79 |
gr.update(visible=False), # interesting_question
|
80 |
gr.update(visible=False, value=""), # feedback_questions_output
|
81 |
+
current_question_index, # current_question_index_state
|
82 |
+
answers_log, # answers_log_state
|
83 |
+
"", # feedback_questions_state
|
84 |
)
|
85 |
|
86 |
# Writing answer in log
|
|
|
89 |
"isCorrect": any(answer['answer'] == selected_answer and answer['isCorrect'] for answer in
|
90 |
questions_data[current_question_index]['answers'])
|
91 |
})
|
92 |
+
print("get_next_question updated answers_log", answers_log)
|
93 |
|
94 |
# Move to the next question
|
95 |
current_question_index += 1
|
96 |
if current_question_index < len(questions_data):
|
97 |
+
question_text, answers = get_question(questions_data, current_question_index)
|
98 |
return (
|
99 |
question_text, # question_radio
|
100 |
gr.update(choices=answers, value=None, interactive=True, visible=True), # answer_radio
|
|
|
106 |
gr.update(visible=False), # answers_correct
|
107 |
gr.update(visible=False), # interesting_question
|
108 |
gr.update(visible=False, value=""), # feedback_questions_output
|
109 |
+
current_question_index, # current_question_index_state
|
110 |
+
answers_log, # answers_log_state
|
111 |
+
"", # feedback_questions_state
|
112 |
)
|
113 |
else:
|
114 |
# All questions are completed — ask for feedback
|
|
|
125 |
gr.update(visible=True), # answers_correct
|
126 |
gr.update(visible=True), # interesting_question
|
127 |
gr.update(visible=True, value=feedback_questions), # feedback_questions_output
|
128 |
+
0, # current_question_index_state
|
129 |
+
answers_log, # answers_log_state
|
130 |
+
feedback_questions, # feedback_questions_state
|
131 |
)
|
132 |
|
133 |
|
|
|
138 |
feedback,
|
139 |
question_correct,
|
140 |
answers_correct,
|
141 |
+
interesting_question,
|
142 |
+
questions_data,
|
143 |
+
answers_log,
|
144 |
+
feedback_questions,
|
145 |
+
available_models):
|
146 |
+
print("summarize_results questions_data", questions_data)
|
147 |
questions = []
|
148 |
|
149 |
if question_correct is None or answers_correct is None or interesting_question is None:
|
|
|
157 |
gr.update(visible=True), # rating_text
|
158 |
gr.update(visible=True, value=feedback_questions), # feedback_questions_output
|
159 |
gr.update(visible=True), # feedback_not_provided
|
160 |
+
questions_data, # questions_data_state
|
161 |
+
0, # current_question_index_state
|
162 |
+
answers_log, # answers_log_state
|
163 |
+
feedback_questions, # feedback_questions_state
|
164 |
+
available_models, # available_models_state
|
165 |
)
|
166 |
|
167 |
+
print("summarize_results answers_log", answers_log)
|
168 |
for question, answer in zip(questions_data, answers_log):
|
169 |
questions.append({
|
170 |
"question": question,
|
|
|
176 |
|
177 |
grade_12 = (correct_answers_count / total_questions) * 12
|
178 |
|
179 |
+
print("summarize_results questions_data before save", questions_data)
|
180 |
save_results(
|
181 |
student_name,
|
182 |
class_name,
|
|
|
197 |
f"#### Оцінка за 12-бальною шкалою: {grade_12:.2f}.\n\n"
|
198 |
)
|
199 |
|
200 |
+
available_models_list = available_models
|
201 |
+
print(available_models_list)
|
202 |
+
|
203 |
+
updated_available_models = [m for m in available_models_list if
|
204 |
+
m != model] if student_name != "Вчитель" else available_models_list
|
205 |
+
|
206 |
+
print(list(updated_available_models))
|
207 |
+
print(updated_available_models)
|
208 |
|
209 |
return (
|
210 |
summary, # question_output
|
|
|
216 |
gr.update(visible=False), # rating_text
|
217 |
gr.update(visible=False, value=""), # feedback_questions_output
|
218 |
gr.update(visible=False), # feedback_not_provided
|
219 |
+
gr.update(choices=list(updated_available_models), value=None, visible=True), # model_radio
|
220 |
+
[], # questions_data_state
|
221 |
+
0, # current_question_index_state
|
222 |
+
[], # answers_log_state
|
223 |
+
"", # feedback_questions_state
|
224 |
+
updated_available_models, # available_models_state
|
225 |
)
|
226 |
|
227 |
|
|
|
244 |
return "\n".join(feedback)
|
245 |
|
246 |
|
247 |
+
def update_students(class_name, available_models):
|
248 |
+
print('update_students: ', available_models)
|
249 |
students = STUDENTS.get(class_name, [])
|
250 |
return (
|
251 |
gr.update(choices=students, value=None, visible=True), # student_name_input
|
252 |
gr.update(choices=[], value=None, visible=False), # book_radio
|
253 |
+
gr.update(choices=available_models, value=None, visible=False), # model_radio
|
254 |
gr.update(value=""), # questions_output
|
255 |
gr.update(choices=[], value=None, visible=False), # answer_radio
|
256 |
gr.update(visible=False), # next_button
|
|
|
262 |
gr.update(value=None, visible=False), # interesting_question_radio
|
263 |
gr.update(value="", visible=False), # feedback_input
|
264 |
gr.update(visible=False), # submit_feedback_button
|
265 |
+
[], # questions_data_state
|
266 |
+
0, # current_question_index_state
|
267 |
+
[], # answers_log_state
|
268 |
+
"", # feedback_questions_state
|
269 |
+
available_models, # available_models_state
|
270 |
)
|
271 |
|
272 |
|
273 |
+
def handle_student_name_change(student_name, available_models):
|
274 |
+
print("handle_student_name_change", available_models)
|
|
|
275 |
if student_name == "Вчитель":
|
276 |
available_models = list(MODELS.keys())
|
277 |
|
278 |
+
print("handle_student_name_change available_models: ", available_models)
|
279 |
if not student_name:
|
280 |
return (
|
281 |
gr.update(choices=[], value=None, visible=False), # book_radio
|
|
|
291 |
gr.update(value=None, visible=False), # interesting_question_radio
|
292 |
gr.update(value="", visible=False), # feedback_input
|
293 |
gr.update(visible=False), # submit_feedback_button
|
294 |
+
[], # questions_data_state
|
295 |
+
0, # current_question_index_state
|
296 |
+
[], # answers_log_state
|
297 |
+
"", # feedback_questions_state
|
298 |
+
available_models, # available_models_state
|
299 |
)
|
300 |
|
301 |
return (
|
|
|
312 |
gr.update(value=None, visible=False), # interesting_question_radio
|
313 |
gr.update(value="", visible=False), # feedback_input
|
314 |
gr.update(visible=False), # submit_feedback_button
|
315 |
+
[], # questions_data_state
|
316 |
+
0, # current_question_index_state
|
317 |
+
[], # answers_log_state
|
318 |
+
"", # feedback_questions_state
|
319 |
+
available_models, # available_models_state
|
320 |
)
|
321 |
|
322 |
|
323 |
+
def filter_models(student_name, class_name, book, available_models):
|
324 |
+
print('filter_models: ', available_models)
|
|
|
|
|
325 |
if not book:
|
326 |
return (
|
327 |
+
gr.update(choices=available_models, value=None, visible=False), # model_radio
|
328 |
gr.update(value=""), # questions_output
|
329 |
gr.update(choices=[], value=None, visible=False), # answer_radio
|
330 |
gr.update(visible=False), # next_button
|
|
|
336 |
gr.update(value=None, visible=False), # interesting_question_radio
|
337 |
gr.update(value="", visible=False), # feedback_input
|
338 |
gr.update(visible=False), # submit_feedback_button
|
339 |
+
[], # questions_data_state
|
340 |
+
0, # current_question_index_state
|
341 |
+
[], # answers_log_state
|
342 |
+
"", # feedback_questions_state
|
343 |
+
available_models, # available_models_state
|
344 |
)
|
345 |
|
346 |
if student_name == "Вчитель":
|
|
|
357 |
gr.update(value=None, visible=False), # interesting_question_radio
|
358 |
gr.update(value="", visible=False), # feedback_input
|
359 |
gr.update(visible=False), # submit_feedback_button
|
360 |
+
[], # questions_data_state
|
361 |
+
0, # current_question_index_state
|
362 |
+
[], # answers_log_state
|
363 |
+
"", # feedback_questions_state
|
364 |
+
MODELS.keys(), # available_models_state
|
365 |
)
|
366 |
|
367 |
tests = get_test_by_student_class_book(student_name, class_name, book)
|
|
|
381 |
for model in list(available_models_names):
|
382 |
models_list.append(MODEL_NAME_TO_CHOICE.get(model))
|
383 |
|
|
|
|
|
384 |
return (
|
385 |
gr.update(choices=models_list, value=None, visible=True), # model_radio
|
386 |
gr.update(value=""), # questions_output
|
|
|
394 |
gr.update(value=None, visible=False), # interesting_question_radio
|
395 |
gr.update(value="", visible=False), # feedback_input
|
396 |
gr.update(visible=False), # submit_feedback_button
|
397 |
+
[], # questions_data_state
|
398 |
+
0, # current_question_index_state
|
399 |
+
[], # answers_log_state
|
400 |
+
"", # feedback_questions_state
|
401 |
+
models_list, # available_models_state
|
402 |
)
|
403 |
|
404 |
|
405 |
+
def handle_model_change(model, available_models):
|
406 |
+
print("handle_model_change", available_models)
|
407 |
print("handle_model_change")
|
408 |
if model is not None:
|
409 |
return (
|
|
|
418 |
gr.update(value=None, visible=False), # interesting_question_radio
|
419 |
gr.update(value="", visible=False), # feedback_input
|
420 |
gr.update(visible=False), # submit_feedback_button
|
421 |
+
[], # questions_data_state
|
422 |
+
0, # current_question_index_state
|
423 |
+
[], # answers_log_state
|
424 |
+
"", # feedback_questions_state
|
425 |
+
available_models, # available_models_state
|
426 |
)
|
427 |
|
428 |
return (
|
|
|
437 |
gr.update(value=None, visible=False), # interesting_question_radio
|
438 |
gr.update(value="", visible=False), # feedback_input
|
439 |
gr.update(visible=False), # submit_feedback_button
|
440 |
+
[], # questions_data_state
|
441 |
+
0, # current_question_index_state
|
442 |
+
[], # answers_log_state
|
443 |
+
"", # feedback_questions_state
|
444 |
+
available_models, # available_models_state
|
445 |
)
|
446 |
|
447 |
|
|
|
473 |
feedback_input = gr.Textbox(label="Будь-який коментар про тест (за бажанням)", visible=False)
|
474 |
submit_feedback_button = gr.Button("Завершити тест", visible=False)
|
475 |
|
476 |
+
questions_data_state = gr.State([])
|
477 |
+
current_question_index_state = gr.State(0)
|
478 |
+
answers_log_state = gr.State([]) # Log for saving answers
|
479 |
+
feedback_questions_state = gr.State("")
|
480 |
+
available_models_state = gr.State([])
|
481 |
+
|
482 |
class_name_input.change(
|
483 |
update_students,
|
484 |
inputs=[
|
485 |
class_name_input,
|
486 |
+
available_models_state,
|
487 |
],
|
488 |
outputs=[
|
489 |
student_name_input,
|
|
|
500 |
interesting_question_radio,
|
501 |
feedback_input,
|
502 |
submit_feedback_button,
|
503 |
+
questions_data_state,
|
504 |
+
current_question_index_state,
|
505 |
+
answers_log_state,
|
506 |
+
feedback_questions_state,
|
507 |
+
available_models_state,
|
508 |
]
|
509 |
)
|
510 |
|
|
|
512 |
handle_student_name_change,
|
513 |
inputs=[
|
514 |
student_name_input,
|
515 |
+
available_models_state,
|
516 |
],
|
517 |
outputs=[
|
518 |
book_radio,
|
|
|
528 |
interesting_question_radio,
|
529 |
feedback_input,
|
530 |
submit_feedback_button,
|
531 |
+
questions_data_state,
|
532 |
+
current_question_index_state,
|
533 |
+
answers_log_state,
|
534 |
+
feedback_questions_state,
|
535 |
+
available_models_state,
|
536 |
]
|
537 |
)
|
538 |
|
|
|
542 |
student_name_input,
|
543 |
class_name_input,
|
544 |
book_radio,
|
545 |
+
available_models_state,
|
546 |
],
|
547 |
outputs=[
|
548 |
model_radio,
|
|
|
557 |
interesting_question_radio,
|
558 |
feedback_input,
|
559 |
submit_feedback_button,
|
560 |
+
questions_data_state,
|
561 |
+
current_question_index_state,
|
562 |
+
answers_log_state,
|
563 |
+
feedback_questions_state,
|
564 |
+
available_models_state,
|
565 |
]
|
566 |
)
|
567 |
|
568 |
model_radio.change(
|
569 |
handle_model_change,
|
570 |
inputs=[
|
571 |
+
model_radio,
|
572 |
+
available_models_state,
|
573 |
],
|
574 |
outputs=[
|
575 |
question_output,
|
|
|
583 |
interesting_question_radio,
|
584 |
feedback_input,
|
585 |
submit_feedback_button,
|
586 |
+
questions_data_state,
|
587 |
+
current_question_index_state,
|
588 |
+
answers_log_state,
|
589 |
+
feedback_questions_state,
|
590 |
+
available_models_state,
|
591 |
]
|
592 |
)
|
593 |
|
594 |
|
595 |
+
def update_question(model, book, student_name, class_name, available_models):
|
596 |
+
question, answers, questions_data = load_questions(model, book, student_name, class_name)
|
597 |
+
print("update_question", questions_data)
|
598 |
is_field_filled = len(answers) > 0
|
599 |
return (
|
600 |
question, # question_output
|
|
|
608 |
gr.update(visible=False, value=None), # interesting_question
|
609 |
gr.update(visible=False, value=""), # feedback_questions_output
|
610 |
gr.update(visible=False), # feedback_not_provided
|
611 |
+
questions_data, # questions_data_state
|
612 |
+
0, # current_question_index_state
|
613 |
+
[], # answers_log_state
|
614 |
+
"", # feedback_questions_state
|
615 |
+
available_models, # available_models_state
|
616 |
)
|
617 |
|
618 |
|
|
|
623 |
book_radio,
|
624 |
student_name_input,
|
625 |
class_name_input,
|
626 |
+
available_models_state,
|
627 |
],
|
628 |
outputs=[
|
629 |
question_output,
|
|
|
637 |
interesting_question_radio,
|
638 |
feedback_questions_output,
|
639 |
feedback_not_provided,
|
640 |
+
questions_data_state,
|
641 |
+
current_question_index_state,
|
642 |
+
answers_log_state,
|
643 |
+
feedback_questions_state,
|
644 |
+
available_models_state,
|
645 |
]
|
646 |
)
|
647 |
|
648 |
next_button.click(
|
649 |
get_next_question,
|
650 |
+
inputs=[
|
651 |
+
answer_radio,
|
652 |
+
questions_data_state,
|
653 |
+
current_question_index_state,
|
654 |
+
answers_log_state
|
655 |
+
],
|
656 |
outputs=[
|
657 |
question_output,
|
658 |
answer_radio,
|
|
|
664 |
answers_correct_radio,
|
665 |
interesting_question_radio,
|
666 |
feedback_questions_output,
|
667 |
+
current_question_index_state,
|
668 |
+
answers_log_state,
|
669 |
+
feedback_questions_state,
|
670 |
]
|
671 |
)
|
672 |
|
|
|
681 |
question_correct_radio,
|
682 |
answers_correct_radio,
|
683 |
interesting_question_radio,
|
684 |
+
questions_data_state,
|
685 |
+
answers_log_state,
|
686 |
+
feedback_questions_state,
|
687 |
+
available_models_state,
|
688 |
],
|
689 |
outputs=[
|
690 |
question_output,
|
|
|
697 |
feedback_questions_output,
|
698 |
feedback_not_provided,
|
699 |
model_radio,
|
700 |
+
questions_data_state,
|
701 |
+
current_question_index_state,
|
702 |
+
answers_log_state,
|
703 |
+
feedback_questions_state,
|
704 |
+
available_models_state,
|
705 |
]
|
706 |
)
|
707 |
|
708 |
if __name__ == "__main__":
|
709 |
+
demo.launch(server_name="0.0.0.0")
|