File size: 15,758 Bytes
074b318 c9a4f7a f86ba47 074b318 fefd70e e872a9e 8974ee4 f86ba47 fefd70e 074b318 706fbde ec98f81 fefd70e 074b318 c059dfd 074b318 c9a4f7a d816512 c9a4f7a c37b8d9 074b318 186dae7 c37b8d9 074b318 c9a4f7a 074b318 c059dfd 074b318 49dd514 c9a4f7a 49dd514 834de25 9f10b71 a8e81a1 9f10b71 159cbd7 49dd514 074b318 f86ba47 074b318 c9a4f7a 49dd514 834de25 9f10b71 a8e81a1 9f10b71 159cbd7 49dd514 074b318 f972eea d816512 c059dfd c37b8d9 49dd514 c37b8d9 49dd514 834de25 9f10b71 a8e81a1 9f10b71 159cbd7 c37b8d9 f86ba47 9f10b71 a8e81a1 9f10b71 c059dfd f86ba47 7958dfb 083d2f0 7958dfb d816512 7958dfb 9f10b71 a8e81a1 9f10b71 159cbd7 c059dfd 7958dfb f86ba47 f972eea 491af5b 9f10b71 a8e81a1 491af5b 9f2f337 9f10b71 f972eea fbaf544 d816512 fbaf544 c37b8d9 9f10b71 7887bc1 9f10b71 159cbd7 c059dfd c37b8d9 074b318 c059dfd d816512 86c8e35 c059dfd 86c8e35 99b8fc4 86c8e35 c059dfd d816512 c059dfd 186dae7 8974ee4 186dae7 8974ee4 e872a9e 8974ee4 e872a9e 8974ee4 e872a9e 8974ee4 e872a9e d5915c6 8974ee4 e872a9e 8974ee4 186dae7 074b318 857d5df 074b318 186dae7 8974ee4 074b318 186dae7 074b318 c37b8d9 074b318 159cbd7 9f10b71 a8e81a1 f972eea 186dae7 8974ee4 186dae7 f86ba47 8974ee4 7958dfb c37b8d9 9f10b71 49dd514 9f10b71 7887bc1 159cbd7 c059dfd c37b8d9 f86ba47 074b318 9f10b71 a8e81a1 c059dfd 9f10b71 074b318 9f10b71 a8e81a1 c059dfd 9f10b71 074b318 f972eea 9f10b71 a8e81a1 9f10b71 a8e81a1 9f10b71 c059dfd 9f10b71 f972eea 074b318 |
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
import json
import os
import random
import gradio as gr
from dotenv import load_dotenv
from constants import MODELS, MODELS_PATH, BOOKS, CLASSES, STUDENTS, MODEL_NAME_TO_CHOICE
from utils import save_results, get_test_by_student_class_book
load_dotenv()
if os.getenv("ENV_TYPE") == "dev":
MODELS["Test"] = "test"
MODELS_PATH["Test"] = "test"
BOOKS["Test"] = "test.json"
MODEL_NAME_TO_CHOICE["test"] = "Test"
questions_data = []
current_question_index = 0
answers_log = [] # Log for saving answers
feedback_questions = ""
def get_question():
global questions_data, current_question_index
question = questions_data[current_question_index]
question_text = f"### Питання {current_question_index + 1}/{len(questions_data)}:\n#### {question['question']}"
answers = [answer['answer'] for answer in question['answers']]
random.shuffle(answers)
return question_text, answers
def load_questions(model, book, student_name, class_name):
global questions_data, current_question_index
current_question_index = 0
answers_log.clear()
if not model or not book or student_name is None or class_name is None:
return "# Будь ласка, заповніть усі поля!", []
model_path = MODELS_PATH[model]
book_filename = BOOKS[book]
file_path = os.path.join("questions", model_path, book_filename)
if not os.path.exists(file_path):
return f"Файл за шляхом {file_path} не знайдено.", []
with open(file_path, "r", encoding="utf-8") as file:
questions_data = json.load(file)
if questions_data:
return get_question()
else:
return "У файлі немає питань.", []
def get_next_question(selected_answer):
global current_question_index, answers_log, feedback_questions
if not selected_answer:
question_text, answers = get_question()
return (
question_text, # question_radio
gr.update(choices=answers, value=None, interactive=True, visible=True), # answer_radio
gr.update(visible=True), # next_button
gr.update(visible=False), # feedback_input
gr.update(visible=False), # submit_feedback_button
gr.update(visible=False), # rating_text
gr.update(visible=False), # question_correct
gr.update(visible=False), # answers_correct
gr.update(visible=False), # interesting_question
gr.update(visible=False, value=""), # feedback_questions_output
)
# Writing answer in log
answers_log.append({
"selected": selected_answer,
"isCorrect": any(answer['answer'] == selected_answer and answer['isCorrect'] for answer in
questions_data[current_question_index]['answers'])
})
# Move to the next question
current_question_index += 1
if current_question_index < len(questions_data):
question_text, answers = get_question()
return (
question_text, # question_radio
gr.update(choices=answers, value=None, interactive=True, visible=True), # answer_radio
gr.update(visible=True), # next_button
gr.update(visible=False), # feedback_input
gr.update(visible=False), # submit_feedback_button
gr.update(visible=False), # rating_text
gr.update(visible=False), # question_correct
gr.update(visible=False), # answers_correct
gr.update(visible=False), # interesting_question
gr.update(visible=False, value=""), # feedback_questions_output
)
else:
# All questions are completed — ask for feedback
question_text = "#### Дякуємо за участь у тесті!\n#### Нижче можете переглянути ваші та правильні відповіді\n#### Залиште, будь ласка, відгук про тест в кінці сторінки.\n---"
feedback_questions = prepare_questions_for_feedback(questions_data, answers_log)
return (
question_text, # question_radio
gr.update(visible=False, value=None), # answer_radio
gr.update(visible=False), # next_button
gr.update(visible=True), # feedback_input
gr.update(visible=True), # submit_feedback_button
gr.update(visible=True), # rating_text
gr.update(visible=True), # question_correct
gr.update(visible=True), # answers_correct
gr.update(visible=True), # interesting_question
gr.update(visible=True, value=feedback_questions), # feedback_questions_output
)
def summarize_results(student_name,
class_name,
model,
book,
feedback,
question_correct,
answers_correct,
interesting_question):
global questions_data, answers_log, feedback_questions
questions = []
if question_correct is None or answers_correct is None or interesting_question is None:
return (
"### Залиште відгук про тест!", # question_output
gr.update(visible=True), # feedback_input
gr.update(visible=True), # submit_feedback_button
gr.update(visible=True), # question_correct
gr.update(visible=True), # answers_correct
gr.update(visible=True), # interesting_question
gr.update(visible=True), # rating_text
gr.update(visible=True, value=feedback_questions), # feedback_questions_output
gr.update(visible=True), # feedback_not_provided
)
for question, answer in zip(questions_data, answers_log):
questions.append({
"question": question,
"answer": answer
})
correct_answers_count = sum(1 for answer in answers_log if answer['isCorrect'])
total_questions = len(questions_data)
grade_12 = (correct_answers_count / total_questions) * 12
save_results(
student_name,
class_name,
MODELS[model],
book,
questions,
feedback,
question_correct,
answers_correct,
interesting_question,
grade_12,
correct_answers_count,
)
summary = (
f"#### Тест для моделі {model} закінчено!\n\n"
f"#### Ви відповіли правильно на {correct_answers_count} з {total_questions} питань.\n\n"
f"#### Оцінка за 12-бальною шкалою: {grade_12:.2f}.\n\n"
)
return (
summary, # question_output
gr.update(visible=False, value=""), # feedback_input
gr.update(visible=False), # submit_feedback_button
gr.update(visible=False, value=None), # question_correct
gr.update(visible=False, value=None), # answers_correct
gr.update(visible=False, value=None), # interesting_question
gr.update(visible=False), # rating_text
gr.update(visible=False, value=""), # feedback_questions_output
gr.update(visible=False), # feedback_not_provided
)
def prepare_questions_for_feedback(questions, answer_log):
feedback = []
for i, question in enumerate(questions):
question_text = f"#### Питання: {question['question']}"
quote = f"#### Цитата: *{question['textPart']}*"
user_answer = answer_log[i]['selected']
answers_text = "\n".join(
[
f"- {'**(Правильна)**' if ans['isCorrect'] else ''} {'**(Обрана)**' if ans['answer'] == user_answer else ''} {ans['answer']}"
for ans in question['answers']
]
)
feedback.append(f"{question_text}\n{quote}\n#### Відповіді:\n{answers_text}\n---")
return "\n".join(feedback)
def update_students(class_name):
print('update_students')
students = STUDENTS.get(class_name, [])
return (
gr.update(choices=students, value=None, visible=True), # student_name_input
gr.update(choices=[], value=None, visible=False), # book_radio
gr.update(choices=[], value=None, visible=False), # model_radio
)
def handle_student_name_change(student_name):
print('handle_student_name_change')
if not student_name:
return (
gr.update(choices=[], value=None, visible=False), # book_radio
gr.update(choices=[], value=None, visible=False), # model_radio
)
return (
gr.update(choices=list(BOOKS.keys()), value=None, visible=True), # book_radio
gr.update(choices=[], value=None, visible=False), # model_radio
)
def filter_models(student_name, class_name, book):
print('filter_models')
if not book:
return gr.update(choices=[], value=None, visible=False)
tests = get_test_by_student_class_book(student_name, class_name, book)
available_models = list(MODELS.values())
for test in tests:
if test.get("model") in available_models:
available_models.remove(test.get("model"))
print("Available models (before update):", available_models)
if not available_models:
return gr.update(choices=[], label="Немає доступних моделей", value=None, visible=True)
models_list = []
for model in list(available_models):
models_list.append(MODEL_NAME_TO_CHOICE.get(model))
return gr.update(choices=models_list, value=None, visible=True)
with gr.Blocks() as demo:
with gr.Column():
gr.Markdown("## Оберіть модель та книгу, щоб завантажити питання")
class_name_input = gr.Dropdown(choices=CLASSES, label="Ваш клас", value=None)
student_name_input = gr.Dropdown(label="Ваше ім'я", value=None, visible=False)
book_radio = gr.Radio([], label="Оберіть книгу", interactive=True, value=None, visible=False)
model_radio = gr.Radio([], label="Оберіть модель", interactive=True, value=None, visible=False)
load_button = gr.Button("Завантажити питання")
question_output = gr.Markdown(label="Питання")
answer_radio = gr.Radio([], label="Варіанти відповіді", interactive=True, visible=False)
next_button = gr.Button("Наступне питання", visible=False)
feedback_questions_output = gr.Markdown(label="Пройдені питання", value="Nothing", visible=False)
rating_text = gr.Markdown(
"### Шкала оцінювання:\n\n#### -2 — дуже погано\n\n#### -1 — погано\n\n#### 0 — задовільно\n\n#### 1 — добре\n\n#### 2 — відмінно",
visible=False)
feedback_not_provided = gr.Markdown("# Заповніть поля відгуку та залишіть оцінку!", visible=False)
question_correct_radio = gr.Radio([-2, -1, 0, 1, 2], label="Чи коректно поставлені запитання?", visible=False,
interactive=True)
answers_correct_radio = gr.Radio([-2, -1, 0, 1, 2], label="Чи коректно поставлені варіанти відповідей?",
visible=False, interactive=True)
interesting_question_radio = gr.Radio([-2, -1, 0, 1, 2], label="Чи цікаві були запитання?", visible=False,
interactive=True)
feedback_input = gr.Textbox(label="Будь-який коментар про тест (за бажанням)", visible=False)
submit_feedback_button = gr.Button("Завершити тест", visible=False)
class_name_input.change(
update_students,
inputs=[
class_name_input,
],
outputs=[
student_name_input,
book_radio,
model_radio,
]
)
student_name_input.change(
handle_student_name_change,
inputs=[
student_name_input,
],
outputs=[
book_radio,
model_radio,
]
)
book_radio.change(
filter_models,
inputs=[
student_name_input,
class_name_input,
book_radio,
],
outputs=[
model_radio,
]
)
def update_question(model, book, student_name, class_name):
question, answers = load_questions(model, book, student_name, class_name)
is_field_filled = len(answers) > 0
return (
question, # question_output
gr.update(choices=answers, interactive=True, visible=is_field_filled), # answer_radio
gr.update(visible=is_field_filled), # next_button
gr.update(visible=False, value=""), # feedback_input
gr.update(visible=False), # submit_feedback_button
gr.update(visible=False), # rating_text
gr.update(visible=False, value=None), # question_correct
gr.update(visible=False, value=None), # answers_correct
gr.update(visible=False, value=None), # interesting_question
gr.update(visible=False, value=""), # feedback_questions_output
gr.update(visible=False), # feedback_not_provided
)
load_button.click(
update_question,
inputs=[
model_radio,
book_radio,
student_name_input,
class_name_input,
],
outputs=[
question_output,
answer_radio,
next_button,
feedback_input,
submit_feedback_button,
rating_text,
question_correct_radio,
answers_correct_radio,
interesting_question_radio,
feedback_questions_output,
feedback_not_provided,
]
)
next_button.click(
get_next_question,
inputs=[answer_radio],
outputs=[
question_output,
answer_radio,
next_button,
feedback_input,
submit_feedback_button,
rating_text,
question_correct_radio,
answers_correct_radio,
interesting_question_radio,
feedback_questions_output,
]
)
submit_feedback_button.click(
summarize_results,
inputs=[
student_name_input,
class_name_input,
model_radio,
book_radio,
feedback_input,
question_correct_radio,
answers_correct_radio,
interesting_question_radio,
],
outputs=[
question_output,
feedback_input,
submit_feedback_button,
question_correct_radio,
answers_correct_radio,
interesting_question_radio,
rating_text,
feedback_questions_output,
feedback_not_provided,
]
)
if __name__ == "__main__":
demo.launch()
|