Update ui.py
Browse files
ui.py
CHANGED
|
@@ -3,7 +3,13 @@
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
from rag import answer_question
|
| 6 |
-
from data.manual_answers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
# ========= Student side =========
|
|
@@ -13,25 +19,145 @@ def chat_fn(message: str, history: list):
|
|
| 13 |
return answer_question(message)
|
| 14 |
|
| 15 |
|
| 16 |
-
# ========= Teacher
|
| 17 |
|
| 18 |
-
def
|
| 19 |
"""
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
if not question.strip() or not answer.strip():
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
try:
|
| 26 |
norm_key = add_manual_qa(question, answer)
|
|
|
|
| 27 |
return (
|
| 28 |
"✅ ບັນທຶກສໍາເລັດ! ຈາກນີ້ chatbot ຈະໃຊ້ຄໍາຕອບນີ້ທັນທີ.\n"
|
| 29 |
-
f"🔑 ຄີທີ່ໃຊ້ຄົ້ນຫາ (normalized): `{norm_key}`"
|
|
|
|
|
|
|
|
|
|
| 30 |
)
|
| 31 |
except Exception as e:
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
def create_app():
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
gr.Markdown(
|
|
@@ -39,7 +165,7 @@ def create_app():
|
|
| 39 |
# 📚 Laos History Chatbot (M1)
|
| 40 |
|
| 41 |
- 👩🎓 **Student Chat**: ນັກຮຽນຖາມຄໍາຖາມປະຫວັດສາດພາສາລາວ
|
| 42 |
-
- 👩🏫 **Teacher Panel**: ຄູເພີ່ມ Q&A ທີ່ຖືກຕ້ອງເອງ
|
| 43 |
"""
|
| 44 |
)
|
| 45 |
|
|
@@ -61,14 +187,15 @@ def create_app():
|
|
| 61 |
with gr.Tab("👩🏫 Teacher Panel"):
|
| 62 |
gr.Markdown(
|
| 63 |
"""
|
| 64 |
-
### ✍️ ເພີ່ມ Q&A ສໍາລັບຄູ
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
"""
|
| 70 |
)
|
| 71 |
|
|
|
|
| 72 |
q_box = gr.Textbox(
|
| 73 |
label="ຄໍາຖາມ (ພາສາລາວ)",
|
| 74 |
lines=2,
|
|
@@ -81,20 +208,79 @@ def create_app():
|
|
| 81 |
placeholder="ຕົວຢ່າງ: ປະຫວັດສາດມີຄວາມສໍາຄັນເພາະ...",
|
| 82 |
)
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
status = gr.Markdown()
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
save_btn.click(
|
| 88 |
fn=save_teacher_qa,
|
| 89 |
inputs=[q_box, a_box],
|
| 90 |
-
outputs=status,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
gr.Markdown(
|
| 94 |
"""
|
| 95 |
-
> ℹ️ ໝາຍເຫດ: Q&A
|
| 96 |
-
>
|
| 97 |
"""
|
| 98 |
)
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
return demo
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
from rag import answer_question
|
| 6 |
+
from data.manual_answers import (
|
| 7 |
+
add_manual_qa,
|
| 8 |
+
list_manual_qa,
|
| 9 |
+
get_manual_qa,
|
| 10 |
+
update_manual_qa,
|
| 11 |
+
delete_manual_qa,
|
| 12 |
+
)
|
| 13 |
|
| 14 |
|
| 15 |
# ========= Student side =========
|
|
|
|
| 19 |
return answer_question(message)
|
| 20 |
|
| 21 |
|
| 22 |
+
# ========= Teacher helpers =========
|
| 23 |
|
| 24 |
+
def _refresh_manual_data():
|
| 25 |
"""
|
| 26 |
+
Helper: get table data + dropdown choices.
|
| 27 |
+
"""
|
| 28 |
+
rows = list_manual_qa()
|
| 29 |
+
table = [[r["norm_q"], r["q"], r["a"]] for r in rows]
|
| 30 |
+
keys = [r["norm_q"] for r in rows]
|
| 31 |
+
return table, keys
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def save_teacher_qa(question: str, answer: str):
|
| 35 |
+
"""
|
| 36 |
+
Add a new Q&A (or overwrite existing same question).
|
| 37 |
+
Returns: status_text, table_data, dropdown_choices, editing_key
|
| 38 |
"""
|
| 39 |
if not question.strip() or not answer.strip():
|
| 40 |
+
table, keys = _refresh_manual_data()
|
| 41 |
+
return (
|
| 42 |
+
"❗ ກະລຸນາພິມທັງຄໍາຖາມ ແລະຄໍາຕອບ.",
|
| 43 |
+
table,
|
| 44 |
+
keys,
|
| 45 |
+
"",
|
| 46 |
+
)
|
| 47 |
|
| 48 |
try:
|
| 49 |
norm_key = add_manual_qa(question, answer)
|
| 50 |
+
table, keys = _refresh_manual_data()
|
| 51 |
return (
|
| 52 |
"✅ ບັນທຶກສໍາເລັດ! ຈາກນີ້ chatbot ຈະໃຊ້ຄໍາຕອບນີ້ທັນທີ.\n"
|
| 53 |
+
f"🔑 ຄີທີ່ໃຊ້ຄົ້ນຫາ (normalized): `{norm_key}`",
|
| 54 |
+
table,
|
| 55 |
+
keys,
|
| 56 |
+
norm_key,
|
| 57 |
)
|
| 58 |
except Exception as e:
|
| 59 |
+
table, keys = _refresh_manual_data()
|
| 60 |
+
return (f"⚠️ ບັນທຶກບໍ່ສໍາເລັດ: {e}", table, keys, "")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def load_for_edit(selected_key: str):
|
| 64 |
+
"""
|
| 65 |
+
Load one row into the question/answer textboxes.
|
| 66 |
+
Returns: question, answer, editing_key
|
| 67 |
+
"""
|
| 68 |
+
if not selected_key:
|
| 69 |
+
return "", "", ""
|
| 70 |
+
|
| 71 |
+
row = get_manual_qa(selected_key)
|
| 72 |
+
if not row:
|
| 73 |
+
return "", "", ""
|
| 74 |
+
|
| 75 |
+
return row["q"], row["a"], row["norm_q"]
|
| 76 |
|
| 77 |
|
| 78 |
+
def update_teacher_qa(current_key: str, question: str, answer: str):
|
| 79 |
+
"""
|
| 80 |
+
Update existing Q&A (identified by current_key).
|
| 81 |
+
Returns: status_text, table_data, dropdown_choices, new_editing_key
|
| 82 |
+
"""
|
| 83 |
+
if not current_key:
|
| 84 |
+
table, keys = _refresh_manual_data()
|
| 85 |
+
return (
|
| 86 |
+
"❗ ກະລຸນາເລືອກ Q&A ຈາກຊ່ອງ Key ຫຼືຕາຕະລາງກ່ອນ.",
|
| 87 |
+
table,
|
| 88 |
+
keys,
|
| 89 |
+
current_key,
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
if not question.strip() or not answer.strip():
|
| 93 |
+
table, keys = _refresh_manual_data()
|
| 94 |
+
return (
|
| 95 |
+
"❗ ຄໍາຖາມ ແລະຄໍາຕອບຕ້ອງບໍ່ວ່າງ.",
|
| 96 |
+
table,
|
| 97 |
+
keys,
|
| 98 |
+
current_key,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
new_key = update_manual_qa(current_key, question, answer)
|
| 103 |
+
table, keys = _refresh_manual_data()
|
| 104 |
+
return (
|
| 105 |
+
"✅ ແກ້ໄຂ Q&A ສໍາເລັດ.",
|
| 106 |
+
table,
|
| 107 |
+
keys,
|
| 108 |
+
new_key,
|
| 109 |
+
)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
table, keys = _refresh_manual_data()
|
| 112 |
+
return (
|
| 113 |
+
f"⚠️ ແກ້ໄຂບໍ່ສໍາເລັດ: {e}",
|
| 114 |
+
table,
|
| 115 |
+
keys,
|
| 116 |
+
current_key,
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def delete_teacher_qa(current_key: str):
|
| 121 |
+
"""
|
| 122 |
+
Delete selected Q&A.
|
| 123 |
+
Returns: status_text, table_data, dropdown_choices, new_editing_key, q_text, a_text
|
| 124 |
+
"""
|
| 125 |
+
if not current_key:
|
| 126 |
+
table, keys = _refresh_manual_data()
|
| 127 |
+
return (
|
| 128 |
+
"❗ ກະລຸນາເລືອກ Q&A ທີ່ຈະລົບກ່ອນ.",
|
| 129 |
+
table,
|
| 130 |
+
keys,
|
| 131 |
+
"",
|
| 132 |
+
"",
|
| 133 |
+
"",
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
try:
|
| 137 |
+
delete_manual_qa(current_key)
|
| 138 |
+
table, keys = _refresh_manual_data()
|
| 139 |
+
return (
|
| 140 |
+
"🗑️ ລົບ Q&A ສໍາເລັດ.",
|
| 141 |
+
table,
|
| 142 |
+
keys,
|
| 143 |
+
"",
|
| 144 |
+
"",
|
| 145 |
+
"",
|
| 146 |
+
)
|
| 147 |
+
except Exception as e:
|
| 148 |
+
table, keys = _refresh_manual_data()
|
| 149 |
+
return (
|
| 150 |
+
f"⚠️ ລົບບໍ່ສໍາເລັດ: {e}",
|
| 151 |
+
table,
|
| 152 |
+
keys,
|
| 153 |
+
current_key,
|
| 154 |
+
"",
|
| 155 |
+
"",
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
# ========= Build Gradio app =========
|
| 160 |
+
|
| 161 |
def create_app():
|
| 162 |
with gr.Blocks() as demo:
|
| 163 |
gr.Markdown(
|
|
|
|
| 165 |
# 📚 Laos History Chatbot (M1)
|
| 166 |
|
| 167 |
- 👩🎓 **Student Chat**: ນັກຮຽນຖາມຄໍາຖາມປະຫວັດສາດພາສາລາວ
|
| 168 |
+
- 👩🏫 **Teacher Panel**: ຄູເພີ່ມ / ແກ້ໄຂ / ລົບ Q&A ທີ່ຖືກຕ້ອງເອງ
|
| 169 |
"""
|
| 170 |
)
|
| 171 |
|
|
|
|
| 187 |
with gr.Tab("👩🏫 Teacher Panel"):
|
| 188 |
gr.Markdown(
|
| 189 |
"""
|
| 190 |
+
### ✍️ ເພີ່ມ / ແກ້ໄຂ / ລົບ Q&A ສໍາລັບຄູ
|
| 191 |
|
| 192 |
+
1. ພິມ **ຄໍາຖາມ** ແລະ **ຄໍາຕອບ** ແລ້ວກົດ **💾 Save Q&A** ເພື່ອເພີ່ມໃໝ່
|
| 193 |
+
2. ຫາກຈະແກ້ໄຂ: ເລືອກ Key, ກົດ **📥 Load**, ແກ້ໄຂ ແລ້ວກົດ **✏️ Update**
|
| 194 |
+
3. ຖ້າຕ້ອງການລົບ: ເລືອກ Key ແລ້ວກົດ **🗑️ Delete**
|
| 195 |
"""
|
| 196 |
)
|
| 197 |
|
| 198 |
+
# Input boxes
|
| 199 |
q_box = gr.Textbox(
|
| 200 |
label="ຄໍາຖາມ (ພາສາລາວ)",
|
| 201 |
lines=2,
|
|
|
|
| 208 |
placeholder="ຕົວຢ່າງ: ປະຫວັດສາດມີຄວາມສໍາຄັນເພາະ...",
|
| 209 |
)
|
| 210 |
|
| 211 |
+
editing_key = gr.State("") # current selected key for edit/delete
|
| 212 |
+
|
| 213 |
+
with gr.Row():
|
| 214 |
+
save_btn = gr.Button("💾 Save Q&A (Add / Overwrite)")
|
| 215 |
+
update_btn = gr.Button("✏️ Update current")
|
| 216 |
+
delete_btn = gr.Button("🗑️ Delete current")
|
| 217 |
+
|
| 218 |
status = gr.Markdown()
|
| 219 |
|
| 220 |
+
gr.Markdown("### 📋 Manual Q&A List")
|
| 221 |
+
|
| 222 |
+
manual_df = gr.Dataframe(
|
| 223 |
+
headers=[
|
| 224 |
+
"Key (normalized)",
|
| 225 |
+
"Question (Lao)",
|
| 226 |
+
"Answer (Lao)",
|
| 227 |
+
],
|
| 228 |
+
interactive=False,
|
| 229 |
+
wrap=True,
|
| 230 |
+
row_count=(0, "dynamic"),
|
| 231 |
+
label="ລາຍການ Q&A ທີ່ chatbot ໃຊ້ຢູ່",
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
key_dropdown = gr.Dropdown(
|
| 235 |
+
label="ເລືອກ Q&A ເພື່ອແກ້ໄຂ / ລົບ (Key)",
|
| 236 |
+
choices=[],
|
| 237 |
+
interactive=True,
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
load_btn = gr.Button("📥 Load selected for edit")
|
| 241 |
+
|
| 242 |
+
# --- Wiring events ---
|
| 243 |
+
|
| 244 |
+
# Load selected row into text boxes
|
| 245 |
+
load_btn.click(
|
| 246 |
+
fn=load_for_edit,
|
| 247 |
+
inputs=key_dropdown,
|
| 248 |
+
outputs=[q_box, a_box, editing_key],
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
# Save new Q&A (or overwrite) and refresh table
|
| 252 |
save_btn.click(
|
| 253 |
fn=save_teacher_qa,
|
| 254 |
inputs=[q_box, a_box],
|
| 255 |
+
outputs=[status, manual_df, key_dropdown, editing_key],
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
# Update existing Q&A
|
| 259 |
+
update_btn.click(
|
| 260 |
+
fn=update_teacher_qa,
|
| 261 |
+
inputs=[editing_key, q_box, a_box],
|
| 262 |
+
outputs=[status, manual_df, key_dropdown, editing_key],
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
# Delete existing Q&A
|
| 266 |
+
delete_btn.click(
|
| 267 |
+
fn=delete_teacher_qa,
|
| 268 |
+
inputs=[editing_key],
|
| 269 |
+
outputs=[status, manual_df, key_dropdown, editing_key, q_box, a_box],
|
| 270 |
)
|
| 271 |
|
| 272 |
gr.Markdown(
|
| 273 |
"""
|
| 274 |
+
> ℹ️ ໝາຍເຫດ: Q&A ທັງໝົດຈະຖືກບັນທຶກໃນ `data/manual_qa.jsonl`.
|
| 275 |
+
> ເມື່ອເຈົ້າກົດ Save / Update / Delete chatbot ຈະອັບເດດທັນທີ (ບໍ່ຕ້ອງ restart Space).
|
| 276 |
"""
|
| 277 |
)
|
| 278 |
|
| 279 |
+
# On app load, fill table + dropdown
|
| 280 |
+
demo.load(
|
| 281 |
+
fn=_refresh_manual_data,
|
| 282 |
+
inputs=None,
|
| 283 |
+
outputs=[manual_df, key_dropdown],
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
return demo
|