File size: 3,340 Bytes
ddc6400
 
a254b10
ddc6400
 
 
 
 
 
 
 
bb47102
ddc6400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224e432
 
 
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
# app.py
import gradio as gr

from model_utils import laos_history_bot
from loader import manual_qa_table_data
from teacher_panel import (
    teacher_save_add_overwrite,
    teacher_update_current,
    teacher_delete_current,
    teacher_on_table_select,
)

# Build Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("## 🐘 Laos History Chatbot – ສໍາລັບນັກຮຽນ ມ.1")

    # ---------------- Student tab ----------------
    with gr.Tab("👩‍🎓 Student Chat"):
        gr.Markdown("ຖາມຄໍາຖາມປະຫວັດສາດພາສາລາວໄດ້ເລີຍ.")
        gr.ChatInterface(
            fn=laos_history_bot,
            title="",
            description="",
            examples=[
                "ປະຫວັດສາດແມ່ນຫຍັງ?",
                "ປະຫວັດສາດມີຄວາມສໍາຄັນຈັ່ງໃດ໋?",
                "ຫຼັກຖານປະຫວັດສາດມີຫຍັງແດ່?",
                "ຈະປ່ຽນຈາກປີຄ.ສ ເປັນປີພ.ສ ໄດ້ແນວໃດ?",
            ],
        )

    # ---------------- Teacher tab ----------------
    with gr.Tab("👩‍🏫 Teacher Panel"):
        gr.Markdown(
            "ໜ້ານີ້ໃຫ້ຄູເພີ່ມ / ແກ້ໄຂ Q&A ເອງ. "
            "ຄໍາຕອບໃໝ່ຈະຖືກນໍາໃຊ້ທັນທີໃນ Student Chat."
        )

        q_box = gr.Textbox(
            label="ຄໍາຖາມ (ພາສາລາວ)",
            lines=3,
            placeholder="ພິມຄໍາຖາມປະຫວັດສາດ...",
        )
        a_box = gr.Textbox(
            label="ຄໍາຕອບ (ພາສາລາວ)",
            lines=4,
            placeholder="ພິມຄໍາຕອບທີ່ຖືກຕ້ອງ ແລະເຂົ້າໃຈງ່າຍ...",
        )

        current_id_state = gr.State("")

        with gr.Row():
            btn_save = gr.Button("💾 Save Q&A (Add / Overwrite)")
            btn_update = gr.Button("✏️ Update current")
            btn_delete = gr.Button("🗑️ Delete current")

        status_md = gr.Markdown("")

        qa_table = gr.Dataframe(
            headers=["ID", "ຄໍາຖາມ", "ຄໍາຕອບ"],
            value=manual_qa_table_data(),
            interactive=True,
            wrap=True,
            row_count=(0, "dynamic"),
        )

        # wire buttons
        btn_save.click(
            fn=teacher_save_add_overwrite,
            inputs=[q_box, a_box, current_id_state],
            outputs=[q_box, a_box, current_id_state, qa_table, status_md],
        )

        btn_update.click(
            fn=teacher_update_current,
            inputs=[q_box, a_box, current_id_state],
            outputs=[q_box, a_box, current_id_state, qa_table, status_md],
        )

        btn_delete.click(
            fn=teacher_delete_current,
            inputs=[current_id_state],
            outputs=[q_box, a_box, current_id_state, qa_table, status_md],
        )

        qa_table.select(
            fn=teacher_on_table_select,
            inputs=[qa_table],
            outputs=[q_box, a_box, current_id_state, status_md],
        )

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