Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -6,45 +6,221 @@ from datetime import datetime, timedelta | |
| 6 | 
             
            import time
         | 
| 7 | 
             
            import uuid
         | 
| 8 |  | 
| 9 | 
            -
            # Page configuration
         | 
| 10 | 
             
            st.set_page_config(
         | 
| 11 | 
             
                page_title="Chat Flow π·",
         | 
| 12 | 
             
                page_icon="π¬",
         | 
| 13 | 
            -
                initial_sidebar_state="collapsed | 
|  | |
| 14 | 
             
            )
         | 
| 15 |  | 
| 16 | 
            -
            #  | 
| 17 | 
             
            st.markdown("""
         | 
| 18 | 
             
            <style>
         | 
|  | |
| 19 | 
             
                .stApp {
         | 
| 20 | 
            -
                    background:  | 
|  | |
| 21 | 
             
                }
         | 
| 22 |  | 
|  | |
| 23 | 
             
                .main .block-container {
         | 
| 24 | 
            -
                    max-width:  | 
|  | |
|  | |
|  | |
| 25 | 
             
                }
         | 
| 26 |  | 
|  | |
| 27 | 
             
                #MainMenu {visibility: hidden;}
         | 
| 28 | 
             
                footer {visibility: hidden;}
         | 
| 29 | 
             
                header {visibility: hidden;}
         | 
| 30 | 
             
                .stDeployButton {display: none;}
         | 
| 31 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 32 | 
             
                .model-id {
         | 
| 33 | 
             
                    color: #28a745;
         | 
| 34 | 
             
                    font-family: monospace;
         | 
|  | |
| 35 | 
             
                }
         | 
| 36 |  | 
| 37 | 
             
                .model-attribution {
         | 
| 38 | 
             
                    color: #28a745;
         | 
| 39 | 
             
                    font-size: 0.8em;
         | 
| 40 | 
             
                    font-style: italic;
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 41 | 
             
                }
         | 
| 42 | 
             
            </style>
         | 
| 43 | 
             
            """, unsafe_allow_html=True)
         | 
| 44 |  | 
| 45 | 
             
            # File to store chat history
         | 
| 46 | 
             
            HISTORY_FILE = "chat_history.json"
         | 
| 47 | 
            -
            #  | 
| 48 | 
             
            USERS_FILE = "online_users.json"
         | 
| 49 |  | 
| 50 | 
             
            def load_chat_history():
         | 
| @@ -74,27 +250,25 @@ def clear_chat_history(): | |
| 74 | 
             
                except Exception as e:
         | 
| 75 | 
             
                    st.error(f"Error clearing chat history: {e}")
         | 
| 76 |  | 
| 77 | 
            -
            #  | 
| 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] | 
| 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}' | 
| 98 | 
             
                    }
         | 
| 99 |  | 
| 100 | 
             
                    # Remove users not seen in last 5 minutes
         | 
| @@ -105,13 +279,12 @@ def update_online_users(): | |
| 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 | 
| 115 |  | 
| 116 | 
             
            def get_online_count():
         | 
| 117 | 
             
                """Get number of people currently online"""
         | 
| @@ -122,7 +295,6 @@ def get_online_count(): | |
| 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():
         | 
| @@ -161,11 +333,10 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"): | |
| 161 | 
             
                headers = {
         | 
| 162 | 
             
                    "Content-Type": "application/json",
         | 
| 163 | 
             
                    "Authorization": f"Bearer {OPENROUTER_API_KEY}",
         | 
| 164 | 
            -
                    "HTTP-Referer": "http://localhost:8501", | 
| 165 | 
            -
                    "X-Title": "Streamlit AI Assistant" | 
| 166 | 
             
                }
         | 
| 167 |  | 
| 168 | 
            -
                # Create system message and user messages
         | 
| 169 | 
             
                api_messages = [{"role": "system", "content": "You are a helpful AI assistant. Provide clear and helpful responses."}]
         | 
| 170 | 
             
                api_messages.extend(messages)
         | 
| 171 |  | 
| @@ -183,7 +354,6 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"): | |
| 183 | 
             
                try:
         | 
| 184 | 
             
                    response = requests.post(url, headers=headers, json=data, stream=True, timeout=60)
         | 
| 185 |  | 
| 186 | 
            -
                    # Better error handling
         | 
| 187 | 
             
                    if response.status_code != 200:
         | 
| 188 | 
             
                        error_detail = ""
         | 
| 189 | 
             
                        try:
         | 
| @@ -196,12 +366,9 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"): | |
| 196 | 
             
                        return
         | 
| 197 |  | 
| 198 | 
             
                    full_response = ""
         | 
| 199 | 
            -
                    buffer = ""
         | 
| 200 |  | 
| 201 | 
            -
                    # Using your working streaming logic
         | 
| 202 | 
             
                    for line in response.iter_lines():
         | 
| 203 | 
             
                        if line:
         | 
| 204 | 
            -
                            # The server sends lines starting with "data: ..."
         | 
| 205 | 
             
                            if line.startswith(b"data: "):
         | 
| 206 | 
             
                                data_str = line[len(b"data: "):].decode("utf-8")
         | 
| 207 | 
             
                                if data_str.strip() == "[DONE]":
         | 
| @@ -226,46 +393,43 @@ def get_ai_response(messages, model="openai/gpt-3.5-turbo"): | |
| 226 | 
             
                except Exception as e:
         | 
| 227 | 
             
                    yield f"Unexpected error: {str(e)}. Please try again or contact support."
         | 
| 228 |  | 
| 229 | 
            -
            #  | 
| 230 | 
            -
            st. | 
| 231 | 
            -
             | 
|  | |
|  | |
| 232 |  | 
| 233 | 
            -
            #  | 
| 234 | 
             
            with st.sidebar:
         | 
| 235 | 
            -
                st.header("Settings")
         | 
| 236 |  | 
| 237 | 
            -
                # API Status
         | 
| 238 | 
             
                status = check_api_status()
         | 
| 239 | 
             
                if status == "Connected":
         | 
| 240 | 
             
                    st.success("π’ API Connected")
         | 
| 241 | 
             
                elif status == "No API Key":
         | 
| 242 | 
            -
                    st.error("No API Key")
         | 
| 243 | 
             
                else:
         | 
| 244 | 
            -
                    st.warning("Connection Issue")
         | 
| 245 |  | 
| 246 | 
             
                st.divider()
         | 
| 247 |  | 
| 248 | 
            -
                #  | 
| 249 | 
            -
                st.header("π₯  | 
| 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 | 
            -
                 | 
| 265 | 
            -
                if st.button("Refresh", use_container_width=True):
         | 
| 266 | 
             
                    st.rerun()
         | 
| 267 |  | 
| 268 | 
            -
                #  | 
| 269 | 
             
                with st.expander("π Debug Info"):
         | 
| 270 | 
             
                    if os.path.exists(USERS_FILE):
         | 
| 271 | 
             
                        with open(USERS_FILE, 'r') as f:
         | 
| @@ -278,11 +442,11 @@ with st.sidebar: | |
| 278 | 
             
                            st.write(f"- {uid}: {minutes_ago} min ago")
         | 
| 279 | 
             
                    else:
         | 
| 280 | 
             
                        st.write("No users file yet")
         | 
| 281 | 
            -
                # === END DEBUG SECTION ===
         | 
| 282 |  | 
| 283 | 
             
                st.divider()
         | 
| 284 |  | 
| 285 | 
            -
                #  | 
|  | |
| 286 | 
             
                models = [
         | 
| 287 | 
             
                    ("GPT-3.5 Turbo", "openai/gpt-3.5-turbo"),
         | 
| 288 | 
             
                    ("LLaMA 3.1 8B", "meta-llama/llama-3.1-8b-instruct"),
         | 
| @@ -299,77 +463,66 @@ with st.sidebar: | |
| 299 | 
             
                model_names = [name for name, _ in models]
         | 
| 300 | 
             
                model_ids = [model_id for _, model_id in models]
         | 
| 301 |  | 
| 302 | 
            -
                selected_index = st.selectbox("Model", range(len(model_names)), 
         | 
| 303 | 
             
                                            format_func=lambda x: model_names[x], 
         | 
| 304 | 
             
                                            index=0)
         | 
| 305 | 
             
                selected_model = model_ids[selected_index]
         | 
| 306 |  | 
| 307 | 
            -
                # Show selected model ID in green
         | 
| 308 | 
             
                st.markdown(f"**Model ID:** <span class='model-id'>{selected_model}</span>", unsafe_allow_html=True)
         | 
| 309 |  | 
| 310 | 
             
                st.divider()
         | 
| 311 |  | 
| 312 | 
            -
                # Chat History Controls
         | 
| 313 | 
            -
                st.header("Chat History")
         | 
| 314 |  | 
| 315 | 
            -
                # Show number of messages
         | 
| 316 | 
             
                if st.session_state.messages:
         | 
| 317 | 
            -
                    st.info(f" | 
| 318 |  | 
| 319 | 
            -
                # Auto-save toggle
         | 
| 320 | 
             
                auto_save = st.checkbox("Auto-save messages", value=True)
         | 
| 321 |  | 
| 322 | 
            -
                #  | 
| 323 | 
             
                col1, col2 = st.columns(2)
         | 
| 324 | 
             
                with col1:
         | 
| 325 | 
            -
                    if st.button("Save | 
| 326 | 
             
                        save_chat_history(st.session_state.messages)
         | 
| 327 | 
            -
                        st.success(" | 
| 328 |  | 
| 329 | 
             
                with col2:
         | 
| 330 | 
            -
                    if st.button("Load | 
| 331 | 
             
                        st.session_state.messages = load_chat_history()
         | 
| 332 | 
            -
                        st.success(" | 
| 333 | 
             
                        st.rerun()
         | 
| 334 |  | 
| 335 | 
            -
                 | 
| 336 | 
            -
                
         | 
| 337 | 
            -
             | 
| 338 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 339 | 
             
                    if os.path.exists(HISTORY_FILE):
         | 
| 340 | 
            -
                        with open(HISTORY_FILE, ' | 
| 341 | 
            -
                             | 
| 342 | 
            -
             | 
| 343 | 
            -
             | 
| 344 | 
            -
             | 
| 345 | 
            -
             | 
| 346 | 
            -
             | 
| 347 | 
            -
             | 
| 348 | 
            -
                     | 
| 349 | 
            -
             | 
| 350 | 
            -
             | 
| 351 | 
            -
             | 
| 352 | 
            -
             | 
| 353 | 
            -
                            mime="application/json",
         | 
| 354 | 
            -
                            use_container_width=True
         | 
| 355 | 
            -
                        )
         | 
| 356 | 
            -
                
         | 
| 357 | 
            -
                st.divider()
         | 
| 358 | 
            -
                
         | 
| 359 | 
            -
                # Clear controls
         | 
| 360 | 
            -
                if st.button("Clear Chat", use_container_width=True, type="secondary"):
         | 
| 361 | 
            -
                    clear_chat_history()
         | 
| 362 | 
            -
                    st.success("Chat cleared!")
         | 
| 363 | 
            -
                    st.rerun()
         | 
| 364 | 
            -
             | 
| 365 | 
            -
            # Show welcome message when no messages
         | 
| 366 |  | 
| 367 | 
            -
            # Display chat messages
         | 
| 368 | 
             
            for message in st.session_state.messages:
         | 
| 369 | 
             
                with st.chat_message(message["role"]):
         | 
| 370 | 
            -
                    # Check if this is an assistant message with attribution
         | 
| 371 | 
             
                    if message["role"] == "assistant" and "Response created by:" in message["content"]:
         | 
| 372 | 
            -
                        # Split content and attribution
         | 
| 373 | 
             
                        parts = message["content"].split("\n\n---\n*Response created by:")
         | 
| 374 | 
             
                        main_content = parts[0]
         | 
| 375 | 
             
                        if len(parts) > 1:
         | 
| @@ -381,24 +534,19 @@ for message in st.session_state.messages: | |
| 381 | 
             
                    else:
         | 
| 382 | 
             
                        st.markdown(message["content"])
         | 
| 383 |  | 
| 384 | 
            -
            # Chat input
         | 
| 385 | 
            -
            if prompt := st.chat_input("Chat  | 
| 386 | 
            -
                # NEW: Update online status when user sends message
         | 
| 387 | 
             
                update_online_users()
         | 
| 388 |  | 
| 389 | 
            -
                # Add user message
         | 
| 390 | 
             
                user_message = {"role": "user", "content": prompt}
         | 
| 391 | 
             
                st.session_state.messages.append(user_message)
         | 
| 392 |  | 
| 393 | 
            -
                # Auto-save if enabled
         | 
| 394 | 
             
                if auto_save:
         | 
| 395 | 
             
                    save_chat_history(st.session_state.messages)
         | 
| 396 |  | 
| 397 | 
            -
                # Display user message
         | 
| 398 | 
             
                with st.chat_message("user"):
         | 
| 399 | 
             
                    st.markdown(prompt)
         | 
| 400 |  | 
| 401 | 
            -
                # Get AI response
         | 
| 402 | 
             
                with st.chat_message("assistant"):
         | 
| 403 | 
             
                    placeholder = st.empty()
         | 
| 404 |  | 
| @@ -408,7 +556,6 @@ if prompt := st.chat_input("Chat Smarter. Chat many Brains"): | |
| 408 | 
             
                            full_response = response
         | 
| 409 | 
             
                            placeholder.markdown(full_response + "β")
         | 
| 410 |  | 
| 411 | 
            -
                        # Remove cursor and show final response
         | 
| 412 | 
             
                        placeholder.markdown(full_response)
         | 
| 413 |  | 
| 414 | 
             
                    except Exception as e:
         | 
| @@ -416,14 +563,15 @@ if prompt := st.chat_input("Chat Smarter. Chat many Brains"): | |
| 416 | 
             
                        placeholder.markdown(error_msg)
         | 
| 417 | 
             
                        full_response = error_msg
         | 
| 418 |  | 
| 419 | 
            -
                # Add AI response to messages with attribution
         | 
| 420 | 
             
                full_response_with_attribution = full_response + f"\n\n---\n*Response created by: **{model_names[selected_index]}***"
         | 
| 421 | 
             
                assistant_message = {"role": "assistant", "content": full_response_with_attribution}
         | 
| 422 | 
             
                st.session_state.messages.append(assistant_message)
         | 
| 423 |  | 
| 424 | 
            -
                # Auto-save if enabled
         | 
| 425 | 
             
                if auto_save:
         | 
| 426 | 
             
                    save_chat_history(st.session_state.messages)
         | 
| 427 |  | 
| 428 | 
            -
            #  | 
| 429 | 
            -
            st. | 
|  | |
|  | |
|  | 
|  | |
| 6 | 
             
            import time
         | 
| 7 | 
             
            import uuid
         | 
| 8 |  | 
| 9 | 
            +
            # Page configuration - IMPROVED for mobile
         | 
| 10 | 
             
            st.set_page_config(
         | 
| 11 | 
             
                page_title="Chat Flow π·",
         | 
| 12 | 
             
                page_icon="π¬",
         | 
| 13 | 
            +
                initial_sidebar_state="auto",  # Changed from collapsed to auto
         | 
| 14 | 
            +
                layout="wide"  # Better for mobile
         | 
| 15 | 
             
            )
         | 
| 16 |  | 
| 17 | 
            +
            # MOBILE-RESPONSIVE CSS
         | 
| 18 | 
             
            st.markdown("""
         | 
| 19 | 
             
            <style>
         | 
| 20 | 
            +
                /* Main app styling */
         | 
| 21 | 
             
                .stApp {
         | 
| 22 | 
            +
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
         | 
| 23 | 
            +
                    min-height: 100vh;
         | 
| 24 | 
             
                }
         | 
| 25 |  | 
| 26 | 
            +
                /* Container responsive sizing */
         | 
| 27 | 
             
                .main .block-container {
         | 
| 28 | 
            +
                    max-width: 100%;
         | 
| 29 | 
            +
                    padding-left: 1rem;
         | 
| 30 | 
            +
                    padding-right: 1rem;
         | 
| 31 | 
            +
                    padding-top: 1rem;
         | 
| 32 | 
             
                }
         | 
| 33 |  | 
| 34 | 
            +
                /* Hide Streamlit branding */
         | 
| 35 | 
             
                #MainMenu {visibility: hidden;}
         | 
| 36 | 
             
                footer {visibility: hidden;}
         | 
| 37 | 
             
                header {visibility: hidden;}
         | 
| 38 | 
             
                .stDeployButton {display: none;}
         | 
| 39 |  | 
| 40 | 
            +
                /* Chat container styling */
         | 
| 41 | 
            +
                .stChatMessage {
         | 
| 42 | 
            +
                    background: rgba(255, 255, 255, 0.95);
         | 
| 43 | 
            +
                    border-radius: 15px;
         | 
| 44 | 
            +
                    margin: 0.5rem 0;
         | 
| 45 | 
            +
                    padding: 1rem;
         | 
| 46 | 
            +
                    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
         | 
| 47 | 
            +
                    backdrop-filter: blur(10px);
         | 
| 48 | 
            +
                }
         | 
| 49 | 
            +
                
         | 
| 50 | 
            +
                /* User messages - right aligned with blue background */
         | 
| 51 | 
            +
                .stChatMessage[data-testid="user-message"] {
         | 
| 52 | 
            +
                    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
         | 
| 53 | 
            +
                    color: white;
         | 
| 54 | 
            +
                    margin-left: 20%;
         | 
| 55 | 
            +
                }
         | 
| 56 | 
            +
                
         | 
| 57 | 
            +
                /* Assistant messages - left aligned with white background */
         | 
| 58 | 
            +
                .stChatMessage[data-testid="assistant-message"] {
         | 
| 59 | 
            +
                    background: rgba(255, 255, 255, 0.98);
         | 
| 60 | 
            +
                    color: #333;
         | 
| 61 | 
            +
                    margin-right: 20%;
         | 
| 62 | 
            +
                }
         | 
| 63 | 
            +
                
         | 
| 64 | 
            +
                /* Mobile responsiveness */
         | 
| 65 | 
            +
                @media (max-width: 768px) {
         | 
| 66 | 
            +
                    .main .block-container {
         | 
| 67 | 
            +
                        padding: 0.5rem;
         | 
| 68 | 
            +
                        max-width: 100%;
         | 
| 69 | 
            +
                    }
         | 
| 70 | 
            +
                    
         | 
| 71 | 
            +
                    .stChatMessage[data-testid="user-message"] {
         | 
| 72 | 
            +
                        margin-left: 10%;
         | 
| 73 | 
            +
                        font-size: 14px;
         | 
| 74 | 
            +
                    }
         | 
| 75 | 
            +
                    
         | 
| 76 | 
            +
                    .stChatMessage[data-testid="assistant-message"] {
         | 
| 77 | 
            +
                        margin-right: 10%;
         | 
| 78 | 
            +
                        font-size: 14px;
         | 
| 79 | 
            +
                    }
         | 
| 80 | 
            +
                    
         | 
| 81 | 
            +
                    /* Make sidebar collapsible on mobile */
         | 
| 82 | 
            +
                    .css-1d391kg {
         | 
| 83 | 
            +
                        width: 100% !important;
         | 
| 84 | 
            +
                        min-width: 100% !important;
         | 
| 85 | 
            +
                    }
         | 
| 86 | 
            +
                    
         | 
| 87 | 
            +
                    /* Header adjustments for mobile */
         | 
| 88 | 
            +
                    h1 {
         | 
| 89 | 
            +
                        font-size: 1.5rem !important;
         | 
| 90 | 
            +
                        text-align: center;
         | 
| 91 | 
            +
                    }
         | 
| 92 | 
            +
                    
         | 
| 93 | 
            +
                    .stSelectbox label {
         | 
| 94 | 
            +
                        font-size: 14px;
         | 
| 95 | 
            +
                    }
         | 
| 96 | 
            +
                    
         | 
| 97 | 
            +
                    .stButton button {
         | 
| 98 | 
            +
                        font-size: 14px;
         | 
| 99 | 
            +
                        padding: 0.5rem;
         | 
| 100 | 
            +
                    }
         | 
| 101 | 
            +
                }
         | 
| 102 | 
            +
                
         | 
| 103 | 
            +
                /* Extra small screens (iPhone 5, etc) */
         | 
| 104 | 
            +
                @media (max-width: 480px) {
         | 
| 105 | 
            +
                    .main .block-container {
         | 
| 106 | 
            +
                        padding: 0.25rem;
         | 
| 107 | 
            +
                    }
         | 
| 108 | 
            +
                    
         | 
| 109 | 
            +
                    .stChatMessage {
         | 
| 110 | 
            +
                        padding: 0.75rem;
         | 
| 111 | 
            +
                        margin: 0.25rem 0;
         | 
| 112 | 
            +
                    }
         | 
| 113 | 
            +
                    
         | 
| 114 | 
            +
                    .stChatMessage[data-testid="user-message"] {
         | 
| 115 | 
            +
                        margin-left: 5%;
         | 
| 116 | 
            +
                    }
         | 
| 117 | 
            +
                    
         | 
| 118 | 
            +
                    .stChatMessage[data-testid="assistant-message"] {
         | 
| 119 | 
            +
                        margin-right: 5%;
         | 
| 120 | 
            +
                    }
         | 
| 121 | 
            +
                    
         | 
| 122 | 
            +
                    h1 {
         | 
| 123 | 
            +
                        font-size: 1.2rem !important;
         | 
| 124 | 
            +
                    }
         | 
| 125 | 
            +
                    
         | 
| 126 | 
            +
                    .stChatInput {
         | 
| 127 | 
            +
                        font-size: 16px; /* Prevents zoom on iOS */
         | 
| 128 | 
            +
                    }
         | 
| 129 | 
            +
                }
         | 
| 130 | 
            +
                
         | 
| 131 | 
            +
                /* Chat input styling */
         | 
| 132 | 
            +
                .stChatInput {
         | 
| 133 | 
            +
                    background: rgba(255, 255, 255, 0.9);
         | 
| 134 | 
            +
                    border-radius: 25px;
         | 
| 135 | 
            +
                    border: 2px solid rgba(255, 255, 255, 0.3);
         | 
| 136 | 
            +
                    backdrop-filter: blur(10px);
         | 
| 137 | 
            +
                }
         | 
| 138 | 
            +
                
         | 
| 139 | 
            +
                .stChatInput input {
         | 
| 140 | 
            +
                    background: transparent;
         | 
| 141 | 
            +
                    border: none;
         | 
| 142 | 
            +
                    color: #333;
         | 
| 143 | 
            +
                }
         | 
| 144 | 
            +
                
         | 
| 145 | 
            +
                .stChatInput input::placeholder {
         | 
| 146 | 
            +
                    color: #666;
         | 
| 147 | 
            +
                }
         | 
| 148 | 
            +
                
         | 
| 149 | 
            +
                /* Sidebar improvements */
         | 
| 150 | 
            +
                .css-1d391kg {
         | 
| 151 | 
            +
                    background: rgba(255, 255, 255, 0.95);
         | 
| 152 | 
            +
                    backdrop-filter: blur(15px);
         | 
| 153 | 
            +
                }
         | 
| 154 | 
            +
                
         | 
| 155 | 
            +
                /* Model info styling */
         | 
| 156 | 
             
                .model-id {
         | 
| 157 | 
             
                    color: #28a745;
         | 
| 158 | 
             
                    font-family: monospace;
         | 
| 159 | 
            +
                    font-weight: bold;
         | 
| 160 | 
             
                }
         | 
| 161 |  | 
| 162 | 
             
                .model-attribution {
         | 
| 163 | 
             
                    color: #28a745;
         | 
| 164 | 
             
                    font-size: 0.8em;
         | 
| 165 | 
             
                    font-style: italic;
         | 
| 166 | 
            +
                    text-align: right;
         | 
| 167 | 
            +
                    margin-top: 0.5rem;
         | 
| 168 | 
            +
                    padding-top: 0.5rem;
         | 
| 169 | 
            +
                    border-top: 1px solid #eee;
         | 
| 170 | 
            +
                }
         | 
| 171 | 
            +
                
         | 
| 172 | 
            +
                /* Status indicators */
         | 
| 173 | 
            +
                .online-indicator {
         | 
| 174 | 
            +
                    display: inline-block;
         | 
| 175 | 
            +
                    width: 10px;
         | 
| 176 | 
            +
                    height: 10px;
         | 
| 177 | 
            +
                    background: #28a745;
         | 
| 178 | 
            +
                    border-radius: 50%;
         | 
| 179 | 
            +
                    margin-right: 5px;
         | 
| 180 | 
            +
                }
         | 
| 181 | 
            +
                
         | 
| 182 | 
            +
                /* Button improvements */
         | 
| 183 | 
            +
                .stButton button {
         | 
| 184 | 
            +
                    border-radius: 10px;
         | 
| 185 | 
            +
                    border: none;
         | 
| 186 | 
            +
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
         | 
| 187 | 
            +
                    color: white;
         | 
| 188 | 
            +
                    transition: all 0.3s;
         | 
| 189 | 
            +
                }
         | 
| 190 | 
            +
                
         | 
| 191 | 
            +
                .stButton button:hover {
         | 
| 192 | 
            +
                    transform: translateY(-2px);
         | 
| 193 | 
            +
                    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
         | 
| 194 | 
            +
                }
         | 
| 195 | 
            +
                
         | 
| 196 | 
            +
                /* Title styling */
         | 
| 197 | 
            +
                h1 {
         | 
| 198 | 
            +
                    color: white;
         | 
| 199 | 
            +
                    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
         | 
| 200 | 
            +
                    margin-bottom: 0;
         | 
| 201 | 
            +
                }
         | 
| 202 | 
            +
                
         | 
| 203 | 
            +
                .stCaption {
         | 
| 204 | 
            +
                    color: rgba(255, 255, 255, 0.8);
         | 
| 205 | 
            +
                    text-align: center;
         | 
| 206 | 
            +
                }
         | 
| 207 | 
            +
                
         | 
| 208 | 
            +
                /* Responsive text sizing */
         | 
| 209 | 
            +
                @media (max-width: 600px) {
         | 
| 210 | 
            +
                    .stMarkdown {
         | 
| 211 | 
            +
                        font-size: 14px;
         | 
| 212 | 
            +
                    }
         | 
| 213 | 
            +
                    
         | 
| 214 | 
            +
                    .stCaption {
         | 
| 215 | 
            +
                        font-size: 12px;
         | 
| 216 | 
            +
                    }
         | 
| 217 | 
             
                }
         | 
| 218 | 
             
            </style>
         | 
| 219 | 
             
            """, unsafe_allow_html=True)
         | 
| 220 |  | 
| 221 | 
             
            # File to store chat history
         | 
| 222 | 
             
            HISTORY_FILE = "chat_history.json"
         | 
| 223 | 
            +
            # File to store online users
         | 
| 224 | 
             
            USERS_FILE = "online_users.json"
         | 
| 225 |  | 
| 226 | 
             
            def load_chat_history():
         | 
|  | |
| 250 | 
             
                except Exception as e:
         | 
| 251 | 
             
                    st.error(f"Error clearing chat history: {e}")
         | 
| 252 |  | 
| 253 | 
            +
            # User tracking functions
         | 
| 254 | 
             
            def get_user_id():
         | 
| 255 | 
             
                """Get unique ID for this user session"""
         | 
| 256 | 
             
                if 'user_id' not in st.session_state:
         | 
| 257 | 
            +
                    st.session_state.user_id = str(uuid.uuid4())[:8]
         | 
| 258 | 
             
                return st.session_state.user_id
         | 
| 259 |  | 
| 260 | 
             
            def update_online_users():
         | 
| 261 | 
             
                """Update that this user is online right now"""
         | 
| 262 | 
             
                try:
         | 
|  | |
| 263 | 
             
                    users = {}
         | 
| 264 | 
             
                    if os.path.exists(USERS_FILE):
         | 
| 265 | 
             
                        with open(USERS_FILE, 'r') as f:
         | 
| 266 | 
             
                            users = json.load(f)
         | 
| 267 |  | 
|  | |
| 268 | 
             
                    user_id = get_user_id()
         | 
| 269 | 
             
                    users[user_id] = {
         | 
| 270 | 
             
                        'last_seen': datetime.now().isoformat(),
         | 
| 271 | 
            +
                        'name': f'User-{user_id}'
         | 
| 272 | 
             
                    }
         | 
| 273 |  | 
| 274 | 
             
                    # Remove users not seen in last 5 minutes
         | 
|  | |
| 279 | 
             
                        if current_time - last_seen < timedelta(minutes=5):
         | 
| 280 | 
             
                            active_users[uid] = data
         | 
| 281 |  | 
|  | |
| 282 | 
             
                    with open(USERS_FILE, 'w') as f:
         | 
| 283 | 
             
                        json.dump(active_users, f, indent=2)
         | 
| 284 |  | 
| 285 | 
             
                    return len(active_users)
         | 
| 286 | 
             
                except Exception:
         | 
| 287 | 
            +
                    return 1
         | 
| 288 |  | 
| 289 | 
             
            def get_online_count():
         | 
| 290 | 
             
                """Get number of people currently online"""
         | 
|  | |
| 295 | 
             
                    with open(USERS_FILE, 'r') as f:
         | 
| 296 | 
             
                        users = json.load(f)
         | 
| 297 |  | 
|  | |
| 298 | 
             
                    current_time = datetime.now()
         | 
| 299 | 
             
                    active_count = 0
         | 
| 300 | 
             
                    for data in users.values():
         | 
|  | |
| 333 | 
             
                headers = {
         | 
| 334 | 
             
                    "Content-Type": "application/json",
         | 
| 335 | 
             
                    "Authorization": f"Bearer {OPENROUTER_API_KEY}",
         | 
| 336 | 
            +
                    "HTTP-Referer": "http://localhost:8501",
         | 
| 337 | 
            +
                    "X-Title": "Streamlit AI Assistant"
         | 
| 338 | 
             
                }
         | 
| 339 |  | 
|  | |
| 340 | 
             
                api_messages = [{"role": "system", "content": "You are a helpful AI assistant. Provide clear and helpful responses."}]
         | 
| 341 | 
             
                api_messages.extend(messages)
         | 
| 342 |  | 
|  | |
| 354 | 
             
                try:
         | 
| 355 | 
             
                    response = requests.post(url, headers=headers, json=data, stream=True, timeout=60)
         | 
| 356 |  | 
|  | |
| 357 | 
             
                    if response.status_code != 200:
         | 
| 358 | 
             
                        error_detail = ""
         | 
| 359 | 
             
                        try:
         | 
|  | |
| 366 | 
             
                        return
         | 
| 367 |  | 
| 368 | 
             
                    full_response = ""
         | 
|  | |
| 369 |  | 
|  | |
| 370 | 
             
                    for line in response.iter_lines():
         | 
| 371 | 
             
                        if line:
         | 
|  | |
| 372 | 
             
                            if line.startswith(b"data: "):
         | 
| 373 | 
             
                                data_str = line[len(b"data: "):].decode("utf-8")
         | 
| 374 | 
             
                                if data_str.strip() == "[DONE]":
         | 
|  | |
| 393 | 
             
                except Exception as e:
         | 
| 394 | 
             
                    yield f"Unexpected error: {str(e)}. Please try again or contact support."
         | 
| 395 |  | 
| 396 | 
            +
            # MAIN UI LAYOUT - Mobile First Approach
         | 
| 397 | 
            +
            col1, col2, col3 = st.columns([1, 2, 1])
         | 
| 398 | 
            +
            with col2:
         | 
| 399 | 
            +
                st.title("Chat Flow π·")
         | 
| 400 | 
            +
                st.caption("10 powerful Models, one simple chat.")
         | 
| 401 |  | 
| 402 | 
            +
            # Mobile-friendly sidebar
         | 
| 403 | 
             
            with st.sidebar:
         | 
| 404 | 
            +
                st.header("βοΈ Settings")
         | 
| 405 |  | 
| 406 | 
            +
                # API Status with better mobile display
         | 
| 407 | 
             
                status = check_api_status()
         | 
| 408 | 
             
                if status == "Connected":
         | 
| 409 | 
             
                    st.success("π’ API Connected")
         | 
| 410 | 
             
                elif status == "No API Key":
         | 
| 411 | 
            +
                    st.error("β No API Key")
         | 
| 412 | 
             
                else:
         | 
| 413 | 
            +
                    st.warning("β οΈ Connection Issue")
         | 
| 414 |  | 
| 415 | 
             
                st.divider()
         | 
| 416 |  | 
| 417 | 
            +
                # Online Users Section - Mobile Optimized
         | 
| 418 | 
            +
                st.header("π₯ Live Users")
         | 
|  | |
|  | |
| 419 | 
             
                online_count = update_online_users()
         | 
| 420 |  | 
|  | |
| 421 | 
             
                if online_count == 1:
         | 
| 422 | 
             
                    st.info("π’ Just you online")
         | 
| 423 | 
             
                else:
         | 
| 424 | 
             
                    st.success(f"π’ {online_count} people online")
         | 
| 425 |  | 
|  | |
| 426 | 
             
                your_id = get_user_id()
         | 
| 427 | 
             
                st.caption(f"You: User-{your_id}")
         | 
| 428 |  | 
| 429 | 
            +
                if st.button("π Refresh", use_container_width=True):
         | 
|  | |
| 430 | 
             
                    st.rerun()
         | 
| 431 |  | 
| 432 | 
            +
                # Debug section - collapsible for mobile
         | 
| 433 | 
             
                with st.expander("π Debug Info"):
         | 
| 434 | 
             
                    if os.path.exists(USERS_FILE):
         | 
| 435 | 
             
                        with open(USERS_FILE, 'r') as f:
         | 
|  | |
| 442 | 
             
                            st.write(f"- {uid}: {minutes_ago} min ago")
         | 
| 443 | 
             
                    else:
         | 
| 444 | 
             
                        st.write("No users file yet")
         | 
|  | |
| 445 |  | 
| 446 | 
             
                st.divider()
         | 
| 447 |  | 
| 448 | 
            +
                # Model Selection - Mobile Optimized
         | 
| 449 | 
            +
                st.header("π€ AI Models")
         | 
| 450 | 
             
                models = [
         | 
| 451 | 
             
                    ("GPT-3.5 Turbo", "openai/gpt-3.5-turbo"),
         | 
| 452 | 
             
                    ("LLaMA 3.1 8B", "meta-llama/llama-3.1-8b-instruct"),
         | 
|  | |
| 463 | 
             
                model_names = [name for name, _ in models]
         | 
| 464 | 
             
                model_ids = [model_id for _, model_id in models]
         | 
| 465 |  | 
| 466 | 
            +
                selected_index = st.selectbox("Choose Model", range(len(model_names)), 
         | 
| 467 | 
             
                                            format_func=lambda x: model_names[x], 
         | 
| 468 | 
             
                                            index=0)
         | 
| 469 | 
             
                selected_model = model_ids[selected_index]
         | 
| 470 |  | 
|  | |
| 471 | 
             
                st.markdown(f"**Model ID:** <span class='model-id'>{selected_model}</span>", unsafe_allow_html=True)
         | 
| 472 |  | 
| 473 | 
             
                st.divider()
         | 
| 474 |  | 
| 475 | 
            +
                # Chat History Controls - Mobile Friendly
         | 
| 476 | 
            +
                st.header("π¬ Chat History")
         | 
| 477 |  | 
|  | |
| 478 | 
             
                if st.session_state.messages:
         | 
| 479 | 
            +
                    st.info(f"π {len(st.session_state.messages)} messages stored")
         | 
| 480 |  | 
|  | |
| 481 | 
             
                auto_save = st.checkbox("Auto-save messages", value=True)
         | 
| 482 |  | 
| 483 | 
            +
                # Compact buttons for mobile
         | 
| 484 | 
             
                col1, col2 = st.columns(2)
         | 
| 485 | 
             
                with col1:
         | 
| 486 | 
            +
                    if st.button("πΎ Save", use_container_width=True):
         | 
| 487 | 
             
                        save_chat_history(st.session_state.messages)
         | 
| 488 | 
            +
                        st.success("Saved!")
         | 
| 489 |  | 
| 490 | 
             
                with col2:
         | 
| 491 | 
            +
                    if st.button("π Load", use_container_width=True):
         | 
| 492 | 
             
                        st.session_state.messages = load_chat_history()
         | 
| 493 | 
            +
                        st.success("Loaded!")
         | 
| 494 | 
             
                        st.rerun()
         | 
| 495 |  | 
| 496 | 
            +
                # Additional controls in expander to save space
         | 
| 497 | 
            +
                with st.expander("More Options"):
         | 
| 498 | 
            +
                    if st.button("ποΈ View History File", use_container_width=True):
         | 
| 499 | 
            +
                        if os.path.exists(HISTORY_FILE):
         | 
| 500 | 
            +
                            with open(HISTORY_FILE, 'r', encoding='utf-8') as f:
         | 
| 501 | 
            +
                                history_content = f.read()
         | 
| 502 | 
            +
                            st.text_area("Chat History (JSON)", history_content, height=150)
         | 
| 503 | 
            +
                        else:
         | 
| 504 | 
            +
                            st.warning("No history file found")
         | 
| 505 | 
            +
                    
         | 
| 506 | 
            +
                    # Download History
         | 
| 507 | 
             
                    if os.path.exists(HISTORY_FILE):
         | 
| 508 | 
            +
                        with open(HISTORY_FILE, 'rb') as f:
         | 
| 509 | 
            +
                            st.download_button(
         | 
| 510 | 
            +
                                label="β¬οΈ Download History",
         | 
| 511 | 
            +
                                data=f.read(),
         | 
| 512 | 
            +
                                file_name=f"chat_history_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
         | 
| 513 | 
            +
                                mime="application/json",
         | 
| 514 | 
            +
                                use_container_width=True
         | 
| 515 | 
            +
                            )
         | 
| 516 | 
            +
                    
         | 
| 517 | 
            +
                    if st.button("ποΈ Clear Chat", use_container_width=True, type="secondary"):
         | 
| 518 | 
            +
                        clear_chat_history()
         | 
| 519 | 
            +
                        st.success("Chat cleared!")
         | 
| 520 | 
            +
                        st.rerun()
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 521 |  | 
| 522 | 
            +
            # Display chat messages with responsive design
         | 
| 523 | 
             
            for message in st.session_state.messages:
         | 
| 524 | 
             
                with st.chat_message(message["role"]):
         | 
|  | |
| 525 | 
             
                    if message["role"] == "assistant" and "Response created by:" in message["content"]:
         | 
|  | |
| 526 | 
             
                        parts = message["content"].split("\n\n---\n*Response created by:")
         | 
| 527 | 
             
                        main_content = parts[0]
         | 
| 528 | 
             
                        if len(parts) > 1:
         | 
|  | |
| 534 | 
             
                    else:
         | 
| 535 | 
             
                        st.markdown(message["content"])
         | 
| 536 |  | 
| 537 | 
            +
            # Chat input with mobile-friendly placeholder
         | 
| 538 | 
            +
            if prompt := st.chat_input("π¬ Chat with AI... (works on all devices!)"):
         | 
|  | |
| 539 | 
             
                update_online_users()
         | 
| 540 |  | 
|  | |
| 541 | 
             
                user_message = {"role": "user", "content": prompt}
         | 
| 542 | 
             
                st.session_state.messages.append(user_message)
         | 
| 543 |  | 
|  | |
| 544 | 
             
                if auto_save:
         | 
| 545 | 
             
                    save_chat_history(st.session_state.messages)
         | 
| 546 |  | 
|  | |
| 547 | 
             
                with st.chat_message("user"):
         | 
| 548 | 
             
                    st.markdown(prompt)
         | 
| 549 |  | 
|  | |
| 550 | 
             
                with st.chat_message("assistant"):
         | 
| 551 | 
             
                    placeholder = st.empty()
         | 
| 552 |  | 
|  | |
| 556 | 
             
                            full_response = response
         | 
| 557 | 
             
                            placeholder.markdown(full_response + "β")
         | 
| 558 |  | 
|  | |
| 559 | 
             
                        placeholder.markdown(full_response)
         | 
| 560 |  | 
| 561 | 
             
                    except Exception as e:
         | 
|  | |
| 563 | 
             
                        placeholder.markdown(error_msg)
         | 
| 564 | 
             
                        full_response = error_msg
         | 
| 565 |  | 
|  | |
| 566 | 
             
                full_response_with_attribution = full_response + f"\n\n---\n*Response created by: **{model_names[selected_index]}***"
         | 
| 567 | 
             
                assistant_message = {"role": "assistant", "content": full_response_with_attribution}
         | 
| 568 | 
             
                st.session_state.messages.append(assistant_message)
         | 
| 569 |  | 
|  | |
| 570 | 
             
                if auto_save:
         | 
| 571 | 
             
                    save_chat_history(st.session_state.messages)
         | 
| 572 |  | 
| 573 | 
            +
            # Mobile-friendly footer
         | 
| 574 | 
            +
            col1, col2, col3 = st.columns([1, 2, 1])
         | 
| 575 | 
            +
            with col2:
         | 
| 576 | 
            +
                st.caption(f"Currently using: **{model_names[selected_index]}**")
         | 
| 577 | 
            +
                st.caption("π± Optimized for all mobile devices")
         |