File size: 3,976 Bytes
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
import json
import os
import gradio as gr

MODELS = {
    "GPT-4o": "gpt-4o",
    "Gemini 1.5 Pro": "gemini-1.5-pro",
    "Claude 3.5 Sonnet": "claude-3-5-sonnet-20241022"
}

MODELS_PATH = {
    "GPT-4o": "gpt_4o",
    "Gemini 1.5 Pro": "gemini_1_5_pro",
    "Claude 3.5 Sonnet": "claude_3_5_sonnet"
}

BOOKS = {
    "Іван Нечуйлевицький - Кайдашева Сім'я": "nechuy-levytskyy-ivan-semenovych-kaydasheva-simia.json",
}

questions_data = []
current_question_index = 0
answers_log = []  # Log for saving answers


def load_questions(model, book):
    global questions_data, current_question_index
    current_question_index = 0
    answers_log.clear()

    if not model or not book:
        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:
        first_question = questions_data[current_question_index]
        question_text = f"**Питання:** {first_question['question']}"
        answers = [answer['answer'] for answer in first_question['answers']]
        return question_text, answers
    else:
        return "У файлі немає питань.", []


def get_next_question(selected_answer):
    global current_question_index, answers_log

    # 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):
        next_question = questions_data[current_question_index]
        question_text = f"**Питання:** {next_question['question']}"
        answers = [answer['answer'] for answer in next_question['answers']]
        return question_text, gr.update(choices=answers, interactive=True), gr.update(visible=True)
    else:
        # All questions are completed — summarizing
        correct_answers_count = sum(1 for answer in answers_log if answer['isCorrect'])
        total_questions = len(questions_data)
        summary = f"# Усі питання пройдено.\n\n**Ви відповіли правильно на {correct_answers_count} з {total_questions} питань.**"
        return summary, gr.update(visible=False), gr.update(visible=False)


with gr.Blocks() as demo:
    with gr.Column():
        gr.Markdown("# Оберіть модель та книгу, щоб завантажити питання")

        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)
        next_button = gr.Button("Наступне питання", visible=False)

        def update_question(model, book):
            question, answers = load_questions(model, book)
            return question, gr.update(choices=answers, interactive=True), gr.update(visible=True)

        load_button.click(
            update_question,
            inputs=[model_radio, book_radio],
            outputs=[question_output, answer_radio, next_button]
        )

        next_button.click(
            get_next_question,
            inputs=[answer_radio],
            outputs=[question_output, answer_radio, next_button]
        )

if __name__ == "__main__":
    demo.launch()