import streamlit as st import time import random import json from datetime import datetime import pytz import platform import uuid import extra_streamlit_components as stx # Set page config st.set_page_config(page_title="Personalized Real-Time Chat", page_icon="💬", layout="wide") # Initialize cookie manager cookie_manager = stx.CookieManager() # File to store chat history and user data CHAT_FILE = "chat_history.txt" # Function to save chat history and user data to file def save_data(): with open(CHAT_FILE, 'w') as f: json.dump({ 'messages': st.session_state.messages, 'users': st.session_state.users }, f) # Function to load chat history and user data from file def load_data(): try: with open(CHAT_FILE, 'r') as f: data = json.load(f) st.session_state.messages = data['messages'] st.session_state.users = data['users'] except FileNotFoundError: st.session_state.messages = [] st.session_state.users = [] # Load data at the start load_data() # Function to get or create user def get_or_create_user(): user_id = cookie_manager.get(cookie='user_id') if not user_id: user_id = str(uuid.uuid4()) cookie_manager.set('user_id', user_id) user = next((u for u in st.session_state.users if u['id'] == user_id), None) if not user: user = { 'id': user_id, 'name': random.choice(['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry']), 'browser': f"{platform.system()} - {st.session_state.get('browser_info', 'Unknown')}" } st.session_state.users.append(user) save_data() return user # Initialize session state if 'messages' not in st.session_state: st.session_state.messages = [] if 'users' not in st.session_state: st.session_state.users = [] if 'current_user' not in st.session_state: st.session_state.current_user = get_or_create_user() # Sidebar for user information and settings with st.sidebar: st.title("User Info") st.write(f"Current User: {st.session_state.current_user['name']}") st.write(f"Browser: {st.session_state.current_user['browser']}") new_name = st.text_input("Change your name:") if st.button("Update Name"): if new_name: for user in st.session_state.users: if user['id'] == st.session_state.current_user['id']: user['name'] = new_name st.session_state.current_user['name'] = new_name save_data() st.success(f"Name updated to {new_name}") break st.title("Active Users") for user in st.session_state.users: st.write(f"{user['name']} ({user['browser']})") # Main chat area st.title("Personalized Real-Time Chat") # Display chat messages chat_container = st.container() # Input for new message new_message = st.text_input("Type your message:") if st.button("Send"): if new_message: timestamp = datetime.now(pytz.utc).strftime('%Y-%m-%d %H:%M:%S %Z') st.session_state.messages.append({ 'user': st.session_state.current_user['name'], 'message': new_message, 'timestamp': timestamp }) save_data() st.experimental_rerun() # Function to display chat messages def display_messages(): for msg in st.session_state.messages: with chat_container.container(): st.write(f"**{msg['user']}** ({msg['timestamp']}): {msg['message']}") # Display messages display_messages() # Polling for updates if st.button("Refresh Chat"): load_data() st.experimental_rerun() # Auto-refresh (note: this will refresh the entire app) time.sleep(5) st.experimental_rerun()