File size: 17,189 Bytes
4a491eb
d67ede3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a491eb
d67ede3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25818e0
d67ede3
 
 
 
 
 
 
 
25818e0
d67ede3
 
25818e0
d67ede3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25818e0
d67ede3
 
 
 
 
 
 
 
 
 
 
25818e0
 
 
 
 
 
 
d67ede3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a491eb
d67ede3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4a491eb
d67ede3
 
 
 
 
 
 
 
 
25818e0
 
d67ede3
 
25818e0
d67ede3
25818e0
d67ede3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25818e0
d67ede3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25818e0
 
 
 
 
 
 
 
 
4a491eb
d67ede3
 
 
 
 
25818e0
d67ede3
 
 
 
 
 
 
 
 
25818e0
 
d67ede3
 
25818e0
d67ede3
25818e0
 
d67ede3
 
25818e0
d67ede3
 
 
 
25818e0
d67ede3
 
25818e0
d67ede3
25818e0
d67ede3
 
 
 
 
 
25818e0
 
 
 
 
 
 
 
 
d67ede3
 
25818e0
d67ede3
 
 
25818e0
 
 
 
 
 
 
 
 
 
d67ede3
25818e0
 
 
 
 
 
 
 
 
 
 
 
d67ede3
25818e0
d67ede3
 
 
 
 
 
 
25818e0
 
d67ede3
 
 
 
 
 
 
25818e0
 
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
import streamlit as st
import asyncio
import websockets
import uuid
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
import pytz
from gradio_client import Client
from streamlit_marquee import streamlit_marquee
from collections import defaultdict, Counter

# Patch asyncio for nesting
nest_asyncio.apply()

# Page Config
st.set_page_config(
    layout="wide",
    page_title="Colorado Sim: Mountains, Memes, & Mayhem ๐Ÿ˜‚๐Ÿ”๏ธ",
    page_icon="๐ŸฆŒ"
)

# Static Config with Character-Driven Sim
Site_Name = "Colorado Sim Chat ๐Ÿ”๏ธโœจ"
START_ROOM = "Rocky Mountain Hub ๐ŸŒฒ"
FUN_USERNAMES = {
    "Trailblazer Tim ๐ŸŒ„": "en-US-GuyNeural",  # Adventurous hiker
    "Meme Queen Mia ๐Ÿ˜‚": "en-US-JennyNeural",  # Meme enthusiast
    "Elk Whisperer Eve ๐ŸฆŒ": "en-GB-SoniaNeural",  # Wildlife lover
    "Tech Titan Tara ๐Ÿ’พ": "en-AU-NatashaNeural",  # Tech-savvy coder
    "Ski Guru Sam โ›ท๏ธ": "en-CA-ClaraNeural",  # Skiing pro
    "Cosmic Camper Cal ๐ŸŒ ": "en-US-AriaNeural",  # Stargazing storyteller
    "Rasta Ranger Rick ๐Ÿƒ": "en-GB-RyanNeural",  # Chill outdoorsman
    "Boulder Bro Ben ๐Ÿชจ": "en-AU-WilliamNeural"  # Rock-climbing dude
}
EDGE_TTS_VOICES = list(set(FUN_USERNAMES.values()))
FILE_EMOJIS = {"md": "๐Ÿ“", "mp3": "๐ŸŽต", "png": "๐Ÿ–ผ๏ธ", "mp4": "๐ŸŽฅ", "zip": "๐Ÿ“ฆ"}

# Directories
for d in ["chat_logs", "audio_logs", "audio_cache"]:
    os.makedirs(d, exist_ok=True)

CHAT_DIR = "chat_logs"
AUDIO_CACHE_DIR = "audio_cache"
AUDIO_DIR = "audio_logs"
STATE_FILE = "user_state.txt"
CHAT_FILE = os.path.join(CHAT_DIR, "sim_chat.md")

# 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}"

# Session State Init
def init_session_state():
    defaults = {
        'server_running': False, 'server_task': None, 'active_connections': {},
        'last_chat_update': 0, 'message_text': "", 'audio_cache': {},
        'transcript_history': [], 'last_transcript': "", 'tts_voice': "en-US-AriaNeural",
        'chat_history': [], 'marquee_settings': {
            "background": "#1E1E1E", "color": "#FFFFFF", "font-size": "14px",
            "animationDuration": "20s", "width": "100%", "lineHeight": "35px"
        }, 'username': None, 'autosend': True, 'last_message': "",
        'mp3_files': {}, 'timer_start': time.time(), 'auto_refresh': True, 'refresh_rate': 10
    }
    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())[:50]

def generate_filename(prompt, username, file_type="md"):
    timestamp = format_timestamp_prefix(username)
    hash_val = hashlib.md5(prompt.encode()).hexdigest()[:8]
    return f"{timestamp}-{hash_val}.{file_type}"

def create_file(prompt, username, file_type="md"):
    filename = generate_filename(prompt, username, file_type)
    with open(filename, 'w', encoding='utf-8') as f:
        f.write(prompt)
    return filename

def get_download_link(file, file_type="mp3"):
    with open(file, "rb") as f:
        b64 = base64.b64encode(f.read()).decode()
    mime_types = {"mp3": "audio/mpeg", "png": "image/png", "md": "text/markdown"}
    return 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 {os.path.basename(file)}</a>'

def save_username(username):
    with open(STATE_FILE, 'w') as f:
        f.write(username)

def load_username():
    if os.path.exists(STATE_FILE):
        with open(STATE_FILE, 'r') as f:
            return f.read().strip()
    return None

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)

# Audio Processing
async def async_edge_tts_generate(text, voice, username):
    cache_key = f"{text[:100]}_{voice}"
    if cache_key in st.session_state['audio_cache']:
        return st.session_state['audio_cache'][cache_key]
    text = clean_text_for_tts(text)
    filename = f"{format_timestamp_prefix(username)}-{hashlib.md5(text.encode()).hexdigest()[:8]}.mp3"
    communicate = edge_tts.Communicate(text, voice)
    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
    return None

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)

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 = await async_edge_tts_generate(message, voice, username)
    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 Colorado Sim! ๐Ÿ”๏ธ\n")
    with open(CHAT_FILE, 'r') as f:
        content = f.read().strip()
    lines = content.split('\n')
    return list(dict.fromkeys(line for line in lines if line.strip()))

# ArXiv Search with Gradio Client
async def perform_arxiv_search(query, username):
    gradio_client = Client("awacke1/Arxiv-Paper-Search-And-QA-RAG-Pattern")
    refs = gradio_client.predict(
        query, 10, "Semantic Search", "mistralai/Mixtral-8x7B-Instruct-v0.1", api_name="/update_with_rag_md"
    )[0]
    result = f"๐Ÿ”Ž ArXiv Insights:\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

# 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())))
    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)
                await perform_arxiv_search(content, username)  # ArXiv response for every chat input
    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())

# Sim-Oriented Mad Libs
def generate_mad_libs_story(username, inputs):
    story = f"""
    ๐ŸŒ„ In the wilds of Colorado, {username} stumbled upon {inputs['plural_noun']}. 
    ๐ŸŒŸ These {inputs['adjective1']} critters were {inputs['verb_ing']} all over the Rockies, 
    ๐ŸŽค freaking out climbers like {inputs['celebrity']}. 
    ๐Ÿ› ๏ธ Holding a {inputs['object']}, {username} felt {inputs['emotion']} as a {inputs['animal']} 
    โณ {inputs['verb_past']} past, yelling {inputs['exclamation']} near {inputs['place']}. 
    ๐Ÿฝ๏ธ Later, munching {inputs['food']} with a {inputs['adjective2']} {inputs['body_part']}, 
    ๐ŸŽต {username} looped โ€œ{inputs['song']}โ€ {inputs['number']} times, 
    ๐Ÿ’ผ deciding to {inputs['verb_present']} as a {inputs['occupation']} 
    ๐ŸŽจ in a {inputs['color']} {inputs['noun']}โ€”all ending with a {inputs['sound']}!
    """
    return story.strip()

# Main Interface
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:
        st.session_state.username = random.choice(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} - {st.session_state.username} ๐Ÿ”๏ธ")
    st.subheader("๐ŸŒฒ Chat, Explore Audio, or Create a Sim Story! ๐ŸŽค")
    update_marquee_settings_ui()
    chat_text = " ".join([line.split(": ")[-1] for line in asyncio.run(load_chat()) if ": " in line])
    display_marquee(f"๐Ÿ”๏ธ {START_ROOM} | ๐ŸŽ™๏ธ {st.session_state.username} | ๐Ÿ’ฌ {chat_text}", st.session_state['marquee_settings'], "welcome")

    tab_main = st.radio("๐ŸŽฎ Action:", ["๐ŸŽค Chat & Voice", "๐ŸŽต Audio Gallery", "โœจ Mad Libs Sim"], horizontal=True)
    st.checkbox("๐Ÿš€ Autosend Chat", key="autosend")

    if tab_main == "๐ŸŽค Chat & Voice":
        st.subheader(f"๐Ÿ’ฌ {START_ROOM} Chat ๐ŸŒŸ")
        chat_content = asyncio.run(load_chat())
        with st.container():
            st.code("\n".join(f"{i+1}. {line}" for i, line in enumerate(chat_content)), language="python")

        message = st.text_input(f"๐Ÿ’ฌ Message as {st.session_state.username}", key="message_input", placeholder="Type your wild thoughts! ๐ŸŒˆ")
        if message and message != st.session_state.last_message:
            st.session_state.last_message = message
            col_send = st.columns(1)[0]
            with col_send:
                if st.session_state.autosend or st.button("๐Ÿš€ Send Message ๐ŸŽ™๏ธ"):
                    voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural")
                    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)
                    st.rerun()

    elif tab_main == "๐ŸŽต Audio Gallery":
        st.subheader("๐ŸŽต Audio Gallery ๐ŸŽง")
        mp3_files = sorted(glob.glob("*.mp3"), key=os.path.getmtime)
        if mp3_files:
            for i, mp3 in enumerate(mp3_files):
                with st.expander(f"๐ŸŽต {i+1}. {os.path.basename(mp3)}"):
                    play_and_download_audio(mp3)
        else:
            st.write("๐ŸŽถ No audio files yetโ€”chat or create a story to generate some! ๐ŸŽค")

    elif tab_main == "โœจ Mad Libs Sim":
        st.subheader("โœจ Create Your Colorado Sim Story! ๐ŸŽ‰")
        col1, col2 = st.columns(2)
        inputs = {}
        with col1:
            inputs['plural_noun'] = st.text_input("๐ŸŒŸ Plural Noun", placeholder="e.g., 'tacos'", key="plural_noun")
            inputs['adjective1'] = st.text_input("๐ŸŽจ Adjective", placeholder="e.g., 'spicy'", key="adjective1")
            inputs['verb_ing'] = st.text_input("โณ Verb ending in -ing", placeholder="e.g., 'yeeting'", key="verb_ing")
            inputs['celebrity'] = st.text_input("๐ŸŽค Celebrity Name", placeholder="e.g., 'Elon Musk'", key="celebrity")
            inputs['object'] = st.text_input("๐Ÿ› ๏ธ Random Object", placeholder="e.g., 'toaster'", key="object")
            inputs['emotion'] = st.text_input("โค๏ธ Emotion", placeholder="e.g., 'salty'", key="emotion")
            inputs['animal'] = st.text_input("๐ŸฆŒ Animal", placeholder="e.g., 'elk'", key="animal")
            inputs['verb_past'] = st.text_input("โฎ๏ธ Verb (past tense)", placeholder="e.g., 'yeeted'", key="verb_past")
            inputs['place'] = st.text_input("๐ŸŒ Place", placeholder="e.g., 'Rockies'", key="place")
            inputs['exclamation'] = st.text_input("โ— Exclamation", placeholder="e.g., 'Bruh!'", key="exclamation")
        with col2:
            inputs['food'] = st.text_input("๐Ÿฝ๏ธ Food Item", placeholder="e.g., 'pizza'", key="food")
            inputs['adjective2'] = st.text_input("โœจ Adjective", placeholder="e.g., 'cringe'", key="adjective2")
            inputs['body_part'] = st.text_input("๐Ÿฆด Body Part", placeholder="e.g., 'elbow'", key="body_part")
            inputs['number'] = st.text_input("๐Ÿ”ข Number", placeholder="e.g., '69'", key="number")
            inputs['song'] = st.text_input("๐ŸŽต Song Title", placeholder="e.g., 'Sweet Caroline'", key="song")
            inputs['verb_present'] = st.text_input("โ–ถ๏ธ Verb (present tense)", placeholder="e.g., 'slay'", key="verb_present")
            inputs['occupation'] = st.text_input("๐Ÿ’ผ Occupation", placeholder="e.g., 'influencer'", key="occupation")
            inputs['color'] = st.text_input("๐ŸŒˆ Color", placeholder="e.g., 'teal'", key="color")
            inputs['noun'] = st.text_input("๐Ÿ“‹ Noun", placeholder="e.g., 'chaos'", key="noun")
            inputs['sound'] = st.text_input("๐Ÿ”Š Silly Sound Effect", placeholder="e.g., 'Boop!'", key="sound")

        if st.button("๐ŸŽ‰ Generate Sim Story! ๐ŸŽค"):
            story = generate_mad_libs_story(st.session_state.username, inputs)
            st.markdown("### ๐ŸŒ„ Your Colorado Sim Adventure ๐Ÿ”๏ธ")
            st.write(story)
            voice = FUN_USERNAMES.get(st.session_state.username, "en-US-AriaNeural")
            md_file, audio_file = asyncio.run(save_chat_entry(st.session_state.username, story, voice, True))
            if audio_file:
                play_and_download_audio(audio_file)

    # Sidebar
    st.sidebar.subheader("๐ŸŽญ Character Settings")
    new_username = st.sidebar.selectbox("๐Ÿ”„ Switch Character", list(FUN_USERNAMES.keys()), index=list(FUN_USERNAMES.keys()).index(st.session_state.username))
    if new_username != st.session_state.username:
        asyncio.run(save_chat_entry("System ๐ŸŒŸ", f"{st.session_state.username} switched to {new_username}", "en-US-AriaNeural"))
        st.session_state.username = new_username
        st.session_state.tts_voice = FUN_USERNAMES[new_username]
        save_username(st.session_state.username)
        st.rerun()

    st.sidebar.subheader("๐Ÿ’ฌ Chat History")
    chat_content = asyncio