| 6 | |
| 7 | |
| 8 | class Sam3ServiceClient: |
| 9 | def __init__(self, base_url: str, timeout: int = 120) -> None: |
| 10 | self.base_url = base_url.rstrip("/") |
| 11 | self.timeout = timeout |
| 12 | |
| 13 | def health(self) -> bool: |
| 14 | resp = requests.get(f"{self.base_url}/health", timeout=5) |
| 15 | return resp.status_code == 200 |
| 16 | |
| 17 | def predict( |
| 18 | self, |
| 19 | image_path: str, |
| 20 | prompts: List[str], |
| 21 | return_masks: bool = False, |
| 22 | mask_format: Literal["rle", "png"] = "rle", |
| 23 | score_threshold: Optional[float] = None, |
| 24 | epsilon_factor: Optional[float] = None, |
| 25 | min_area: Optional[int] = None, |
| 26 | ) -> Dict: |
| 27 | payload = { |
| 28 | "image_path": image_path, |
| 29 | "prompts": prompts, |
| 30 | "return_masks": return_masks, |
| 31 | "mask_format": mask_format, |
| 32 | } |
| 33 | if score_threshold is not None: |
| 34 | payload["score_threshold"] = score_threshold |
| 35 | if epsilon_factor is not None: |
| 36 | payload["epsilon_factor"] = epsilon_factor |
| 37 | if min_area is not None: |
| 38 | payload["min_area"] = min_area |
| 39 | |
| 40 | resp = requests.post(f"{self.base_url}/predict", json=payload, timeout=self.timeout) |
| 41 | resp.raise_for_status() |
| 42 | return resp.json() |
| 43 | |
| 44 | |
| 45 | class Sam3ServicePool: |