Spaces:
Sleeping
Sleeping
Deploy Myco from CI
Browse files- ui/interface.py +31 -190
ui/interface.py
CHANGED
|
@@ -1,196 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
Generates happy, blinking mushrooms that grow, sway, and shrink.
|
| 8 |
-
"""
|
| 9 |
-
iframe_content = """
|
| 10 |
-
<!DOCTYPE html>
|
| 11 |
-
<html>
|
| 12 |
-
<head>
|
| 13 |
-
<style>
|
| 14 |
-
body {
|
| 15 |
-
margin: 0;
|
| 16 |
-
overflow: hidden;
|
| 17 |
-
/* Soft magical forest background */
|
| 18 |
-
background: linear-gradient(180deg, #1a2b4c 0%, #2a4c5e 50%, #4a7c59 100%);
|
| 19 |
-
}
|
| 20 |
-
canvas { display: block; }
|
| 21 |
-
</style>
|
| 22 |
-
</head>
|
| 23 |
-
<body>
|
| 24 |
-
<canvas id="cute-canvas"></canvas>
|
| 25 |
-
<script>
|
| 26 |
-
const canvas = document.getElementById('cute-canvas');
|
| 27 |
-
const ctx = canvas.getContext('2d');
|
| 28 |
-
|
| 29 |
-
function resize() {
|
| 30 |
-
canvas.width = window.innerWidth;
|
| 31 |
-
canvas.height = window.innerHeight;
|
| 32 |
-
}
|
| 33 |
-
window.addEventListener('resize', resize);
|
| 34 |
-
resize();
|
| 35 |
-
|
| 36 |
-
const mushrooms = [];
|
| 37 |
-
const fireflies = [];
|
| 38 |
-
|
| 39 |
-
// Helper to draw a single cute mushroom
|
| 40 |
-
function drawMushroom(m, time) {
|
| 41 |
-
ctx.save();
|
| 42 |
-
ctx.translate(m.x, m.y);
|
| 43 |
-
|
| 44 |
-
// Bouncing and swaying math
|
| 45 |
-
const sway = Math.sin(time * 0.002 + m.phase) * 0.1;
|
| 46 |
-
const bounce = Math.sin(time * 0.005 + m.phase) * 0.05 + 1;
|
| 47 |
-
|
| 48 |
-
ctx.rotate(sway);
|
| 49 |
-
ctx.scale(m.currentSize, m.currentSize * bounce);
|
| 50 |
-
|
| 51 |
-
// 1. Draw Stem
|
| 52 |
-
ctx.fillStyle = '#fff4e6';
|
| 53 |
-
ctx.beginPath();
|
| 54 |
-
ctx.roundRect(-12, 0, 24, 35, 10);
|
| 55 |
-
ctx.fill();
|
| 56 |
-
|
| 57 |
-
// 2. Draw Cute Face
|
| 58 |
-
ctx.fillStyle = '#4a3b32';
|
| 59 |
-
const isBlinking = (time % m.blinkInterval) < 150;
|
| 60 |
-
|
| 61 |
-
if (isBlinking) {
|
| 62 |
-
// Closed eyes (wincing/blinking)
|
| 63 |
-
ctx.fillRect(-7, 12, 4, 2);
|
| 64 |
-
ctx.fillRect(3, 12, 4, 2);
|
| 65 |
-
} else {
|
| 66 |
-
// Open eyes
|
| 67 |
-
ctx.beginPath(); ctx.arc(-5, 12, 2.5, 0, Math.PI*2); ctx.fill();
|
| 68 |
-
ctx.beginPath(); ctx.arc(5, 12, 2.5, 0, Math.PI*2); ctx.fill();
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
// Tiny smile
|
| 72 |
-
ctx.strokeStyle = '#4a3b32';
|
| 73 |
-
ctx.lineWidth = 1.5;
|
| 74 |
-
ctx.beginPath();
|
| 75 |
-
ctx.arc(0, 15, 3, 0, Math.PI, false);
|
| 76 |
-
ctx.stroke();
|
| 77 |
-
|
| 78 |
-
// Blush
|
| 79 |
-
ctx.fillStyle = 'rgba(255, 150, 150, 0.5)';
|
| 80 |
-
ctx.beginPath(); ctx.arc(-9, 16, 3, 0, Math.PI*2); ctx.fill();
|
| 81 |
-
ctx.beginPath(); ctx.arc(9, 16, 3, 0, Math.PI*2); ctx.fill();
|
| 82 |
-
|
| 83 |
-
// 3. Draw Cap
|
| 84 |
-
ctx.fillStyle = `hsl(${m.hue}, 80%, 75%)`; // Soft pastel colors
|
| 85 |
-
ctx.beginPath();
|
| 86 |
-
ctx.moveTo(-35, 5);
|
| 87 |
-
// Bezier curves make it look like a puffy umbrella
|
| 88 |
-
ctx.bezierCurveTo(-35, -30, 35, -30, 35, 5);
|
| 89 |
-
ctx.bezierCurveTo(35, 15, -35, 15, -35, 5);
|
| 90 |
-
ctx.fill();
|
| 91 |
-
|
| 92 |
-
// Cap Shadow/Detail
|
| 93 |
-
ctx.fillStyle = 'rgba(255, 255, 255, 0.3)';
|
| 94 |
-
ctx.beginPath(); ctx.arc(-15, -12, 6, 0, Math.PI*2); ctx.fill();
|
| 95 |
-
ctx.beginPath(); ctx.arc(12, -8, 4, 0, Math.PI*2); ctx.fill();
|
| 96 |
-
ctx.beginPath(); ctx.arc(0, -18, 5, 0, Math.PI*2); ctx.fill();
|
| 97 |
-
|
| 98 |
-
ctx.restore();
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
function animate(time) {
|
| 102 |
-
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 103 |
-
|
| 104 |
-
// --- Fireflies Logic ---
|
| 105 |
-
if (Math.random() < 0.1 && fireflies.length < 50) {
|
| 106 |
-
fireflies.push({
|
| 107 |
-
x: Math.random() * canvas.width,
|
| 108 |
-
y: canvas.height + 10,
|
| 109 |
-
size: Math.random() * 2 + 1,
|
| 110 |
-
speed: Math.random() * 1 + 0.5,
|
| 111 |
-
wobble: Math.random() * Math.PI * 2
|
| 112 |
-
});
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
ctx.fillStyle = '#fffae6';
|
| 116 |
-
for (let i = fireflies.length - 1; i >= 0; i--) {
|
| 117 |
-
let f = fireflies[i];
|
| 118 |
-
f.y -= f.speed;
|
| 119 |
-
f.x += Math.sin(time * 0.002 + f.wobble) * 0.5;
|
| 120 |
-
|
| 121 |
-
ctx.globalAlpha = Math.sin(time * 0.005 + f.wobble) * 0.5 + 0.5;
|
| 122 |
-
ctx.beginPath();
|
| 123 |
-
ctx.arc(f.x, f.y, f.size, 0, Math.PI * 2);
|
| 124 |
-
ctx.fill();
|
| 125 |
-
|
| 126 |
-
if (f.y < -10) fireflies.splice(i, 1);
|
| 127 |
-
}
|
| 128 |
-
ctx.globalAlpha = 1.0;
|
| 129 |
-
|
| 130 |
-
// --- Mushroom Logic ---
|
| 131 |
-
// Randomly spawn new mushrooms
|
| 132 |
-
if (Math.random() < 0.02 && mushrooms.length < 35) {
|
| 133 |
-
mushrooms.push({
|
| 134 |
-
x: Math.random() * canvas.width,
|
| 135 |
-
y: (canvas.height * 0.4) + (Math.random() * canvas.height * 0.6), // Spawn in bottom 60%
|
| 136 |
-
targetSize: Math.random() * 1.5 + 0.8,
|
| 137 |
-
currentSize: 0,
|
| 138 |
-
hue: Math.random() * 360,
|
| 139 |
-
phase: Math.random() * Math.PI * 2,
|
| 140 |
-
blinkInterval: Math.floor(Math.random() * 3000) + 2000,
|
| 141 |
-
life: Math.random() * 500 + 300 // How long they stay before shrinking
|
| 142 |
-
});
|
| 143 |
-
}
|
| 144 |
-
|
| 145 |
-
// Sort mushrooms by Y coordinate so closer ones draw on top (Depth sorting)
|
| 146 |
-
mushrooms.sort((a, b) => a.y - b.y);
|
| 147 |
-
|
| 148 |
-
for (let i = mushrooms.length - 1; i >= 0; i--) {
|
| 149 |
-
let m = mushrooms[i];
|
| 150 |
-
|
| 151 |
-
m.life--;
|
| 152 |
-
if (m.life < 0) {
|
| 153 |
-
m.targetSize = 0; // Shrink away
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
// Smoothly grow or shrink
|
| 157 |
-
m.currentSize += (m.targetSize - m.currentSize) * 0.05;
|
| 158 |
-
|
| 159 |
-
drawMushroom(m, time);
|
| 160 |
-
|
| 161 |
-
// Remove if totally shrunk
|
| 162 |
-
if (m.life < 0 && m.currentSize < 0.05) {
|
| 163 |
-
mushrooms.splice(i, 1);
|
| 164 |
-
}
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
requestAnimationFrame(animate);
|
| 168 |
-
}
|
| 169 |
-
|
| 170 |
-
requestAnimationFrame(animate);
|
| 171 |
-
</script>
|
| 172 |
-
</body>
|
| 173 |
-
</html>
|
| 174 |
-
"""
|
| 175 |
-
|
| 176 |
-
# Safely escape the HTML
|
| 177 |
-
escaped_srcdoc = html.escape(iframe_content)
|
| 178 |
-
|
| 179 |
-
# Return a clean, borderless container
|
| 180 |
-
return f'''
|
| 181 |
-
<div style="width: 100%; height: 500px; border-radius: 16px; overflow: hidden; border: 4px solid #f8c8dc; box-shadow: 0 8px 32px rgba(0,0,0,0.15);">
|
| 182 |
-
<iframe
|
| 183 |
-
srcdoc="{escaped_srcdoc}"
|
| 184 |
-
style="width: 100%; height: 100%; border: none;"
|
| 185 |
-
scrolling="no">
|
| 186 |
-
</iframe>
|
| 187 |
-
</div>
|
| 188 |
-
'''
|
| 189 |
|
| 190 |
def render_shroom_tab():
|
| 191 |
with gr.Tabs():
|
| 192 |
-
with gr.Tab("✨ Magic Mushroom Garden"):
|
| 193 |
-
#
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from game.engine import get_myco_narrative
|
| 3 |
|
| 4 |
+
def get_new_narrative_json():
|
| 5 |
+
# This prepares the text to be passed to the JS layer
|
| 6 |
+
return get_myco_narrative()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def render_shroom_tab():
|
| 9 |
with gr.Tabs():
|
| 10 |
+
with gr.Tab("✨ Gemma's Magic Mushroom Garden"):
|
| 11 |
+
# A hidden textbox to hold the latest AI thought
|
| 12 |
+
narrative_box = gr.Textbox(
|
| 13 |
+
value=get_myco_narrative(),
|
| 14 |
+
visible=False,
|
| 15 |
+
elem_id="myco-text-bridge"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
# The garden container
|
| 19 |
+
gr.HTML(value="""
|
| 20 |
+
<div id="garden-container" style="position:relative; width:100%; height:500px;">
|
| 21 |
+
<div id="gemma-bubble" style="position:absolute; top:20px; z-index:100; ..."></div>
|
| 22 |
+
<canvas id="cute-canvas"></canvas>
|
| 23 |
+
</div>
|
| 24 |
+
<script>
|
| 25 |
+
// This JS reads the text from the hidden Gradio textbox
|
| 26 |
+
function updateBubble() {
|
| 27 |
+
const bridge = document.getElementById('myco-text-bridge');
|
| 28 |
+
const bubble = document.getElementById('gemma-bubble');
|
| 29 |
+
if(bridge) bubble.innerText = "Gemma Myco AI: " + bridge.value;
|
| 30 |
+
}
|
| 31 |
+
// Run updateBubble whenever the hidden bridge changes
|
| 32 |
+
</script>
|
| 33 |
+
""")
|
| 34 |
+
|
| 35 |
+
# Use an event to refresh the narrative every 10 seconds
|
| 36 |
+
gr.Timer(10).tick(get_myco_narrative, outputs=narrative_box)
|
| 37 |
+
|