import io import re import streamlit as st import glob import os from PIL import Image import fitz from reportlab.lib.pagesizes import A4 from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib import colors from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont import unicodedata import asyncio import websockets import uuid from datetime import datetime import random import time import hashlib import base64 import streamlit.components.v1 as components import edge_tts from audio_recorder_streamlit import audio_recorder import nest_asyncio import pytz import shutil import anthropic import openai from PyPDF2 import PdfReader import threading import json import zipfile from gradio_client import Client from dotenv import load_dotenv from streamlit_marquee import streamlit_marquee from collections import defaultdict, Counter import pandas as pd nest_asyncio.apply() st.set_page_config(layout="wide", initial_sidebar_state="collapsed") icons = '๐ค๐ง ๐ฌ๐' Site_Name = '๐ค๐ง Chat & Quote Node๐๐ฌ' START_ROOM = "Sector ๐" FUN_USERNAMES = { "CosmicJester ๐": "en-US-AriaNeural", "PixelPanda ๐ผ": "en-US-JennyNeural", "QuantumQuack ๐ฆ": "en-GB-SoniaNeural", "StellarSquirrel ๐ฟ๏ธ": "en-AU-NatashaNeural", "GizmoGuru โ๏ธ": "en-CA-ClaraNeural", "NebulaNinja ๐ ": "en-US-GuyNeural", "ByteBuster ๐พ": "en-GB-RyanNeural", "GalacticGopher ๐": "en-AU-WilliamNeural", "RocketRaccoon ๐": "en-CA-LiamNeural", "EchoElf ๐ง": "en-US-AnaNeural", "PhantomFox ๐ฆ": "en-US-BrandonNeural", "WittyWizard ๐ง": "en-GB-ThomasNeural", "LunarLlama ๐": "en-AU-FreyaNeural", "SolarSloth โ๏ธ": "en-CA-LindaNeural", "AstroAlpaca ๐ฆ": "en-US-ChristopherNeural", "CyberCoyote ๐บ": "en-GB-ElliotNeural", "MysticMoose ๐ฆ": "en-AU-JamesNeural", "GlitchGnome ๐ง": "en-CA-EthanNeural", "VortexViper ๐": "en-US-AmberNeural", "ChronoChimp ๐": "en-GB-LibbyNeural" } EDGE_TTS_VOICES = list(set(FUN_USERNAMES.values())) FILE_EMOJIS = {"md": "๐", "mp3": "๐ต", "png": "๐ผ๏ธ", "mp4": "๐ฅ", "zip": "๐ฆ"} for d in ["chat_logs", "vote_logs", "audio_logs", "history_logs", "audio_cache", "paper_metadata"]: os.makedirs(d, exist_ok=True) CHAT_DIR = "chat_logs" VOTE_DIR = "vote_logs" MEDIA_DIR = "." AUDIO_CACHE_DIR = "audio_cache" AUDIO_DIR = "audio_logs" PAPER_DIR = "paper_metadata" STATE_FILE = "user_state.txt" CHAT_FILE = os.path.join(CHAT_DIR, "global_chat.md") QUOTE_VOTES_FILE = os.path.join(VOTE_DIR, "quote_votes.md") IMAGE_VOTES_FILE = os.path.join(VOTE_DIR, "image_votes.md") HISTORY_FILE = os.path.join(VOTE_DIR, "vote_history.md") load_dotenv() anthropic_key = os.getenv('ANTHROPIC_API_KEY', st.secrets.get('ANTHROPIC_API_KEY', "")) openai_api_key = os.getenv('OPENAI_API_KEY', st.secrets.get('OPENAI_API_KEY', "")) openai_client = openai.OpenAI(api_key=openai_api_key) def format_timestamp_prefix(username=""): central = pytz.timezone('US/Central') now = datetime.now(central) return f"{now.strftime('%Y%m%d_%H%M%S')}-by-{username}" class PerformanceTimer: def __init__(self, name): self.name, self.start = name, None def __enter__(self): self.start = time.time() return self def __exit__(self, *args): duration = time.time() - self.start st.session_state['operation_timings'][self.name] = duration st.session_state['performance_metrics'][self.name].append(duration) def init_session_state(): defaults = { 'server_running': False, 'server_task': None, 'active_connections': {}, 'media_notifications': [], 'last_chat_update': 0, 'displayed_chat_lines': [], 'message_text': "", 'audio_cache': {}, 'pasted_image_data': None, 'quote_line': None, 'refresh_rate': 10, 'base64_cache': {}, 'transcript_history': [], 'last_transcript': "", 'image_hashes': set(), 'tts_voice': "en-US-AriaNeural", 'chat_history': [], 'marquee_settings': { "background": "#1E1E1E", "color": "#FFFFFF", "font-size": "14px", "animationDuration": "20s", "width": "100%", "lineHeight": "35px" }, 'operation_timings': {}, 'performance_metrics': defaultdict(list), 'enable_audio': True, 'download_link_cache': {}, 'username': None, 'autosend': True, 'autosearch': True, 'last_message': "", 'last_query': "", 'mp3_files': {}, 'timer_start': time.time(), 'quote_index': 0, 'quote_source': "famous", 'last_sent_transcript': "", 'old_val': None, 'last_refresh': time.time(), 'paper_metadata': {}, 'paste_image_base64': "", 'use_arxiv': True, 'use_arxiv_audio': False, 'speech_processed': False, 'auto_refresh': True } for k, v in defaults.items(): if k not in st.session_state: st.session_state[k] = v def update_marquee_settings_ui(): st.sidebar.markdown("### ๐ฏ Marquee Settings") cols = st.sidebar.columns(2) with cols[0]: st.session_state['marquee_settings']['background'] = st.color_picker("๐จ Background", "#1E1E1E") st.session_state['marquee_settings']['color'] = st.color_picker("โ๏ธ Text", "#FFFFFF") with cols[1]: st.session_state['marquee_settings']['font-size'] = f"{st.slider('๐ Size', 10, 24, 14)}px" st.session_state['marquee_settings']['animationDuration'] = f"{st.slider('โฑ๏ธ Speed', 1, 20, 20)}s" def display_marquee(text, settings, key_suffix=""): truncated = text[:280] + "..." if len(text) > 280 else text streamlit_marquee(content=truncated, **settings, key=f"marquee_{key_suffix}") st.write("") def clean_text_for_tts(text): return re.sub(r'[#*!\[\]]+', '', ' '.join(text.split()))[:200] or "No text" def clean_text_for_filename(text): return '_'.join(re.sub(r'[^\w\s-]', '', text.lower()).split())[:50] def get_high_info_terms(text, top_n=10): stop_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with'} words = re.findall(r'\b\w+(?:-\w+)*\b', text.lower()) bi_grams = [' '.join(pair) for pair in zip(words, words[1:])] filtered = [t for t in words + bi_grams if t not in stop_words and len(t.split()) <= 2] return [t for t, _ in Counter(filtered).most_common(top_n)] def generate_filename(prompt, username, file_type="md", title=None): timestamp = format_timestamp_prefix(username) if title: high_info = '-'.join(get_high_info_terms(title, 5)) return f"{timestamp}-{clean_text_for_filename(prompt[:20])}-{high_info}.{file_type}" hash_val = hashlib.md5(prompt.encode()).hexdigest()[:8] return f"{timestamp}-{hash_val}.{file_type}" def create_file(prompt, username, file_type="md", title=None): filename = generate_filename(prompt, username, file_type, title) with open(filename, 'w', encoding='utf-8') as f: f.write(prompt) return filename def get_download_link(file, file_type="mp3"): cache_key = f"dl_{file}" if cache_key not in st.session_state['download_link_cache']: with open(file, "rb") as f: b64 = base64.b64encode(f.read()).decode() mime_types = {"mp3": "audio/mpeg", "png": "image/png", "mp4": "video/mp4", "md": "text/markdown", "zip": "application/zip"} st.session_state['download_link_cache'][cache_key] = f'{FILE_EMOJIS.get(file_type, "Download")} Download {os.path.basename(file)}' return st.session_state['download_link_cache'][cache_key] def save_username(username): try: with open(STATE_FILE, 'w') as f: f.write(username) except Exception as e: print(f"Failed to save username: {e}") def load_username(): if os.path.exists(STATE_FILE): try: with open(STATE_FILE, 'r') as f: return f.read().strip() except Exception as e: print(f"Failed to load username: {e}") return None def concatenate_markdown_files(exclude_files=["README.md"]): md_files = sorted([f for f in glob.glob("*.md") if os.path.basename(f) not in exclude_files], key=os.path.getmtime) all_md_content = "" for i, md_file in enumerate(md_files, 1): with open(md_file, 'r', encoding='utf-8') as f: content = f.read().strip() all_md_content += f"{i}. {content}\n" return all_md_content.rstrip() def get_chat_text_only(exclude_files=["README.md"]): md_files = sorted([f for f in glob.glob("*.md") if os.path.basename(f) not in exclude_files], key=os.path.getmtime) chat_text = "" for i, md_file in enumerate(md_files, 1): with open(md_file, 'r', encoding='utf-8') as f: content = f.read().strip() lines = content.split('\n') for line in lines: if line.strip() and not line.startswith('#'): match = re.match(r'\[(.*?)\]\s(.*?)\s\((.*?)\):\s*(.*)', line) if match: message = match.group(4).strip() if message.startswith('```markdown'): message = message.replace('```markdown', '').replace('```', '').strip() chat_text += f"{message}\n" return chat_text.rstrip() async def async_edge_tts_generate(text, voice, username, rate=0, pitch=0, file_format="mp3"): cache_key = f"{text[:100]}_{voice}_{rate}_{pitch}_{file_format}" if cache_key in st.session_state['audio_cache']: return st.session_state['audio_cache'][cache_key], 0 start_time = time.time() text = clean_text_for_tts(text) if not text or text == "No text": print(f"Skipping audio generation for empty/invalid text: '{text}'") return None, 0 filename = f"{format_timestamp_prefix(username)}-{hashlib.md5(text.encode()).hexdigest()[:8]}.{file_format}" try: communicate = edge_tts.Communicate(text, voice, rate=f"{rate:+d}%", pitch=f"{pitch:+d}Hz") await communicate.save(filename) if os.path.exists(filename) and os.path.getsize(filename) > 0: st.session_state['audio_cache'][cache_key] = filename return filename, time.time() - start_time else: print(f"Audio file {filename} was not created or is empty.") return None, 0 except edge_tts.exceptions.NoAudioReceived as e: print(f"No audio received for text: '{text}' with voice: {voice}. Error: {e}") return None, 0 except Exception as e: print(f"Error generating audio for text: '{text}' with voice: {voice}. Error: {e}") return None, 0 def play_and_download_audio(file_path): if file_path and os.path.exists(file_path): st.audio(file_path) st.markdown(get_download_link(file_path), unsafe_allow_html=True) else: st.warning(f"Audio file not found: {file_path}") def load_mp3_viewer(): mp3_files = sorted(glob.glob("*.mp3"), key=os.path.getmtime) for i, mp3 in enumerate(mp3_files, 1): filename = os.path.basename(mp3) if filename not in st.session_state['mp3_files']: st.session_state['mp3_files'][filename] = (i, mp3) async def save_chat_entry(username, message, voice, is_markdown=False): if not message.strip() or message == st.session_state.last_transcript: return None, None central = pytz.timezone('US/Central') timestamp = datetime.now(central).strftime("%Y-%m-%d %H:%M:%S") entry = f"[{timestamp}] {username} ({voice}): {message}" if not is_markdown else f"[{timestamp}] {username} ({voice}):\n```markdown\n{message}\n```" md_file = create_file(entry, username, "md") with open(CHAT_FILE, 'a') as f: f.write(f"{entry}\n") audio_file, _ Distance = await async_edge_tts_generate(message, voice, username) if audio_file: with open(HISTORY_FILE, 'a') as f: f.write(f"[{timestamp}] {username}: Audio - {audio_file}\n") st.session_state['mp3_files'][os.path.basename(audio_file)] = (len(st.session_state['chat_history']) + 1, audio_file) if st.session_state.get('speech_processed', False) and st.session_state.get('message_input', '') == message: st.session_state['message_input'] = "" st.session_state['speech_processed'] = False else: st.warning(f"Failed to generate audio for: {message}") await broadcast_message(f"{username}|{message}", "chat") st.session_state.last_chat_update = time.time() st.session_state.chat_history.append(entry) st.session_state.last_transcript = message return md_file, audio_file async def load_chat(): if not os.path.exists(CHAT_FILE): with open(CHAT_FILE, 'a') as f: f.write(f"# {START_ROOM} Chat\n\nWelcome to the cosmic hub! ๐ค\n") with open(CHAT_FILE, 'r') as f: content = f.read().strip() lines = content.split('\n') unique_lines = list(dict.fromkeys(line for line in lines if line.strip())) return unique_lines async def perform_claude_search(query, username, image=None): if not query.strip() or query == st.session_state.last_transcript: return None, None, None client = anthropic.Anthropic(api_key=anthropic_key) message_content = [{"type": "text", "text": query}] if image: buffered = io.BytesIO() image.save(buffered, format="PNG") img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8') message_content.append({ "type": "image", "source": { "type": "base64", "media_type": "image/png", "data": img_base64 } }) try: response = client.messages.create( model="claude-3-sonnet-20240229", max_tokens=1000, messages=[{"role": "user", "content": message_content}] ) result = response.content[0].text st.markdown(f"### Claude's Reply ๐ง \n{result}") except Exception as e: st.error(f"Claude processing failed: {e}") return None, None, None voice = FUN_USERNAMES.get(username, "en-US-AriaNeural") full_text = f"Prompt: {query}\nResponse: {result}" md_file, audio_file = await save_chat_entry(username, full_text, voice, True) return md_file, audio_file, result async def perform_arxiv_search(query, username, claude_result=None): if not query.strip() or query == st.session_state.last_transcript: return None, None if claude_result is None: client = anthropic.Anthropic(api_key=anthropic_key) claude_response = client.messages.create( model="claude-3-sonnet-20240229", max_tokens=1000, messages=[{"role": "user", "content": query}] ) claude_result = claude_response.content[0].text st.markdown(f"### Claude's Reply ๐ง \n{claude_result}") enhanced_query = f"{query}\n\n{claude_result}" gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern") refs = gradio_client.predict( enhanced_query, 10, "Semantic Search", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md" )[0] result = f"๐ {enhanced_query}\n\n{refs}" voice = FUN_USERNAMES.get(username, "en-US-AriaNeural") md_file, audio_file = await save_chat_entry(username, result, voice, True) return md_file, audio_file async def perform_ai_lookup(q, vocal_summary=True, extended_refs=False, titles_summary=True, full_audio=False, useArxiv=True, useArxivAudio=False): start = time.time() client = anthropic.Anthropic(api_key=anthropic_key) response = client.messages.create( model="claude-3-sonnet-20240229", max_tokens=1000, messages=[{"role": "user", "content": q}] ) st.write("Claude's reply ๐ง :") st.markdown(response.content[0].text) result = response.content[0].text md_file = create_file(result, "System", "md") audio_file, _ = await async_edge_tts_generate(result, st.session_state['tts_voice'], "System") st.subheader("๐ Main Response Audio") play_and_download_audio(audio_file) papers = [] if useArxiv: q = q + result st.write('Running Arxiv RAG with Claude inputs.') gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern") refs = gradio_client.predict( q, 20, "Semantic Search", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md" )[0] papers = parse_arxiv_refs(refs, q) for paper in papers: filename = create_file(generate_5min_feature_markdown(paper), "System", "md", paper['title']) paper['md_file'] = filename st.session_state['paper_metadata'][paper['title']] = filename if papers and useArxivAudio: await create_paper_audio_files(papers, q) elapsed = time.time() - start st.write(f"**Total Elapsed:** {elapsed:.2f} s") return result, papers async def websocket_handler(websocket, path): client_id = str(uuid.uuid4()) room_id = "chat" if room_id not in st.session_state.active_connections: st.session_state.active_connections[room_id] = {} st.session_state.active_connections[room_id][client_id] = websocket username = st.session_state.get('username', random.choice(list(FUN_USERNAMES.keys()))) chat_content = await load_chat() if not any(f"Client-{client_id}" in line for line in chat_content): await save_chat_entry("System ๐", f"{username} has joined {START_ROOM}!", "en-US-AriaNeural") try: async for message in websocket: if '|' in message: username, content = message.split('|', 1) voice = FUN_USERNAMES.get(username, "en-US-AriaNeural") await save_chat_entry(username, content, voice) else: await websocket.send("ERROR|Message format: username|content") except websockets.ConnectionClosed: await save_chat_entry("System ๐", f"{username} has left {START_ROOM}!", "en-US-AriaNeural") finally: if room_id in st.session_state.active_connections and client_id in st.session_state.active_connections[room_id]: del st.session_state.active_connections[room_id][client_id] async def broadcast_message(message, room_id): if room_id in st.session_state.active_connections: disconnected = [] for client_id, ws in st.session_state.active_connections[room_id].items(): try: await ws.send(message) except websockets.ConnectionClosed: disconnected.append(client_id) for client_id in disconnected: if client_id in st.session_state.active_connections[room_id]: del st.session_state.active_connections[room_id][client_id] async def run_websocket_server(): if not st.session_state.get('server_running', False): server = await websockets.serve(websocket_handler, '0.0.0.0', 8765) st.session_state['server_running'] = True await server.wait_closed() def start_websocket_server(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(run_websocket_server()) class AudioProcessor: def __init__(self): self.cache_dir = AUDIO_CACHE_DIR os.makedirs(self.cache_dir, exist_ok=True) self.metadata = json.load(open(f"{self.cache_dir}/metadata.json")) if os.path.exists(f"{self.cache_dir}/metadata.json") else {} def _save_metadata(self): with open(f"{self.cache_dir}/metadata.json", 'w') as f: json.dump(self.metadata, f) async def create_audio(self, text, voice='en-US-AriaNeural'): cache_key = hashlib.md5(f"{text}:{voice}".encode()).hexdigest() cache_path = f"{self.cache_dir}/{cache_key}.mp3" if cache_key in self.metadata and os.path.exists(cache_path): return cache_path text = clean_text_for_tts(text) if not text: return None communicate = edge_tts.Communicate(text, voice) await communicate.save(cache_path) self.metadata[cache_key] = {'timestamp': datetime.now().isoformat(), 'text_length': len(text), 'voice': voice} self._save_metadata() return cache_path def process_pdf(pdf_file, max_pages, voice, audio_processor): reader = PdfReader(pdf_file) total_pages = min(len(reader.pages), max_pages) texts, audios = [], {} async def process_page(i, text): audio_path = await audio_processor.create_audio(text, voice) if audio_path: audios[i] = audio_path for i in range(total_pages): text = reader.pages[i].extract_text() texts.append(text) threading.Thread(target=lambda: asyncio.run(process_page(i, text))).start() return texts, audios, total_pages def parse_arxiv_refs(ref_text, query): if not ref_text: return [] papers = [] current = {} for line in ref_text.split('\n'): if line.count('|') == 2: if current: papers.append(current) date, title, *_ = line.strip('* ').split('|') url = re.search(r'(https://arxiv.org/\S+)', line).group(1) if re.search(r'(https://arxiv.org/\S+)', line) else f"paper_{len(papers)}" current = {'date': date, 'title': title, 'url': url, 'authors': '', 'summary': '', 'full_audio': None, 'download_base64': '', 'query': query} elif current: if not current['authors']: current['authors'] = line.strip('* ') else: current['summary'] += ' ' + line.strip() if current['summary'] else line.strip() if current: papers.append(current) return papers[:20] def generate_5min_feature_markdown(paper): title, summary, authors, date, url = paper['title'], paper['summary'], paper['authors'], paper['date'], paper['url'] pdf_url = url.replace("abs", "pdf") + (".pdf" if not url.endswith(".pdf") else "") wct, sw = len(title.split()), len(summary.split()) terms = get_high_info_terms(summary, 15) rouge = round((len(terms) / max(sw, 1)) * 100, 2) mermaid = "```mermaid\nflowchart TD\n" + "\n".join(f' T{i+1}["{terms[i]}"] --> T{i+2}["{terms[i+1]}"]' for i in range(len(terms)-1)) + "\n```" return f""" ## ๐ {title} **Authors:** {authors} **Date:** {date} **Words:** Title: {wct}, Summary: {sw} **Links:** [Abstract]({url}) | [PDF]({pdf_url}) **Terms:** {', '.join(terms)} **ROUGE:** {rouge}% ### ๐ค TTF Read Aloud - **Title:** {title} - **Terms:** {', '.join(terms)} - **ROUGE:** {rouge}% #### Concepts Graph {mermaid} --- """ async def create_paper_audio_files(papers, query): for p in papers: audio_text = clean_text_for_tts(f"{p['title']} by {p['authors']}. {p['summary']}") p['full_audio'], _ = await async_edge_tts_generate(audio_text, st.session_state['tts_voice'], p['authors']) if p['full_audio']: p['download_base64'] = get_download_link(p['full_audio']) def save_vote(file, item, user_hash): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") entry = f"[{timestamp}] {user_hash} voted for {item}" try: with open(file, 'a') as f: f.write(f"{entry}\n") with open(HISTORY_FILE, 'a') as f: f.write(f"- {timestamp} - User {user_hash} voted for {item}\n") return True except Exception as e: print(f"Vote save flop: {e}") return False def load_votes(file): if not os.path.exists(file): with open(file, 'w') as f: f.write("# Vote Tally\n\nNo votes yet - get clicking! ๐ฑ๏ธ\n") try: with open(file, 'r') as f: lines = f.read().strip().split('\n') votes = {} for line in lines[2:]: if line.strip() and 'voted for' in line: item = line.split('voted for ')[1] votes[item] = votes.get(item, 0) + 1 return votes except Exception as e: print(f"Vote load oopsie: {e}") return {} def generate_user_hash(): if 'user_hash' not in st.session_state: session_id = str(random.getrandbits(128)) hash_object = hashlib.md5(session_id.encode()) st.session_state['user_hash'] = hash_object.hexdigest()[:8] return st.session_state['user_hash'] async def save_pasted_image(image, username, prompt=""): img_hash = hashlib.md5(image.tobytes()).hexdigest()[:8] if img_hash in st.session_state.image_hashes: return None context = prompt if prompt else st.session_state.get('last_message', "pasted_image") timestamp = format_timestamp_prefix(username) filename = f"{timestamp}-{clean_text_for_filename(context)}-{img_hash}.png" filepath = filename try: image.save(filepath, "PNG") st.session_state.image_hashes.add(img_hash) await save_chat_entry(username, f"Pasted image saved: {filepath}", FUN_USERNAMES.get(username, "en-US-AriaNeural")) return filepath except Exception as e: st.error(f"Failed to save image: {e}") return None def create_zip_of_files(files, prefix="All", query="latest"): if not files: return None terms = get_high_info_terms(" ".join([open(f, 'r', encoding='utf-8').read() if f.endswith('.md') else os.path.splitext(os.path.basename(f))[0].replace('_', ' ') for f in files] + [query]), 5) zip_name = f"{prefix}_{format_timestamp_prefix()}_{'-'.join(terms)[:20]}.zip" with zipfile.ZipFile(zip_name, 'w') as z: [z.write(f) for f in files] return zip_name def delete_files(file_type, exclude_files=["README.md"]): files = glob.glob(f"*.{file_type}") if file_type == "md": files = [f for f in files if os.path.basename(f) not in exclude_files] for f in files: try: os.remove(f) st.session_state['mp3_files'] = {k: v for k, v in st.session_state['mp3_files'].items() if not k.endswith(f".{file_type}")} except Exception as e: st.error(f"Failed to delete {f}: {e}") if file_type in ["md", "mp3", "png", "mp4"]: st.session_state['download_link_cache'] = {} def paste_image_component(): with st.form(key="paste_form"): paste_input = st.text_area("Paste Image Data Here", key="paste_input", height=100) submit_button = st.form_submit_button("Paste Image ๐") if submit_button and paste_input: try: if paste_input.startswith('data:image'): mime_type = paste_input.split(';')[0].split(':')[1] base64_str = paste_input.split(',')[1] img_bytes = base64.b64decode(base64_str) img = Image.open(io.BytesIO(img_bytes)) st.image(img, caption=f"Pasted Image ({mime_type.split('/')[1].upper()})", use_column_width=True) return img, mime_type.split('/')[1] else: st.warning("Pasted data is not a recognized image format.") return None, None except Exception as e: st.error(f"Error decoding pasted image: {e}") return None, None return None, None def create_pdf_tab(default_markdown): font_files = glob.glob("*.ttf") if not font_files: st.error("No .ttf font files found in the current directory. Please add some, e.g., NotoEmoji-Bold.ttf and DejaVuSans.ttf.") return available_fonts = {os.path.splitext(os.path.basename(f))[0]: f for f in font_files} md_files = [f for f in glob.glob("*.md") if os.path.basename(f) != "README.md"] md_options = [os.path.splitext(os.path.basename(f))[0] for f in md_files] with st.sidebar: selected_md = st.selectbox("Select Markdown File", options=md_options, index=0 if md_options else -1) selected_font_name = st.selectbox("Select Emoji Font", options=list(available_fonts.keys()), index=0 if "NotoEmoji-Bold" in available_fonts else 0) selected_font_path = available_fonts[selected_font_name] base_font_size = st.slider("Font Size (points)", min_value=6, max_value=16, value=8, step=1) plain_text_mode = st.checkbox("Render as Plain Text (Preserve Bold Only)", value=False) auto_bold_numbers = st.checkbox("Auto-Bold Numbered Lines", value=False) enlarge_font_size = st.checkbox("Enlarge Font Size for Numbered Lines", value=True) num_columns = st.selectbox("Number of Columns", options=[1, 2, 3, 4, 5, 6], index=3) if 'markdown_content' not in st.session_state or not md_options: st.session_state.markdown_content = default_markdown if md_options and selected_md: with open(f"{selected_md}.md", "r", encoding="utf-8") as f: st.session_state.markdown_content = f.read() edited_markdown = st.text_area("Modify the markdown content below:", value=st.session_state.markdown_content, height=300, key=f"markdown_{selected_md}_{selected_font_name}_{num_columns}") if st.button("Update PDF"): st.session_state.markdown_content = edited_markdown with open(f"{selected_md}.md", "w", encoding="utf-8") as f: f.write(edited_markdown) st.rerun() st.download_button(label="Save Markdown", data=st.session_state.markdown_content, file_name=f"{selected_md}.md", mime="text/markdown") st.subheader("Voice Settings") voice = st.selectbox("Select Voice", options=EDGE_TTS_VOICES, index=0) if st.button("Generate MP3"): audio_file, _ = asyncio.run(async_edge_tts_generate(edited_markdown, voice, "System")) if audio_file: st.audio(audio_file) st.markdown(get_download_link(audio_file), unsafe_allow_html=True) if not md_options: st.warning("No .md files found in the directory (excluding README.md). Using default content.") return try: pdfmetrics.registerFont(TTFont(selected_font_name, selected_font_path)) pdfmetrics.registerFont(TTFont("DejaVuSans", "DejaVuSans.ttf")) except Exception as e: st.error(f"Failed to register fonts: {e}. Ensure both {selected_font_name}.ttf and DejaVuSans.ttf are in the directory.") return def apply_emoji_font(text, emoji_font): emoji_pattern = re.compile( r"([\U0001F300-\U0001F5FF" r"\U0001F600-\U0001F64F" r"\U0001F680-\U0001F6FF" r"\U0001F700-\U0001F77F" r"\U0001F780-\U0001F7FF" r"\U0001F800-\U0001F8FF" r"\U0001F900-\U0001F9FF" r"\U0001FA00-\U0001FA6F" r"\U0001FA70-\U0001FAFF" r"\u2600-\u26FF" r"\u2700-\u27BF]+)" ) def replace_emoji(match): emoji = match.group(1) emoji = unicodedata.normalize('NFC', emoji) return f'{emoji}' segments = [] last_pos = 0 for match in emoji_pattern.finditer(text): start, end = match.span() if last_pos < start: segments.append(f'{text[last_pos:start]}') segments.append(replace_emoji(match)) last_pos = end if last_pos < len(text): segments.append(f'{text[last_pos:]}') return ''.join(segments) def markdown_to_pdf_content(markdown_text, plain_text_mode, auto_bold_numbers): lines = markdown_text.strip().split('\n') pdf_content = [] number_pattern = re.compile(r'^\d+\.\s') if plain_text_mode: for line in lines: line = line.strip() if not line or line.startswith('# '): continue bold_pattern = re.compile(r'\*\*(.*?)\*\*') line = bold_pattern.sub(r'\1', line) line = re.sub(r'\*\*', '', line) pdf_content.append(line) else: for line in lines: line = line.strip() if not line or line.startswith('# '): continue bold_pattern = re.compile(r'\*\*(.*?)\*\*') if bold_pattern.search(line): line = bold_pattern.sub(r'\1', line) line = re.sub(r'\*\*', '', line) if line.startswith('## ') or line.startswith('### '): text = line.replace('## ', '').replace('### ', '').strip() pdf_content.append(f"{text}") elif auto_bold_numbers and number_pattern.match(line): pdf_content.append(f"{line}") else: pdf_content.append(line.strip()) total_lines = len(pdf_content) return pdf_content, total_lines def create_pdf(markdown_text, base_font_size, plain_text_mode, num_columns, auto_bold_numbers, enlarge_font_size): buffer = io.BytesIO() page_width = A4[0] * 2 page_height = A4[1] doc = SimpleDocTemplate(buffer, pagesize=(page_width, page_height), leftMargin=36, rightMargin=36, topMargin=36, bottomMargin=36) styles = getSampleStyleSheet() story = [] spacer_height = 10 section_spacer_height = 15 pdf_content, total_lines = markdown_to_pdf_content(markdown_text, plain_text_mode, auto_bold_numbers) item_font_size = base_font_size section_font_size = base_font_size * 1.1 numbered_font_size = base_font_size + 1 if enlarge_font_size else base_font_size section_style = ParagraphStyle( 'SectionStyle', parent=styles['Heading2'], fontName="DejaVuSans", textColor=colors.darkblue, fontSize=section_font_size, leading=section_font_size * 1.2, spaceAfter=2 ) item_style = ParagraphStyle( 'ItemStyle', parent=styles['Normal'], fontName="DejaVuSans", fontSize=item_font_size, leading=item_font_size * 1.15, spaceAfter=1 ) numbered_style = ParagraphStyle( 'NumberedStyle', parent=styles['Normal'], fontName="DejaVuSans", fontSize=numbered_font_size, leading=numbered_font_size * 1.15, spaceAfter=1 ) story.append(Spacer(1, spacer_height)) columns = [[] for _ in range(num_columns)] lines_per_column = total_lines / num_columns if num_columns > 0 else total_lines current_line_count = 0 current_column = 0 number_pattern = re.compile(r'^\d+\.\s') for i, item in enumerate(pdf_content): if i > 0 and number_pattern.match(item.replace('', '').replace('', '')): columns[current_column].append(Spacer(1, section_spacer_height)) if current_line_count >= lines_per_column and current_column < num_columns - 1: current_column += 1 current_line_count = 0 columns[current_column].append(item) current_line_count += 1 column_cells = [[] for _ in range(num_columns)] for col_idx, column in enumerate(columns): for item in column: if isinstance(item, Spacer): column_cells[col_idx].append(item) elif isinstance(item, str) and item.startswith(''): text = item.replace('', '').replace('', '') column_cells[col_idx].append(Paragraph(apply_emoji_font(text, selected_font_name), section_style)) elif number_pattern.match(item): column_cells[col_idx].append(Paragraph(apply_emoji_font(item, selected_font_name), numbered_style)) else: column_cells[col_idx].append(Paragraph(apply_emoji_font(item, selected_font_name), item_style)) max_cells = max(len(cells) for cells in column_cells) if column_cells else 0 for cells in column_cells: cells.extend([Paragraph("", item_style)] * (max_cells - len(cells))) col_width = (page_width - 72) / num_columns if num_columns > 0 else page_width - 72 table_data = list(zip(*column_cells)) if column_cells else [[]] table = Table(table_data, colWidths=[col_width] * num_columns, hAlign='CENTER') table.setStyle(TableStyle([ ('VALIGN', (0, 0), (-1, -1), 'TOP'), ('ALIGN', (0, 0), (-1, -1), 'LEFT'), ('BACKGROUND', (0, 0), (-1, -1), colors.white), ('GRID', (0, 0), (-1, -1), 0, colors.white), ('LINEAFTER', (0, 0), (num_columns-1, -1), 0.5, colors.grey), ('LEFTPADDING', (0, 0), (-1, -1), 2), ('RIGHTPADDING', (0, 0), (-1, -1), 2), ('TOPPADDING', (0, 0), (-1, -1), 1), ('BOTTOMPADDING', (0, 0), (-1, -1), 1), ])) story.append(table) doc.build(story) buffer.seek(0) return buffer.getvalue() def pdf_to_image(pdf_bytes): try: doc = fitz.open(stream=pdf_bytes, filetype="pdf") images = [] for page in doc: pix = page.get_pixmap(matrix=fitz.Matrix(2.0, 2.0)) img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) images.append(img) doc.close() return images except Exception as e: st.error(f"Failed to render PDF preview: {e}") return None with st.spinner("Generating PDF..."): pdf_bytes = create_pdf(st.session_state.markdown_content, base_font_size, plain_text_mode, num_columns, auto_bold_numbers, enlarge_font_size) with st.container(): pdf_images = pdf_to_image(pdf_bytes) if pdf_images: for img in pdf_images: st.image(img, use_container_width=True) else: st.info("Download the PDF to view it locally.") with st.sidebar: st.download_button(label="Download PDF", data=pdf_bytes, file_name="deities_guide.pdf", mime="application/pdf") def main(): init_session_state() load_mp3_viewer() saved_username = load_username() if saved_username and saved_username in FUN_USERNAMES: st.session_state.username = saved_username if not st.session_state.username: available = [n for n in FUN_USERNAMES if not any(f"{n} has joined" in l for l in asyncio.run(load_chat()))] st.session_state.username = random.choice(available or list(FUN_USERNAMES.keys())) st.session_state.tts_voice = FUN_USERNAMES[st.session_state.username] asyncio.run(save_chat_entry("System ๐", f"{st.session_state.username} has joined {START_ROOM}!", "en-US-AriaNeural")) save_username(st.session_state.username) st.title(f"{Site_Name} for {st.session_state.username}") update_marquee_settings_ui() chat_text = get_chat_text_only() display_marquee(f"๐ Welcome to {START_ROOM} | ๐ค {st.session_state.username} | Chat: {chat_text}", st.session_state['marquee_settings'], "welcome") mycomponent = components.declare_component("mycomponent", path="mycomponent") val = mycomponent(my_input_value="", key=f"speech_{st.session_state.get('speech_processed', False)}") if val and val != st.session_state.last_transcript: val_stripped = val.strip().replace('\n', ' ') if val_stripped: voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural") st.session_state['speech_processed'] = True md_file, audio_file = asyncio.run(save_chat_entry(st.session_state.username, val_stripped, voice)) if audio_file: play_and_download_audio(audio_file) st.rerun() tab_main = st.radio("Action:", ["๐ค Chat & Voice", "๐ ArXiv", "๐ PDF to Audio", "๐ PDF Output"], horizontal=True, key="tab_main") st.checkbox("Search ArXiv", key="use_arxiv") st.checkbox("ArXiv Audio", key="use_arxiv_audio") st.checkbox("Autosend Chat", key="autosend") st.checkbox("Autosearch ArXiv", key="autosearch") if tab_main == "๐ค Chat & Voice": st.subheader(f"{START_ROOM} Chat ๐ฌ") chat_content = asyncio.run(load_chat()) chat_container = st.container() with chat_container: numbered_content = "\n".join(f"{i+1}. {line}" for i, line in enumerate(chat_content)) st.code(numbered_content, language="python") message = st.text_input(f"Message as {st.session_state.username}", key="message_input") col_paste, col_upload = st.columns(2) with col_paste: pasted_image, img_type = paste_image_component() with col_upload: uploaded_files = st.file_uploader("Upload Files", accept_multiple_files=True, type=["mp3", "png", "mp4", "md"], key="file_upload") if pasted_image is not None: if st.session_state['paste_image_base64'] != base64.b64encode(pasted_image.tobytes()).decode('utf-8'): st.session_state['paste_image_base64'] = base64.b64encode(pasted_image.tobytes()).decode('utf-8') voice = FUN_USERNAMES.get(st.session_state.username, "en-USA-AriaNeural") image_prompt = st.text_input("Add a prompt for Claude (e.g., 'OCR this image')", key="image_prompt", value="") with st.spinner("Saving image..."): filename = asyncio.run(save_pasted_image(pasted_image, st.session_state.username, image_prompt)) if filename: st.success(f"Image saved as: {filename}") if image_prompt: with st.spinner("Processing with Claude..."): md_file_claude, audio_file_claude, claude_result = asyncio.run( perform_claude_search(image_prompt, st.session_state.username, pasted_image) ) if audio_file_claude: play_and_download_audio(audio_file_claude) if claude_result: with st.spinner("Searching ArXiv..."): md_file_arxiv, audio_file_arxiv = asyncio.run( perform_arxiv_search(image_prompt, st.session_state.username, claude_result) ) if audio_file_arxiv: play_and_download_audio(audio_file_arxiv) st.session_state.pasted_image_data = None st.session_state['paste_image_base64'] = "" st.session_state.timer_start = time.time() save_username(st.session_state.username) st.rerun() if uploaded_files: for uploaded_file in uploaded_files: file_type = uploaded_file.name.split('.')[-1].lower() if file_type in ["mp3", "png", "mp4", "md"]: timestamp = format_timestamp_prefix(st.session_state.username) filename = f"{timestamp}-{clean_text_for_filename(uploaded_file.name)}" with open(filename, "wb") as f: f.write(uploaded_file.getbuffer()) st.success(f"Uploaded {file_type.upper()} as: {filename}") if file_type == "png": img = Image.open(filename) st.image(img, caption=f"Uploaded Image: {filename}", use_column_width=True) elif file_type == "mp3": st.audio(filename) elif file_type == "mp4": st.video(filename) elif file_type == "md": with open(filename, 'r', encoding='utf-8') as f: st.markdown(f.read()) asyncio.run(save_chat_entry(st.session_state.username, f"Uploaded {file_type.upper()}: {filename}", FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural"))) st.session_state.timer_start = time.time() save_username(st.session_state.username) st.rerun() if (message and message != st.session_state.last_message) or (st.session_state.pasted_image_data and not st.session_state['paste_image_base64']): st.session_state.last_message = message col_send, col_claude, col_arxiv = st.columns([1, 1, 1]) with col_send: if st.session_state.autosend or st.button("Send ๐", key="send_button"): voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural") if message.strip(): md_file, audio_file = asyncio.run(save_chat_entry(st.session_state.username, message, voice, True)) if audio_file: play_and_download_audio(audio_file) if st.session_state.pasted_image_data: asyncio.run(save_chat_entry(st.session_state.username, f"Pasted image: {st.session_state.pasted_image_data}", voice)) st.session_state.pasted_image_data = None st.session_state.timer_start = time.time() save_username(st.session_state.username) st.rerun() with col_claude: if st.button("๐ง Claude", key="claude_button"): voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural") if message.strip(): md_file, audio_file, _ = asyncio.run(perform_claude_search(message, st.session_state.username)) if audio_file: play_and_download_audio(audio_file) st.session_state.timer_start = time.time() save_username(st.session_state.username) st.rerun() with col_arxiv: if st.button("๐ ArXiv", key="arxiv_button"): voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural") if message.strip(): md_file, audio_file = asyncio.run(perform_arxiv_search(message, st.session_state.username)) if audio_file: play_and_download_audio(audio_file) st.session_state.timer_start = time.time() save_username(st.session_state.username) st.rerun() elif tab_main == "๐ ArXiv": st.subheader("๐ Query ArXiv") q = st.text_input("๐ Query:", key="arxiv_query") if q and q != st.session_state.last_query: st.session_state.last_query = q if st.session_state.autosearch or st.button("๐ Run", key="arxiv_run"): result, papers = asyncio.run(perform_ai_lookup(q, useArxiv=st.session_state['use_arxiv'], useArxivAudio=st.session_state['use_arxiv_audio'])) st.markdown(f"### Query: {q}") for i, p in enumerate(papers, 1): expander_label = f"{p['title']} | [arXiv Link]({p['url']})" with st.expander(expander_label): with open(p['md_file'], 'r', encoding='utf-8') as f: content = f.read() numbered_content = "\n".join(f"{j+1}. {line}" for j, line in enumerate(content.split('\n'))) st.code(numbered_content, language="python") elif tab_main == "๐ PDF to Audio": audio_processor = AudioProcessor() pdf_file = st.file_uploader("Choose PDF", "pdf", key="pdf_upload") max_pagesย W = st.slider('Pages', 1, 100, 10, key="pdf_pages") if pdf_file: with st.spinner('Processing...'): texts, audios, total = process_pdf(pdf_file, max_pages, st.session_state['tts_voice'], audio_processor) for i, text in enumerate(texts): with st.expander(f"Page {i+1}"): st.markdown(text) while i not in audios: time.sleep(0.1) if audios.get(i): st.audio(audios[i]) st.markdown(get_download_link(audios[i], "mp3"), unsafe_allow_html=True) voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural") asyncio.run(save_chat_entry(st.session_state.username, f"PDF Page {i+1} converted to audio: {audios[i]}", voice)) elif tab_main == "๐ PDF Output": create_pdf_tab(default_markdown) st.header("๐ธ Media Gallery") all_files = sorted(glob.glob("*.md") + glob.glob("*.mp3") + glob.glob("*.png") + glob.glob("*.mp4"), key=os.path.getmtime) md_files = [f for f in all_files if f.endswith('.md') and os.path.basename(f) != "README.md"] mp3_files = [f for f in all_files if f.endswith('.mp3')] png_files = [f for f in all_files if f.endswith('.png')] mp4_files = [f for f in all_files if f.endswith('.mp4')] st.subheader("All Submitted Text") all_md_content = concatenate_markdown_files() with st.expander("View All Markdown Content"): st.markdown(all_md_content) st.subheader("๐ต Audio (MP3)") for filename, (num, mp3) in sorted(st.session_state['mp3_files'].items(), key=lambda x: x[1][0]): with st.expander(f"{num}. {os.path.basename(mp3)}"): st.audio(mp3) st.markdown(get_download_link(mp3, "mp3"), unsafe_allow_html=True) st.subheader("๐ผ๏ธ Images (PNG)") for png in sorted(png_files, key=os.path.getmtime): with st.expander(os.path.basename(png)): st.image(png, use_container_width=True) st.markdown(get_download_link(png, "png"), unsafe_allow_html=True) st.subheader("๐ฅ Videos (MP4)") for mp4 in sorted(mp4_files, key=os.path.getmtime): with st.expander(os.path.basename(mp4)): st.video(mp4) st.markdown(get_download_link(mp4, "mp4"), unsafe_allow_html=True) st.sidebar.subheader("Voice Settings") new_username = st.sidebar.selectbox("Change Name/Voice", list(FUN_USERNAMES.keys()), index=list(FUN_USERNAMES.keys()).index(st.session_state.username), key="username_select") if new_username != st.session_state.username: asyncio.run(save_chat_entry("System ๐", f"{st.session_state.username} changed to {new_username}", "en-US-AriaNeural")) st.session_state.username, st.session_state.tts_voice = new_username, FUN_USERNAMES[new_username] st.session_state.timer_start = time.time() save_username(st.session_state.username) st.rerun() st.sidebar.markdown("### ๐ฌ Chat Dialog") chat_content = asyncio.run(load_chat()) with st.sidebar.expander("Chat History"): numbered_content = "\n".join(f"{i+1}. {line}" for i, line in enumerate(chat_content)) st.code(numbered_content, language="python") st.sidebar.markdown("### ๐ฌ Chat Text Only") chat_text_only = get_chat_text_only() with st.sidebar.expander("Text Only History"): numbered_text = "\n".join(f"{i+1}. {line}" for i, line in enumerate(chat_text_only.split('\n'))) st.code(numbered_text, language="python") st.sidebar.subheader("Vote Totals") chat_votes = load_votes(QUOTE_VOTES_FILE) image_votes = load_votes(IMAGE_VOTES_FILE) for item, count in chat_votes.items(): st.sidebar.write(f"{item}: {count} votes") for image, count in image_votes.items(): st.sidebar.write(f"{image}: {count} votes") st.sidebar.markdown("### ๐ File History") for f in all_files[:10]: st.sidebar.write(f"{FILE_EMOJIS.get(f.split('.')[-1], '๐')} {os.path.basename(f)}") st.sidebar.subheader("๐ฆ Zip & Delete") col_zip, col_del = st.sidebar.columns(2) with col_zip: if st.button("โฌ๏ธ Zip All", key="zip_all"): zip_name = create_zip_of_files(all_files, "All") if zip_name: st.session_state['download_link_cache'] = {} if st.button("โฌ๏ธ Zip All MD", key="zip_md"): zip_name = create_zip_of_files(md_files, "MD") if zip_name: st.session_state['download_link_cache'] = {} if st.button("โฌ๏ธ Zip All MP3", key="zip_mp3"): zip_name = create_zip_of_files(mp3_files, "MP3") if zip_name: st.session_state['download_link_cache'] = {} if st.button("โฌ๏ธ Zip All PNG", key="zip_png"): zip_name = create_zip_of_files(png_files, "PNG") if zip_name: st.session_state['download_link_cache'] = {} if st.button("โฌ๏ธ Zip All MP4", key="zip_mp4"): zip_name = create_zip_of_files(mp4_files, "MP4") if zip_name: st.session_state['download_link_cache'] = {} with col_del: if st.button("๐๏ธ Del All", key="del_all"): for ft in ["md", "mp3", "png", "mp4"]: delete_files(ft) st.rerun() if st.button("๐๏ธ Del All MD", key="del_md"): delete_files("md") st.rerun() if st.button("๐๏ธ Del All MP3", key="del_mp3"): delete_files("mp3") st.rerun() if st.button("๐๏ธ Del All PNG", key="del_png"): delete_files("png") st.rerun() if st.button("๐๏ธ Del All MP4", key="del_mp4"): delete_files("mp4") st.rerun() if st.button("๐๏ธ Del All Zip", key="del_zip"): delete_files("zip", exclude_files=[]) st.rerun() zip_files = sorted(glob.glob("*.zip"), key=os.path.getmtime, reverse=True) for zip_file in zip_files: st.sidebar.markdown(get_download_link(zip_file, "zip"), unsafe_allow_html=True) st.sidebar.subheader("Set Refresh Rate โณ") st.session_state['auto_refresh'] = st.sidebar.radio("Auto Refresh", ["On", "Off"], index=0 if st.session_state['auto_refresh'] else 1) == "On" st.markdown(""" """, unsafe_allow_html=True) refresh_rate = st.sidebar.slider("Refresh Rate (seconds)", min_value=1, max_value=300, value=st.session_state.refresh_rate, step=1) if refresh_rate != st.session_state.refresh_rate: st.session_state.refresh_rate = refresh_rate st.session_state.timer_start = time.time() save_username(st.session_state.username) col1, col2, col3 = st.sidebar.columns(3) with col1: if st.button("๐ Small (1s)"): st.session_state.refresh_rate = 1 st.session_state.timer_start = time.time() save_username(st.session_state.username) with col2: if st.button("๐ข Medium (10s)"): st.session_state.refresh_rate = 10 st.session_state.timer_start = time.time() save_username(st.session_state.username) with col3: if st.button("๐ Large (5m)"): st.session_state.refresh_rate = 300 st.session_state.timer_start = time.time() save_username(st.session_state.username) timer_placeholder = st.sidebar.empty() def update_timer(): start_time = st.session_state.timer_start remaining_time = max(0, int(st.session_state.refresh_rate - (time.time() - start_time))) timer_placeholder.markdown(f"
โณ Next refresh in: {remaining_time} seconds
", unsafe_allow_html=True) if st.session_state['auto_refresh'] and remaining_time <= 0: st.session_state.timer_start = time.time() st.session_state.last_refresh = time.time() st.rerun() threading.Thread(target=lambda: [time.sleep(1) or update_timer() for _ in range(int(st.session_state.refresh_rate)+1)], daemon=True).start() update_timer() if not st.session_state.get('server_running', False) and not st.session_state.get('server_task', None): st.session_state.server_task = threading.Thread(target=start_websocket_server, daemon=True) st.session_state.server_task.start() default_markdown = """# Deities Guide: Mythology and Moral Lessons ๐โจ 1. ๐ **Introduction** - **Purpose**: Explore deities, spirits, saints, and beings with their epic stories and morals! ๐๐ - **Usage**: A guide for learning and storytelling across traditions. ๐ญโ๏ธ - **Themes**: Justice โ๏ธ, faith ๐, hubris ๐ค, redemption ๐, cosmic order ๐. 2. ๐ ๏ธ **Core Concepts of Divinity** - **Powers**: Creation ๐, omniscience ๐๏ธโ๐จ๏ธ, shapeshifting ๐ฆ across entities. - **Life Cycle**: Mortality ๐, immortality โจ, transitions like saints and avatars ๐. - **Communication**: Omens ๐ฉ๏ธ, visions ๐๏ธ, miracles โจ from gods and spirits. 3. โก **Standard Abilities** - **Creation**: Gods and spirits shape worlds, e.g., Allah ๐ and Vishnu ๐. - **Influence**: Saints and prophets intercede, like Muhammad ๐ and Paul โ๏ธ. - **Transformation**: Angels and avatars shift forms, e.g., Gabriel ๐ and Krishna ๐ฆ. - **Knowledge**: Foresight ๐ฎ or revelation ๐, as with the Holy Spirit ๐๏ธ and Brahma ๐ง . - **Judgment**: Divine authority ๐, e.g., Yahweh โ๏ธ and Yama ๐. 4. โณ **Mortality and Immortality** - **Gods**: Eternal โฐ, like Allah ๐ and Shiva ๐๏ธ. - **Spirits**: Realm-bound ๐ , e.g., jinn ๐ฅ and devas โจ. - **Saints/Prophets**: Mortal to divine ๐โก๏ธ๐, e.g., Moses ๐ and Rama ๐น. - **Beings**: Limbo states โ, like cherubim ๐ and rakshasas ๐น. - **Lessons**: Faith ๐ and duty โ๏ธ define transitions. 5. ๐ **Ascension and Signs** - **Paths**: Birth ๐ถ, deeds ๐ก๏ธ, revelation ๐, as with Jesus โ๏ธ and Arjuna ๐น. - **Signs**: Miracles โจ and prophecies ๐ฎ, like those in the Quran ๐ and Gita ๐. - **Morals**: Obedience ๐ง and devotion โค๏ธ shape destiny ๐. 6. ๐ฒ **Storytelling and Games** - **Portrayal**: Gods, spirits, and saints in narratives or RPGs ๐ฎ๐. - **Dynamics**: Clerics โช, imams ๐, and sadhus ๐ง serve higher powers. - **Balance**: Power ๐ช vs. personality ๐ for depth. 7. ๐ฎ **Dungeon Mastering Beings** - **Gods**: Epic scope ๐, e.g., Allah โจ and Vishnu ๐. - **Spirits**: Local influence ๐๏ธ, like jinn ๐ฅ and apsaras ๐. - **Saints**: Moral anchors โ, e.g., St. Francis ๐พ and Ali โ๏ธ. 8. ๐ **Devotee Relationships** - **Clerics**: Serve gods, e.g., Krishnaโs priests ๐ฆ. - **Mediums**: Channel spirits, like jinn whisperers ๐ฅ๐๏ธ. - **Faithful**: Venerate saints and prophets, e.g., Fatimaโs followers ๐น. 9. ๐ฆ **American Indian Traditions** - **Coyote, Raven, White Buffalo Woman**: Trickster kin ๐ฆ๐ฆ and wise mother ๐. - **Relation**: Siblings and guide teach balance โ๏ธ. - **Lesson**: Chaos ๐ช๏ธ breeds wisdom ๐ง . 10. โ๏ธ **Arthurian Legends** - **Merlin, Morgan le Fay, Arthur**: Mentor ๐ง, rival ๐งโโ๏ธ, son ๐. - **Relation**: Family tests loyalty ๐ค. - **Lesson**: Honor ๐ก๏ธ vs. betrayal ๐ก๏ธ. 11. ๐๏ธ **Babylonian Mythology** - **Marduk, Tiamat, Ishtar**: Son โ๏ธ, mother ๐, lover โค๏ธ. - **Relation**: Kinship drives order ๐ฐ. - **Lesson**: Power ๐ช reshapes chaos ๐ช๏ธ. 12. โ๏ธ **Christian Trinity** - **God (Yahweh), Jesus, Holy Spirit**: Father ๐, Son โ๏ธ, Spirit ๐๏ธ. - **Relation**: Divine family redeems ๐. - **Lesson**: Faith ๐ restores grace โจ. 13. ๐ **Christian Saints & Angels** - **St. Michael, Gabriel, Mary**: Warrior โ๏ธ, messenger ๐, mother ๐น. - **Relation**: Heavenly kin serve God ๐. - **Lesson**: Duty โ๏ธ upholds divine will ๐. 14. ๐ **Celtic Mythology** - **Lugh, Morrigan, Cernunnos**: Son โ๏ธ, mother ๐ฆ, father ๐ฆ. - **Relation**: Family governs cycles ๐. - **Lesson**: Courage ๐ช in fate ๐ฒ. 15. ๐ **Central American Traditions** - **Quetzalcoatl, Tezcatlipoca, Huitzilopochtli**: Brothers ๐๐ and war son โ๏ธ. - **Relation**: Sibling rivalry creates ๐. - **Lesson**: Sacrifice ๐ฉธ builds worlds ๐ฐ. 16. ๐ **Chinese Mythology** - **Jade Emperor, Nuwa, Sun Wukong**: Father ๐, mother ๐, rebel son ๐. - **Relation**: Family enforces harmony ๐ถ. - **Lesson**: Duty โ๏ธ curbs chaos ๐ช๏ธ. 17. ๐ **Cthulhu Mythos** - **Cthulhu, Nyarlathotep, Yog-Sothoth**: Elder kin ๐๐๏ธโ๐จ๏ธ๐. - **Relation**: Cosmic trio overwhelms ๐ฑ. - **Lesson**: Insignificance ๐ humbles ๐. 18. โฅ **Egyptian Mythology** - **Ra, Osiris, Isis**: Father โ๏ธ, son โฐ๏ธ, mother ๐. - **Relation**: Family ensures renewal ๐. - **Lesson**: Justice โ๏ธ prevails. 19. โ๏ธ **Finnish Mythology** - **Vรคinรคmรถinen, Louhi, Ukko**: Son ๐ถ, mother โ๏ธ, father โก. - **Relation**: Kinship tests wisdom ๐ง . - **Lesson**: Perseverance ๐๏ธ wins. 20. ๐๏ธ **Greek Mythology** - **Zeus, Hera, Athena**: Father โก, mother ๐, daughter ๐ฆ. - **Relation**: Family rules with tension โ๏ธ. - **Lesson**: Hubris ๐ค meets wisdom ๐ง . 21. ๐๏ธ **Hindu Trimurti** - **Brahma, Vishnu, Shiva**: Creator ๐, preserver ๐ก๏ธ, destroyer ๐ฅ. - **Relation**: Divine trio cycles existence ๐. - **Lesson**: Balance โ๏ธ sustains life ๐. 22. ๐บ **Hindu Avatars & Devis** - **Krishna, Rama, Durga**: Sons ๐ฆ๐น and fierce mother ๐ก๏ธ. - **Relation**: Avatars and goddess protect dharma โ๏ธ. - **Lesson**: Duty โ๏ธ defeats evil ๐น. 23. ๐ธ **Japanese Mythology** - **Amaterasu, Susanoo, Tsukuyomi**: Sister โ๏ธ, brothers ๐๐. - **Relation**: Siblings balance cosmos ๐. - **Lesson**: Harmony ๐ถ vs. chaos ๐ช๏ธ. 24. ๐ก๏ธ **Melnibonean Legends** - **Arioch, Xiombarg, Elric**: Lords ๐ and mortal son โ๏ธ. - **Relation**: Pact binds chaos ๐ช๏ธ. - **Lesson**: Power ๐ช corrupts ๐. 25. โช๏ธ **Muslim Divine & Messengers** - **Allah, Muhammad, Gabriel**: God ๐, prophet ๐, angel ๐. - **Relation**: Messenger reveals divine will ๐. - **Lesson**: Submission ๐ brings peace โฎ๏ธ. 26. ๐ป **Muslim Spirits & Kin** - **Jinn, Iblis, Khidr**: Spirits ๐ฅ๐ and guide ๐ฟ defy or aid. - **Relation**: Supernatural kin test faith ๐. - **Lesson**: Obedience ๐ง vs. rebellion ๐ก. 27. ๐ฐ **Nehwon Legends** - **Death, Ningauble, Sheelba**: Fateful trio ๐๐๏ธโ๐จ๏ธ๐ฟ. - **Relation**: Guides shape destiny ๐ฒ. - **Lesson**: Cunning ๐ง defies fate โฐ๏ธ. 28. ๐ง **Nonhuman Traditions** - **Corellon, Moradin, Gruumsh**: Elf ๐ง, dwarf โ๏ธ, orc ๐ก๏ธ fathers. - **Relation**: Rivals define purpose โ๏ธ. - **Lesson**: Community ๐ค endures. 29. แฑ **Norse Mythology** - **Odin, Frigg, Loki**: Father ๐๏ธ, mother ๐, trickster son ๐ฆ. - **Relation**: Family faces doom โก. - **Lesson**: Sacrifice ๐ฉธ costs. 30. ๐ฟ **Sumerian Mythology** - **Enki, Inanna, Anu**: Son ๐, daughter โค๏ธ, father ๐. - **Relation**: Kin wield knowledge ๐ง . - **Lesson**: Ambition ๐ shapes. 31. ๐ **Appendices** - **Planes**: Realms of gods, spirits, saints, e.g., Paradise ๐ and Svarga โจ. - **Symbols**: Rituals ๐๏ธ and artifacts ๐ฟ of faith. - **Charts**: Domains and duties for devotees ๐. 32. ๐ **Planes of Existence** - **Heaven/Paradise**: Christian/Muslim abode ๐. - **Svarga**: Hindu divine realm โจ. - **Underworld**: Spirits linger, e.g., Sheol โฐ๏ธ and Naraka ๐ฅ. 33. ๐ **Temple Trappings** - **Cross/Crescent**: Christian/Muslim faith โ๏ธโช๏ธ. - **Mandalas**: Hindu devotion ๐. - **Relics**: Saintsโ and prophetsโ legacy ๐๏ธ. 34. ๐ **Clerical Chart** - **Gods**: Domains, e.g., creation ๐ and mercy โค๏ธ. - **Spirits**: Influence, like guidance ๐ฟ and mischief ๐. - **Saints/Prophets**: Virtues, e.g., justice โ๏ธ and prophecy ๐ฎ. """ if __name__ == "__main__": main()