Rahul Bhoyar
commited on
Commit
·
0f19397
1
Parent(s):
9140a4d
Updated files
Browse files- .streamlit/config.toml +6 -0
- app.py +68 -0
- requirements.txt +3 -0
.streamlit/config.toml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
primaryColor="#6528f7"
|
3 |
+
backgroundColor="#FFFFFF"
|
4 |
+
secondaryBackgroundColor="#F0F2F6"
|
5 |
+
textColor="#000000"
|
6 |
+
font="sans serif"
|
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import streamlit as st
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
import shelve
|
6 |
+
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
st.title("Streamlit Chatbot Interface")
|
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 = load_chat_history()
|
35 |
+
|
36 |
+
# Sidebar with a button to delete chat history
|
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 |
+
avatar = USER_AVATAR if message["role"] == "user" else BOT_AVATAR
|
45 |
+
with st.chat_message(message["role"], avatar=avatar):
|
46 |
+
st.markdown(message["content"])
|
47 |
+
|
48 |
+
# Main chat interface
|
49 |
+
if prompt := st.chat_input("How can I help?"):
|
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 |
+
# Save chat history after each interaction
|
68 |
+
save_chat_history(st.session_state.messages)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
python-dotenv
|
3 |
+
openai
|