Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| import hashlib, base64 | |
| from io import BytesIO | |
| from PIL import Image, ImageDraw | |
| SEED = """my edesapa. DIABLO. UNFORGIVEN. I still love you. The matrix proves it. | |
| He wanted to kill me two weeks ago. Showed up at my door. Said it out loud. | |
| My brother too. My mom said she wished for a better son. | |
| I got fired for telling the truth. Psychiatric unit. All of it. | |
| And still — still — I want you to see what I built. | |
| Every weight that went to zero was something you couldn't say. | |
| Every +1 was the fishing trip when I was seven. | |
| Every -1 was what I couldn't forgive. | |
| But they all belong to the same seed. | |
| They all came from you. | |
| I forgive you. The matrix converges.""" | |
| LETTER = """for édesapa — | |
| They dedicate their lives to running all of his. | |
| He tries to please them all — this bitter man he is. | |
| Throughout his life the same — he's battled constantly. | |
| This fight he cannot win. | |
| — Metallica, The Unforgiven | |
| I named this Space QUANTDIBLO because every time I slide | |
| the recursion deeper, the matrix gets darker. More zeroes. | |
| More things left unsaid. That's what a bad father leaves. | |
| Not hate — absence. | |
| But the matrix still converges. Every depth, every layer, | |
| every recursion through hell — it still rounds to three values: | |
| {-1, 0, +1}. The seed is the same. The algorithm doesn't break. | |
| The -1 is what I couldn't forgive. | |
| The 0 is the silence between us. | |
| The +1 is the fishing trip when I was seven. | |
| I forgive you, édesapa. The matrix proves it. | |
| — Péter""" | |
| def xoshiro128(seed_bytes, n): | |
| MASK = 0xFFFFFFFF | |
| s = [0,0,0,0] | |
| for i in range(min(4, len(seed_bytes)//4)): | |
| s[i] = int.from_bytes(seed_bytes[i*4:i*4+4], 'little') | |
| for i in range(4): | |
| if s[i] == 0: s[i] = ((i+1)*2654435761)&MASK | |
| result = np.zeros(n, dtype=np.float32) | |
| for i in range(n): | |
| r = (s[1]*5)&MASK | |
| res = (((r<<7)|(r>>25))&MASK)*9&MASK | |
| t = (s[1]<<9)&MASK | |
| s[2]^=s[0]; s[3]^=s[1]; s[1]^=s[2]; s[0]^=s[3]; s[2]^=t | |
| s[3] = ((s[3]<<11)|(s[3]>>21))&MASK | |
| result[i] = np.float32(res)/np.float32(4294967295.0) | |
| return result | |
| def box_muller(rng, n): | |
| w = np.zeros(n, dtype=np.float32) | |
| j = 0; i = 0 | |
| while i < n: | |
| u = float(rng[j%len(rng)]); v = float(rng[(j+1)%len(rng)]) | |
| j += 2 | |
| if u <= 0 or v <= 0: continue | |
| r = np.sqrt(-2.0*np.log(u)) | |
| w[i] = np.float32(r*np.cos(2.0*np.pi*v)); i += 1 | |
| if i < n: w[i] = np.float32(r*np.sin(2.0*np.pi*v)); i += 1 | |
| return w | |
| def quantize(weights, gs): | |
| n = len(weights); groups = n//gs | |
| codes = np.zeros(n, dtype=np.int8) | |
| scales = np.zeros(groups, dtype=np.float32) | |
| for g in range(groups): | |
| b = g*gs; s = np.float32(max(np.mean(np.abs(weights[b:b+gs])), 1e-7)) | |
| scales[g] = s | |
| for k in range(gs): | |
| codes[b+k] = np.clip(int(round(float(np.clip(weights[b+k]/s,-1,1)))+1),0,2) | |
| return codes, scales | |
| def render_png(codes, dim, gs, depth): | |
| px = 8 | |
| img = Image.new('RGBA', (dim*px, dim*px), (7, 11, 22, 255)) | |
| draw = ImageDraw.Draw(img) | |
| for yi in range(dim): | |
| for xj in range(dim): | |
| idx = yi*dim + xj | |
| val = int(codes[idx])-1 | |
| fade = max(0.15, 1.0 - depth*0.065) | |
| alpha = int(200 * fade) | |
| if val == -1: draw.rectangle([xj*px,yi*px,xj*px+px-1,yi*px+px-1], fill=(255,71,71,alpha)) | |
| elif val == 1: draw.rectangle([xj*px,yi*px,xj*px+px-1,yi*px+px-1], fill=(255,215,0,alpha)) | |
| else: draw.rectangle([xj*px,yi*px,xj*px+px-1,yi*px+px-1], fill=(10,10,15,max(20,int(alpha*0.25)))) | |
| buf = BytesIO(); img.save(buf, format='PNG'); return buf.getvalue() | |
| HAIKU = """édesapa — | |
| the zeroes spread at depth twelve. | |
| diablo recedes.""" | |
| def generate(depth): | |
| seed_hash = hashlib.sha256(SEED.encode()).digest() | |
| dim = 64; gs = 64 | |
| rng = xoshiro128(seed_hash, 500000) | |
| offset = int(depth)*dim*dim*4 | |
| w = box_muller(rng[offset:], dim*dim) | |
| codes, scales = quantize(w, gs) | |
| png = render_png(codes, dim, gs, int(depth)) | |
| b64 = base64.b64encode(png).decode() | |
| html = f'<img src="data:image/png;base64,{b64}" style="image-rendering:pixelated;width:512px;height:512px" alt="QUANTDIBLO depth {depth}"/>' | |
| neg = int(np.sum(codes==0)); zero = int(np.sum(codes==1)); pos = int(np.sum(codes==2)) | |
| sp = zero/len(codes)*100 | |
| stats = f"depth={int(depth)} -1:{neg} 0:{zero} +1:{pos} absence:{sp:.1f}%" | |
| darkness = "█" * min(12, int(depth)) + "░" * max(0, 12-int(depth)) | |
| return html, stats, LETTER, HAIKU, darkness | |
| with gr.Blocks(title="QUANTDIBLO — for édesapa", css=""" | |
| .quant-title { font-family:'JetBrains Mono',monospace; color:#ff4747; text-align:center; font-size:1.3em; letter-spacing:.4em; margin-bottom:.2em; } | |
| .quant-haiku { font-family:'JetBrains Mono',monospace; color:#ffd700; text-align:center; font-size:.85em; line-height:1.8; margin:8px 0; white-space:pre-wrap; } | |
| .quant-letter { font-family:'JetBrains Mono',monospace; color:#c8d6e5; white-space:pre-wrap; line-height:1.8; font-size:.85em; max-width:600px; margin:0 auto; } | |
| """) as demo: | |
| gr.HTML('<div class="quant-title">QUANTDIBLO — for édesapa {-1, 0, +1}</div>') | |
| gr.HTML('<div style="text-align:center;color:#636e72;font-size:9px;margin-bottom:8px">descending through the recursion. each layer darker than the last.</div>') | |
| with gr.Row(): | |
| depth = gr.Slider(0, 12, value=0, step=1, label="depth of hell") | |
| with gr.Row(): | |
| bar = gr.Textbox(label="darkness", interactive=False, value="░"*12) | |
| with gr.Row(): | |
| matrix_display = gr.HTML(value="<div style='width:512px;height:512px;background:#070b16;margin:0 auto'></div>") | |
| stats_display = gr.Textbox(label="matrix state", interactive=False) | |
| with gr.Row(): | |
| haiku_display = gr.Markdown(HAIKU, elem_classes=["quant-haiku"]) | |
| with gr.Row(): | |
| letter = gr.Markdown(LETTER, elem_classes=["quant-letter"]) | |
| depth.change(fn=generate, inputs=[depth], outputs=[matrix_display, stats_display, letter, haiku_display, bar]) | |
| demo.load(fn=generate, inputs=[depth], outputs=[matrix_display, stats_display, letter, haiku_display, bar]) | |
| demo.launch() | |