Spaces:
Sleeping
Sleeping
Upload utils_chatbox.py
Browse files- utils/utils_chatbox.py +93 -0
utils/utils_chatbox.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
def display_chat_history(model_name: str):
|
6 |
+
for message in st.session_state[model_name]:
|
7 |
+
with st.chat_message(message["role"]):
|
8 |
+
st.markdown(message["content"])
|
9 |
+
|
10 |
+
def chat_input(model_name: str):
|
11 |
+
if prompt := st.chat_input("Say something"):
|
12 |
+
# Display user message in chat message container
|
13 |
+
st.chat_message("user").markdown(prompt)
|
14 |
+
|
15 |
+
# Add user message to chat history
|
16 |
+
st.session_state[model_name].append({"role": "user", "content": prompt})
|
17 |
+
|
18 |
+
return prompt
|
19 |
+
|
20 |
+
def display_bot_msg(model_name: str, bot_response: str):
|
21 |
+
# Display assistant response in chat message container
|
22 |
+
with st.chat_message("assistant"):
|
23 |
+
message_placeholder = st.empty()
|
24 |
+
full_response = ""
|
25 |
+
|
26 |
+
# simulate the chatbot "thinking" before responding
|
27 |
+
# (or stream its response)
|
28 |
+
for chunk in bot_response.split():
|
29 |
+
full_response += chunk + " "
|
30 |
+
time.sleep(0.05)
|
31 |
+
|
32 |
+
# add a blinking cursor to simulate typing
|
33 |
+
message_placeholder.markdown(full_response + "▌")
|
34 |
+
|
35 |
+
message_placeholder.markdown(full_response)
|
36 |
+
# st.markdown(response)
|
37 |
+
|
38 |
+
# Add assistant response to chat history
|
39 |
+
st.session_state[model_name].append(
|
40 |
+
{"model_name": model_name, "role": "assistant", "content": full_response}
|
41 |
+
)
|
42 |
+
|
43 |
+
def chatbox(model_name: str, model: None):
|
44 |
+
# Display chat messages from history on app rerun
|
45 |
+
for message in st.session_state.messages:
|
46 |
+
if (message["model_name"] == model_name):
|
47 |
+
with st.chat_message(message["role"]):
|
48 |
+
st.markdown(message["content"])
|
49 |
+
|
50 |
+
if prompt := st.chat_input("Say something"):
|
51 |
+
# Display user message in chat message container
|
52 |
+
st.chat_message("user").markdown(prompt)
|
53 |
+
|
54 |
+
# Add user message to chat history
|
55 |
+
st.session_state.messages.append({"model_name": model_name, "role": "user", "content": prompt})
|
56 |
+
|
57 |
+
with st.spinner("Processing your query..."):
|
58 |
+
bot_response = model.get_response(prompt)
|
59 |
+
|
60 |
+
print("bot: ", bot_response)
|
61 |
+
|
62 |
+
# Display assistant response in chat message container
|
63 |
+
with st.chat_message("assistant"):
|
64 |
+
message_placeholder = st.empty()
|
65 |
+
full_response = ""
|
66 |
+
|
67 |
+
# simulate the chatbot "thinking" before responding
|
68 |
+
# (or stream its response)
|
69 |
+
for chunk in bot_response.split():
|
70 |
+
full_response += chunk + " "
|
71 |
+
time.sleep(0.05)
|
72 |
+
|
73 |
+
# add a blinking cursor to simulate typing
|
74 |
+
message_placeholder.markdown(full_response + "▌")
|
75 |
+
|
76 |
+
message_placeholder.markdown(full_response)
|
77 |
+
# st.markdown(response)
|
78 |
+
|
79 |
+
# Add assistant response to chat history
|
80 |
+
st.session_state.messages.append(
|
81 |
+
{"model_name": model_name, "role": "assistant", "content": full_response}
|
82 |
+
)
|
83 |
+
|
84 |
+
# Scroll to the bottom of the chat container
|
85 |
+
# st.markdown(
|
86 |
+
# """
|
87 |
+
# <script>
|
88 |
+
# const chatContainer = document.getElementsByClassName("css-1n76uvr")[0];
|
89 |
+
# chatContainer.scrollTop = chatContainer.scrollHeight;
|
90 |
+
# </script>
|
91 |
+
# """,
|
92 |
+
# unsafe_allow_html=True,
|
93 |
+
# )
|