Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,26 @@
|
|
| 1 |
-
from openai import OpenAI
|
| 2 |
import streamlit as st
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
import os
|
| 5 |
-
import shelve
|
| 6 |
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
USER_AVATAR = "👤"
|
| 12 |
-
BOT_AVATAR = "🤖"
|
| 13 |
-
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 14 |
-
|
| 15 |
-
# Ensure openai_model is initialized in session state
|
| 16 |
-
if "openai_model" not in st.session_state:
|
| 17 |
-
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Load chat history from shelve file
|
| 21 |
-
def load_chat_history():
|
| 22 |
-
with shelve.open("chat_history") as db:
|
| 23 |
-
return db.get("messages", [])
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Save chat history to shelve file
|
| 27 |
-
def save_chat_history(messages):
|
| 28 |
-
with shelve.open("chat_history") as db:
|
| 29 |
-
db["messages"] = messages
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# Initialize or load chat history
|
| 33 |
if "messages" not in st.session_state:
|
| 34 |
-
st.session_state.messages =
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
with st.sidebar:
|
| 38 |
-
if st.button("Delete Chat History"):
|
| 39 |
-
st.session_state.messages = []
|
| 40 |
-
save_chat_history([])
|
| 41 |
-
|
| 42 |
-
# Display chat messages
|
| 43 |
for message in st.session_state.messages:
|
| 44 |
-
|
| 45 |
-
with st.chat_message(message["role"], avatar=avatar):
|
| 46 |
st.markdown(message["content"])
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
if prompt := st.chat_input("
|
|
|
|
|
|
|
|
|
|
| 50 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 51 |
-
with st.chat_message("user", avatar=USER_AVATAR):
|
| 52 |
-
st.markdown(prompt)
|
| 53 |
-
|
| 54 |
-
with st.chat_message("assistant", avatar=BOT_AVATAR):
|
| 55 |
-
message_placeholder = st.empty()
|
| 56 |
-
full_response = ""
|
| 57 |
-
for response in client.chat.completions.create(
|
| 58 |
-
model=st.session_state["openai_model"],
|
| 59 |
-
messages=st.session_state["messages"],
|
| 60 |
-
stream=True,
|
| 61 |
-
):
|
| 62 |
-
full_response += response.choices[0].delta.content or ""
|
| 63 |
-
message_placeholder.markdown(full_response + "|")
|
| 64 |
-
message_placeholder.markdown(full_response)
|
| 65 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
st.title("Echo Bot")
|
| 4 |
|
| 5 |
+
# Initialize chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
if "messages" not in st.session_state:
|
| 7 |
+
st.session_state.messages = []
|
| 8 |
|
| 9 |
+
# Display chat messages from history on app rerun
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
for message in st.session_state.messages:
|
| 11 |
+
with st.chat_message(message["role"]):
|
|
|
|
| 12 |
st.markdown(message["content"])
|
| 13 |
|
| 14 |
+
# React to user input
|
| 15 |
+
if prompt := st.chat_input("What is up?"):
|
| 16 |
+
# Display user message in chat message container
|
| 17 |
+
st.chat_message("user").markdown(prompt)
|
| 18 |
+
# Add user message to chat history
|
| 19 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
response = f"Echo: {prompt}"
|
| 22 |
+
# Display assistant response in chat message container
|
| 23 |
+
with st.chat_message("assistant"):
|
| 24 |
+
st.markdown(response)
|
| 25 |
+
# Add assistant response to chat history
|
| 26 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|