Spaces:
Running on Zero
Running on Zero
Download app.py from mujtaba1212/tooth-sam: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/spaces/mujtaba1212/tooth-sam/resolve/main/app.py
- Command line
-
hf download hf://spaces/mujtaba1212/tooth-sam/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mujtaba1212/tooth-sam/resolve/main/app.py
2.34 kB
| # Tooth outline AI for PixelCeph Workspace. | |
| # The app sends the photo once (set_image), then each click (segment) gets a tooth mask back. | |
| # Photos are kept in memory only (last 30), never saved to disk. | |
| import spaces | |
| import io, json, base64, uuid | |
| from collections import OrderedDict | |
| import numpy as np, torch, gradio as gr | |
| from PIL import Image | |
| from transformers import SamModel, SamProcessor | |
| torch.set_num_threads(2) | |
| MODEL = "Zigeng/SlimSAM-uniform-77" | |
| model = SamModel.from_pretrained(MODEL).eval() | |
| processor = SamProcessor.from_pretrained(MODEL) | |
| CACHE = OrderedDict() # id -> (embeddings, original size, resized size) | |
| def _gpu_check(): # free Spaces run on ZeroGPU, which needs one GPU function; the outline work runs on the CPU | |
| return True | |
| def set_image(jpeg_b64: str) -> str: | |
| img = Image.open(io.BytesIO(base64.b64decode(jpeg_b64))).convert("RGB") | |
| inputs = processor(img, return_tensors="pt") | |
| with torch.no_grad(): | |
| emb = model.get_image_embeddings(inputs["pixel_values"]) | |
| key = uuid.uuid4().hex | |
| CACHE[key] = (emb, inputs["original_sizes"], inputs["reshaped_input_sizes"]) | |
| while len(CACHE) > 30: | |
| CACHE.popitem(last=False) | |
| return key | |
| def segment(payload: str) -> str: | |
| body = json.loads(payload) | |
| if body.get("id") not in CACHE: | |
| return json.dumps({"missing": True}) # the Space restarted: the app sends the photo again | |
| emb, orig, resized = CACHE[body["id"]] | |
| (h, w), (rh, rw) = orig[0].tolist(), resized[0].tolist() | |
| pts = torch.tensor([[[[p["fx"] * rw, p["fy"] * rh] for p in body["points"]]]], dtype=torch.float) | |
| labels = torch.tensor([[[int(p["label"]) for p in body["points"]]]]) | |
| with torch.no_grad(): | |
| out = model(image_embeddings=emb, input_points=pts, input_labels=labels, multimask_output=True) | |
| masks = processor.post_process_masks(out.pred_masks, orig, resized)[0][0] # 3 candidate masks | |
| best = int(out.iou_scores[0, 0].argmax()) | |
| png = io.BytesIO() | |
| Image.fromarray(masks[best].numpy().astype(np.uint8) * 255).save(png, "PNG") | |
| return json.dumps({"w": w, "h": h, "png": base64.b64encode(png.getvalue()).decode()}) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Tooth outline AI for PixelCeph Workspace. Used by the app, nothing to do here.") | |
| gr.api(set_image) | |
| gr.api(segment) | |
| demo.launch() | |