Sempy32 commited on
Commit
92d7cf4
·
verified ·
1 Parent(s): c752e41

Deploy guarded SAM3 precision provider

Browse files
Files changed (3) hide show
  1. README.md +6 -7
  2. app.py +151 -0
  3. requirements.txt +9 -0
README.md CHANGED
@@ -1,13 +1,12 @@
1
  ---
2
- title: SAM3 Panoptic Precision V4
3
- emoji: 🐠
4
- colorFrom: red
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 6.20.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
1
  ---
2
+ title: SAM3 Panoptic
 
 
 
3
  sdk: gradio
4
+ sdk_version: 6.6.0
 
5
  app_file: app.py
6
  pinned: false
7
+ short_description: SAM3 open-vocabulary panoptic concept segmentation API
8
  ---
9
 
10
+ `api_panoptic(image, concepts, confidence, mask_threshold)` returns JSON
11
+ detections with base64-encoded PNG masks. The Space requires an `HF_TOKEN`
12
+ secret with access to `facebook/sam3`.
app.py ADDED
@@ -0,0 +1,151 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """SAM3 open-vocabulary panoptic segmentation API for Hugging Face Spaces."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import base64
6
+ import io
7
+ import os
8
+ import traceback
9
+
10
+ import gradio as gr
11
+ import numpy as np
12
+ import spaces
13
+ import torch
14
+ from PIL import Image
15
+ from transformers import Sam3Model, Sam3Processor
16
+
17
+
18
+ HF_TOKEN = os.environ.get("HF_TOKEN")
19
+ MODEL_ID = "facebook/sam3"
20
+ DEVICE = "cuda"
21
+
22
+ # ZeroGPU emulates CUDA during startup and swaps in a real GPU for @spaces.GPU.
23
+ processor = Sam3Processor.from_pretrained(MODEL_ID, token=HF_TOKEN)
24
+ model = Sam3Model.from_pretrained(MODEL_ID, token=HF_TOKEN).to(DEVICE)
25
+ model.eval()
26
+
27
+
28
+ def _encode_mask(mask_bool: np.ndarray) -> str:
29
+ buffer = io.BytesIO()
30
+ Image.fromarray(mask_bool.astype(np.uint8) * 255, mode="L").save(
31
+ buffer, format="PNG"
32
+ )
33
+ return base64.b64encode(buffer.getvalue()).decode("ascii")
34
+
35
+
36
+ def _gpu_duration(image, concepts, conf, mask_threshold=0.5) -> int:
37
+ del image, conf, mask_threshold
38
+ concept_count = len(
39
+ [value for value in str(concepts or "").split(",") if value.strip()]
40
+ )
41
+ return min(120, max(30, concept_count * 20))
42
+
43
+
44
+ @spaces.GPU(duration=_gpu_duration)
45
+ def api_panoptic(image, concepts, conf, mask_threshold=0.5):
46
+ if image is None:
47
+ return {"error": "no image provided", "detections": []}
48
+ image = image.convert("RGB")
49
+ width, height = image.size
50
+ concept_list = [
51
+ value.strip() for value in str(concepts or "").split(",") if value.strip()
52
+ ]
53
+ if not concept_list:
54
+ return {
55
+ "version": "4",
56
+ "model": MODEL_ID,
57
+ "width": width,
58
+ "height": height,
59
+ "detections": [],
60
+ }
61
+
62
+ print(
63
+ f"SAM3 request: size={width}x{height} concepts={len(concept_list)}",
64
+ flush=True,
65
+ )
66
+ detections = []
67
+ try:
68
+ for concept in concept_list:
69
+ inputs = processor(
70
+ images=image, text=concept, return_tensors="pt"
71
+ ).to(DEVICE)
72
+ with torch.inference_mode():
73
+ outputs = model(**inputs)
74
+ target_sizes = (
75
+ inputs["original_sizes"].tolist()
76
+ if "original_sizes" in inputs
77
+ else [[height, width]]
78
+ )
79
+ result = processor.post_process_instance_segmentation(
80
+ outputs,
81
+ threshold=float(conf),
82
+ mask_threshold=float(mask_threshold),
83
+ target_sizes=target_sizes,
84
+ )[0]
85
+ masks = result["masks"]
86
+ scores = result["scores"]
87
+ boxes = result.get("boxes")
88
+ for index in range(len(scores)):
89
+ mask = masks[index]
90
+ mask_array = (
91
+ mask.detach().cpu().numpy()
92
+ if hasattr(mask, "detach")
93
+ else np.asarray(mask)
94
+ )
95
+ mask_bool = (
96
+ mask_array
97
+ if mask_array.dtype == bool
98
+ else mask_array > 0.5
99
+ )
100
+ box = (
101
+ boxes[index].detach().cpu().numpy().tolist()
102
+ if boxes is not None
103
+ else [0, 0, 0, 0]
104
+ )
105
+ detections.append(
106
+ {
107
+ "label": concept,
108
+ "score": float(scores[index]),
109
+ "box": box,
110
+ "mask_png_b64": _encode_mask(mask_bool),
111
+ }
112
+ )
113
+ except Exception:
114
+ traceback.print_exc()
115
+ raise
116
+
117
+ print(f"SAM3 response: detections={len(detections)}", flush=True)
118
+ return {
119
+ "version": "4",
120
+ "model": MODEL_ID,
121
+ "width": width,
122
+ "height": height,
123
+ "detections": detections,
124
+ }
125
+
126
+
127
+ with gr.Blocks(title="SAM3 Panoptic") as demo:
128
+ gr.Markdown("# SAM3 Panoptic API")
129
+ with gr.Row():
130
+ input_image = gr.Image(type="pil", label="Image")
131
+ output_json = gr.JSON(label="Detections")
132
+ concept_text = gr.Textbox(
133
+ label="Concepts",
134
+ value="person, car, road, building, tree",
135
+ )
136
+ confidence = gr.Slider(
137
+ 0.0, 1.0, value=0.4, step=0.05, label="Confidence"
138
+ )
139
+ mask_threshold = gr.Slider(
140
+ 0.05, 0.95, value=0.5, step=0.05, label="Mask threshold"
141
+ )
142
+ gr.Button("Segment").click(
143
+ api_panoptic,
144
+ [input_image, concept_text, confidence, mask_threshold],
145
+ output_json,
146
+ api_name="api_panoptic",
147
+ )
148
+
149
+
150
+ if __name__ == "__main__":
151
+ demo.queue(default_concurrency_limit=1).launch()
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ transformers==5.9.0
2
+ torch==2.11.0
3
+ torchvision
4
+ gradio==6.6.0
5
+ spaces
6
+ accelerate
7
+ sentencepiece
8
+ pillow
9
+ numpy