File size: 7,492 Bytes
074b318 c9a4f7a f86ba47 074b318 fefd70e 1abc030 f86ba47 fefd70e 074b318 fefd70e 074b318 c9a4f7a c37b8d9 074b318 c37b8d9 074b318 c9a4f7a 074b318 49dd514 c9a4f7a 49dd514 074b318 f86ba47 074b318 c9a4f7a 49dd514 074b318 f972eea e4f1674 c37b8d9 49dd514 c37b8d9 49dd514 c37b8d9 f86ba47 7958dfb f86ba47 f972eea 1abc030 f972eea fbaf544 e4f1674 fbaf544 e4f1674 fbaf544 c37b8d9 074b318 f86ba47 074b318 c37b8d9 074b318 f972eea f86ba47 7958dfb c37b8d9 49dd514 c37b8d9 f86ba47 074b318 7958dfb f972eea 074b318 f972eea 074b318 f972eea f86ba47 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 |
import json
import os
import random
import gradio as gr
from dotenv import load_dotenv
from constants import MODELS, MODELS_PATH, BOOKS
from utils import save_results
load_dotenv()
if os.getenv("ENV_TYPE") == "dev":
MODELS["Test"] = "test"
MODELS_PATH["Test"] = "test"
BOOKS["Test"] = "test.json"
questions_data = []
current_question_index = 0
answers_log = [] # Log for saving answers
def get_question():
global questions_data, current_question_index
question = questions_data[current_question_index]
question_text = f"# Питання:\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 not student_name or not class_name:
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
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), # rating_slider
gr.update(visible=False), # submit_feedback_button
)
# 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), # rating_slider
gr.update(visible=False), # submit_feedback_button
)
else:
# All questions are completed — ask for feedback
question_text = "# Дякуємо за участь у тесті! Залиште, будь ласка, свій відгук і оцініть тест."
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), # rating_slider
gr.update(visible=True), # submit_feedback_button
)
def summarize_results(student_name, class_name, model, book, feedback, rating):
global questions_data, answers_log
questions = []
if not feedback:
return (
"# Залиште відгук про тест!", # question_output
gr.update(visible=True), # feedback_input
gr.update(visible=True), # rating_slider
gr.update(visible=True) # submit_feedback_button
)
for question, answer in zip(questions_data, answers_log):
questions.append({
"question": question,
"answer": answer
})
save_results(student_name, class_name, MODELS[model], book, questions, feedback, rating)
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
summary = (
f"# Усі питання пройдено!\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, value=0), # rating_slider
gr.update(visible=False) # submit_feedback_button
)
with gr.Blocks() as demo:
with gr.Column():
gr.Markdown("# Оберіть модель та книгу, щоб завантажити питання")
student_name_input = gr.Textbox(label="Ваше ім'я")
class_name_input = gr.Textbox(label="Ваш клас")
model_radio = gr.Radio(list(MODELS.keys()), label="Оберіть модель")
book_radio = gr.Radio(list(BOOKS.keys()), label="Оберіть книгу")
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_input = gr.Textbox(label="Ваш відгук про тест", visible=False)
rating_slider = gr.Slider(1, 5, step=1, label="Оцініть тест", visible=False)
submit_feedback_button = gr.Button("Завершити тест", visible=False)
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), # feedback_input
gr.update(visible=False), # rating_slider
gr.update(visible=False), # submit_feedback_button
)
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, rating_slider, submit_feedback_button]
)
next_button.click(
get_next_question,
inputs=[answer_radio],
outputs=[question_output, answer_radio, next_button, feedback_input, rating_slider, submit_feedback_button]
)
submit_feedback_button.click(
summarize_results,
inputs=[student_name_input, class_name_input, model_radio, book_radio, feedback_input, rating_slider],
outputs=[question_output, feedback_input, rating_slider, submit_feedback_button]
)
if __name__ == "__main__":
demo.launch()
|