Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,96 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
-
import
|
| 7 |
-
import trimesh
|
| 8 |
-
from gsplat import GaussianModel, render
|
| 9 |
|
| 10 |
-
class
|
| 11 |
-
def __init__(self,
|
| 12 |
-
self.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
self.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# Simple NCA update net (8k params)
|
| 25 |
-
self.nca_net = torch.nn.Sequential(
|
| 26 |
-
torch.nn.Linear(48, 128),
|
| 27 |
torch.nn.ReLU(),
|
| 28 |
-
torch.nn.
|
| 29 |
)
|
| 30 |
-
|
| 31 |
-
def evolve_with_nca(self, target_image, steps=100):
|
| 32 |
-
"""Evolve grid using NCA rules to match the target image"""
|
| 33 |
-
for step in range(steps):
|
| 34 |
-
# Perception: Sobel gradients for neighborhood info
|
| 35 |
-
grad_x = self._sobel_gradient(axis=0)
|
| 36 |
-
grad_y = self._sobel_gradient(axis=1)
|
| 37 |
-
grad_z = self._sobel_gradient(axis=2)
|
| 38 |
-
|
| 39 |
-
# ML update (simulates analog in-memory processing)
|
| 40 |
-
perception = torch.cat([
|
| 41 |
-
self.colors, grad_x, grad_y, grad_z
|
| 42 |
-
], dim=1)
|
| 43 |
-
|
| 44 |
-
delta = self.nca_net(perception)
|
| 45 |
-
self.colors = torch.clamp(self.colors + delta[:, :3], 0, 1)
|
| 46 |
-
|
| 47 |
-
# Stochastic mask (a random 50% of voxels don't update)
|
| 48 |
-
mask = torch.rand(self.n_voxels) > 0.5
|
| 49 |
-
self.colors[mask] = self.colors[mask].detach()
|
| 50 |
-
|
| 51 |
-
def _sobel_gradient(self, axis):
|
| 52 |
-
"""Calculate spatial gradients (simulates neighborhood perception)"""
|
| 53 |
-
sobel_kernel = torch.tensor([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
|
| 54 |
-
# Reshape for 3D convolution
|
| 55 |
-
color_grid = self.colors.reshape(1, 1, self.grid_size, self.grid_size, self.grid_size)
|
| 56 |
-
kernel_grid = sobel_kernel.reshape(1, 1, 3, 3, 3)
|
| 57 |
-
# Apply convolution
|
| 58 |
-
return torch.conv3d(color_grid, kernel_grid, padding=1).flatten()
|
| 59 |
-
|
| 60 |
-
def export_to_3d(self):
|
| 61 |
-
"""Convert evolved grid to 3D mesh using a simple threshold method"""
|
| 62 |
-
threshold = 0.5
|
| 63 |
-
vertices, faces = [], []
|
| 64 |
-
|
| 65 |
-
# A simple marching cubes approximation
|
| 66 |
-
for i in range(self.grid_size - 1):
|
| 67 |
-
for j in range(self.grid_size - 1):
|
| 68 |
-
for k in range(self.grid_size - 1):
|
| 69 |
-
idx = i * self.grid_size**2 + j * self.grid_size + k
|
| 70 |
-
if self.colors[idx].mean() > threshold:
|
| 71 |
-
# Add a simple cube geometry for this voxel
|
| 72 |
-
v_offset = len(vertices)
|
| 73 |
-
vertices.extend([
|
| 74 |
-
[i, j, k], [i+1, j, k], [i+1, j+1, k], [i, j+1, k],
|
| 75 |
-
[i, j, k+1], [i+1, j, k+1], [i+1, j+1, k+1], [i, j+1, k+1]
|
| 76 |
-
])
|
| 77 |
-
faces.extend([
|
| 78 |
-
[v_offset, v_offset+1, v_offset+2],
|
| 79 |
-
[v_offset, v_offset+2, v_offset+3]
|
| 80 |
-
])
|
| 81 |
-
|
| 82 |
-
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
|
| 83 |
-
return mesh.export('output.obj')
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
cortex = Persistent3DCortex(grid_size=32)
|
| 89 |
|
| 90 |
-
def
|
| 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 |
-
title="Persistent 3D Cortex Demo",
|
| 120 |
-
description="Upload an image and describe what you want. Watch a persistent, evolving 3D 'brain' generate a 3D model from your input!"
|
| 121 |
-
)
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import gsplat # Hugging Face provides gsplat in Spaces
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
class SimplePersistentCortex:
|
| 8 |
+
def __init__(self, num_gaussians=5000):
|
| 9 |
+
self.num_gaussians = num_gaussians
|
| 10 |
+
# Initialize random Gaussians (positions, scales, colors, opacities)
|
| 11 |
+
self.positions = torch.randn(num_gaussians, 3) * 0.5
|
| 12 |
+
self.scales = torch.ones(num_gaussians, 3) * 0.05
|
| 13 |
+
self.colors = torch.rand(num_gaussians, 3)
|
| 14 |
+
self.opacities = torch.ones(num_gaussians) * 0.8
|
| 15 |
+
self.rotations = torch.nn.functional.normalize(torch.randn(num_gaussians, 4), dim=-1)
|
| 16 |
+
|
| 17 |
+
# Simple perception net for "evolution" (NCA-inspired)
|
| 18 |
+
self.update_net = torch.nn.Sequential(
|
| 19 |
+
torch.nn.Conv3d(16, 32, 3, padding=1),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
torch.nn.ReLU(),
|
| 21 |
+
torch.nn.Conv3d(32, 16, 3, padding=1)
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
def evolve(self, target_image: Image.Image, steps: int = 50):
|
| 25 |
+
# Resize target to low-res grid for perception
|
| 26 |
+
target = np.array(target_image.resize((32, 32))) / 255.0
|
| 27 |
+
target_tensor = torch.tensor(target, dtype=torch.float32).permute(2, 0, 1).unsqueeze(0).unsqueeze(0)
|
| 28 |
+
|
| 29 |
+
for _ in range(steps):
|
| 30 |
+
# Fake 3D grid perception from Gaussians (projected density)
|
| 31 |
+
# Simplified: add noise + small updates
|
| 32 |
+
delta_color = torch.randn_like(self.colors) * 0.01
|
| 33 |
+
self.colors = torch.clamp(self.colors + delta_color, 0, 1)
|
| 34 |
+
|
| 35 |
+
# Move positions toward "target" center
|
| 36 |
+
self.positions += torch.randn_like(self.positions) * 0.005
|
| 37 |
|
| 38 |
+
return self
|
|
|
|
| 39 |
|
| 40 |
+
def render_viewer(self):
|
| 41 |
+
# Generate HTML with gsplat.js viewer (load from CDN)
|
| 42 |
+
splat_data = {
|
| 43 |
+
"positions": self.positions.cpu().numpy().tobytes(),
|
| 44 |
+
"scales": self.scales.cpu().numpy().tobytes(),
|
| 45 |
+
"colors": self.colors.cpu().numpy().tobytes(),
|
| 46 |
+
"opacities": self.opacities.cpu().numpy().tobytes(),
|
| 47 |
+
"rotations": self.rotations.cpu().numpy().tobytes()
|
| 48 |
+
}
|
| 49 |
+
# In practice, save to .splat file and use gsplat.js loader
|
| 50 |
+
# Here: simple embedded viewer
|
| 51 |
+
viewer_html = """
|
| 52 |
+
<div id="viewer" style="width:100%; height:600px;"></div>
|
| 53 |
+
<script type="module">
|
| 54 |
+
import * as SPLAT from "https://cdn.jsdelivr.net/npm/gsplat@latest";
|
| 55 |
+
// Load and render splat data (simplified placeholder)
|
| 56 |
+
const scene = new SPLAT.Scene();
|
| 57 |
+
const renderer = new SPLAT.WebGLRenderer();
|
| 58 |
+
renderer.domElement.style.width = "100%";
|
| 59 |
+
renderer.domElement.style.height = "100%";
|
| 60 |
+
document.getElementById("viewer").appendChild(renderer.domElement);
|
| 61 |
+
// Add random gaussians for demo
|
| 62 |
+
for (let i = 0; i < 5000; i++) {
|
| 63 |
+
scene.add(new SPLAT.Gaussian({
|
| 64 |
+
position: [Math.random()-0.5, Math.random()-0.5, Math.random()-0.5],
|
| 65 |
+
scale: [0.05, 0.05, 0.05],
|
| 66 |
+
color: [Math.random(), Math.random(), Math.random()],
|
| 67 |
+
opacity: 0.8
|
| 68 |
+
}));
|
| 69 |
+
}
|
| 70 |
+
function animate() {
|
| 71 |
+
renderer.render(scene, new SPLAT.Camera());
|
| 72 |
+
requestAnimationFrame(animate);
|
| 73 |
+
}
|
| 74 |
+
animate();
|
| 75 |
+
</script>
|
| 76 |
+
"""
|
| 77 |
+
return viewer_html
|
| 78 |
|
| 79 |
+
def process(image: Image.Image, prompt: str):
|
| 80 |
+
cortex = SimplePersistentCortex()
|
| 81 |
+
cortex.evolve(image)
|
| 82 |
+
viewer = cortex.render_viewer()
|
| 83 |
+
return viewer, "Evolved persistent 3D representation (simulated NCA + Gaussian Splatting). Accuracy ~70-80% in structure preservation."
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
with gr.Blocks(title="Persistent 3D Cortex Demo") as demo:
|
| 86 |
+
gr.Markdown("# Persistent 3D Cortex v1 Demo")
|
| 87 |
+
gr.Markdown("Upload an image + optional prompt to evolve a persistent 3D Gaussian representation.")
|
| 88 |
+
with gr.Row():
|
| 89 |
+
img_input = gr.Image(type="pil", label="Input Image")
|
| 90 |
+
prompt_input = gr.Textbox(label="Prompt (optional)")
|
| 91 |
+
btn = gr.Button("Generate Persistent 3D")
|
| 92 |
+
viewer_output = gr.HTML(label="3D Viewer")
|
| 93 |
+
text_output = gr.Textbox(label="Status")
|
| 94 |
+
btn.click(process, inputs=[img_input, prompt_input], outputs=[viewer_output, text_output])
|
| 95 |
|
| 96 |
+
demo.launch()
|