File size: 65,555 Bytes
6532be7 1de8c8e 5612f2e 6532be7 5612f2e 6532be7 5612f2e 1de8c8e 5612f2e 6532be7 5612f2e 6532be7 5612f2e 1de8c8e 5612f2e 1de8c8e 5612f2e 1de8c8e 5612f2e 1de8c8e 5612f2e 1de8c8e 5612f2e |
1 2 3 4 5 6 7 8 9 10 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 |
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'<a href="data:{mime_types.get(file_type, "application/octet-stream")};base64,{b64}" download="{os.path.basename(file)}">{FILE_EMOJIS.get(file_type, "Download")} Download {os.path.basename(file)}</a>'
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'<font face="{emoji_font}">{emoji}</font>'
segments = []
last_pos = 0
for match in emoji_pattern.finditer(text):
start, end = match.span()
if last_pos < start:
segments.append(f'<font face="DejaVuSans">{text[last_pos:start]}</font>')
segments.append(replace_emoji(match))
last_pos = end
if last_pos < len(text):
segments.append(f'<font face="DejaVuSans">{text[last_pos:]}</font>')
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'<b>\1</b>', 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'<b>\1</b>', line)
line = re.sub(r'\*\*', '', line)
if line.startswith('## ') or line.startswith('### '):
text = line.replace('## ', '').replace('### ', '').strip()
pdf_content.append(f"<b>{text}</b>")
elif auto_bold_numbers and number_pattern.match(line):
pdf_content.append(f"<b>{line}</b>")
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('<b>', '').replace('</b>', '')):
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('<b>'):
text = item.replace('<b>', '').replace('</b>', '')
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("""
<style>
.timer {
font-size: 24px;
color: #ffcc00;
text-align: center;
animation: pulse 1s infinite;
}
@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}
</style>
""", 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"<p class='timer'>โณ Next refresh in: {remaining_time} seconds</p>", 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() |