Brave_Leo / app.py
lamtung16's picture
Update app.py
b2d7c54
import streamlit as st
from responses import generate_response
# form for each user
if "disabled" not in st.session_state:
st.session_state.disabled = False
def disable():
st.session_state.disabled = True
username = st.text_input(
"participant id",
disabled=st.session_state.disabled,
on_change=disable
).lower()
# user_list
user_list = ['1', '2', '3']
user_name_list = ['Paricipant 1', 'Paricipant 2', 'Paricipant 3']
# App
if(username.lower() in user_list):
conv_id = user_list.index(username)
history = []
# Store LLM generated responses
if "messages" not in st.session_state.keys():
st.session_state.messages = [{"role": "assistant", "content": "Welcome back, " + user_name_list[conv_id]}]
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# User-provided prompt
if prompt := st.chat_input("What's on your mind"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate a new response if last message is not from assistant
if st.session_state.messages[-1]["role"] != "assistant":
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
# generate prompt
for i in range(len(st.session_state.messages)):
if(st.session_state.messages[i]["role"] != "assistant"):
history.append(st.session_state.messages[i]["content"])
gen_prompt = "List of my past requests: " + str(history[-6:-1]) + ". Based on my past requests, please answer me only this request: \"" + prompt + "\""
# give prompt to chatbot
print(gen_prompt)
response = generate_response(gen_prompt)
st.markdown(response)
message = {"role": "assistant", "content": response}
st.session_state.messages.append(message)
elif(username != ""):
st.markdown("You are not in the user list")