Spaces:
Sleeping
Sleeping
File size: 17,177 Bytes
7c18a0a aae749f 92fb852 1595043 37bd66e 92fb852 37bd66e 0a391cf d7b0863 1d92311 e1a482c 1595043 d7b0863 1d92311 02881ef d7b0863 7c18a0a 02881ef a9cd437 d7b0863 92fb852 d7b0863 173a005 92fb852 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 d7b0863 ce9fd39 b8263fb 02881ef 7c18a0a 02881ef 90ccd1d ce9fd39 37bd66e 90ccd1d 92fb852 90ccd1d 92fb852 90ccd1d ce9fd39 37bd66e d7b0863 37bd66e d7b0863 37bd66e d7b0863 37bd66e d7b0863 37bd66e d7b0863 37bd66e d7b0863 37bd66e d7b0863 37bd66e 92fb852 1595043 90ccd1d 02881ef 1595043 8237ec2 1d92311 8237ec2 1595043 8237ec2 7c18a0a 71519dd 1595043 d6bd63d 8237ec2 92fb852 8237ec2 1595043 92fb852 1595043 92fb852 d7b0863 1595043 d7b0863 8237ec2 1595043 fcb5808 8237ec2 1595043 fcb5808 92fb852 d7b0863 d6bd63d 92fb852 8237ec2 d6bd63d 173a005 1595043 d7b0863 92fb852 d7b0863 492796b 92fb852 d7b0863 92fb852 1595043 d7b0863 1d92311 d7b0863 1d92311 d7b0863 1d92311 d7b0863 8237ec2 d7b0863 8237ec2 d7b0863 173a005 8237ec2 d7b0863 37bd66e d7b0863 37bd66e d7b0863 37bd66e d7b0863 37bd66e d7b0863 d665700 37bd66e d7b0863 8237ec2 fcb5808 d7b0863 92fb852 d6bd63d dff5142 d7b0863 fcb5808 1d92311 8237ec2 d7b0863 92fb852 d7b0863 90ccd1d d7b0863 90ccd1d d7b0863 fcb5808 90ccd1d d7b0863 90ccd1d d7b0863 90ccd1d d7b0863 90ccd1d d7b0863 92fb852 d7b0863 92fb852 d7b0863 92fb852 d7b0863 d163888 d7b0863 fcb5808 1595043 d7b0863 8237ec2 d7b0863 8237ec2 1595043 d7b0863 37bd66e d7b0863 90ccd1d d7b0863 90ccd1d 1595043 d7b0863 1595043 d7b0863 1595043 facf71f 92fb852 d7b0863 92fb852 d7b0863 92fb852 d7b0863 92fb852 dff5142 d7b0863 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
import requests
import os
import json
import streamlit as st
from datetime import datetime, timedelta
import time
import uuid
# Page configuration
st.set_page_config(
page_title="Chat Flow π·",
page_icon="π¬",
initial_sidebar_state="collapsed"
)
# CSS - Keep original web design, add mobile responsiveness only
st.markdown("""
<style>
.stApp {
background: white;
}
.main .block-container {
max-width: 800px;
}
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
.stDeployButton {display: none;}
.model-id {
color: #28a745;
font-family: monospace;
}
.model-attribution {
color: #28a745;
font-size: 0.8em;
font-style: italic;
}
/* MOBILE ONLY - doesn't affect web */
@media (max-width: 768px) {
.stApp {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.main .block-container {
max-width: 100%;
padding: 0.5rem;
}
/* Make sidebar work better on mobile */
.css-1d391kg {
width: 100% !important;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
}
/* Chat messages styling for mobile */
.stChatMessage {
background: rgba(255, 255, 255, 0.95);
border-radius: 15px;
margin: 0.5rem 0;
padding: 1rem;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
/* User messages - right aligned */
.stChatMessage[data-testid="user-message"] {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
color: white;
margin-left: 15%;
}
/* Assistant messages - left aligned */
.stChatMessage[data-testid="assistant-message"] {
background: rgba(255, 255, 255, 0.98);
color: #333;
margin-right: 15%;
}
/* Mobile text sizing */
.stMarkdown {
font-size: 14px;
}
h1 {
font-size: 1.5rem !important;
color: white;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
text-align: center;
}
.stCaption {
color: rgba(255, 255, 255, 0.8);
text-align: center;
}
/* Mobile buttons */
.stButton button {
border-radius: 8px;
font-size: 14px;
}
/* Chat input for mobile */
.stChatInput {
background: rgba(255, 255, 255, 0.9);
border-radius: 20px;
backdrop-filter: blur(10px);
}
.stChatInput input {
font-size: 16px; /* Prevents zoom on iOS */
}
}
/* Extra small screens (iPhone 5, old phones) */
@media (max-width: 480px) {
.main .block-container {
padding: 0.25rem;
}
.stChatMessage {
padding: 0.75rem;
margin: 0.25rem 0;
}
.stChatMessage[data-testid="user-message"] {
margin-left: 8%;
}
.stChatMessage[data-testid="assistant-message"] {
margin-right: 8%;
}
h1 {
font-size: 1.3rem !important;
}
.stButton button {
font-size: 12px;
padding: 0.4rem;
}
.stMarkdown {
font-size: 13px;
}
}
</style>
""", unsafe_allow_html=True)
# File to store chat history
HISTORY_FILE = "chat_history.json"
# File to store online users
USERS_FILE = "online_users.json"
def load_chat_history():
"""Load chat history from file"""
try:
if os.path.exists(HISTORY_FILE):
with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
st.error(f"Error loading chat history: {e}")
return []
def save_chat_history(messages):
"""Save chat history to file"""
try:
with open(HISTORY_FILE, 'w', encoding='utf-8') as f:
json.dump(messages, f, ensure_ascii=False, indent=2)
except Exception as e:
st.error(f"Error saving chat history: {e}")
def clear_chat_history():
"""Clear chat history file"""
try:
if os.path.exists(HISTORY_FILE):
os.remove(HISTORY_FILE)
st.session_state.messages = []
except Exception as e:
st.error(f"Error clearing chat history: {e}")
# User tracking functions
def get_user_id():
"""Get unique ID for this user session"""
if 'user_id' not in st.session_state:
st.session_state.user_id = str(uuid.uuid4())[:8] # Short ID for family use
return st.session_state.user_id
def update_online_users():
"""Update that this user is online right now"""
try:
# Load current online users
users = {}
if os.path.exists(USERS_FILE):
with open(USERS_FILE, 'r') as f:
users = json.load(f)
# Add/update this user
user_id = get_user_id()
users[user_id] = {
'last_seen': datetime.now().isoformat(),
'name': f'User-{user_id}' # You can customize this
}
# Remove users not seen in last 5 minutes
current_time = datetime.now()
active_users = {}
for uid, data in users.items():
last_seen = datetime.fromisoformat(data['last_seen'])
if current_time - last_seen < timedelta(minutes=5):
active_users[uid] = data
# Save updated list
with open(USERS_FILE, 'w') as f:
json.dump(active_users, f, indent=2)
return len(active_users)
except Exception:
return 1 # If error, assume at least you're online
def get_online_count():
"""Get number of people currently online"""
try:
if not os.path.exists(USERS_FILE):
return 0
with open(USERS_FILE, 'r') as f:
users = json.load(f)
# Check who's still active (last 5 minutes)
current_time = datetime.now()
active_count = 0
for data in users.values():
last_seen = datetime.fromisoformat(data['last_seen'])
if current_time - last_seen < timedelta(minutes=5):
active_count += 1
return active_count
except Exception:
return 0
# Initialize session state with saved history
if "messages" not in st.session_state:
st.session_state.messages = load_chat_history()
# Get API key
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
@st.cache_data(ttl=300)
def check_api_status():
if not OPENROUTER_API_KEY:
return "No API Key"
try:
url = "https://openrouter.ai/api/v1/models"
headers = {"Authorization": f"Bearer {OPENROUTER_API_KEY}"}
response = requests.get(url, headers=headers, timeout=10)
return "Connected" if response.status_code == 200 else "Error"
except:
return "Error"
def get_ai_response(messages, model="openai/gpt-3.5-turbo"):
if not OPENROUTER_API_KEY:
return "No API key found. Please add OPENROUTER_API_KEY to environment variables."
url = "https://openrouter.ai/api/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"HTTP-Referer": "http://localhost:8501", # Optional: Your site URL
"X-Title": "Streamlit AI Assistant" # Optional: Your app name
}
# Create system message and user messages
api_messages = [{"role": "system", "content": "You are a helpful AI assistant. Provide clear and helpful responses."}]
api_messages.extend(messages)
data = {
"model": model,
"messages": api_messages,
"stream": True,
"max_tokens": 2000,
"temperature": 0.7,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}
try:
response = requests.post(url, headers=headers, json=data, stream=True, timeout=60)
# Better error handling
if response.status_code != 200:
error_detail = ""
try:
error_data = response.json()
error_detail = error_data.get('error', {}).get('message', f"HTTP {response.status_code}")
except:
error_detail = f"HTTP {response.status_code}: {response.reason}"
yield f"API Error: {error_detail}. Please try a different model or check your API key."
return
full_response = ""
buffer = ""
# Using your working streaming logic
for line in response.iter_lines():
if line:
# The server sends lines starting with "data: ..."
if line.startswith(b"data: "):
data_str = line[len(b"data: "):].decode("utf-8")
if data_str.strip() == "[DONE]":
break
try:
data = json.loads(data_str)
delta = data["choices"][0]["delta"].get("content", "")
if delta:
full_response += delta
yield full_response
except json.JSONDecodeError:
continue
except (KeyError, IndexError):
continue
except requests.exceptions.Timeout:
yield "Request timed out. Please try again with a shorter message or different model."
except requests.exceptions.ConnectionError:
yield "Connection error. Please check your internet connection and try again."
except requests.exceptions.RequestException as e:
yield f"Request error: {str(e)}. Please try again."
except Exception as e:
yield f"Unexpected error: {str(e)}. Please try again or contact support."
# Header
st.title("Chat Flow π·")
st.caption("10 powerful Models, one simple chat.")
# Sidebar
with st.sidebar:
st.header("Settings")
# API Status
status = check_api_status()
if status == "Connected":
st.success("π’ API Connected")
elif status == "No API Key":
st.error("No API Key")
else:
st.warning("Connection Issue")
st.divider()
# Live Users Section
st.header("π₯ Who's Online")
# Update that you're online
online_count = update_online_users()
# Show live count
if online_count == 1:
st.info("π’ Just you online")
else:
st.success(f"π’ {online_count} people online")
# Show your session
your_id = get_user_id()
st.caption(f"You: User-{your_id}")
# Quick refresh button
if st.button("Refresh", use_container_width=True):
st.rerun()
# Debug Section
with st.expander("π Debug Info"):
if os.path.exists(USERS_FILE):
with open(USERS_FILE, 'r') as f:
users = json.load(f)
st.write(f"Users in file: {len(users)}")
for uid, data in users.items():
last_seen_time = datetime.fromisoformat(data['last_seen'])
time_ago = datetime.now() - last_seen_time
minutes_ago = int(time_ago.total_seconds() / 60)
st.write(f"- {uid}: {minutes_ago} min ago")
else:
st.write("No users file yet")
st.divider()
# All models including new ones
models = [
("GPT-3.5 Turbo", "openai/gpt-3.5-turbo"),
("LLaMA 3.1 8B", "meta-llama/llama-3.1-8b-instruct"),
("LLaMA 3.1 70B", "meta-llama/llama-3.1-70b-instruct"),
("DeepSeek Chat v3", "deepseek/deepseek-chat-v3-0324:free"),
("DeepSeek R1", "deepseek/deepseek-r1-0528:free"),
("Qwen3 Coder", "qwen/qwen3-coder:free"),
("Microsoft MAI DS R1", "microsoft/mai-ds-r1:free"),
("Gemma 3 27B", "google/gemma-3-27b-it:free"),
("Gemma 3 4B", "google/gemma-3-4b-it:free"),
("Auto (Best Available)", "openrouter/auto")
]
model_names = [name for name, _ in models]
model_ids = [model_id for _, model_id in models]
selected_index = st.selectbox("Model", range(len(model_names)),
format_func=lambda x: model_names[x],
index=0)
selected_model = model_ids[selected_index]
# Show selected model ID in green
st.markdown(f"**Model ID:** <span class='model-id'>{selected_model}</span>", unsafe_allow_html=True)
st.divider()
# Chat History Controls
st.header("Chat History")
# Show number of messages
if st.session_state.messages:
st.info(f"Messages stored: {len(st.session_state.messages)}")
# Auto-save toggle
auto_save = st.checkbox("Auto-save messages", value=True)
# Manual save/load buttons
col1, col2 = st.columns(2)
with col1:
if st.button("Save History", use_container_width=True):
save_chat_history(st.session_state.messages)
st.success("History saved!")
with col2:
if st.button("Load History", use_container_width=True):
st.session_state.messages = load_chat_history()
st.success("History loaded!")
st.rerun()
st.divider()
# View History
if st.button("View History File", use_container_width=True):
if os.path.exists(HISTORY_FILE):
with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
history_content = f.read()
st.text_area("Chat History (JSON)", history_content, height=200)
else:
st.warning("No history file found")
# Download History
if os.path.exists(HISTORY_FILE):
with open(HISTORY_FILE, 'rb') as f:
st.download_button(
label="Download History",
data=f.read(),
file_name=f"chat_history_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
mime="application/json",
use_container_width=True
)
st.divider()
# Clear controls
if st.button("Clear Chat", use_container_width=True, type="secondary"):
clear_chat_history()
st.success("Chat cleared!")
st.rerun()
# Display chat messages
for message in st.session_state.messages:
with st.chat_message(message["role"]):
# Check if this is an assistant message with attribution
if message["role"] == "assistant" and "Response created by:" in message["content"]:
# Split content and attribution
parts = message["content"].split("\n\n---\n*Response created by:")
main_content = parts[0]
if len(parts) > 1:
model_name = parts[1].replace("***", "").replace("**", "")
st.markdown(main_content)
st.markdown(f"<div class='model-attribution'>Response created by: <strong>{model_name}</strong></div>", unsafe_allow_html=True)
else:
st.markdown(message["content"])
else:
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Chat Smarter. Chat many Brains"):
# Update online status when user sends message
update_online_users()
# Add user message
user_message = {"role": "user", "content": prompt}
st.session_state.messages.append(user_message)
# Auto-save if enabled
if auto_save:
save_chat_history(st.session_state.messages)
# Display user message
with st.chat_message("user"):
st.markdown(prompt)
# Get AI response
with st.chat_message("assistant"):
placeholder = st.empty()
full_response = ""
try:
for response in get_ai_response(st.session_state.messages, selected_model):
full_response = response
placeholder.markdown(full_response + "β")
# Remove cursor and show final response
placeholder.markdown(full_response)
except Exception as e:
error_msg = f"An error occurred: {str(e)}"
placeholder.markdown(error_msg)
full_response = error_msg
# Add AI response to messages with attribution
full_response_with_attribution = full_response + f"\n\n---\n*Response created by: **{model_names[selected_index]}***"
assistant_message = {"role": "assistant", "content": full_response_with_attribution}
st.session_state.messages.append(assistant_message)
# Auto-save if enabled
if auto_save:
save_chat_history(st.session_state.messages)
# Show currently using model
st.caption(f"Currently using: **{model_names[selected_index]}**") |