Spaces:
Sleeping
Sleeping
Fix SAM3 optional import; upload full code
Browse files- app.py +2 -0
- models/__pycache__/model_bank_knn.cpython-311.pyc +0 -0
- segmenters/__init__.py +25 -17
- segmenters/__pycache__/__init__.cpython-311.pyc +0 -0
- segmenters/__pycache__/base_segmenter.cpython-311.pyc +0 -0
- segmenters/__pycache__/sam3.cpython-311.pyc +0 -0
- utils/__pycache__/visualize.cpython-311.pyc +0 -0
app.py
CHANGED
|
@@ -31,6 +31,8 @@ def load_sam3(prompt: str, device: str):
|
|
| 31 |
Cache SAM3 per (prompt, device) to avoid reloading weights.
|
| 32 |
Created only inside inference after GPU slice is granted.
|
| 33 |
"""
|
|
|
|
|
|
|
| 34 |
return SAM3Segmenter(text_prompt=prompt, device=device)
|
| 35 |
|
| 36 |
|
|
|
|
| 31 |
Cache SAM3 per (prompt, device) to avoid reloading weights.
|
| 32 |
Created only inside inference after GPU slice is granted.
|
| 33 |
"""
|
| 34 |
+
if SAM3Segmenter is None:
|
| 35 |
+
raise gr.Error("SAM3 is unavailable in this Space (transformers build missing Sam3Processor/Sam3Model). Disable the SAM3 option.")
|
| 36 |
return SAM3Segmenter(text_prompt=prompt, device=device)
|
| 37 |
|
| 38 |
|
models/__pycache__/model_bank_knn.cpython-311.pyc
ADDED
|
Binary file (12.3 kB). View file
|
|
|
segmenters/__init__.py
CHANGED
|
@@ -1,21 +1,29 @@
|
|
| 1 |
-
from segmenters.base_segmenter import BaseSegmenter
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
"""
|
| 9 |
Model registry for segmenters
|
| 10 |
"""
|
| 11 |
-
_SEGMENTERS = {}
|
| 12 |
-
if SAM3Segmenter is not None:
|
| 13 |
-
_SEGMENTERS["sam3"] = SAM3Segmenter
|
| 14 |
-
if PCASegmenter is not None:
|
| 15 |
-
_SEGMENTERS["pca"] = PCASegmenter
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def get_segmenter(name: str, **kwargs):
|
| 19 |
-
if name not in _SEGMENTERS:
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from segmenters.base_segmenter import BaseSegmenter
|
| 2 |
+
|
| 3 |
+
# SAM3 is optional; import lazily to avoid crashing when the model is unavailable
|
| 4 |
+
try:
|
| 5 |
+
from segmenters.sam3 import SAM3Segmenter # type: ignore
|
| 6 |
+
except Exception as _sam3_err: # pragma: no cover - best-effort optional import
|
| 7 |
+
SAM3Segmenter = None # type: ignore
|
| 8 |
+
_SAM3_IMPORT_ERROR = _sam3_err
|
| 9 |
+
|
| 10 |
+
from segmenters.pca_segmenter import PCASegmenter
|
| 11 |
|
| 12 |
"""
|
| 13 |
Model registry for segmenters
|
| 14 |
"""
|
| 15 |
+
_SEGMENTERS = {}
|
| 16 |
+
if SAM3Segmenter is not None:
|
| 17 |
+
_SEGMENTERS["sam3"] = SAM3Segmenter
|
| 18 |
+
if PCASegmenter is not None:
|
| 19 |
+
_SEGMENTERS["pca"] = PCASegmenter
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def get_segmenter(name: str, **kwargs):
|
| 23 |
+
if name not in _SEGMENTERS:
|
| 24 |
+
available = list(_SEGMENTERS)
|
| 25 |
+
msg = f"Unknown segmenter '{name}'. Available: {available}"
|
| 26 |
+
if name == "sam3" and 'SAM3Segmenter' in globals() and SAM3Segmenter is None:
|
| 27 |
+
msg += f". SAM3 import error: {_SAM3_IMPORT_ERROR}"
|
| 28 |
+
raise ValueError(msg)
|
| 29 |
+
return _SEGMENTERS[name](**kwargs)
|
segmenters/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (1.27 kB). View file
|
|
|
segmenters/__pycache__/base_segmenter.cpython-311.pyc
ADDED
|
Binary file (884 Bytes). View file
|
|
|
segmenters/__pycache__/sam3.cpython-311.pyc
ADDED
|
Binary file (5.07 kB). View file
|
|
|
utils/__pycache__/visualize.cpython-311.pyc
ADDED
|
Binary file (5.79 kB). View file
|
|
|