File size: 3,514 Bytes
ddc6400
 
a254b10
1536b04
ddc6400
 
 
 
 
 
 
bb47102
ddc6400
 
ba87b00
ddc6400
 
 
41e8c14
ddc6400
d082e69
ddc6400
 
 
41e8c14
 
ddc6400
 
 
 
 
 
 
 
 
 
 
 
 
41e8c14
ddc6400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3c2899
ddc6400
 
ab33eee
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
# app.py
import gradio as gr

from model_utils import laos_science_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 Natural Science Chatbot – ສໍາລັບນັກຮຽນ ມ.1 - ມ.4")

    # ---------------- Student tab ----------------
    with gr.Tab("👩‍🎓 Student Chat"):
        gr.Markdown("ຖາມຄໍາຖາມວິທະຍາສາດທຳມະຊາດພາສາລາວໄດ້ເລີຍ.")
        gr.ChatInterface(
            fn=laos_science_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=None,  # we only use the event data
            outputs=[q_box, a_box, current_id_state, status_md],
        )
        # 👇👇👇 ADD THIS NEW BLOCK HERE 👇👇👇
        # This ensures that whenever the page is refreshed, the table updates 
        # to show the REAL current data from memory.
        demo.load(
            fn=manual_qa_table_data, 
            inputs=None, 
            outputs=qa_table
        )
        # 👆👆👆 END OF NEW BLOCK 👆👆👆

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