Heng2004 commited on
Commit
9b1af99
·
verified ·
1 Parent(s): 2b8a999

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +89 -12
ui.py CHANGED
@@ -1,23 +1,100 @@
1
- # ui.py – Gradio UI
2
 
3
  import gradio as gr
 
4
  from rag import answer_question
 
 
5
 
 
6
 
7
  def chat_fn(message: str, history: list):
8
  # history is ignored for now
9
  return answer_question(message)
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def create_app():
13
- return gr.ChatInterface(
14
- fn=chat_fn,
15
- title="Laos History Chatbot (Lao language)",
16
- description="ຖາມຂໍ້ມູນກ່ຽວກັບປະຫວັດສາດຂອງປະເທດລາວ (ສໍາລັບນັກຮຽນ ມ.1)",
17
- examples=[
18
- "ປະຫວັດສາດແມ່ນຫຍັງ?",
19
- "ປະຫວັດສາດມີຄວາມສໍາຄັນຈັ່ງໃດ໋?",
20
- "ຫຼັກຖານປະຫວັດສາດມີຫຍັງແດ່?",
21
- "ປະຕິທິນຈັນທະລະຄະຕິແມ່ນຫຍັງ?",
22
- ],
23
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ui.py – Gradio UI (Student + Teacher tabs)
2
 
3
  import gradio as gr
4
+
5
  from rag import answer_question
6
+ from data.manual_answers import add_manual_qa
7
+
8
 
9
+ # ========= Student side =========
10
 
11
  def chat_fn(message: str, history: list):
12
  # history is ignored for now
13
  return answer_question(message)
14
 
15
 
16
+ # ========= Teacher side =========
17
+
18
+ def save_teacher_qa(question: str, answer: str) -> str:
19
+ """
20
+ Called when teacher clicks 'Save Q&A'.
21
+ """
22
+ if not question.strip() or not answer.strip():
23
+ return "❗ ກະລຸນາພິມທັງຄໍາຖາມ ແລະຄໍາຕອບ."
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
+ return f"⚠️ ບັນທຶກບໍ່ສໍາເລັດ: {e}"
33
+
34
+
35
  def create_app():
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown(
38
+ """
39
+ # 📚 Laos History Chatbot (M1)
40
+
41
+ - 👩‍🎓 **Student Chat**: ນັກຮຽນຖາມຄໍາຖາມປະຫວັດສາດພາສາລາວ
42
+ - 👩‍🏫 **Teacher Panel**: ຄູເພີ່ມ Q&A ທີ່ຖືກຕ້ອງເອງ ເພື່ອໃຫ້ chatbot ຕອບໄດ້ດີຂຶ້ນ
43
+ """
44
+ )
45
+
46
+ # ---------- Tab 1: Student Chat ----------
47
+ with gr.Tab("👩‍🎓 Student Chat"):
48
+ gr.ChatInterface(
49
+ fn=chat_fn,
50
+ title="Laos History Chatbot (Lao language)",
51
+ description="ຖາມຂໍ້ມູນກ່ຽວກັບປະຫວັດສາດຂອງປະເທດລາວ (ສໍາລັບນັກຮຽນ ມ.1)",
52
+ examples=[
53
+ "ປະຫວັດສາດແມ່ນຫຍັງ?",
54
+ "ປະຫວັດສາດມີຄວາມສໍາຄັນຈັ່ງໃດ໋?",
55
+ "ຫຼັກຖານປະຫວັດສາດມີຫຍັງແດ່?",
56
+ "ປະຕິທິນຈັນທະລະຄະຕິແມ່ນຫຍັງ?",
57
+ ],
58
+ )
59
+
60
+ # ---------- Tab 2: Teacher Panel ----------
61
+ with gr.Tab("👩‍🏫 Teacher Panel"):
62
+ gr.Markdown(
63
+ """
64
+ ### ✍️ ເພີ່ມ Q&A ສໍາລັບຄູ
65
+
66
+ - ພິມ **ຄໍາຖາມ** ແລະ **ຄໍາຕອບ** ພາສາລາວ
67
+ - ກົດ **"💾 Save Q&A"**
68
+ - Chatbot ຈະໃຊ້ຄໍາຕອບນີ້ທັນທີເມື່ອນັກຮຽນຖາມຄໍາຖາມຄ້າຍກັນ
69
+ """
70
+ )
71
+
72
+ q_box = gr.Textbox(
73
+ label="ຄໍາຖາມ (ພາສາລາວ)",
74
+ lines=2,
75
+ placeholder="ຕົວຢ່າງ: ປະຫວັດສາດມີຄວາມສໍາຄັນຈັ່ງໃດ໋?",
76
+ )
77
+
78
+ a_box = gr.Textbox(
79
+ label="ຄໍາຕອບ (ພາສາລາວ)",
80
+ lines=4,
81
+ placeholder="ຕົວຢ່າງ: ປະຫວັດສາດມີຄວາມສໍາຄັນເພາະ...",
82
+ )
83
+
84
+ save_btn = gr.Button("💾 Save Q&A")
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 ທີ່ເພີ່ມໃໝ່ຈະຖືກບັນທຶກໃນ `data/manual_qa.jsonl`.
96
+ > ມັນຈະໃຊ້ງານໄດ້ທັນທີໃນ chatbot ໂດຍບໍ່ຕ້ອງ restart Space.
97
+ """
98
+ )
99
+
100
+ return demo