from functools import lru_cache
import gradio as gr
from transformers import pipeline
from lab_core import SAMPLE_LABELS, analyze_depth_signal, colorize_depth, generate_scene
MODEL_ID = "depth-anything/Depth-Anything-V2-Small-hf"
@lru_cache(maxsize=1)
def get_depth_pipeline():
"""Load the reviewed Apache-2.0 model once per Space process."""
return pipeline(task="depth-estimation", model=MODEL_ID, device=-1)
def select_sample(sample_label: str):
key = SAMPLE_LABELS.get(sample_label, "training_room")
return (
generate_scene(key),
None,
"Select **Run reviewed model** to produce a relative-depth preview. "
"The scene is synthetic and contains no client data.",
)
def run_analysis(sample_label: str):
key = SAMPLE_LABELS.get(sample_label, "training_room")
source = generate_scene(key)
try:
result = get_depth_pipeline()(source)
raw_depth = result["depth"]
depth_preview = colorize_depth(raw_depth)
report = analyze_depth_signal(raw_depth, sample_label, MODEL_ID)
except Exception as exc: # Keep the public demo honest and inspectable.
depth_preview = None
report = (
"### Model unavailable\n\n"
"The synthetic source remains visible, but the reviewed model did not "
"complete. No fallback result is presented as model output.\n\n"
f"Technical class: `{type(exc).__name__}`. Try again after the Space restarts."
)
return source, depth_preview, report
CSS = """
.gradio-container {max-width: 1180px !important; margin: 0 auto;}
.pis-hero {padding: 18px 22px; border: 1px solid rgba(100,116,139,.35);
border-radius: 18px; background: linear-gradient(135deg,#07111f,#102a43); color:#f8fafc;}
.pis-hero h1 {margin: 0 0 8px 0; font-size: 2rem; color:#f8fafc !important;}
.pis-hero p {margin: 0; color:#dbeafe !important;}
.boundary {border-left: 4px solid #38bdf8; padding-left: 14px;}
"""
with gr.Blocks(title="PIS Spatial Intelligence Lab") as demo:
gr.HTML(
"""
Privacy-safe experiments for reality capture, depth and spatial understanding.PIS Spatial Intelligence Lab