File size: 25,199 Bytes
1ebfa27
 
 
 
 
1b77036
1ebfa27
 
 
 
 
 
 
 
 
 
 
 
 
1b77036
1ebfa27
 
a68d9ac
 
 
 
 
 
 
 
 
 
 
1ebfa27
1b77036
1ebfa27
 
a68d9ac
1ebfa27
a68d9ac
 
1ebfa27
 
 
 
a68d9ac
 
 
1ebfa27
 
 
 
 
 
 
 
 
 
 
1b77036
 
 
 
 
 
 
 
 
 
1ebfa27
1b77036
a68d9ac
 
 
 
 
1ebfa27
a68d9ac
 
1ebfa27
a68d9ac
1b77036
a68d9ac
 
 
 
 
 
 
 
 
1ebfa27
 
a68d9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ebfa27
 
 
a68d9ac
 
1ebfa27
a68d9ac
1ebfa27
a68d9ac
 
1ebfa27
 
a68d9ac
1ebfa27
 
 
 
a68d9ac
 
 
 
 
 
 
 
1ebfa27
a68d9ac
 
 
 
 
 
 
 
 
1ebfa27
 
a68d9ac
 
1ebfa27
a68d9ac
 
1ebfa27
a68d9ac
1ebfa27
 
 
 
 
 
 
 
 
 
 
 
 
a68d9ac
 
1ebfa27
 
 
 
 
 
 
a68d9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ebfa27
 
a68d9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b77036
a68d9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ebfa27
 
a68d9ac
 
 
 
 
1ebfa27
a68d9ac
 
 
 
 
 
 
 
 
 
1ebfa27
 
a68d9ac
1b77036
a68d9ac
 
 
 
 
 
 
 
 
 
 
 
1ebfa27
a68d9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ebfa27
 
 
a68d9ac
 
 
1ebfa27
 
a68d9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b77036
a68d9ac
 
 
 
 
1ebfa27
 
1b77036
 
 
 
 
 
 
 
 
 
 
 
 
 
a68d9ac
 
 
 
 
 
 
1ebfa27
a68d9ac
 
1ebfa27
 
 
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
import streamlit as st
import asyncio
import websockets
import uuid
import argparse
from datetime import datetime
import os
import random
import time
import hashlib
from PIL import Image
import glob
import base64
import io
import streamlit.components.v1 as components
import edge_tts
from audio_recorder_streamlit import audio_recorder
import nest_asyncio
import re
from streamlit_paste_button import paste_image_button
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

# ๐Ÿ› ๏ธ Patch asyncio for nesting
nest_asyncio.apply()

# ๐ŸŽจ Page Config
st.set_page_config(
    page_title="๐ŸšฒTalkingAIResearcher๐Ÿ†",
    page_icon="๐Ÿšฒ๐Ÿ†",
    layout="wide",
    initial_sidebar_state="auto"
)

# ๐ŸŒŸ Static Config
icons = '๐Ÿค–๐Ÿง ๐Ÿ”ฌ๐Ÿ“'
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": "๐ŸŽต", "wav": "๐Ÿ”Š"}

# ๐Ÿ“ Directories
for d in ["chat_logs", "vote_logs", "audio_logs", "history_logs", "media_files", "audio_cache"]:
    os.makedirs(d, exist_ok=True)

CHAT_FILE = "chat_logs/global_chat.md"
HISTORY_FILE = "history_logs/chat_history.md"
MEDIA_DIR = "media_files"
AUDIO_CACHE_DIR = "audio_cache"
AUDIO_DIR = "audio_logs"

# ๐Ÿ”‘ API Keys
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)

# ๐Ÿ•’ Timestamp Helper
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}"

# ๐Ÿ“ˆ Performance Timer
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)

# ๐ŸŽ›๏ธ Session State Init
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': 5, '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': {}  # Store MP3s with chat lines
    }
    for k, v in defaults.items():
        if k not in st.session_state: st.session_state[k] = v

# ๐Ÿ–Œ๏ธ Marquee Helpers
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("")

# ๐Ÿ“ Text & File Helpers
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())[:200]
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, response, file_type="md"):
    prefix = format_timestamp_prefix()
    terms = get_high_info_terms(prompt + " " + response, 5)
    snippet = clean_text_for_filename(prompt[:40] + " " + response[:40])
    wct, sw = len(prompt.split()), len(response.split())
    dur = round((wct + sw) / 2.5)
    base = '_'.join(list(dict.fromkeys(terms + [snippet])))[:200 - len(prefix) - len(f"_wct{wct}_sw{sw}_dur{dur}.{file_type}")]
    return f"{prefix}{base}_wct{wct}_sw{sw}_dur{dur}.{file_type}"

def create_file(prompt, response, file_type="md"):
    filename = generate_filename(prompt, response, file_type)
    with open(filename, 'w', encoding='utf-8') as f: f.write(prompt + "\n\n" + response)
    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()
        st.session_state['download_link_cache'][cache_key] = f'<a href="data:audio/mpeg;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]

# ๐ŸŽถ Audio Processing
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: return None, 0
    filename = f"{AUDIO_DIR}/{format_timestamp_prefix(username)}_{voice}.{file_format}"
    communicate = edge_tts.Communicate(text, voice, rate=f"{rate:+d}%", pitch=f"{pitch:+d}Hz")
    await communicate.save(filename)
    st.session_state['audio_cache'][cache_key] = filename
    
    md_filename = filename.replace(".mp3", ".md")
    md_content = f"# Chat Audio Log\n\n**Player:** {username}\n**Voice:** {voice}\n**Text:**\n```markdown\n{text}\n```"
    with open(md_filename, 'w', encoding='utf-8') as f: f.write(md_content)
    
    return filename, time.time() - start_time

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)

def load_mp3_viewer():
    mp3_files = glob.glob(f"{AUDIO_DIR}/*.mp3")
    for mp3 in mp3_files:
        filename = os.path.basename(mp3)
        if filename not in st.session_state['mp3_files']:
            st.session_state['mp3_files'][filename] = mp3

async def save_chat_entry(username, message, is_markdown=False):
    central = pytz.timezone('US/Central')
    timestamp = datetime.now(central).strftime("%Y-%m-%d %H:%M:%S")
    entry = f"[{timestamp}] {username}: {message}" if not is_markdown else f"[{timestamp}] {username}:\n```markdown\n{message}\n```"
    with open(CHAT_FILE, 'a') as f: f.write(f"{entry}\n")
    voice = FUN_USERNAMES.get(username, "en-US-AriaNeural")
    audio_file, _ = 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)] = audio_file
    await broadcast_message(f"{username}|{message}", "chat")
    st.session_state.last_chat_update = time.time()
    st.session_state.chat_history.append(entry)
    return 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')
    numbered_content = "\n".join(f"{i+1}. {line}" for i, line in enumerate(lines) if line.strip())
    return numbered_content

# ๐ŸŒ WebSocket Handling
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.split('\n')):
        await save_chat_entry("System ๐ŸŒŸ", f"{username} has joined {START_ROOM}!")
    try:
        async for message in websocket:
            if '|' in message:
                username, content = message.split('|', 1)
                await save_chat_entry(username, content)
            else:
                await websocket.send("ERROR|Message format: username|content")
    except websockets.ConnectionClosed:
        await save_chat_entry("System ๐ŸŒŸ", f"{username} has left {START_ROOM}!")
    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.server_running:
        server = await websockets.serve(websocket_handler, '0.0.0.0', 8765)
        st.session_state.server_running = True
        await server.wait_closed()

# ๐Ÿ“š PDF to Audio
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 open(cache_path, 'rb').read()
        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 open(cache_path, 'rb').read()

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): audios[i] = await audio_processor.create_audio(text, voice)
    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

# ๐Ÿ” ArXiv & AI Lookup
def parse_arxiv_refs(ref_text):
    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': ''}
        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}["{t}"] --> 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}
---
"""

def create_detailed_paper_md(papers): return "# Detailed Summary\n" + "\n".join(generate_5min_feature_markdown(p) for p in papers)

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'])

async def perform_ai_lookup(q, useArxiv=True, useArxivAudio=False):
    client = anthropic.Anthropic(api_key=anthropic_key)
    response = client.messages.create(model="claude-3-sonnet-20240229", max_tokens=1000, messages=[{"role": "user", "content": q}])
    result = response.content[0].text
    st.markdown("### Claude's Reply ๐Ÿง \n" + result)
    md_file = create_file(q, result)
    audio_file, _ = await async_edge_tts_generate(result, st.session_state['tts_voice'], "System")
    play_and_download_audio(audio_file)

    if useArxiv:
        q += result
        gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
        refs = gradio_client.predict(q, 10, "Semantic Search", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md")[0]
        result = f"๐Ÿ”Ž {q}\n\n{refs}"
        md_file, audio_file = create_file(q, result), (await async_edge_tts_generate(result, st.session_state['tts_voice'], "System"))[0]
        play_and_download_audio(audio_file)
        papers = parse_arxiv_refs(refs)
        if papers and useArxivAudio: await create_paper_audio_files(papers, q)
        return result, papers
    return result, []

# ๐Ÿ“ฆ Zip Files
def create_zip_of_files(md_files, mp3_files, query):
    all_files = md_files + mp3_files
    if not all_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 all_files] + [query]), 5)
    zip_name = f"{format_timestamp_prefix()}_{'-'.join(terms)[:20]}.zip"
    with zipfile.ZipFile(zip_name, 'w') as z: [z.write(f) for f in all_files]
    return zip_name

# ๐ŸŽฎ Main Interface
async def async_interface():
    init_session_state()
    load_mp3_viewer()
    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 (await load_chat()).split('\n'))]
        st.session_state.username = random.choice(available or list(FUN_USERNAMES.keys()))
        st.session_state.tts_voice = FUN_USERNAMES[st.session_state.username]
        await save_chat_entry("System ๐ŸŒŸ", f"{st.session_state.username} has joined {START_ROOM}!")

    st.title(f"๐Ÿค–๐Ÿง MMO Chat & Research for {st.session_state.username}๐Ÿ“๐Ÿ”ฌ")
    update_marquee_settings_ui()
    display_marquee(f"๐Ÿš€ Welcome to {START_ROOM} | ๐Ÿค– {st.session_state.username}", st.session_state['marquee_settings'], "welcome")

    if not st.session_state.server_task:
        st.session_state.server_task = asyncio.create_task(run_websocket_server())

    tab_main = st.radio("Action:", ["๐ŸŽค Chat & Voice", "๐Ÿ“ธ Media", "๐Ÿ” ArXiv", "๐Ÿ“š PDF to Audio"], horizontal=True)
    useArxiv, useArxivAudio = st.checkbox("Search ArXiv", True), st.checkbox("ArXiv Audio", False)
    st.session_state.autosend = st.checkbox("Autosend Chat", value=True)
    st.session_state.autosearch = st.checkbox("Autosearch ArXiv", value=True)

    # ๐ŸŽค Chat & Voice
    if tab_main == "๐ŸŽค Chat & Voice":
        st.subheader(f"{START_ROOM} Chat ๐Ÿ’ฌ")
        chat_content = await load_chat()
        chat_container = st.container()
        with chat_container:
            lines = chat_content.split('\n')
            for i, line in enumerate(lines):
                if line.strip():
                    st.markdown(line)
                    for mp3_name, mp3_path in st.session_state['mp3_files'].items():
                        if line.strip() in mp3_name and st.session_state.username in mp3_name:
                            st.audio(mp3_path, key=f"audio_{i}_{mp3_name}")
                            break
        
        message = st.text_input(f"Message as {st.session_state.username}", key="message_input")
        if message and message != st.session_state.last_message:
            st.session_state.last_message = message
            if st.session_state.autosend or st.button("Send ๐Ÿš€"):
                await save_chat_entry(st.session_state.username, message, True)
                st.rerun()

        st.subheader("๐ŸŽค Speech-to-Chat")
        from mycomponent import speech_component
        transcript_data = speech_component(default_value=st.session_state.get('last_transcript', ''))
        if transcript_data and 'value' in transcript_data:
            transcript = transcript_data['value'].strip()
            st.write(f"๐ŸŽ™๏ธ You said: {transcript}")
            if transcript and transcript != st.session_state.last_transcript:
                st.session_state.last_transcript = transcript
                if st.session_state.autosend:
                    await save_chat_entry(st.session_state.username, transcript, True)
                    st.rerun()
                elif st.button("Send to Chat"):
                    await save_chat_entry(st.session_state.username, transcript, True)
                    st.rerun()

    # ๐Ÿ“ธ Media
    elif tab_main == "๐Ÿ“ธ Media":
        st.header("๐Ÿ“ธ Media Gallery")
        tabs = st.tabs(["๐ŸŽต Audio", "๐Ÿ–ผ Images", "๐ŸŽฅ Video"])
        with tabs[0]:
            for a in glob.glob(f"{MEDIA_DIR}/*.mp3"):
                with st.expander(os.path.basename(a)): play_and_download_audio(a)
        with tabs[1]:
            imgs = glob.glob(f"{MEDIA_DIR}/*.png") + glob.glob(f"{MEDIA_DIR}/*.jpg")
            if imgs:
                cols = st.columns(3)
                for i, f in enumerate(imgs): cols[i % 3].image(f, use_container_width=True)
        with tabs[2]:
            for v in glob.glob(f"{MEDIA_DIR}/*.mp4"):
                with st.expander(os.path.basename(v)): st.video(v)

        uploaded_file = st.file_uploader("Upload Media", type=['png', 'jpg', 'mp4', 'mp3'])
        if uploaded_file:
            filename = f"{format_timestamp_prefix(st.session_state.username)}-{hashlib.md5(uploaded_file.getbuffer()).hexdigest()[:8]}.{uploaded_file.name.split('.')[-1]}"
            with open(f"{MEDIA_DIR}/{filename}", 'wb') as f: f.write(uploaded_file.getbuffer())
            await save_chat_entry(st.session_state.username, f"Uploaded: {filename}")
            st.rerun()

    # ๐Ÿ” ArXiv
    elif tab_main == "๐Ÿ” 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"):
                result, papers = await perform_ai_lookup(q, useArxiv, useArxivAudio)
                for i, p in enumerate(papers, 1):
                    with st.expander(f"{i}. ๐Ÿ“„ {p['title']}"):
                        st.markdown(f"**{p['date']} | {p['title']}** โ€” [Link]({p['url']})")
                        st.markdown(generate_5min_feature_markdown(p))
                        if p.get('full_audio'): play_and_download_audio(p['full_audio'])

    # ๐Ÿ“š PDF to Audio
    elif tab_main == "๐Ÿ“š PDF to Audio":
        audio_processor = AudioProcessor()
        pdf_file = st.file_uploader("Choose PDF", "pdf")
        max_pages = st.slider('Pages', 1, 100, 10)
        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[i]:
                            st.audio(audios[i], format='audio/mp3')
                            st.markdown(get_download_link(io.BytesIO(audios[i]), "mp3"), unsafe_allow_html=True)

    # ๐Ÿ—‚๏ธ Sidebar with Dialog and Audio
    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))
    if new_username != st.session_state.username:
        await save_chat_entry("System ๐ŸŒŸ", f"{st.session_state.username} changed to {new_username}")
        st.session_state.username, st.session_state.tts_voice = new_username, FUN_USERNAMES[new_username]
        st.rerun()

    st.sidebar.markdown("### ๐Ÿ’ฌ Chat Dialog & Audio")
    chat_content = await load_chat()
    lines = chat_content.split('\n')
    audio_files = sorted(glob.glob(f"{AUDIO_DIR}/*.mp3"), key=os.path.getmtime, reverse=True)
    for line in lines[-10:]:  # Show last 10 lines for brevity
        if line.strip():
            st.sidebar.markdown(f"**{line}**")
            for mp3 in audio_files:
                mp3_name = os.path.basename(mp3)
                if st.session_state.username in mp3_name and any(word in mp3_name for word in line.split()):
                    st.sidebar.audio(mp3, key=f"sidebar_audio_{mp3_name}")
                    st.sidebar.markdown(get_download_link(mp3), unsafe_allow_html=True)
                    break

    md_files, mp3_files = glob.glob("*.md"), glob.glob(f"{AUDIO_DIR}/*.mp3")
    st.sidebar.markdown("### ๐Ÿ“‚ File History")
    for f in sorted(md_files + mp3_files, key=os.path.getmtime, reverse=True)[:10]:
        st.sidebar.write(f"{FILE_EMOJIS.get(f.split('.')[-1], '๐Ÿ“„')} {os.path.basename(f)}")
    if st.sidebar.button("โฌ‡๏ธ Zip All"):
        zip_name = create_zip_of_files(md_files, mp3_files, "latest_query")
        if zip_name: st.sidebar.markdown(get_download_link(zip_name, "zip"), unsafe_allow_html=True)

def main():
    asyncio.run(async_interface())

if __name__ == "__main__":
    main()