Add chatbot documentation

main
Lasse Studion 2 years ago
parent b5286a9da1
commit 463d0c57e8
  1. 51
      streamlit_interface.py

@ -7,6 +7,17 @@ from time import sleep
def send(message, check=True, add_to_memory=True):
"""
Sends a message to the chat interface.
Parameters:
- message (str): The message to be sent.
- check (bool): Whether to check the message for tips. Default is True.
- add_to_memory (bool): Whether to add the message to the chatbot's memory. Default is True.
Returns:
- bool: True if the message was sent successfully, False if it was a tip.
"""
store_state()
# Check if the message is a tip.
if check:
@ -27,11 +38,17 @@ def send(message, check=True, add_to_memory=True):
return True
def store_state():
"""
Stores the current state of the chatbot and report in the session state.
"""
st.session_state.chatbot = chatbot
st.session_state.report = report
def generate_report():
"""
Generates a report using a Jinja2 template and stores it in the session state.
"""
from jinja2 import Environment, FileSystemLoader
# Load the Jinja2 template
@ -42,95 +59,103 @@ def generate_report():
st.session_state.report_html = report_html
st.rerun()
st.title("UNMAS Bot")
# Check if 'report_html', 'messages', 'upload_image',are already in the session state, if not, initialize them
if 'report_html' not in st.session_state:
st.session_state.report_html = ''
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
if 'upload_image' not in st.session_state:
st.session_state.upload_image = False
# Load report from session state or create a new one
# Check if 'report' and the bots are already in the session state, if not, initialize them
if "report" not in st.session_state:
report = Report()
st.session_state.report = report
else:
report = st.session_state.report
# Load chatbot from session state or create a new one
if "chatbot" not in st.session_state:
chatbot = Chatbot()
st.session_state.chatbot = chatbot
else:
# If 'chatbot' is already in the session state, retrieve it
chatbot = st.session_state.chatbot
# If the report object is not None, update the chatbot's memory with a new system prompt
if report.object is not None:
new_system_prompt = botstrings.checker_bot_system_prompt + f' The user seems to have found {report.object}.'
chatbot.memory[0] = {"role":"system", "content": new_system_prompt}
print(chatbot.memory)
# Load checker and general bot from session state or create a new one
if "checker_bot" not in st.session_state:
checker_bot = CheckerBot()
st.session_state.checker_bot = checker_bot
else:
checker_bot = st.session_state.checker_bot
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Write the report to screen
# If 'report_html' in the session state is not an empty string, write the report to the screen and reset 'report_html' to an empty string
if st.session_state.report_html != '':
st.markdown(st.session_state.report_html, unsafe_allow_html=True)
st.session_state.report_html = ''
# Check if 'upload_image' is set to True in the session state
if st.session_state.upload_image:
# Initialize user_input to None
user_input = None
# Create a file uploader widget that accepts png, jpg, and jpeg files
img_file = st.file_uploader('Upload an image', type=['png', 'jpg', 'jpeg'])
# Create a chat input widget and store the user's input in user_input
user_input = st.chat_input('')
# If an image file was uploaded
if img_file is not None:
# Open the image file and convert it to a numpy array and store the image array in the report object
image = Image.open(img_file)
img_array = np.array(image)
report.image = img_array
send('Thanks!', check=False, add_to_memory=False)
# Ask the chatbot for more information and store the question
question = chatbot.ask_for_info(report, chatbot=chatbot)
send(question, check=False)
# Set 'upload_image' to False in the session state
st.session_state.upload_image = False
store_state()
st.rerun()
# If the user entered something in the chat input widget
elif user_input:
# Check the user's answer to the image question
answer = checker_bot.check_image_answer(user_input, report)
if answer == 'no' or answer == 'help':
# If the user answered 'no'
if answer == 'no':
# Send a message to the user and set 'upload_image' to False in the session state
send('No problem, we will continue without the image.', check=False, add_to_memory=False)
st.session_state.upload_image = False
elif answer == 'help':
# Send a help message to the user and set 'upload_image' to False in the session state
send('We will provide help for sending a picture in WhatsAPP', check=False) #TODO
st.session_state.upload_image = False
# Ask the chatbot for more information and store the question
question = chatbot.ask_for_info(report, chatbot=chatbot)
send(question, check=False)
# If the user answered 'yes'
elif answer == 'yes':
send('Great! Please upload the image.', check=False, add_to_memory=False)
st.session_state.upload_image = True
store_state()
st.rerun()
else:
user_input = st.chat_input('')

Loading…
Cancel
Save