Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import time
|
3 |
+
import random
|
4 |
+
import json
|
5 |
+
from datetime import datetime
|
6 |
+
import pytz
|
7 |
+
import platform
|
8 |
+
import uuid
|
9 |
+
import extra_streamlit_components as stx
|
10 |
+
|
11 |
+
# Set page config
|
12 |
+
st.set_page_config(page_title="Personalized Real-Time Chat", page_icon="💬", layout="wide")
|
13 |
+
|
14 |
+
# Initialize cookie manager
|
15 |
+
cookie_manager = stx.CookieManager()
|
16 |
+
|
17 |
+
# File to store chat history and user data
|
18 |
+
CHAT_FILE = "chat_history.txt"
|
19 |
+
|
20 |
+
# Function to save chat history and user data to file
|
21 |
+
def save_data():
|
22 |
+
with open(CHAT_FILE, 'w') as f:
|
23 |
+
json.dump({
|
24 |
+
'messages': st.session_state.messages,
|
25 |
+
'users': st.session_state.users
|
26 |
+
}, f)
|
27 |
+
|
28 |
+
# Function to load chat history and user data from file
|
29 |
+
def load_data():
|
30 |
+
try:
|
31 |
+
with open(CHAT_FILE, 'r') as f:
|
32 |
+
data = json.load(f)
|
33 |
+
st.session_state.messages = data['messages']
|
34 |
+
st.session_state.users = data['users']
|
35 |
+
except FileNotFoundError:
|
36 |
+
st.session_state.messages = []
|
37 |
+
st.session_state.users = []
|
38 |
+
|
39 |
+
# Load data at the start
|
40 |
+
load_data()
|
41 |
+
|
42 |
+
# Function to get or create user
|
43 |
+
def get_or_create_user():
|
44 |
+
user_id = cookie_manager.get(cookie='user_id')
|
45 |
+
if not user_id:
|
46 |
+
user_id = str(uuid.uuid4())
|
47 |
+
cookie_manager.set('user_id', user_id)
|
48 |
+
|
49 |
+
user = next((u for u in st.session_state.users if u['id'] == user_id), None)
|
50 |
+
if not user:
|
51 |
+
user = {
|
52 |
+
'id': user_id,
|
53 |
+
'name': random.choice(['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Henry']),
|
54 |
+
'browser': f"{platform.system()} - {st.session_state.get('browser_info', 'Unknown')}"
|
55 |
+
}
|
56 |
+
st.session_state.users.append(user)
|
57 |
+
save_data()
|
58 |
+
|
59 |
+
return user
|
60 |
+
|
61 |
+
# Initialize session state
|
62 |
+
if 'messages' not in st.session_state:
|
63 |
+
st.session_state.messages = []
|
64 |
+
if 'users' not in st.session_state:
|
65 |
+
st.session_state.users = []
|
66 |
+
if 'current_user' not in st.session_state:
|
67 |
+
st.session_state.current_user = get_or_create_user()
|
68 |
+
|
69 |
+
# Sidebar for user information and settings
|
70 |
+
with st.sidebar:
|
71 |
+
st.title("User Info")
|
72 |
+
st.write(f"Current User: {st.session_state.current_user['name']}")
|
73 |
+
st.write(f"Browser: {st.session_state.current_user['browser']}")
|
74 |
+
|
75 |
+
new_name = st.text_input("Change your name:")
|
76 |
+
if st.button("Update Name"):
|
77 |
+
if new_name:
|
78 |
+
for user in st.session_state.users:
|
79 |
+
if user['id'] == st.session_state.current_user['id']:
|
80 |
+
user['name'] = new_name
|
81 |
+
st.session_state.current_user['name'] = new_name
|
82 |
+
save_data()
|
83 |
+
st.success(f"Name updated to {new_name}")
|
84 |
+
break
|
85 |
+
|
86 |
+
st.title("Active Users")
|
87 |
+
for user in st.session_state.users:
|
88 |
+
st.write(f"{user['name']} ({user['browser']})")
|
89 |
+
|
90 |
+
# Main chat area
|
91 |
+
st.title("Personalized Real-Time Chat")
|
92 |
+
|
93 |
+
# Display chat messages
|
94 |
+
chat_container = st.container()
|
95 |
+
|
96 |
+
# Input for new message
|
97 |
+
new_message = st.text_input("Type your message:")
|
98 |
+
if st.button("Send"):
|
99 |
+
if new_message:
|
100 |
+
timestamp = datetime.now(pytz.utc).strftime('%Y-%m-%d %H:%M:%S %Z')
|
101 |
+
st.session_state.messages.append({
|
102 |
+
'user': st.session_state.current_user['name'],
|
103 |
+
'message': new_message,
|
104 |
+
'timestamp': timestamp
|
105 |
+
})
|
106 |
+
save_data()
|
107 |
+
st.experimental_rerun()
|
108 |
+
|
109 |
+
# Function to display chat messages
|
110 |
+
def display_messages():
|
111 |
+
for msg in st.session_state.messages:
|
112 |
+
with chat_container.container():
|
113 |
+
st.write(f"**{msg['user']}** ({msg['timestamp']}): {msg['message']}")
|
114 |
+
|
115 |
+
# Display messages
|
116 |
+
display_messages()
|
117 |
+
|
118 |
+
# Polling for updates
|
119 |
+
if st.button("Refresh Chat"):
|
120 |
+
load_data()
|
121 |
+
st.experimental_rerun()
|
122 |
+
|
123 |
+
# Auto-refresh (note: this will refresh the entire app)
|
124 |
+
time.sleep(5)
|
125 |
+
st.experimental_rerun()
|