Spaces:
Running
Running
Update pages/chatbot.py
Browse files- pages/chatbot.py +49 -49
pages/chatbot.py
CHANGED
|
@@ -1,49 +1,49 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from app import rag_query, process_feedback
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
st.title("RAG Chatbot")
|
| 6 |
-
|
| 7 |
-
# Initialize chat history
|
| 8 |
-
if "messages" not in st.session_state:
|
| 9 |
-
st.session_state.messages = []
|
| 10 |
-
|
| 11 |
-
# Display chat messages from history on app rerun
|
| 12 |
-
for i, message in enumerate(st.session_state.messages):
|
| 13 |
-
with st.chat_message(message["role"]):
|
| 14 |
-
st.markdown(message["content"])
|
| 15 |
-
if message["role"] == "assistant":
|
| 16 |
-
col1, col2 = st.columns([1,15])
|
| 17 |
-
with col1:
|
| 18 |
-
if st.button("👍", key=f"thumbs_up_{i}"):
|
| 19 |
-
process_feedback(st.session_state.messages[i-1]["content"], message["content"], True)
|
| 20 |
-
with col2:
|
| 21 |
-
if st.button("👎", key=f"thumbs_down_{i}"):
|
| 22 |
-
process_feedback(st.session_state.messages[i-1]["content"], message["content"], False)
|
| 23 |
-
# st.session_state.messages.pop() # Remove the last assistant message
|
| 24 |
-
#st.rerun() # Rerun the app to regenerate the response
|
| 25 |
-
|
| 26 |
-
# React to user input
|
| 27 |
-
if prompt := st.chat_input("
|
| 28 |
-
# Display user message in chat message container
|
| 29 |
-
st.chat_message("user").markdown(prompt)
|
| 30 |
-
# Add user message to chat history
|
| 31 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 32 |
-
|
| 33 |
-
response = rag_query(prompt)
|
| 34 |
-
|
| 35 |
-
# Display assistant response in chat message container
|
| 36 |
-
with st.chat_message("assistant"):
|
| 37 |
-
st.markdown(response)
|
| 38 |
-
# Add assistant response to chat history
|
| 39 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 40 |
-
|
| 41 |
-
# Rerun the app to display the feedback buttons
|
| 42 |
-
st.experimental_rerun()
|
| 43 |
-
|
| 44 |
-
# Sidebar for additional controls
|
| 45 |
-
with st.sidebar:
|
| 46 |
-
st.header("
|
| 47 |
-
if st.button("
|
| 48 |
-
st.session_state.messages = []
|
| 49 |
-
st.
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from app import rag_query, process_feedback
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
st.title("RAG Chatbot")
|
| 6 |
+
|
| 7 |
+
# Initialize chat history
|
| 8 |
+
if "messages" not in st.session_state:
|
| 9 |
+
st.session_state.messages = []
|
| 10 |
+
|
| 11 |
+
# Display chat messages from history on app rerun
|
| 12 |
+
for i, message in enumerate(st.session_state.messages):
|
| 13 |
+
with st.chat_message(message["role"]):
|
| 14 |
+
st.markdown(message["content"])
|
| 15 |
+
if message["role"] == "assistant":
|
| 16 |
+
col1, col2 = st.columns([1,15])
|
| 17 |
+
with col1:
|
| 18 |
+
if st.button("👍", key=f"thumbs_up_{i}"):
|
| 19 |
+
process_feedback(st.session_state.messages[i-1]["content"], message["content"], True)
|
| 20 |
+
with col2:
|
| 21 |
+
if st.button("👎", key=f"thumbs_down_{i}"):
|
| 22 |
+
process_feedback(st.session_state.messages[i-1]["content"], message["content"], False)
|
| 23 |
+
# st.session_state.messages.pop() # Remove the last assistant message
|
| 24 |
+
#st.rerun() # Rerun the app to regenerate the response
|
| 25 |
+
|
| 26 |
+
# React to user input
|
| 27 |
+
if prompt := st.chat_input("Bạn cần tư vấn về điều gì? Hãy chia sẻ nhu cầu và thông tin của bạn nhé!"):
|
| 28 |
+
# Display user message in chat message container
|
| 29 |
+
st.chat_message("user").markdown(prompt)
|
| 30 |
+
# Add user message to chat history
|
| 31 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 32 |
+
|
| 33 |
+
response = rag_query(prompt)
|
| 34 |
+
|
| 35 |
+
# Display assistant response in chat message container
|
| 36 |
+
with st.chat_message("assistant"):
|
| 37 |
+
st.markdown(response)
|
| 38 |
+
# Add assistant response to chat history
|
| 39 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 40 |
+
|
| 41 |
+
# Rerun the app to display the feedback buttons
|
| 42 |
+
st.experimental_rerun()
|
| 43 |
+
|
| 44 |
+
# Sidebar for additional controls
|
| 45 |
+
with st.sidebar:
|
| 46 |
+
st.header("Lựa chọn khác")
|
| 47 |
+
if st.button("Xóa lịch sử chat"):
|
| 48 |
+
st.session_state.messages = []
|
| 49 |
+
st.rerun()
|