File size: 4,500 Bytes
0e72013
6265935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376e17c
6265935
 
 
 
88585ce
6265935
 
 
 
 
 
88585ce
6265935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376e17c
6265935
 
88585ce
6265935
 
376e17c
 
6265935
 
 
 
 
 
 
 
376e17c
6265935
376e17c
6265935
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376e17c
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
import gradio as gr
import pandas as pd

# Ініціалізація таблиці замін на основі зображення
initial_data = {
    "Дата": ["02.05.2025", "02.05.2025", "02.05.2025", "02.05.2025"],
    "Клас": ["6-А", "6-Б", "7-А", "7-Б"],
    "Урок 1": ["За.рл", "", "", ""],
    "Урок 2": ["", "Матем", "Теометр", "Фіз.л"],
    "Урок 3": ["", "", "Укр.м", "Укр.м"],
    "Урок 4": ["", "Муз.м", "", "Теометр"],
    "Урок 5": ["", "", "", "Нім.м"],
    "Урок 6": ["", "", "", ""],
    "Урок 7": ["", "", "", ""],
    "Урок 8": ["", "", "", ""]
}
substitutions_df = pd.DataFrame(initial_data)

# Пароль адміністратора
ADMIN_PASSWORD = "admin123"

# Функція для перевірки пароля
def check_admin(password):
    return password == ADMIN_PASSWORD

# Функція для оновлення таблиці
def update_table(password, new_data):
    global substitutions_df
    if not check_admin(password):
        return "Неправильний пароль!", substitutions_df
    try:
        new_df = pd.DataFrame(new_data)
        required_columns = ["Дата", "Клас"] + [f"Урок {i}" for i in range(1, 9)]
        if not all(col in new_df.columns for col in required_columns):
            return "Таблиця повинна містити колонки: Дата, Клас, Урок 1–Урок 8!", substitutions_df
        substitutions_df = new_df
        return "Таблицю успішно оновлено!", substitutions_df
    except Exception as e:
        return f"Помилка при оновленні таблиці: {str(e)}", substitutions_df

# Функція для обробки запитів у чаті
def chat_handler(message, history):
    global substitutions_df
    if substitutions_df.empty:
        return "Таблиця замін порожня. Зверніться до адміністратора для оновлення даних."

    message = message.lower()
    if "які заміни" in message:
        for class_name in substitutions_df["Клас"].unique():
            if class_name.lower() in message:
                class_subs = substitutions_df[substitutions_df["Клас"].str.lower() == class_name.lower()]
                if class_subs.empty:
                    return f"Для класу {class_name} немає замін."
                response = f"Заміни для {class_name} на {class_subs['Дата'].iloc[0]}:\n"
                for col in [f"Урок {i}" for i in range(1, 9)]:
                    value = class_subs[col].iloc[0]
                    if value and value != "":
                        response += f"{col}: {value}\n"
                return response
        return "Будь ласка, вкажіть клас (наприклад, 'Які заміни в 6-А?')."
    return "Я розумію запити типу 'Які заміни в 6-А?'. Спробуйте ще раз!"

# Налаштування інтерфейсу Gradio
with gr.Blocks() as demo:
    gr.Markdown("## Чат-бот для перегляду замін уроків")

    with gr.Row():
        password_input = gr.Textbox(label="Пароль (для адміністратора)", type="password", visible=True)
        table_input = gr.DataFrame(
            label="Введіть дані замін",
            headers=["Дата", "Клас", "Урок 1", "Урок 2", "Урок 3", "Урок 4", "Урок 5", "Урок 6", "Урок 7", "Урок 8"],
            value=initial_data
        )
        update_button = gr.Button("Оновити таблицю")
        admin_output = gr.Textbox(label="Статус")

    with gr.Row():
        table_output = gr.DataFrame(label="Таблиця замін", value=substitutions_df, interactive=False)
        chatbot = gr.Chatbot(label="Чат-бот")
        user_input = gr.Textbox(label="Ваше повідомлення", placeholder="Які заміни в 6-А?")
        chat_button = gr.Button("Надіслати")

    update_button.click(
        fn=update_table,
        inputs=[password_input, table_input],
        outputs=[admin_output, table_output]
    )
    chat_button.click(
        fn=chat_handler,
        inputs=[user_input, chatbot],
        outputs=chatbot
    )

# Запуск додатка
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)