Spaces:
Running
on
Zero
Running
on
Zero
Martin Tomov
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -13,8 +13,6 @@ import matplotlib.pyplot as plt
|
|
13 |
from transformers import AutoModelForMaskGeneration, AutoProcessor, pipeline
|
14 |
import gradio as gr
|
15 |
import spaces
|
16 |
-
import time
|
17 |
-
import httpx
|
18 |
|
19 |
@dataclass
|
20 |
class BoundingBox:
|
@@ -106,26 +104,10 @@ def refine_masks(masks: torch.BoolTensor, polygon_refinement: bool = False) -> L
|
|
106 |
masks[idx] = cv2.fillPoly(np.zeros(shape, dtype=np.uint8), [polygon], 1)
|
107 |
return list(masks)
|
108 |
|
109 |
-
def startup_report_with_retries(client, retries=5, delay=2):
|
110 |
-
for i in range(retries):
|
111 |
-
try:
|
112 |
-
client.startup_report()
|
113 |
-
return
|
114 |
-
except httpx.ConnectTimeout:
|
115 |
-
if i < retries - 1:
|
116 |
-
time.sleep(delay)
|
117 |
-
else:
|
118 |
-
raise
|
119 |
-
|
120 |
@spaces.GPU
|
121 |
def detect(image: Image.Image, labels: List[str], threshold: float = 0.3, detector_id: Optional[str] = None) -> List[Dict[str, Any]]:
|
122 |
detector_id = detector_id if detector_id else "IDEA-Research/grounding-dino-base"
|
123 |
object_detector = pipeline(model=detector_id, task="zero-shot-object-detection", device="cuda")
|
124 |
-
|
125 |
-
# Initialize and call startup report with retries
|
126 |
-
client = httpx.Client()
|
127 |
-
startup_report_with_retries(client)
|
128 |
-
|
129 |
labels = [label if label.endswith(".") else label+"." for label in labels]
|
130 |
results = object_detector(image, candidate_labels=labels, threshold=threshold)
|
131 |
return [DetectionResult.from_dict(result) for result in results]
|
|
|
13 |
from transformers import AutoModelForMaskGeneration, AutoProcessor, pipeline
|
14 |
import gradio as gr
|
15 |
import spaces
|
|
|
|
|
16 |
|
17 |
@dataclass
|
18 |
class BoundingBox:
|
|
|
104 |
masks[idx] = cv2.fillPoly(np.zeros(shape, dtype=np.uint8), [polygon], 1)
|
105 |
return list(masks)
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
@spaces.GPU
|
108 |
def detect(image: Image.Image, labels: List[str], threshold: float = 0.3, detector_id: Optional[str] = None) -> List[Dict[str, Any]]:
|
109 |
detector_id = detector_id if detector_id else "IDEA-Research/grounding-dino-base"
|
110 |
object_detector = pipeline(model=detector_id, task="zero-shot-object-detection", device="cuda")
|
|
|
|
|
|
|
|
|
|
|
111 |
labels = [label if label.endswith(".") else label+"." for label in labels]
|
112 |
results = object_detector(image, candidate_labels=labels, threshold=threshold)
|
113 |
return [DetectionResult.from_dict(result) for result in results]
|