Update app.py
Browse files
app.py
CHANGED
|
@@ -13,12 +13,9 @@ CHAT_HISTORY_DIR = "chat_histories" # Directory to save chat histories
|
|
| 13 |
# Ensure the directory exists
|
| 14 |
os.makedirs(CHAT_HISTORY_DIR, exist_ok=True)
|
| 15 |
|
| 16 |
-
# Streamlit Configurations
|
| 17 |
st.set_page_config(page_title="🐶 DUBSChat", layout="wide")
|
| 18 |
|
| 19 |
-
with st.sidebar:
|
| 20 |
-
dubs_key = st.text_input("Enter Dubs Key", key="chatbot_api_key", type="password")
|
| 21 |
-
|
| 22 |
# Utility Functions
|
| 23 |
def save_chat_history(session_name, messages):
|
| 24 |
"""
|
|
@@ -44,28 +41,28 @@ def get_saved_sessions():
|
|
| 44 |
|
| 45 |
# Sidebar Configuration
|
| 46 |
with st.sidebar:
|
| 47 |
-
st.title("📂
|
| 48 |
saved_sessions = get_saved_sessions()
|
| 49 |
|
| 50 |
# Display saved sessions
|
| 51 |
if saved_sessions:
|
| 52 |
-
selected_session = st.radio("
|
| 53 |
if st.button("Load Session"):
|
| 54 |
st.session_state["messages"] = load_chat_history(f"{selected_session}.json")
|
|
|
|
| 55 |
st.success(f"Loaded session: {selected_session}")
|
| 56 |
else:
|
| 57 |
-
st.write("No
|
| 58 |
-
|
| 59 |
-
#
|
| 60 |
-
if st.button("
|
| 61 |
-
session_name = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 62 |
st.session_state["messages"] = [
|
| 63 |
{"role": "system", "content": "You are DUBS, a helpful assistant capable of conversing in a friendly and knowledgeable way."},
|
| 64 |
{"role": "assistant", "content": "Hello! How can I assist you today?"}
|
| 65 |
]
|
| 66 |
-
st.session_state["session_name"] =
|
| 67 |
-
save_chat_history(session_name, st.session_state["messages"])
|
| 68 |
-
st.success(
|
| 69 |
|
| 70 |
# Chat History Initialization
|
| 71 |
if "messages" not in st.session_state:
|
|
@@ -106,7 +103,7 @@ if prompt := st.chat_input():
|
|
| 106 |
response = requests.post(
|
| 107 |
SPACE_URL,
|
| 108 |
json={"prompt": chat_history},
|
| 109 |
-
headers={"Authorization": f"Bearer {
|
| 110 |
timeout=60,
|
| 111 |
)
|
| 112 |
response.raise_for_status()
|
|
@@ -121,3 +118,4 @@ if prompt := st.chat_input():
|
|
| 121 |
|
| 122 |
# Auto-save the updated chat history
|
| 123 |
save_chat_history(st.session_state["session_name"], st.session_state["messages"])
|
|
|
|
|
|
| 13 |
# Ensure the directory exists
|
| 14 |
os.makedirs(CHAT_HISTORY_DIR, exist_ok=True)
|
| 15 |
|
| 16 |
+
# Streamlit Configurations (must come first)
|
| 17 |
st.set_page_config(page_title="🐶 DUBSChat", layout="wide")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Utility Functions
|
| 20 |
def save_chat_history(session_name, messages):
|
| 21 |
"""
|
|
|
|
| 41 |
|
| 42 |
# Sidebar Configuration
|
| 43 |
with st.sidebar:
|
| 44 |
+
st.title("📂 Dubs Recall")
|
| 45 |
saved_sessions = get_saved_sessions()
|
| 46 |
|
| 47 |
# Display saved sessions
|
| 48 |
if saved_sessions:
|
| 49 |
+
selected_session = st.radio("Past Sessions:", saved_sessions)
|
| 50 |
if st.button("Load Session"):
|
| 51 |
st.session_state["messages"] = load_chat_history(f"{selected_session}.json")
|
| 52 |
+
st.session_state["session_name"] = selected_session
|
| 53 |
st.success(f"Loaded session: {selected_session}")
|
| 54 |
else:
|
| 55 |
+
st.write("No past sessions available.")
|
| 56 |
+
|
| 57 |
+
# Reset chat
|
| 58 |
+
if st.button("🔄 Reset Chat"):
|
|
|
|
| 59 |
st.session_state["messages"] = [
|
| 60 |
{"role": "system", "content": "You are DUBS, a helpful assistant capable of conversing in a friendly and knowledgeable way."},
|
| 61 |
{"role": "assistant", "content": "Hello! How can I assist you today?"}
|
| 62 |
]
|
| 63 |
+
st.session_state["session_name"] = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 64 |
+
save_chat_history(st.session_state["session_name"], st.session_state["messages"])
|
| 65 |
+
st.success("Chat reset and new session started.")
|
| 66 |
|
| 67 |
# Chat History Initialization
|
| 68 |
if "messages" not in st.session_state:
|
|
|
|
| 103 |
response = requests.post(
|
| 104 |
SPACE_URL,
|
| 105 |
json={"prompt": chat_history},
|
| 106 |
+
headers={"Authorization": f"Bearer {API_KEY}"},
|
| 107 |
timeout=60,
|
| 108 |
)
|
| 109 |
response.raise_for_status()
|
|
|
|
| 118 |
|
| 119 |
# Auto-save the updated chat history
|
| 120 |
save_chat_history(st.session_state["session_name"], st.session_state["messages"])
|
| 121 |
+
|