-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatbot_app.py
39 lines (21 loc) · 1.76 KB
/
chatbot_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import streamlit as st #all streamlit commands will be available through the "st" alias
import chatbot_lib as glib #reference to local lib script
st.set_page_config(page_title="Chatbot") #HTML title
st.title("Chatbot") #page title
if 'memory' not in st.session_state: #see if the memory hasn't been created yet
st.session_state.memory = glib.get_memory() #initialize the memory
if 'chat_history' not in st.session_state: #see if the chat history hasn't been created yet
st.session_state.chat_history = [] #initialize the chat history
#Re-render the chat history (Streamlit re-runs this script, so need this to preserve previous chat messages)
for message in st.session_state.chat_history: #loop through the chat history
with st.chat_message(message["role"]): #renders a chat line for the given role, containing everything in the with block
st.markdown(message["text"]) #display the chat content
input_text = st.chat_input("Chat with your bot here") #display a chat input box
if input_text: #run the code in this if block after the user submits a chat message
with st.chat_message("user"): #display a user chat message
st.markdown(input_text) #renders the user's latest message
st.session_state.chat_history.append({"role":"user", "text":input_text}) #append the user's latest message to the chat history
chat_response = glib.get_chat_response(input_text=input_text, memory=st.session_state.memory) #call the model through the supporting library
with st.chat_message("assistant"): #display a bot chat message
st.markdown(chat_response) #display bot's latest response
st.session_state.chat_history.append({"role":"assistant", "text":chat_response}) #append the bot's latest message to the chat history