Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,8 @@ import requests
|
|
2 |
import os
|
3 |
import json
|
4 |
import streamlit as st
|
|
|
|
|
5 |
|
6 |
# Page configuration
|
7 |
st.set_page_config(
|
@@ -28,9 +30,39 @@ st.markdown("""
|
|
28 |
</style>
|
29 |
""", unsafe_allow_html=True)
|
30 |
|
31 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
if "messages" not in st.session_state:
|
33 |
-
st.session_state.messages =
|
34 |
|
35 |
# Get API key
|
36 |
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
@@ -123,9 +155,35 @@ with st.sidebar:
|
|
123 |
|
124 |
st.divider()
|
125 |
|
126 |
-
# Controls
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
st.rerun()
|
130 |
|
131 |
# Show welcome message when no messages
|
@@ -140,7 +198,12 @@ for message in st.session_state.messages:
|
|
140 |
# Chat input
|
141 |
if prompt := st.chat_input("Ask anything..."):
|
142 |
# Add user message
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
# Display user message
|
146 |
with st.chat_message("user"):
|
@@ -158,4 +221,9 @@ if prompt := st.chat_input("Ask anything..."):
|
|
158 |
placeholder.markdown(full_response)
|
159 |
|
160 |
# Add AI response to messages
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import os
|
3 |
import json
|
4 |
import streamlit as st
|
5 |
+
from datetime import datetime
|
6 |
+
import pickle
|
7 |
|
8 |
# Page configuration
|
9 |
st.set_page_config(
|
|
|
30 |
</style>
|
31 |
""", unsafe_allow_html=True)
|
32 |
|
33 |
+
# File to store chat history
|
34 |
+
HISTORY_FILE = "chat_history.json"
|
35 |
+
|
36 |
+
def load_chat_history():
|
37 |
+
"""Load chat history from file"""
|
38 |
+
try:
|
39 |
+
if os.path.exists(HISTORY_FILE):
|
40 |
+
with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
|
41 |
+
return json.load(f)
|
42 |
+
except Exception as e:
|
43 |
+
st.error(f"Error loading chat history: {e}")
|
44 |
+
return []
|
45 |
+
|
46 |
+
def save_chat_history(messages):
|
47 |
+
"""Save chat history to file"""
|
48 |
+
try:
|
49 |
+
with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
|
50 |
+
json.dump(messages, f, ensure_ascii=False, indent=2)
|
51 |
+
except Exception as e:
|
52 |
+
st.error(f"Error saving chat history: {e}")
|
53 |
+
|
54 |
+
def clear_chat_history():
|
55 |
+
"""Clear chat history file"""
|
56 |
+
try:
|
57 |
+
if os.path.exists(HISTORY_FILE):
|
58 |
+
os.remove(HISTORY_FILE)
|
59 |
+
st.session_state.messages = []
|
60 |
+
except Exception as e:
|
61 |
+
st.error(f"Error clearing chat history: {e}")
|
62 |
+
|
63 |
+
# Initialize session state with saved history
|
64 |
if "messages" not in st.session_state:
|
65 |
+
st.session_state.messages = load_chat_history()
|
66 |
|
67 |
# Get API key
|
68 |
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
|
|
155 |
|
156 |
st.divider()
|
157 |
|
158 |
+
# Chat History Controls
|
159 |
+
st.header("Chat History")
|
160 |
+
|
161 |
+
# Show number of messages
|
162 |
+
if st.session_state.messages:
|
163 |
+
st.info(f"Messages stored: {len(st.session_state.messages)}")
|
164 |
+
|
165 |
+
# Auto-save toggle
|
166 |
+
auto_save = st.checkbox("Auto-save messages", value=True)
|
167 |
+
|
168 |
+
# Manual save/load buttons
|
169 |
+
col1, col2 = st.columns(2)
|
170 |
+
with col1:
|
171 |
+
if st.button("Save History", use_container_width=True):
|
172 |
+
save_chat_history(st.session_state.messages)
|
173 |
+
st.success("History saved!")
|
174 |
+
|
175 |
+
with col2:
|
176 |
+
if st.button("Load History", use_container_width=True):
|
177 |
+
st.session_state.messages = load_chat_history()
|
178 |
+
st.success("History loaded!")
|
179 |
+
st.rerun()
|
180 |
+
|
181 |
+
st.divider()
|
182 |
+
|
183 |
+
# Clear controls
|
184 |
+
if st.button("Clear Chat", use_container_width=True, type="secondary"):
|
185 |
+
clear_chat_history()
|
186 |
+
st.success("Chat cleared!")
|
187 |
st.rerun()
|
188 |
|
189 |
# Show welcome message when no messages
|
|
|
198 |
# Chat input
|
199 |
if prompt := st.chat_input("Ask anything..."):
|
200 |
# Add user message
|
201 |
+
user_message = {"role": "user", "content": prompt}
|
202 |
+
st.session_state.messages.append(user_message)
|
203 |
+
|
204 |
+
# Auto-save if enabled
|
205 |
+
if auto_save:
|
206 |
+
save_chat_history(st.session_state.messages)
|
207 |
|
208 |
# Display user message
|
209 |
with st.chat_message("user"):
|
|
|
221 |
placeholder.markdown(full_response)
|
222 |
|
223 |
# Add AI response to messages
|
224 |
+
assistant_message = {"role": "assistant", "content": full_response}
|
225 |
+
st.session_state.messages.append(assistant_message)
|
226 |
+
|
227 |
+
# Auto-save if enabled
|
228 |
+
if auto_save:
|
229 |
+
save_chat_history(st.session_state.messages)
|