Martin Tomov commited on
Commit
d722625
Β·
verified Β·
1 Parent(s): 9f37f40

startup_report

Browse files
Files changed (1) hide show
  1. app.py +19 -1
app.py CHANGED
@@ -13,6 +13,8 @@ import matplotlib.pyplot as plt
13
  from transformers import AutoModelForMaskGeneration, AutoProcessor, pipeline
14
  import gradio as gr
15
  import spaces
 
 
16
 
17
  @dataclass
18
  class BoundingBox:
@@ -104,10 +106,26 @@ def refine_masks(masks: torch.BoolTensor, polygon_refinement: bool = False) -> L
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]
@@ -198,4 +216,4 @@ gr.Interface(
198
  inputs=gr.Image(type="pil"),
199
  outputs=[gr.Image(type="numpy"), gr.Image(type="numpy")],
200
  title="🐞 InsectSAM + GroundingDINO Inference",
201
- ).launch()
 
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
  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]
 
216
  inputs=gr.Image(type="pil"),
217
  outputs=[gr.Image(type="numpy"), gr.Image(type="numpy")],
218
  title="🐞 InsectSAM + GroundingDINO Inference",
219
+ ).launch()