Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -10,10 +10,10 @@ import uuid
|
|
10 |
st.set_page_config(
|
11 |
page_title="Chat Flow π·",
|
12 |
page_icon="π¬",
|
13 |
-
initial_sidebar_state="
|
14 |
)
|
15 |
|
16 |
-
#
|
17 |
st.markdown("""
|
18 |
<style>
|
19 |
.stApp {
|
@@ -39,6 +39,44 @@ st.markdown("""
|
|
39 |
font-size: 0.8em;
|
40 |
font-style: italic;
|
41 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
</style>
|
43 |
""", unsafe_allow_html=True)
|
44 |
|
@@ -46,6 +84,8 @@ st.markdown("""
|
|
46 |
HISTORY_FILE = "chat_history.json"
|
47 |
# NEW: File to store online users
|
48 |
USERS_FILE = "online_users.json"
|
|
|
|
|
49 |
|
50 |
|
51 |
def load_chat_history():
|
@@ -77,9 +117,100 @@ def clear_chat_history():
|
|
77 |
except Exception as e:
|
78 |
st.error(f"Error clearing chat history: {e}")
|
79 |
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
def get_user_id():
|
84 |
"""Get unique ID for this user session"""
|
85 |
if 'user_id' not in st.session_state:
|
@@ -147,6 +278,10 @@ def get_online_count():
|
|
147 |
if "messages" not in st.session_state:
|
148 |
st.session_state.messages = load_chat_history()
|
149 |
|
|
|
|
|
|
|
|
|
150 |
# Get API key
|
151 |
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
152 |
|
@@ -245,8 +380,82 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
|
|
245 |
st.title("Chat Flow π·")
|
246 |
st.caption("10 powerful Models, one simple chat.")
|
247 |
|
248 |
-
# Sidebar
|
249 |
with st.sidebar:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
st.header("Settings")
|
251 |
|
252 |
# API Status
|
|
|
10 |
st.set_page_config(
|
11 |
page_title="Chat Flow π·",
|
12 |
page_icon="π¬",
|
13 |
+
initial_sidebar_state="expanded" # Changed to show chat history by default
|
14 |
)
|
15 |
|
16 |
+
# Enhanced CSS with chat history styling
|
17 |
st.markdown("""
|
18 |
<style>
|
19 |
.stApp {
|
|
|
39 |
font-size: 0.8em;
|
40 |
font-style: italic;
|
41 |
}
|
42 |
+
|
43 |
+
/* Chat history styling */
|
44 |
+
.chat-history-item {
|
45 |
+
padding: 8px 12px;
|
46 |
+
margin: 4px 0;
|
47 |
+
border-radius: 8px;
|
48 |
+
border: 1px solid #e0e0e0;
|
49 |
+
background: #f8f9fa;
|
50 |
+
cursor: pointer;
|
51 |
+
transition: all 0.2s;
|
52 |
+
}
|
53 |
+
|
54 |
+
.chat-history-item:hover {
|
55 |
+
background: #e9ecef;
|
56 |
+
border-color: #28a745;
|
57 |
+
}
|
58 |
+
|
59 |
+
.chat-history-item.active {
|
60 |
+
background: #28a745;
|
61 |
+
color: white;
|
62 |
+
border-color: #28a745;
|
63 |
+
}
|
64 |
+
|
65 |
+
.chat-title {
|
66 |
+
font-weight: 500;
|
67 |
+
font-size: 0.9em;
|
68 |
+
margin-bottom: 2px;
|
69 |
+
}
|
70 |
+
|
71 |
+
.chat-date {
|
72 |
+
font-size: 0.75em;
|
73 |
+
opacity: 0.7;
|
74 |
+
}
|
75 |
+
|
76 |
+
.new-chat-btn {
|
77 |
+
width: 100%;
|
78 |
+
margin-bottom: 16px;
|
79 |
+
}
|
80 |
</style>
|
81 |
""", unsafe_allow_html=True)
|
82 |
|
|
|
84 |
HISTORY_FILE = "chat_history.json"
|
85 |
# NEW: File to store online users
|
86 |
USERS_FILE = "online_users.json"
|
87 |
+
# NEW: File to store chat sessions
|
88 |
+
SESSIONS_FILE = "chat_sessions.json"
|
89 |
|
90 |
|
91 |
def load_chat_history():
|
|
|
117 |
except Exception as e:
|
118 |
st.error(f"Error clearing chat history: {e}")
|
119 |
|
120 |
+
|
121 |
+
# NEW: Chat Sessions Management
|
122 |
+
def load_chat_sessions():
|
123 |
+
"""Load all chat sessions"""
|
124 |
+
try:
|
125 |
+
if os.path.exists(SESSIONS_FILE):
|
126 |
+
with open(SESSIONS_FILE, 'r', encoding='utf-8') as f:
|
127 |
+
return json.load(f)
|
128 |
+
except Exception as e:
|
129 |
+
st.error(f"Error loading chat sessions: {e}")
|
130 |
+
return {}
|
131 |
+
|
132 |
+
|
133 |
+
def save_chat_sessions(sessions):
|
134 |
+
"""Save chat sessions to file"""
|
135 |
+
try:
|
136 |
+
with open(SESSIONS_FILE, 'w', encoding='utf-8') as f:
|
137 |
+
json.dump(sessions, f, ensure_ascii=False, indent=2)
|
138 |
+
except Exception as e:
|
139 |
+
st.error(f"Error saving chat sessions: {e}")
|
140 |
+
|
141 |
+
|
142 |
+
def get_session_id():
|
143 |
+
"""Get or create session ID"""
|
144 |
+
if 'session_id' not in st.session_state:
|
145 |
+
st.session_state.session_id = str(uuid.uuid4())
|
146 |
+
return st.session_state.session_id
|
147 |
+
|
148 |
+
|
149 |
+
def get_chat_title(messages):
|
150 |
+
"""Generate a title for the chat based on first user message"""
|
151 |
+
if not messages:
|
152 |
+
return "New Chat"
|
153 |
+
|
154 |
+
for msg in messages:
|
155 |
+
if msg["role"] == "user":
|
156 |
+
content = msg["content"]
|
157 |
+
# Take first 30 characters and add ellipsis if longer
|
158 |
+
if len(content) > 30:
|
159 |
+
return content[:30] + "..."
|
160 |
+
return content
|
161 |
+
return "New Chat"
|
162 |
+
|
163 |
+
|
164 |
+
def save_current_session():
|
165 |
+
"""Save current chat session"""
|
166 |
+
if not st.session_state.messages:
|
167 |
+
return
|
168 |
+
|
169 |
+
sessions = load_chat_sessions()
|
170 |
+
session_id = get_session_id()
|
171 |
+
|
172 |
+
sessions[session_id] = {
|
173 |
+
"title": get_chat_title(st.session_state.messages),
|
174 |
+
"messages": st.session_state.messages,
|
175 |
+
"created_at": datetime.now().isoformat(),
|
176 |
+
"updated_at": datetime.now().isoformat()
|
177 |
+
}
|
178 |
+
|
179 |
+
save_chat_sessions(sessions)
|
180 |
+
|
181 |
+
|
182 |
+
def load_session(session_id):
|
183 |
+
"""Load a specific chat session"""
|
184 |
+
sessions = load_chat_sessions()
|
185 |
+
if session_id in sessions:
|
186 |
+
st.session_state.messages = sessions[session_id]["messages"]
|
187 |
+
st.session_state.session_id = session_id
|
188 |
+
return True
|
189 |
+
return False
|
190 |
+
|
191 |
+
|
192 |
+
def delete_session(session_id):
|
193 |
+
"""Delete a chat session"""
|
194 |
+
sessions = load_chat_sessions()
|
195 |
+
if session_id in sessions:
|
196 |
+
del sessions[session_id]
|
197 |
+
save_chat_sessions(sessions)
|
198 |
+
return True
|
199 |
+
return False
|
200 |
|
201 |
|
202 |
+
def start_new_chat():
|
203 |
+
"""Start a new chat session"""
|
204 |
+
# Save current session if it has messages
|
205 |
+
if st.session_state.messages:
|
206 |
+
save_current_session()
|
207 |
+
|
208 |
+
# Clear current chat and create new session
|
209 |
+
st.session_state.messages = []
|
210 |
+
st.session_state.session_id = str(uuid.uuid4())
|
211 |
+
|
212 |
+
|
213 |
+
# NEW: User tracking functions
|
214 |
def get_user_id():
|
215 |
"""Get unique ID for this user session"""
|
216 |
if 'user_id' not in st.session_state:
|
|
|
278 |
if "messages" not in st.session_state:
|
279 |
st.session_state.messages = load_chat_history()
|
280 |
|
281 |
+
# Initialize session ID
|
282 |
+
if "session_id" not in st.session_state:
|
283 |
+
st.session_state.session_id = str(uuid.uuid4())
|
284 |
+
|
285 |
# Get API key
|
286 |
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
|
287 |
|
|
|
380 |
st.title("Chat Flow π·")
|
381 |
st.caption("10 powerful Models, one simple chat.")
|
382 |
|
383 |
+
# Sidebar with Chat History
|
384 |
with st.sidebar:
|
385 |
+
# NEW: Chat History Section at the top
|
386 |
+
st.header("π¬ Chat History")
|
387 |
+
|
388 |
+
# New Chat Button
|
389 |
+
if st.button("β New Chat", use_container_width=True, type="primary"):
|
390 |
+
start_new_chat()
|
391 |
+
st.rerun()
|
392 |
+
|
393 |
+
st.divider()
|
394 |
+
|
395 |
+
# Load and display chat sessions
|
396 |
+
sessions = load_chat_sessions()
|
397 |
+
current_session_id = get_session_id()
|
398 |
+
|
399 |
+
if sessions:
|
400 |
+
st.subheader("Previous Chats")
|
401 |
+
|
402 |
+
# Sort sessions by updated_at (most recent first)
|
403 |
+
sorted_sessions = sorted(
|
404 |
+
sessions.items(),
|
405 |
+
key=lambda x: x[1].get("updated_at", x[1].get("created_at", "")),
|
406 |
+
reverse=True
|
407 |
+
)
|
408 |
+
|
409 |
+
for session_id, session_data in sorted_sessions:
|
410 |
+
# Create container for each chat item
|
411 |
+
chat_container = st.container()
|
412 |
+
|
413 |
+
with chat_container:
|
414 |
+
# Show current chat with different styling
|
415 |
+
if session_id == current_session_id:
|
416 |
+
st.markdown(f"πΉ **{session_data['title']}**")
|
417 |
+
else:
|
418 |
+
col_load, col_delete = st.columns([3, 1])
|
419 |
+
|
420 |
+
with col_load:
|
421 |
+
if st.button(
|
422 |
+
f"π {session_data['title']}",
|
423 |
+
key=f"load_{session_id}",
|
424 |
+
use_container_width=True
|
425 |
+
):
|
426 |
+
# Save current session before switching
|
427 |
+
if st.session_state.messages:
|
428 |
+
save_current_session()
|
429 |
+
|
430 |
+
# Load selected session
|
431 |
+
load_session(session_id)
|
432 |
+
st.rerun()
|
433 |
+
|
434 |
+
with col_delete:
|
435 |
+
if st.button("ποΈ", key=f"delete_{session_id}"):
|
436 |
+
delete_session(session_id)
|
437 |
+
# If deleted session was current, start new chat
|
438 |
+
if session_id == current_session_id:
|
439 |
+
start_new_chat()
|
440 |
+
st.rerun()
|
441 |
+
|
442 |
+
# Show session info
|
443 |
+
if "updated_at" in session_data:
|
444 |
+
update_time = datetime.fromisoformat(session_data["updated_at"])
|
445 |
+
st.caption(f"Updated: {update_time.strftime('%m/%d %H:%M')}")
|
446 |
+
|
447 |
+
st.markdown("---")
|
448 |
+
|
449 |
+
else:
|
450 |
+
st.info("No previous chats yet")
|
451 |
+
|
452 |
+
# Auto-save current session periodically
|
453 |
+
if st.session_state.messages:
|
454 |
+
save_current_session()
|
455 |
+
|
456 |
+
st.divider()
|
457 |
+
|
458 |
+
# Settings Section
|
459 |
st.header("Settings")
|
460 |
|
461 |
# API Status
|