import gc
from functools import lru_cache, partial
from html import escape
import json
import logging
import math
import os
from pathlib import Path
import re
import shutil
import sys
import time
import uuid
from urllib.parse import quote
import spaces
import gradio as gr
import torch
from fastapi import APIRouter, HTTPException
from fastapi.responses import FileResponse
from starlette.middleware import Middleware
from starlette.middleware.gzip import GZipMiddleware
HERE = Path(__file__).resolve().parent
ROOT = HERE if (HERE / "pi4").is_dir() else HERE.parent
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
VIEWER = ROOT / "viewer"
OUTPUTS = Path(os.environ.get("PI4_OUTPUT_DIR", ROOT / "outputs" / "space")).resolve()
RETENTION_SECONDS = 6 * 60 * 60
IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".webp", ".bmp"}
RUN_ID = re.compile(r"[0-9a-f]{32}")
LOGGER = logging.getLogger(__name__)
STYLE = (HERE / "space.css").read_text()
EXAMPLE_COVERS = json.loads((HERE / "example-covers.json").read_text())
EXAMPLES = json.loads((ROOT / "examples" / "catalog.json").read_text())
HEADER = """
Physical Intelligence Team
"""
FOOTER = """
"""
EMPTY_VIEWER = """
:π⁴: A Foundation Model for 4D Scene Geometry and Motion
Your scene, in another dimension.
Upload a clip or try an example. Explore geometry, cameras, and motion right here.
01 Add media02 Reconstruct03 Explore
"""
def create_theme():
palette = {
"body_background_fill": "#f7f7f9",
"background_fill_primary": "#ffffff",
"background_fill_secondary": "#faf9fc",
"body_text_color": "#26272e",
"body_text_color_subdued": "#737580",
"border_color_primary": "#e9e7ee",
"block_background_fill": "#ffffff",
"block_border_color": "#e9e7ee",
"block_label_background_fill": "#ffffff",
"block_label_text_color": "#737580",
"input_background_fill": "#ffffff",
"input_border_color": "#e9e7ee",
"input_border_color_focus": "#a78bce",
"button_primary_background_fill": "#26272e",
"button_primary_background_fill_hover": "#454650",
"button_primary_border_color": "#26272e",
"button_primary_text_color": "#ffffff",
"checkbox_background_color_selected": "#8060a6",
"checkbox_border_color_selected": "#8060a6",
"checkbox_label_background_fill_selected": "#f5f0fb",
"checkbox_label_border_color_selected": "#d7c8eb",
"slider_color": "#a78bce",
"loader_color": "#8060a6",
}
theme = gr.themes.Base(
primary_hue="purple",
neutral_hue="gray",
).set(**palette)
values = theme.to_dict()["theme"]
return theme.set(**{
f"{name}_dark": value for name, value in values.items()
if not name.endswith("_dark") and f"{name}_dark" in values
})
def integer(value, name, minimum, maximum):
if (
isinstance(value, bool)
or not isinstance(value, (int, float))
or not math.isfinite(value)
or value != int(value)
or not minimum <= value <= maximum
):
raise gr.Error(f"{name} must be an integer from {minimum} to {maximum}.")
return int(value)
def prune_outputs():
OUTPUTS.mkdir(parents=True, exist_ok=True)
cutoff = time.time() - RETENTION_SECONDS
for directory in OUTPUTS.iterdir():
if RUN_ID.fullmatch(directory.name) and directory.is_dir():
try:
if directory.stat().st_mtime < cutoff:
shutil.rmtree(directory)
except OSError:
LOGGER.warning("Could not expire %s", directory.name, exc_info=True)
@lru_cache(maxsize=1)
def get_model():
from pi4 import Pi4
return Pi4.from_pretrained(
os.environ.get("PI4_MODEL_ID", "yyfz233/Pi4"),
revision=os.environ.get("PI4_MODEL_REVISION") or None,
)
@spaces.GPU(duration=120)
def predict(frames, query_frame, model):
if not torch.cuda.is_available():
raise gr.Error("A CUDA GPU is required. Select ZeroGPU in the Space settings.")
images = None
prediction = None
try:
model.cuda()
images = (
torch.from_numpy(frames.copy())
.permute(0, 3, 1, 2)
.float()
.div_(255)
.unsqueeze(0)
.cuda()
)
torch.cuda.synchronize()
started = time.perf_counter()
with torch.inference_mode():
prediction = model(images, query_frame=query_frame)
torch.cuda.synchronize()
elapsed = time.perf_counter() - started
result = {
name: value.detach().cpu() if isinstance(value, torch.Tensor) else value
for name, value in prediction.items()
}
return result, elapsed
except torch.cuda.OutOfMemoryError as error:
raise gr.Error(
"GPU memory is full. Reduce the frame limit and retry. "
"The demo does not silently split the sequence or lower its resolution."
) from error
finally:
model.cpu()
del images, prediction
gc.collect()
torch.cuda.empty_cache()
def reconstruct(
input_type,
video,
images,
tracking,
query_frame,
stride,
max_frames,
progress=gr.Progress(),
):
from pi4.io import load_images, save_results
stride = integer(stride, "Sampling stride", 1, 60)
max_frames = integer(max_frames, "Frame limit", 1, 150)
query = integer(query_frame, "Query frame", 0, 149) if tracking else None
if input_type == "Video":
if not video or not Path(video).is_file():
raise gr.Error("Upload a video or select an example first.")
elif input_type == "Images":
if not images or len(images) > 300:
raise gr.Error("Upload between 1 and 300 images.")
if any(
not Path(image).is_file() or Path(image).suffix.lower() not in IMAGE_SUFFIXES
for image in images
):
raise gr.Error("Images must be JPG, PNG, WEBP, or BMP files.")
else:
raise gr.Error("Choose Video or Images as the input type.")
prune_outputs()
run = OUTPUTS / uuid.uuid4().hex
run.mkdir()
try:
progress(0.05, desc="Loading and sampling RGB frames on CPU")
if input_type == "Images":
source = run / "input"
source.mkdir()
for index, image in enumerate(sorted(images, key=lambda item: Path(item).name)):
image = Path(image)
shutil.copyfile(image, source / f"{index:06d}{image.suffix.lower()}")
else:
source = Path(video)
frames, metadata = load_images(
source, stride=stride, max_frames=max_frames, max_size=512
)
metadata["source"] = "uploaded images" if input_type == "Images" else source.name
if input_type == "Images":
shutil.rmtree(source)
if tracking and len(frames) < 2:
raise gr.Error("Tracking needs at least two sampled frames. Disable tracking for one image.")
if query is not None and query >= len(frames):
raise gr.Error(
f"Only {len(frames)} frames were sampled. Query frame must be from 0 "
f"to {len(frames) - 1}, after sampling."
)
progress(0.2, desc="Loading cached weights on CPU; first use may take longer")
model = get_model()
progress(0.35, desc="Waiting for ZeroGPU and running one joint forward pass")
prediction, elapsed = predict(frames, query, model)
progress(0.8, desc="Exporting the interactive viewer and downloads on CPU")
data = run / "data"
scene = save_results(data, frames, prediction, metadata, viewer=True)
del prediction
archive = shutil.make_archive(str(run / "pi4-result"), "zip", root_dir=data)
width, height = scene["image_size"]
status = (
f"**Done:** {len(frames)} frames · {width} × {height} · "
f"{elapsed:.1f} s model forward · {scene['preview']['points']:,} preview points.\n\n"
"One joint forward pass · Results kept for up to 6 hours."
)
url = f"/view/{run.name}/"
preview_source = "" if input_type == "Images" else escape(f"/gradio_api/file={quote(str(source.resolve()), safe='/')}", quote=True)
viewer = (
f'