Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,8 +2,9 @@ import requests
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import streamlit as st
|
| 5 |
-
from datetime import datetime
|
| 6 |
import time
|
|
|
|
| 7 |
|
| 8 |
# Page configuration
|
| 9 |
st.set_page_config(
|
|
@@ -43,6 +44,8 @@ st.markdown("""
|
|
| 43 |
|
| 44 |
# File to store chat history
|
| 45 |
HISTORY_FILE = "chat_history.json"
|
|
|
|
|
|
|
| 46 |
|
| 47 |
def load_chat_history():
|
| 48 |
"""Load chat history from file"""
|
|
@@ -71,6 +74,66 @@ def clear_chat_history():
|
|
| 71 |
except Exception as e:
|
| 72 |
st.error(f"Error clearing chat history: {e}")
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
# Initialize session state with saved history
|
| 75 |
if "messages" not in st.session_state:
|
| 76 |
st.session_state.messages = load_chat_history()
|
|
@@ -90,7 +153,6 @@ def check_api_status():
|
|
| 90 |
except:
|
| 91 |
return "Error"
|
| 92 |
|
| 93 |
-
|
| 94 |
def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
|
| 95 |
if not OPENROUTER_API_KEY:
|
| 96 |
return "No API key found. Please add OPENROUTER_API_KEY to environment variables."
|
|
@@ -183,6 +245,28 @@ with st.sidebar:
|
|
| 183 |
|
| 184 |
st.divider()
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
# All models including new ones
|
| 187 |
models = [
|
| 188 |
("GPT-3.5 Turbo", "openai/gpt-3.5-turbo"),
|
|
@@ -286,6 +370,9 @@ for message in st.session_state.messages:
|
|
| 286 |
|
| 287 |
# Chat input
|
| 288 |
if prompt := st.chat_input("Ask anything..."):
|
|
|
|
|
|
|
|
|
|
| 289 |
# Add user message
|
| 290 |
user_message = {"role": "user", "content": prompt}
|
| 291 |
st.session_state.messages.append(user_message)
|
|
|
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import streamlit as st
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
import time
|
| 7 |
+
import uuid
|
| 8 |
|
| 9 |
# Page configuration
|
| 10 |
st.set_page_config(
|
|
|
|
| 44 |
|
| 45 |
# File to store chat history
|
| 46 |
HISTORY_FILE = "chat_history.json"
|
| 47 |
+
# NEW: File to store online users
|
| 48 |
+
USERS_FILE = "online_users.json"
|
| 49 |
|
| 50 |
def load_chat_history():
|
| 51 |
"""Load chat history from file"""
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
st.error(f"Error clearing chat history: {e}")
|
| 76 |
|
| 77 |
+
# NEW: User tracking functions
|
| 78 |
+
def get_user_id():
|
| 79 |
+
"""Get unique ID for this user session"""
|
| 80 |
+
if 'user_id' not in st.session_state:
|
| 81 |
+
st.session_state.user_id = str(uuid.uuid4())[:8] # Short ID for family use
|
| 82 |
+
return st.session_state.user_id
|
| 83 |
+
|
| 84 |
+
def update_online_users():
|
| 85 |
+
"""Update that this user is online right now"""
|
| 86 |
+
try:
|
| 87 |
+
# Load current online users
|
| 88 |
+
users = {}
|
| 89 |
+
if os.path.exists(USERS_FILE):
|
| 90 |
+
with open(USERS_FILE, 'r') as f:
|
| 91 |
+
users = json.load(f)
|
| 92 |
+
|
| 93 |
+
# Add/update this user
|
| 94 |
+
user_id = get_user_id()
|
| 95 |
+
users[user_id] = {
|
| 96 |
+
'last_seen': datetime.now().isoformat(),
|
| 97 |
+
'name': f'User-{user_id}' # You can customize this
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
# Remove users not seen in last 5 minutes
|
| 101 |
+
current_time = datetime.now()
|
| 102 |
+
active_users = {}
|
| 103 |
+
for uid, data in users.items():
|
| 104 |
+
last_seen = datetime.fromisoformat(data['last_seen'])
|
| 105 |
+
if current_time - last_seen < timedelta(minutes=5):
|
| 106 |
+
active_users[uid] = data
|
| 107 |
+
|
| 108 |
+
# Save updated list
|
| 109 |
+
with open(USERS_FILE, 'w') as f:
|
| 110 |
+
json.dump(active_users, f, indent=2)
|
| 111 |
+
|
| 112 |
+
return len(active_users)
|
| 113 |
+
except Exception:
|
| 114 |
+
return 1 # If error, assume at least you're online
|
| 115 |
+
|
| 116 |
+
def get_online_count():
|
| 117 |
+
"""Get number of people currently online"""
|
| 118 |
+
try:
|
| 119 |
+
if not os.path.exists(USERS_FILE):
|
| 120 |
+
return 0
|
| 121 |
+
|
| 122 |
+
with open(USERS_FILE, 'r') as f:
|
| 123 |
+
users = json.load(f)
|
| 124 |
+
|
| 125 |
+
# Check who's still active (last 5 minutes)
|
| 126 |
+
current_time = datetime.now()
|
| 127 |
+
active_count = 0
|
| 128 |
+
for data in users.values():
|
| 129 |
+
last_seen = datetime.fromisoformat(data['last_seen'])
|
| 130 |
+
if current_time - last_seen < timedelta(minutes=5):
|
| 131 |
+
active_count += 1
|
| 132 |
+
|
| 133 |
+
return active_count
|
| 134 |
+
except Exception:
|
| 135 |
+
return 0
|
| 136 |
+
|
| 137 |
# Initialize session state with saved history
|
| 138 |
if "messages" not in st.session_state:
|
| 139 |
st.session_state.messages = load_chat_history()
|
|
|
|
| 153 |
except:
|
| 154 |
return "Error"
|
| 155 |
|
|
|
|
| 156 |
def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
|
| 157 |
if not OPENROUTER_API_KEY:
|
| 158 |
return "No API key found. Please add OPENROUTER_API_KEY to environment variables."
|
|
|
|
| 245 |
|
| 246 |
st.divider()
|
| 247 |
|
| 248 |
+
# NEW: Live Users Section
|
| 249 |
+
st.header("👥 Who's Online")
|
| 250 |
+
|
| 251 |
+
# Update that you're online
|
| 252 |
+
online_count = update_online_users()
|
| 253 |
+
|
| 254 |
+
# Show live count
|
| 255 |
+
if online_count == 1:
|
| 256 |
+
st.info("🟢 Just you online")
|
| 257 |
+
else:
|
| 258 |
+
st.success(f"🟢 {online_count} people online")
|
| 259 |
+
|
| 260 |
+
# Show your session
|
| 261 |
+
your_id = get_user_id()
|
| 262 |
+
st.caption(f"You: User-{your_id}")
|
| 263 |
+
|
| 264 |
+
# Quick refresh button
|
| 265 |
+
if st.button("Refresh", use_container_width=True):
|
| 266 |
+
st.rerun()
|
| 267 |
+
|
| 268 |
+
st.divider()
|
| 269 |
+
|
| 270 |
# All models including new ones
|
| 271 |
models = [
|
| 272 |
("GPT-3.5 Turbo", "openai/gpt-3.5-turbo"),
|
|
|
|
| 370 |
|
| 371 |
# Chat input
|
| 372 |
if prompt := st.chat_input("Ask anything..."):
|
| 373 |
+
# NEW: Update online status when user sends message
|
| 374 |
+
update_online_users()
|
| 375 |
+
|
| 376 |
# Add user message
|
| 377 |
user_message = {"role": "user", "content": prompt}
|
| 378 |
st.session_state.messages.append(user_message)
|