Heng2004's picture
Update app.py
ab33eee verified
# 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()