import gradio as gr
import time
import random
# --- THE ALCHEMICAL PALETTE v4.1 (CSS) ---
# Enhanced with Error Handling styles
custom_css = """
/* --- CSS Variables: The Elements of Change --- */
:root {
/* The Deep Void */
--bg-deep: #020204;
--bg-panel: rgba(15, 15, 19, 0.7);
/* Prince's Legacy */
--prince-dark: #2e1065;
--prince-main: #7c3aed;
--prince-light: #c084fc;
--prince-pink: #f472b6;
/* Wu-Tang Gold */
--wu-gold: #fbbf24;
--wu-amber: #d97706;
/* Attenborough's Green */
--nature-green: #10b981;
--nature-glow: #34d399;
/* Error Red */
--error-red: #ef4444;
--error-glow: #b91c1c;
/* Typography */
--font-display: 'Cinzel', serif;
--font-tech: 'Space Grotesk', sans-serif;
--font-funk: 'Rye', serif;
/* Effects */
--border-glass: rgba(255, 255, 255, 0.08);
--glow-purple: 0 0 20px rgba(124, 58, 237, 0.6);
--glow-gold: 0 0 15px rgba(251, 191, 36, 0.5);
--glow-error: 0 0 15px rgba(239, 68, 68, 0.6);
--scanline-color: rgba(0, 0, 0, 0.5);
}
/* --- Global Atmosphere & Dreamscape --- */
.gradio-container {
background-color: var(--bg-deep) !important;
color: #f3f4f6 !important;
font-family: var(--font-tech);
overflow-x: hidden;
position: relative;
}
/* The Neural Network Canvas Background */
#dreamscape-canvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
pointer-events: none;
}
/* --- CRT Scanline Effect Overlay --- */
.crt-overlay {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: linear-gradient(
rgba(18, 16, 16, 0) 50%,
rgba(0, 0, 0, 0.25) 50%
), linear-gradient(
90deg,
rgba(255, 0, 0, 0.06),
rgba(0, 255, 0, 0.02),
rgba(0, 0, 255, 0.06)
);
background-size: 100% 2px, 3px 100%;
z-index: 9999;
pointer-events: none;
opacity: 0.15;
}
/* --- Typography & Glitch Effects --- */
h1, h2, h3 {
font-family: var(--font-display) !important;
text-transform: uppercase;
letter-spacing: 0.1em;
}
.glitch-text {
position: relative;
animation: glitch-skew 3s infinite linear alternate-reverse;
}
/* --- The Odyssey Header --- */
.odyssey-header {
background: linear-gradient(90deg, rgba(46, 16, 101, 0.8), rgba(2, 2, 4, 0.9));
border-bottom: 2px solid var(--prince-main);
padding: 2rem;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.6);
backdrop-filter: blur(5px);
position: relative;
z-index: 10;
}
/* --- Component Alchemy --- */
/* The Terminal (The Log of the Journey) - CRT Style */
.terminal-log {
background: #000505 !important;
border: 1px solid var(--border-glass) !important;
border-left: 3px solid var(--nature-green);
font-family: 'Fira Code', monospace;
color: #33ff33;
padding: 1.5rem;
border-radius: 4px;
min-height: 300px;
white-space: pre-wrap;
box-shadow: inset 0 0 40px rgba(0, 255, 0, 0.1);
text-shadow: 0 0 5px rgba(0, 255, 0, 0.5);
line-height: 1.4;
font-size: 0.9rem;
}
.log-prince { color: var(--prince-pink); text-shadow: 0 0 8px var(--prince-pink); }
.log-wu { color: var(--wu-gold); font-weight: bold; text-shadow: 0 0 8px var(--wu-gold); }
.log-nature { color: var(--nature-glow); }
.log-system { color: #6b7280; font-style: italic; }
/* NEW: Error Log Styling */
.log-error {
color: var(--error-red);
font-weight: bold;
text-shadow: var(--glow-error);
border-left: 3px solid var(--error-red);
padding-left: 10px;
background: rgba(239, 68, 68, 0.1);
display: block;
margin: 5px 0;
}
/* The Code Editor */
.ace_editor {
border: 1px solid var(--border-glass) !important;
border-radius: 8px !important;
background: rgba(10, 10, 12, 0.8) !important;
font-family: 'Fira Code', monospace !important;
box-shadow: 0 0 15px rgba(124, 58, 237, 0.1);
}
/* Buttons: Enhanced Hover */
.prince-btn {
background: linear-gradient(135deg, var(--prince-dark), var(--prince-main)) !important;
color: white !important;
border: 1px solid var(--prince-light) !important;
text-transform: uppercase;
font-weight: 700;
letter-spacing: 1px;
transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.prince-btn:hover {
transform: translateY(-2px) scale(1.02);
box-shadow: 0 10px 25px -5px var(--prince-main);
}
.wu-btn {
background: transparent !important;
color: var(--wu-gold) !important;
border: 2px solid var(--wu-gold) !important;
font-family: var(--font-funk) !important;
text-shadow: 0 0 5px var(--wu-amber);
transition: all 0.3s ease;
}
.wu-btn:hover {
background: var(--wu-gold) !important;
color: black !important;
box-shadow: 0 0 30px var(--wu-gold);
transform: rotate(-1deg);
}
.crash-btn {
background: rgba(239, 68, 68, 0.2) !important;
color: var(--error-red) !important;
border: 1px solid var(--error-red) !important;
text-transform: uppercase;
font-weight: bold;
letter-spacing: 1px;
}
.crash-btn:hover {
background: var(--error-red) !important;
color: white !important;
box-shadow: var(--glow-error);
}
.cyber-card {
background: rgba(20, 20, 25, 0.6);
backdrop-filter: blur(15px);
border: 1px solid var(--border-glass);
border-radius: 12px;
padding: 1.5rem;
transition: all 0.4s ease;
position: relative;
overflow: hidden;
}
.cyber-card::before {
content: '';
position: absolute;
top: 0; left: -100%;
width: 100%; height: 100%;
background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
transition: 0.5s;
}
.cyber-card:hover::before {
left: 100%;
}
.cyber-card:hover {
border-color: var(--prince-main);
transform: translateY(-5px);
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
/* --- Splash Screen Overlay (Injected by JS) --- */
#splash-screen {
position: fixed;
top: 0; left: 0; width: 100%; height: 100%;
background: #000;
z-index: 10000;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
transition: opacity 1s ease-out;
}
.loader-bar {
width: 300px;
height: 2px;
background: #111;
margin-top: 20px;
position: relative;
overflow: hidden;
}
.loader-progress {
position: absolute;
top: 0; left: 0; height: 100%;
background: var(--prince-main);
width: 0%;
box-shadow: 0 0 10px var(--prince-main);
animation: load 2s ease-in-out forwards;
}
@keyframes load {
0% { width: 0%; }
50% { width: 70%; }
100% { width: 100%; }
}
"""
# --- THE LIVING MANIFESTO (LOGIC) ---
def run_odyssey(action_type: str, code: str, model: str, language: str):
"""
The core processor with robust error handling.
Returns HTML string for rich formatting in the terminal.
"""
logs = []
# --- ERROR HANDLING WRAPPER ---
try:
# 1. Input Validation
if not code:
# Instead of just a warning, we log it to the terminal AND warn the user
error_msg = '[CRITICAL] INPUT BUFFER EMPTY. ABORTING SEQUENCE.'
logs.append(error_msg)
gr.Warning("The canvas is blank, darling. Paint something first.")
return "
".join(logs), ""
logs.append(f'[SYSTEM] INITIATING SEQUENCE: {action_type.upper()}')
logs.append(f'[SYSTEM] MODEL: {model} // LANGUAGE: {language.upper()}')
# Simulate processing
time.sleep(random.uniform(0.3, 0.8))
# 2. Action Logic
if action_type == "analyze":
logs.append('[NATURE] The syntax trees begin to unfurl in the morning light...')
logs.append('[NATURE] We observe the function \'main\' stalking its prey...')
time.sleep(0.4)
logs.append('[NATURE] Structure integrity: 98.4%. The species is cataloged.')
result = f"# Analysis Report: The Digital Species\n# Habitat: {language}\n# Observer: {model}\n\n1. Behavior: Predictable yet elegant.\n2. Diet: Consumes memory efficiently.\n3. Status: Thriving in the compiler."
gr.Info("Analysis complete.")
elif action_type == "optimize":
logs.append('[PRINCE] Ooh! I like this code. It\'s got a nasty groove.')
logs.append('[PRINCE] Let\'s take it to the bridge... refactoring the rhythm...')
time.sleep(0.6)
logs.append('[PRINCE] Optimization complete. Doves cry when this runs.')
result = code + "\n\n# --- THE PURPLE RAIN REMIX ---\n# Optimized for maximum funk."
gr.Info("The Purple Rain Remix is ready.")
elif action_type == "secure":
logs.append('[WU-TANG] PROTECT YA NECK! Scanning for intruders...')
logs.append('[WU-TANG] Bring the ruckus! Patching vulnerabilities...')
time.sleep(0.5)
logs.append('[WU-TANG] C.R.E.A.M. (Code Rules Everything Around Me). System secured.')
result = code + "\n\n# --- WU-TANG SECURITY PROTOCOL ---\n# 36 Chambers of Defense activated."
gr.Info("Protect Ya Neck! Security protocols active.")
elif action_type == "crash":
# --- DELIBERATE ERROR FOR TESTING ---
raise ValueError("Simulated Neural Failure: Synapse Misfire Detected.")
return "
".join(logs), result
except Exception as e:
# --- CATCH ALL ERRORS ---
error_trace = str(e)
error_html = f'''
[FATAL ERROR] {error_trace}
> System attempting recovery...
> Recovery failed. Manual intervention required.
'''
# Show Gradio native error modal AND log to terminal
gr.Error(f"System Crash: {error_trace}")
return error_html, f"# SYSTEM HALTED\n# ERROR: {error_trace}"
def create_habitat(name: str, framework: str, desc: str):
"""Founding a new ecosystem with error handling."""
try:
if not name:
raise ValueError("Genesis requires a name.")
card = f"""
Create life to populate this sector.
© 2026 THE ODYSSEY ENGINE. Code is the canvas. Creativity is the color.