Update app.py
Browse files
app.py
CHANGED
@@ -63,6 +63,40 @@ def add_to_history(role: str, message: str):
|
|
63 |
"""Add message to chat history"""
|
64 |
st.session_state.chat_history.append((role, message))
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
def main():
|
67 |
# Header
|
68 |
st.title("🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา")
|
@@ -90,50 +124,12 @@ def main():
|
|
90 |
# Add query button and clear chat button in the same line
|
91 |
col1, col2 = st.columns([1, 4])
|
92 |
with col1:
|
93 |
-
|
|
|
94 |
with col2:
|
95 |
if st.button("ล้างประวัติการสนทนา", type="secondary", use_container_width=True):
|
96 |
st.session_state.chat_history = []
|
97 |
st.rerun()
|
98 |
-
|
99 |
-
# Process query when button is clicked
|
100 |
-
if send_query and query:
|
101 |
-
if st.session_state.pipeline is None:
|
102 |
-
st.error("ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
103 |
-
return
|
104 |
-
|
105 |
-
# Add user query to history
|
106 |
-
add_to_history("user", query)
|
107 |
-
|
108 |
-
try:
|
109 |
-
with st.spinner("กำลังค้นหาคำตอบ..."):
|
110 |
-
# Process query
|
111 |
-
result = st.session_state.pipeline.process_query(query)
|
112 |
-
|
113 |
-
# Add assistant response to history
|
114 |
-
add_to_history("assistant", result["answer"])
|
115 |
-
|
116 |
-
# Create containers for additional information
|
117 |
-
with st.expander("📚 แสดงข้อมูลอ้างอิง"):
|
118 |
-
for i, doc in enumerate(result["documents"], 1):
|
119 |
-
st.markdown(f"**เอกสารที่ {i}:**")
|
120 |
-
st.text(doc.content)
|
121 |
-
st.markdown("---")
|
122 |
-
|
123 |
-
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม"):
|
124 |
-
st.json(result["query_info"])
|
125 |
-
|
126 |
-
# Clear the input field
|
127 |
-
st.session_state.query_input = ""
|
128 |
-
|
129 |
-
# Rerun to update chat display
|
130 |
-
st.rerun()
|
131 |
-
|
132 |
-
except Exception as e:
|
133 |
-
st.error(f"เกิดข้อผิดพลาด: {str(e)}")
|
134 |
-
|
135 |
-
elif send_query and not query:
|
136 |
-
st.warning("กรุณาระบุคำถาม")
|
137 |
|
138 |
with info_col:
|
139 |
# System information
|
|
|
63 |
"""Add message to chat history"""
|
64 |
st.session_state.chat_history.append((role, message))
|
65 |
|
66 |
+
def process_query(query: str):
|
67 |
+
"""Process a query and update the chat history"""
|
68 |
+
if not query:
|
69 |
+
st.warning("กรุณาระบุคำถาม")
|
70 |
+
return
|
71 |
+
|
72 |
+
if st.session_state.pipeline is None:
|
73 |
+
st.error("ไม่สามารถเชื่อมต่อกับระบบได้ กรุณาลองใหม่อีกครั้ง")
|
74 |
+
return
|
75 |
+
|
76 |
+
# Add user query to history
|
77 |
+
add_to_history("user", query)
|
78 |
+
|
79 |
+
try:
|
80 |
+
with st.spinner("กำลังค้นหาคำตอบ..."):
|
81 |
+
# Process query
|
82 |
+
result = st.session_state.pipeline.process_query(query)
|
83 |
+
|
84 |
+
# Add assistant response to history
|
85 |
+
add_to_history("assistant", result["answer"])
|
86 |
+
|
87 |
+
# Create containers for additional information
|
88 |
+
with st.expander("📚 แสดงข้อมูลอ้างอิง"):
|
89 |
+
for i, doc in enumerate(result["documents"], 1):
|
90 |
+
st.markdown(f"**เอกสารที่ {i}:**")
|
91 |
+
st.text(doc.content)
|
92 |
+
st.markdown("---")
|
93 |
+
|
94 |
+
with st.expander("🔍 รายละเอียดการวิเคราะห์คำถาม"):
|
95 |
+
st.json(result["query_info"])
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
st.error(f"เกิดข้อผิดพลาด: {str(e)}")
|
99 |
+
|
100 |
def main():
|
101 |
# Header
|
102 |
st.title("🎓 ระบบค้นหาข้อมูลปฏิทินการศึกษา")
|
|
|
124 |
# Add query button and clear chat button in the same line
|
125 |
col1, col2 = st.columns([1, 4])
|
126 |
with col1:
|
127 |
+
if st.button("ส่งคำถาม", type="primary", use_container_width=True):
|
128 |
+
process_query(query)
|
129 |
with col2:
|
130 |
if st.button("ล้างประวัติการสนทนา", type="secondary", use_container_width=True):
|
131 |
st.session_state.chat_history = []
|
132 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
134 |
with info_col:
|
135 |
# System information
|