Update app.py
Browse files
app.py
CHANGED
|
@@ -1,419 +1,215 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import websockets
|
| 4 |
-
import uuid
|
| 5 |
-
import argparse
|
| 6 |
-
from datetime import datetime
|
| 7 |
import os
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
import
|
| 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 |
-
"Every age unfolds a new lesson. Life's chapters evolve, each teaching us anew.",
|
| 128 |
-
"From infancy to twilight, our journey is painted in growth. Every stage shines with its own wisdom.",
|
| 129 |
-
"Love is the universal language, transcending boundaries and touching souls.",
|
| 130 |
-
"Through love, we find connection, unity, and the essence of existence."
|
| 131 |
-
]
|
| 132 |
-
quotes = famous_quotes if source == "famous" else custom_quotes
|
| 133 |
-
return quotes if quotes else ["No quotes available - check back later! π"]
|
| 134 |
-
|
| 135 |
-
# Vote saver - the tally keeper counting thumbs up! ππ
|
| 136 |
-
def save_vote(file, item, user_hash):
|
| 137 |
-
"""βοΈ Tallies a vote in the grand ledger - your opinion matters! π³οΈ"""
|
| 138 |
-
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 139 |
-
entry = f"[{timestamp}] {user_hash} voted for {item}"
|
| 140 |
-
try:
|
| 141 |
-
with open(file, 'a') as f:
|
| 142 |
-
f.write(f"{entry}\n")
|
| 143 |
-
with open(HISTORY_FILE, 'a') as f:
|
| 144 |
-
f.write(f"- {timestamp} - User {user_hash} voted for {item}\n")
|
| 145 |
-
return True
|
| 146 |
-
except Exception as e:
|
| 147 |
-
print(f"Vote save flop: {e}")
|
| 148 |
-
return False
|
| 149 |
-
|
| 150 |
-
# Vote loader - the scorekeeper tallying the crowdβs cheers! ππ
|
| 151 |
-
def load_votes(file):
|
| 152 |
-
"""π Counts the votes from the ledger - whoβs winning the popularity contest? π"""
|
| 153 |
-
if not os.path.exists(file):
|
| 154 |
-
with open(file, 'w') as f:
|
| 155 |
-
f.write("# Vote Tally\n\nNo votes yet - get clicking! π±οΈ\n")
|
| 156 |
-
try:
|
| 157 |
-
with open(file, 'r') as f:
|
| 158 |
-
lines = f.read().strip().split('\n')
|
| 159 |
-
votes = {}
|
| 160 |
-
for line in lines[2:]: # Skip header
|
| 161 |
-
if line.strip() and 'voted for' in line:
|
| 162 |
-
item = line.split('voted for ')[1]
|
| 163 |
-
votes[item] = votes.get(item, 0) + 1
|
| 164 |
-
return votes
|
| 165 |
-
except Exception as e:
|
| 166 |
-
print(f"Vote load oopsie: {e}")
|
| 167 |
-
return {}
|
| 168 |
-
|
| 169 |
-
# User hash generator - the secret agent giving you a cool code! π΅οΈββοΈπ
|
| 170 |
-
def generate_user_hash():
|
| 171 |
-
"""π΅οΈ Crafts a snazzy 8-digit ID badge - youβre in the club now! ποΈ"""
|
| 172 |
-
if 'user_hash' not in st.session_state:
|
| 173 |
-
session_id = str(random.getrandbits(128))
|
| 174 |
-
hash_object = hashlib.md5(session_id.encode())
|
| 175 |
-
st.session_state['user_hash'] = hash_object.hexdigest()[:8]
|
| 176 |
-
return st.session_state['user_hash']
|
| 177 |
-
|
| 178 |
-
active_connections = {}
|
| 179 |
-
|
| 180 |
-
# WebSocket handler - the bouncer at the chat rave, keeping it hopping! ππͺ
|
| 181 |
-
async def websocket_handler(websocket, path):
|
| 182 |
-
"""π§ Guards the chat gate, letting messages fly and booting crashers! π¨"""
|
| 183 |
-
try:
|
| 184 |
-
client_id = str(uuid.uuid4())
|
| 185 |
-
room_id = "chat"
|
| 186 |
-
active_connections.setdefault(room_id, {})[client_id] = websocket
|
| 187 |
-
print(f"Client {client_id} joined the chat party!")
|
| 188 |
-
# Auto-join message
|
| 189 |
-
username = st.session_state.username if 'username' in st.session_state else random.choice(FUN_USERNAMES)
|
| 190 |
-
save_chat_entry("System π", f"{username} has joined Sector π!")
|
| 191 |
-
|
| 192 |
-
async for message in websocket:
|
| 193 |
-
try:
|
| 194 |
-
parts = message.split('|', 1)
|
| 195 |
-
if len(parts) == 2:
|
| 196 |
-
username, content = parts
|
| 197 |
-
save_chat_entry(username, content)
|
| 198 |
-
await broadcast_message(f"{username}|{content}", room_id)
|
| 199 |
-
except Exception as e:
|
| 200 |
-
print(f"Message mishap: {e}")
|
| 201 |
-
await websocket.send(f"ERROR|Oops, bad message format! π¬")
|
| 202 |
-
except websockets.ConnectionClosed:
|
| 203 |
-
print(f"Client {client_id} bailed from the chat!")
|
| 204 |
-
finally:
|
| 205 |
-
if room_id in active_connections and client_id in active_connections[room_id]:
|
| 206 |
-
del active_connections[room_id][client_id]
|
| 207 |
-
if not active_connections[room_id]:
|
| 208 |
-
del active_connections[room_id]
|
| 209 |
-
|
| 210 |
-
# Broadcaster - the megaphone blasting chat vibes to all! π£πΆ
|
| 211 |
-
async def broadcast_message(message, room_id):
|
| 212 |
-
"""π’ Shouts the latest chat beat to every dancer in the room - hear it loud! π΅"""
|
| 213 |
-
if room_id in active_connections:
|
| 214 |
-
disconnected = []
|
| 215 |
-
for client_id, ws in active_connections[room_id].items():
|
| 216 |
-
try:
|
| 217 |
-
await ws.send(message)
|
| 218 |
-
except websockets.ConnectionClosed:
|
| 219 |
-
disconnected.append(client_id)
|
| 220 |
-
for client_id in disconnected:
|
| 221 |
-
del active_connections[room_id][client_id]
|
| 222 |
-
|
| 223 |
-
# WebSocket starter - the DJ spinning up the chat tunes! π§π₯
|
| 224 |
-
async def start_websocket_server(host='0.0.0.0', port=8765):
|
| 225 |
-
"""π Cranks up the WebSocket jukebox, ready to rock the chat scene! πΈ"""
|
| 226 |
-
server = await websockets.serve(websocket_handler, host, port)
|
| 227 |
-
print(f"WebSocket server jamming on ws://{host}:{port}")
|
| 228 |
-
return server
|
| 229 |
-
|
| 230 |
-
# Chat interface maker - the stage builder for our chat extravaganza! πποΈ
|
| 231 |
-
def create_streamlit_interface(initial_username):
|
| 232 |
-
"""ποΈ Sets up the chat stage with live updates, pulsing timers, voting, and refresh flair! π"""
|
| 233 |
-
# Custom CSS for a sleek chat box and timer
|
| 234 |
-
st.markdown("""
|
| 235 |
-
<style>
|
| 236 |
-
.chat-box {
|
| 237 |
-
font-family: monospace;
|
| 238 |
-
background: #1e1e1e;
|
| 239 |
-
color: #d4d4d4;
|
| 240 |
-
padding: 10px;
|
| 241 |
-
border-radius: 5px;
|
| 242 |
-
height: 300px;
|
| 243 |
-
overflow-y: auto;
|
| 244 |
-
}
|
| 245 |
-
.timer {
|
| 246 |
-
font-size: 24px;
|
| 247 |
-
color: #ffcc00;
|
| 248 |
-
text-align: center;
|
| 249 |
-
animation: pulse 1s infinite;
|
| 250 |
-
}
|
| 251 |
-
@keyframes pulse {
|
| 252 |
-
0% { transform: scale(1); }
|
| 253 |
-
50% { transform: scale(1.1); }
|
| 254 |
-
100% { transform: scale(1); }
|
| 255 |
-
}
|
| 256 |
-
</style>
|
| 257 |
-
""", unsafe_allow_html=True)
|
| 258 |
-
|
| 259 |
-
# Title and intro
|
| 260 |
-
st.title(f"Chat & Quote Node: {NODE_NAME}")
|
| 261 |
-
st.markdown("Welcome to Sector π - chat, vote, and watch the timer pulse! π")
|
| 262 |
-
|
| 263 |
-
# Session state initialization
|
| 264 |
-
if 'username' not in st.session_state:
|
| 265 |
-
st.session_state.username = initial_username
|
| 266 |
-
save_chat_entry("System π", f"{initial_username} has joined Sector π!")
|
| 267 |
-
if 'refresh_rate' not in st.session_state:
|
| 268 |
-
st.session_state.refresh_rate = 5
|
| 269 |
-
if 'last_refresh' not in st.session_state:
|
| 270 |
-
st.session_state.last_refresh = time.time()
|
| 271 |
-
if 'timer_start' not in st.session_state:
|
| 272 |
-
st.session_state.timer_start = time.time() # Autostart timer
|
| 273 |
-
if 'quote_index' not in st.session_state:
|
| 274 |
-
quotes = load_quotes("famous")
|
| 275 |
-
st.session_state.quote_index = random.randint(0, max(0, len(quotes) - 1)) if quotes else 0
|
| 276 |
-
if 'quote_source' not in st.session_state:
|
| 277 |
-
st.session_state.quote_source = "famous"
|
| 278 |
-
|
| 279 |
-
# Chat section
|
| 280 |
-
st.subheader("Sector Chat π")
|
| 281 |
-
chat_content = load_chat()
|
| 282 |
-
chat_lines = chat_content.split('\n')
|
| 283 |
-
for i, line in enumerate(chat_lines):
|
| 284 |
-
if line.strip() and ': ' in line:
|
| 285 |
-
col1, col2 = st.columns([5, 1])
|
| 286 |
-
with col1:
|
| 287 |
-
st.markdown(line)
|
| 288 |
-
with col2:
|
| 289 |
-
if st.button(f"π", key=f"chat_vote_{i}"):
|
| 290 |
-
user_hash = generate_user_hash()
|
| 291 |
-
save_vote(QUOTE_VOTES_FILE, line, user_hash)
|
| 292 |
-
st.session_state.timer_start = time.time()
|
| 293 |
-
st.rerun()
|
| 294 |
-
|
| 295 |
-
user_list = get_user_list(chat_content)
|
| 296 |
-
new_username = st.selectbox("Switch User", user_list + [st.session_state.username], index=len(user_list))
|
| 297 |
-
if new_username != st.session_state.username:
|
| 298 |
-
save_chat_entry("System π", f"{st.session_state.username} switched to {new_username} in Sector π!")
|
| 299 |
-
st.session_state.username = new_username
|
| 300 |
-
st.session_state.timer_start = time.time()
|
| 301 |
-
|
| 302 |
-
message = st.text_input("Message", placeholder="Type your epic line here! βοΈ")
|
| 303 |
-
if st.button("Send π") and message.strip():
|
| 304 |
-
save_chat_entry(st.session_state.username, message)
|
| 305 |
-
st.session_state.timer_start = time.time()
|
| 306 |
st.rerun()
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
if quotes:
|
| 312 |
-
st.session_state.quote_index = st.session_state.quote_index % len(quotes)
|
| 313 |
-
quote = quotes[st.session_state.quote_index]
|
| 314 |
-
col1, col2 = st.columns([5, 1])
|
| 315 |
-
with col1:
|
| 316 |
-
st.markdown(quote)
|
| 317 |
-
with col2:
|
| 318 |
-
if st.button("π Upvote", key="quote_vote"):
|
| 319 |
-
user_hash = generate_user_hash()
|
| 320 |
-
save_vote(QUOTE_VOTES_FILE, quote, user_hash)
|
| 321 |
-
st.session_state.timer_start = time.time()
|
| 322 |
-
st.rerun()
|
| 323 |
-
if time.time() - st.session_state.last_refresh > 10:
|
| 324 |
-
st.session_state.quote_index = (st.session_state.quote_index + 1) % len(quotes)
|
| 325 |
-
st.session_state.quote_source = "custom" if st.session_state.quote_source == "famous" else "famous"
|
| 326 |
-
quotes = load_quotes(st.session_state.quote_source)
|
| 327 |
-
st.session_state.quote_index = st.session_state.quote_index % len(quotes) if quotes else 0
|
| 328 |
-
st.session_state.last_refresh = time.time()
|
| 329 |
-
st.rerun()
|
| 330 |
-
else:
|
| 331 |
-
st.markdown("No quotes available - check back later! π")
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
image_dir = '.'
|
| 336 |
-
valid_extensions = ('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp')
|
| 337 |
-
images = [f for f in os.listdir(image_dir) if f.lower().endswith(valid_extensions)]
|
| 338 |
-
if len(images) >= 2:
|
| 339 |
-
image1, image2 = random.sample(images, 2)
|
| 340 |
-
col1, col2 = st.columns(2)
|
| 341 |
with col1:
|
| 342 |
-
st.
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
save_vote(IMAGE_VOTES_FILE, image1, user_hash)
|
| 346 |
-
st.session_state.timer_start = time.time()
|
| 347 |
-
st.rerun()
|
| 348 |
with col2:
|
| 349 |
-
st.
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
st.session_state.
|
|
|
|
|
|
|
|
|
|
| 354 |
st.rerun()
|
| 355 |
-
else:
|
| 356 |
-
st.error("Need at least 2 images in the directory for voting!")
|
| 357 |
-
|
| 358 |
-
# Refresh rate controls with pulsing timer
|
| 359 |
-
st.subheader("Set Refresh Rate β³")
|
| 360 |
-
refresh_rate = st.slider("Refresh Rate (seconds)", min_value=1, max_value=300, value=st.session_state.refresh_rate, step=1)
|
| 361 |
-
if refresh_rate != st.session_state.refresh_rate:
|
| 362 |
-
st.session_state.refresh_rate = refresh_rate
|
| 363 |
-
st.session_state.timer_start = time.time()
|
| 364 |
-
|
| 365 |
-
col1, col2, col3 = st.columns(3)
|
| 366 |
-
with col1:
|
| 367 |
-
if st.button("π Small (1s)"):
|
| 368 |
-
st.session_state.refresh_rate = 1
|
| 369 |
-
st.session_state.timer_start = time.time()
|
| 370 |
-
with col2:
|
| 371 |
-
if st.button("π’ Medium (5s)"):
|
| 372 |
-
st.session_state.refresh_rate = 5
|
| 373 |
-
st.session_state.timer_start = time.time()
|
| 374 |
-
with col3:
|
| 375 |
-
if st.button("π Large (5m)"):
|
| 376 |
-
st.session_state.refresh_rate = 300
|
| 377 |
-
st.session_state.timer_start = time.time()
|
| 378 |
-
|
| 379 |
-
# Pulsing countdown timer with emoji digits and random Unicode font
|
| 380 |
-
timer_placeholder = st.empty()
|
| 381 |
-
start_time = st.session_state.timer_start
|
| 382 |
-
while True:
|
| 383 |
-
elapsed = int(time.time() - start_time)
|
| 384 |
-
remaining = max(0, st.session_state.refresh_rate - elapsed)
|
| 385 |
-
font_name, font_func = random.choice(UNICODE_FONTS)
|
| 386 |
-
countdown_str = "".join(UNICODE_DIGITS[int(d)] for d in str(remaining)) if remaining < 10 else font_func(str(remaining))
|
| 387 |
-
timer_emoji = "β³" if elapsed % 2 == 0 else "π" # Pulse effect
|
| 388 |
-
timer_placeholder.markdown(f"<p class='timer'>{timer_emoji} {font_func('Next refresh in:')} {countdown_str} {font_func('seconds')}</p>", unsafe_allow_html=True)
|
| 389 |
-
if remaining == 0:
|
| 390 |
-
st.session_state.last_refresh = time.time()
|
| 391 |
-
st.session_state.timer_start = time.time()
|
| 392 |
-
st.rerun()
|
| 393 |
-
break
|
| 394 |
-
time.sleep(1) # Pulse every second
|
| 395 |
-
|
| 396 |
-
# Sidebar vote stats
|
| 397 |
-
st.sidebar.subheader("Vote Totals")
|
| 398 |
-
chat_votes = load_votes(QUOTE_VOTES_FILE)
|
| 399 |
-
image_votes = load_votes(IMAGE_VOTES_FILE)
|
| 400 |
-
for item, count in chat_votes.items():
|
| 401 |
-
st.sidebar.write(f"{item}: {count} votes")
|
| 402 |
-
for image, count in image_votes.items():
|
| 403 |
-
st.sidebar.write(f"{image}: {count} votes")
|
| 404 |
-
|
| 405 |
-
# Main event - the ringmaster kicking off the chat circus! πͺπ€‘
|
| 406 |
-
async def main():
|
| 407 |
-
"""π€ Drops the mic and starts the chat party - itβs showtime, folks! π"""
|
| 408 |
-
global NODE_NAME
|
| 409 |
-
NODE_NAME, port = get_node_name()
|
| 410 |
-
await start_websocket_server()
|
| 411 |
-
|
| 412 |
-
query_params = st.query_params if hasattr(st, 'query_params') else {}
|
| 413 |
-
initial_username = query_params.get("username", random.choice(FUN_USERNAMES)) if query_params else random.choice(FUN_USERNAMES)
|
| 414 |
-
print(f"Welcoming {initial_username} to the chat bash!")
|
| 415 |
|
| 416 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 417 |
|
| 418 |
if __name__ == "__main__":
|
| 419 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import anthropic
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
+
import glob
|
| 6 |
+
import json
|
| 7 |
+
import pytz
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
from streamlit.components.v1 import html
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import re
|
| 12 |
+
from urllib.parse import quote
|
| 13 |
+
|
| 14 |
+
# 1. App Configuration
|
| 15 |
+
Site_Name = 'π€π§ Claude35ππ¬'
|
| 16 |
+
title="π€π§ Claude35ππ¬"
|
| 17 |
+
helpURL='https://huggingface.co/awacke1'
|
| 18 |
+
bugURL='https://huggingface.co/spaces/awacke1'
|
| 19 |
+
icons='π€π§ π¬π'
|
| 20 |
+
|
| 21 |
+
st.set_page_config(
|
| 22 |
+
page_title=title,
|
| 23 |
+
page_icon=icons,
|
| 24 |
+
layout="wide",
|
| 25 |
+
initial_sidebar_state="auto",
|
| 26 |
+
menu_items={
|
| 27 |
+
'Get Help': helpURL,
|
| 28 |
+
'Report a bug': bugURL,
|
| 29 |
+
'About': title
|
| 30 |
+
}
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Set up the Anthropic client
|
| 34 |
+
client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
| 35 |
+
|
| 36 |
+
# Initialize session state
|
| 37 |
+
if "chat_history" not in st.session_state:
|
| 38 |
+
st.session_state.chat_history = []
|
| 39 |
+
|
| 40 |
+
# Helper Functions: All Your Essentials π
|
| 41 |
+
|
| 42 |
+
# Function to get a file download link (because you deserve easy downloads π)
|
| 43 |
+
def get_download_link(file_path):
|
| 44 |
+
with open(file_path, "rb") as file:
|
| 45 |
+
contents = file.read()
|
| 46 |
+
b64 = base64.b64encode(contents).decode()
|
| 47 |
+
file_name = os.path.basename(file_path)
|
| 48 |
+
return f'<a href="data:file/txt;base64,{b64}" download="{file_name}">Download {file_name}π</a>'
|
| 49 |
+
|
| 50 |
+
# Function to generate a filename based on prompt and time (because names matter π)
|
| 51 |
+
def generate_filename(prompt, file_type):
|
| 52 |
+
central = pytz.timezone('US/Central')
|
| 53 |
+
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
| 54 |
+
safe_prompt = re.sub(r'\W+', '_', prompt)[:90]
|
| 55 |
+
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
| 56 |
+
|
| 57 |
+
# Function to create and save a file (and avoid the black hole of lost data π³)
|
| 58 |
+
def create_file(filename, prompt, response, should_save=True):
|
| 59 |
+
if not should_save:
|
| 60 |
+
return
|
| 61 |
+
with open(filename, 'w', encoding='utf-8') as file:
|
| 62 |
+
file.write(prompt + "\n\n" + response)
|
| 63 |
+
|
| 64 |
+
# Function to load file content (for revisiting the past π)
|
| 65 |
+
def load_file(file_name):
|
| 66 |
+
with open(file_name, "r", encoding='utf-8') as file:
|
| 67 |
+
content = file.read()
|
| 68 |
+
return content
|
| 69 |
+
|
| 70 |
+
# Function to display handy glossary entity links (search like a pro π)
|
| 71 |
+
def display_glossary_entity(k):
|
| 72 |
+
search_urls = {
|
| 73 |
+
"ππArXiv": lambda k: f"/?q={quote(k)}",
|
| 74 |
+
"π": lambda k: f"https://en.wikipedia.org/wiki/{quote(k)}",
|
| 75 |
+
"π": lambda k: f"https://www.google.com/search?q={quote(k)}",
|
| 76 |
+
"π₯": lambda k: f"https://www.youtube.com/results?search_query={quote(k)}",
|
| 77 |
+
}
|
| 78 |
+
links_md = ' '.join([f"[{emoji}]({url(k)})" for emoji, url in search_urls.items()])
|
| 79 |
+
st.markdown(f"**{k}** <small>{links_md}</small>", unsafe_allow_html=True)
|
| 80 |
+
|
| 81 |
+
# Function to create zip of files (because more is more π§³)
|
| 82 |
+
def create_zip_of_files(files):
|
| 83 |
+
import zipfile
|
| 84 |
+
zip_name = "all_files.zip"
|
| 85 |
+
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
| 86 |
+
for file in files:
|
| 87 |
+
zipf.write(file)
|
| 88 |
+
return zip_name
|
| 89 |
+
|
| 90 |
+
# Function to create HTML for autoplaying and looping video (for the full cinematic effect π₯)
|
| 91 |
+
def get_video_html(video_path, width="100%"):
|
| 92 |
+
video_url = f"data:video/mp4;base64,{base64.b64encode(open(video_path, 'rb').read()).decode()}"
|
| 93 |
+
return f'''
|
| 94 |
+
<video width="{width}" controls autoplay muted loop>
|
| 95 |
+
<source src="{video_url}" type="video/mp4">
|
| 96 |
+
Your browser does not support the video tag.
|
| 97 |
+
</video>
|
| 98 |
+
'''
|
| 99 |
+
|
| 100 |
+
# Function to create HTML for audio player (when life needs a soundtrack πΆ)
|
| 101 |
+
def get_audio_html(audio_path, width="100%"):
|
| 102 |
+
audio_url = f"data:audio/mpeg;base64,{base64.b64encode(open(audio_path, 'rb').read()).decode()}"
|
| 103 |
+
return f'''
|
| 104 |
+
<audio controls style="width: {width};">
|
| 105 |
+
<source src="{audio_url}" type="audio/mpeg">
|
| 106 |
+
Your browser does not support the audio element.
|
| 107 |
+
</audio>
|
| 108 |
+
'''
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# Streamlit App Layout (like a house with better flow π‘)
|
| 112 |
+
def main():
|
| 113 |
+
|
| 114 |
+
# Sidebar with Useful Controls (All the VIP actions π)
|
| 115 |
+
st.sidebar.title("π§ Claudeπ")
|
| 116 |
+
|
| 117 |
+
all_files = glob.glob("*.md")
|
| 118 |
+
all_files.sort(reverse=True)
|
| 119 |
+
|
| 120 |
+
if st.sidebar.button("π Delete All"):
|
| 121 |
+
for file in all_files:
|
| 122 |
+
os.remove(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
st.rerun()
|
| 124 |
|
| 125 |
+
if st.sidebar.button("β¬οΈ Download All"):
|
| 126 |
+
zip_file = create_zip_of_files(all_files)
|
| 127 |
+
st.sidebar.markdown(get_download_link(zip_file), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
for file in all_files:
|
| 130 |
+
col1, col2, col3, col4 = st.sidebar.columns([1,3,1,1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
with col1:
|
| 132 |
+
if st.button("π", key="view_"+file):
|
| 133 |
+
st.session_state.current_file = file
|
| 134 |
+
st.session_state.file_content = load_file(file)
|
|
|
|
|
|
|
|
|
|
| 135 |
with col2:
|
| 136 |
+
st.markdown(get_download_link(file), unsafe_allow_html=True)
|
| 137 |
+
with col3:
|
| 138 |
+
if st.button("π", key="edit_"+file):
|
| 139 |
+
st.session_state.current_file = file
|
| 140 |
+
st.session_state.file_content = load_file(file)
|
| 141 |
+
with col4:
|
| 142 |
+
if st.button("π", key="delete_"+file):
|
| 143 |
+
os.remove(file)
|
| 144 |
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
# Main Area: Chat with Claude (Heβs a good listener π¬)
|
| 147 |
+
user_input = st.text_area("Message π¨:", height=100)
|
| 148 |
+
|
| 149 |
+
if st.button("Send π¨"):
|
| 150 |
+
if user_input:
|
| 151 |
+
response = client.messages.create(
|
| 152 |
+
model="claude-3-sonnet-20240229",
|
| 153 |
+
max_tokens=1000,
|
| 154 |
+
messages=[
|
| 155 |
+
{"role": "user", "content": user_input}
|
| 156 |
+
]
|
| 157 |
+
)
|
| 158 |
+
st.write("Claude's reply π§ :")
|
| 159 |
+
st.write(response.content[0].text)
|
| 160 |
+
|
| 161 |
+
filename = generate_filename(user_input, "md")
|
| 162 |
+
create_file(filename, user_input, response.content[0].text)
|
| 163 |
+
|
| 164 |
+
st.session_state.chat_history.append({"user": user_input, "claude": response.content[0].text})
|
| 165 |
+
|
| 166 |
+
# Display Chat History (Never forget a good chat π)
|
| 167 |
+
st.subheader("Past Conversations π")
|
| 168 |
+
for chat in st.session_state.chat_history:
|
| 169 |
+
st.text_area("You said π¬:", chat["user"], height=100, disabled=True)
|
| 170 |
+
st.text_area("Claude replied π€:", chat["claude"], height=200, disabled=True)
|
| 171 |
+
st.markdown("---")
|
| 172 |
+
|
| 173 |
+
# File Editor (When you need to tweak things βοΈ)
|
| 174 |
+
if hasattr(st.session_state, 'current_file'):
|
| 175 |
+
st.subheader(f"Editing: {st.session_state.current_file} π ")
|
| 176 |
+
new_content = st.text_area("File Content βοΈ:", st.session_state.file_content, height=300)
|
| 177 |
+
if st.button("Save Changes πΎ"):
|
| 178 |
+
with open(st.session_state.current_file, 'w', encoding='utf-8') as file:
|
| 179 |
+
file.write(new_content)
|
| 180 |
+
st.success("File updated successfully! π")
|
| 181 |
+
|
| 182 |
+
# Image Gallery (For your viewing pleasure πΈ)
|
| 183 |
+
st.subheader("Image Gallery πΌ")
|
| 184 |
+
image_files = glob.glob("*.png") + glob.glob("*.jpg") + glob.glob("*.jpeg")
|
| 185 |
+
image_cols = st.slider("Gallery Columns πΌ", min_value=1, max_value=15, value=5)
|
| 186 |
+
cols = st.columns(image_cols)
|
| 187 |
+
for idx, image_file in enumerate(image_files):
|
| 188 |
+
with cols[idx % image_cols]:
|
| 189 |
+
img = Image.open(image_file)
|
| 190 |
+
#st.image(img, caption=image_file, use_container_width=True)
|
| 191 |
+
st.image(img, use_container_width=True)
|
| 192 |
+
display_glossary_entity(os.path.splitext(image_file)[0])
|
| 193 |
+
|
| 194 |
+
# Video Gallery (Letβs roll the tapes π¬)
|
| 195 |
+
st.subheader("Video Gallery π₯")
|
| 196 |
+
video_files = glob.glob("*.mp4")
|
| 197 |
+
video_cols = st.slider("Gallery Columns π¬", min_value=1, max_value=5, value=3)
|
| 198 |
+
cols = st.columns(video_cols)
|
| 199 |
+
for idx, video_file in enumerate(video_files):
|
| 200 |
+
with cols[idx % video_cols]:
|
| 201 |
+
st.markdown(get_video_html(video_file, width="100%"), unsafe_allow_html=True)
|
| 202 |
+
display_glossary_entity(os.path.splitext(video_file)[0])
|
| 203 |
+
|
| 204 |
+
# Audio Gallery (Tunes for the mood πΆ)
|
| 205 |
+
st.subheader("Audio Gallery π§")
|
| 206 |
+
audio_files = glob.glob("*.mp3") + glob.glob("*.wav")
|
| 207 |
+
audio_cols = st.slider("Gallery Columns πΆ", min_value=1, max_value=15, value=5)
|
| 208 |
+
cols = st.columns(audio_cols)
|
| 209 |
+
for idx, audio_file in enumerate(audio_files):
|
| 210 |
+
with cols[idx % audio_cols]:
|
| 211 |
+
st.markdown(get_audio_html(audio_file, width="100%"), unsafe_allow_html=True)
|
| 212 |
+
display_glossary_entity(os.path.splitext(audio_file)[0])
|
| 213 |
|
| 214 |
if __name__ == "__main__":
|
| 215 |
+
main()
|