-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
54 lines (40 loc) · 1.48 KB
/
main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import streamlit as st
import config
import shelve
from bot import bot_response
st.title("Personal Assistant")
USER_AVATAR = "👤"
BOT_AVATAR = "🤖"
# Load chat history
def load_chat_history():
with shelve.open("chat_history") as db:
return db.get("messages", [])
# Save chat history
def save_chat_history(messages):
with shelve.open("chat_history") as db:
db["messages"] = messages
# Initialize or load chat history
if "messages" not in st.session_state:
st.session_state.messages = load_chat_history()
# Sidebar with a button to delete chat history
with st.sidebar:
if st.button("Delete Chat History"):
st.session_state.messages = []
save_chat_history([])
# Display chat messages
for message in st.session_state.messages:
avatar = USER_AVATAR if message["role"] == "user" else BOT_AVATAR
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
# Main chat interface
if prompt := st.chat_input("How can I help?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar=USER_AVATAR):
st.markdown(prompt)
with st.chat_message("assistant", avatar=BOT_AVATAR):
# Generate response
response = bot_response(prompt)
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
# Save chat history after each interaction
save_chat_history(st.session_state.messages)