Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,95 +2,165 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
| 6 |
-
import plyfile
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
self.rotations = torch.nn.functional.normalize(torch.randn(num_gaussians, 4), dim=-1)
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
|
| 20 |
-
|
| 21 |
|
| 22 |
for _ in range(steps):
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
| 36 |
|
|
|
|
| 37 |
return self
|
| 38 |
|
| 39 |
-
def export_ply(self, path
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
(
|
| 49 |
-
|
| 50 |
-
(
|
| 51 |
-
|
| 52 |
-
('rot_0', 'f4'), ('rot_1', 'f4'), ('rot_2', 'f4'), ('rot_3', 'f4')
|
| 53 |
-
])
|
| 54 |
-
|
| 55 |
-
vertex_data['x'], vertex_data['y'], vertex_data['z'] = positions[:, 0], positions[:, 1], positions[:, 2]
|
| 56 |
-
vertex_data['f_dc_0'], vertex_data['f_dc_1'], vertex_data['f_dc_2'] = sh_dc[:, 0], sh_dc[:, 1], sh_dc[:, 2]
|
| 57 |
-
vertex_data['opacity'] = opacities
|
| 58 |
-
vertex_data['scale_0'], vertex_data['scale_1'], vertex_data['scale_2'] = scales[:, 0], scales[:, 1], scales[:, 2]
|
| 59 |
-
vertex_data['rot_0'], vertex_data['rot_1'], vertex_data['rot_2'], vertex_data['rot_3'] = rot[:, 0], rot[:, 1], rot[:, 2], rot[:, 3]
|
| 60 |
|
| 61 |
el = plyfile.PlyElement.describe(vertex_data, 'vertex')
|
| 62 |
-
plyfile.PlyData([el]).write(path)
|
|
|
|
| 63 |
|
| 64 |
def process(image: Image.Image, prompt: str = ""):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
cortex = SimplePersistentCortex()
|
| 69 |
-
cortex.evolve(image)
|
| 70 |
-
|
| 71 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".ply") as tmp:
|
| 72 |
-
cortex.export_ply(tmp.name)
|
| 73 |
-
ply_path = tmp.name
|
| 74 |
-
|
| 75 |
-
status = "Persistent 3D Gaussian splat generated. Use mouse to orbit/zoom in viewer."
|
| 76 |
if prompt:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
with gr.Row():
|
| 86 |
img_input = gr.Image(type="pil", label="Input Image")
|
| 87 |
-
prompt_input = gr.Textbox(label="Prompt (
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
demo.launch()
|
|
|
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import os
|
| 6 |
+
import plyfile
|
| 7 |
+
import open_clip
|
| 8 |
+
|
| 9 |
+
# Load OpenCLIP for prompt conditioning (runs efficiently on CPU or GPU)
|
| 10 |
+
model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='laion2b_s34b_b79K')
|
| 11 |
+
model.eval()
|
| 12 |
+
tokenizer = open_clip.get_tokenizer('ViT-B-32')
|
| 13 |
+
|
| 14 |
+
class PersistentCortex:
|
| 15 |
+
def __init__(self, num_gaussians=8000):
|
| 16 |
+
self.num = num_gaussians
|
| 17 |
+
# Initialize a compact spherical distribution (similar to your working PLY)
|
| 18 |
+
angles = torch.rand(num_gaussians, 2) * 2 * np.pi
|
| 19 |
+
radius = torch.rand(num_gaussians).pow(1/3) * 0.6
|
| 20 |
+
self.positions = torch.stack([
|
| 21 |
+
radius * torch.sin(angles[:, 0]) * torch.cos(angles[:, 1]),
|
| 22 |
+
radius * torch.sin(angles[:, 0]) * torch.sin(angles[:, 1]),
|
| 23 |
+
radius * torch.cos(angles[:, 0])
|
| 24 |
+
], dim=1)
|
| 25 |
+
|
| 26 |
+
self.scales = torch.exp(torch.randn(num_gaussians, 3) * -2.5 - 2.0)
|
| 27 |
+
self.colors = torch.rand(num_gaussians, 3) * 0.7 + 0.3 # neutral gray start
|
| 28 |
+
self.opacities = torch.sigmoid(torch.randn(num_gaussians) * 1.5 + 2.0)
|
| 29 |
self.rotations = torch.nn.functional.normalize(torch.randn(num_gaussians, 4), dim=-1)
|
| 30 |
|
| 31 |
+
def evolve_from_image(self, image: Image.Image, steps=800):
|
| 32 |
+
img_array = np.array(image.resize((256, 256))) / 255.0
|
| 33 |
+
target = torch.tensor(img_array, dtype=torch.float32)
|
| 34 |
|
| 35 |
for _ in range(steps):
|
| 36 |
+
# Simple orthographic projection to 2D for guidance
|
| 37 |
+
proj = self.positions[:, :2].clone()
|
| 38 |
+
proj = proj / proj.abs().max() # normalize to [-1, 1]
|
| 39 |
+
grid = proj.unsqueeze(0).unsqueeze(0) # (1,1,N,2)
|
| 40 |
+
|
| 41 |
+
sampled = torch.nn.functional.grid_sample(
|
| 42 |
+
target.permute(2, 0, 1).unsqueeze(0),
|
| 43 |
+
grid,
|
| 44 |
+
mode='bilinear',
|
| 45 |
+
padding_mode='border',
|
| 46 |
+
align_corners=True
|
| 47 |
+
).squeeze(0).squeeze(0) # (N, 3)
|
| 48 |
|
| 49 |
+
# Attract positions toward brighter areas
|
| 50 |
+
brightness = sampled.mean(dim=1)
|
| 51 |
+
attraction = (brightness - brightness.mean()) * 0.015
|
| 52 |
+
self.positions[:, :2] += attraction.unsqueeze(1) * proj
|
| 53 |
+
|
| 54 |
+
# Gradually adopt average color from target
|
| 55 |
+
self.colors = torch.lerp(self.colors, target.mean(dim=[0,1]).repeat(self.num, 1), 0.02)
|
| 56 |
+
|
| 57 |
+
return self
|
| 58 |
|
| 59 |
+
def condition_on_prompt(self, prompt: str):
|
| 60 |
+
if not prompt.strip():
|
| 61 |
+
return self
|
| 62 |
+
text_tokens = tokenizer([prompt])
|
| 63 |
+
with torch.no_grad():
|
| 64 |
+
text_emb = model.encode_text(text_tokens).float()
|
| 65 |
+
text_emb = text_emb / text_emb.norm(dim=-1, keepdim=True)
|
| 66 |
|
| 67 |
+
# Simple linear projection to RGB shift (deterministic for reproducibility)
|
| 68 |
+
proj = torch.nn.Linear(512, 3, bias=False)
|
| 69 |
+
torch.nn.init.normal_(proj.weight, std=0.2)
|
| 70 |
+
color_shift = proj(text_emb)
|
| 71 |
|
| 72 |
+
self.colors = torch.clamp(self.colors + color_shift.repeat(self.num, 1) * 0.3, 0, 1)
|
| 73 |
return self
|
| 74 |
|
| 75 |
+
def export_ply(self, path="output.ply"):
|
| 76 |
+
# Prepare vertex data matching gsplat.js expected properties
|
| 77 |
+
zeros = np.zeros((self.num, 3), dtype=np.float32)
|
| 78 |
+
log_scales = np.log(self.scales.cpu().numpy()).astype(np.float32)
|
| 79 |
+
|
| 80 |
+
vertex_data = np.core.records.fromarrays([
|
| 81 |
+
self.positions.cpu().numpy().astype(np.float32),
|
| 82 |
+
zeros, # normals (unused)
|
| 83 |
+
self.colors.cpu().numpy().astype(np.float32),
|
| 84 |
+
self.opacities.cpu().numpy().astype(np.float32),
|
| 85 |
+
log_scales,
|
| 86 |
+
self.rotations.cpu().numpy().astype(np.float32)
|
| 87 |
+
], names='x,y,z,nx,ny,nz,f_dc_0,f_dc_1,f_dc_2,opacity,scale_0,scale_1,scale_2,rot_0,rot_1,rot_2,rot_3')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
el = plyfile.PlyElement.describe(vertex_data, 'vertex')
|
| 90 |
+
plyfile.PlyData([el], text=True).write(path)
|
| 91 |
+
return os.path.abspath(path)
|
| 92 |
|
| 93 |
def process(image: Image.Image, prompt: str = ""):
|
| 94 |
+
cortex = PersistentCortex(num_gaussians=8000)
|
| 95 |
+
cortex.evolve_from_image(image, steps=800)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if prompt:
|
| 97 |
+
cortex.condition_on_prompt(prompt)
|
| 98 |
+
|
| 99 |
+
ply_path = cortex.export_ply("/tmp/output.ply")
|
| 100 |
|
| 101 |
+
# In Hugging Face Spaces, /tmp files are automatically served under /files/
|
| 102 |
+
viewer_html = f"""
|
| 103 |
+
<div id="viewer" style="width:100%; height:600px; background:#000;"></div>
|
| 104 |
+
<script type="module">
|
| 105 |
+
import * as SPLAT from "https://cdn.jsdelivr.net/npm/gsplat@latest";
|
| 106 |
+
|
| 107 |
+
const container = document.getElementById('viewer');
|
| 108 |
+
const canvas = document.createElement('canvas');
|
| 109 |
+
canvas.style.width = '100%';
|
| 110 |
+
canvas.style.height = '100%';
|
| 111 |
+
container.appendChild(canvas);
|
| 112 |
+
|
| 113 |
+
const scene = new SPLAT.Scene();
|
| 114 |
+
const camera = new SPLAT.Camera();
|
| 115 |
+
const renderer = new SPLAT.WebGLRenderer({{ canvas }});
|
| 116 |
+
const controls = new SPLAT.OrbitControls(camera, canvas);
|
| 117 |
+
|
| 118 |
+
controls.autoRotate = false;
|
| 119 |
+
controls.enableDamping = true;
|
| 120 |
+
controls.dampingFactor = 0.05;
|
| 121 |
+
controls.rotateSpeed = 1.0;
|
| 122 |
+
controls.zoomSpeed = 1.2;
|
| 123 |
+
controls.panSpeed = 0.8;
|
| 124 |
+
|
| 125 |
+
await SPLAT.Loader.LoadAsync("/files/output.ply", scene);
|
| 126 |
+
|
| 127 |
+
function animate() {{
|
| 128 |
+
controls.update();
|
| 129 |
+
renderer.render(scene, camera);
|
| 130 |
+
requestAnimationFrame(animate);
|
| 131 |
+
}}
|
| 132 |
+
animate();
|
| 133 |
+
</script>
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
status = "3D Gaussian splat generated and evolved from your image"
|
| 137 |
+
if prompt:
|
| 138 |
+
status += f" with prompt conditioning: '{prompt}'."
|
| 139 |
+
else:
|
| 140 |
+
status += "."
|
| 141 |
+
|
| 142 |
+
return viewer_html, status
|
| 143 |
+
|
| 144 |
+
with gr.Blocks(title="Persistent 3D Cortex Demo") as demo:
|
| 145 |
+
gr.Markdown("# Persistent 3D Cortex – Interactive Demo")
|
| 146 |
+
gr.Markdown("""
|
| 147 |
+
Upload an image and optionally add a text prompt.
|
| 148 |
+
The system evolves a persistent 3D Gaussian splat representation influenced by the image content and prompt.
|
| 149 |
+
""")
|
| 150 |
|
| 151 |
with gr.Row():
|
| 152 |
img_input = gr.Image(type="pil", label="Input Image")
|
| 153 |
+
prompt_input = gr.Textbox(label="Prompt (e.g., 'shiny red apple', 'futuristic city')", placeholder="Optional text prompt")
|
| 154 |
|
| 155 |
+
generate_btn = gr.Button("Generate & Evolve 3D", variant="primary")
|
| 156 |
|
| 157 |
+
viewer_output = gr.HTML(label="Interactive 3D Viewer")
|
| 158 |
+
status_output = gr.Textbox(label="Status")
|
| 159 |
+
|
| 160 |
+
generate_btn.click(
|
| 161 |
+
fn=process,
|
| 162 |
+
inputs=[img_input, prompt_input],
|
| 163 |
+
outputs=[viewer_output, status_output]
|
| 164 |
+
)
|
| 165 |
|
| 166 |
demo.launch()
|