|
13 | 13 | from groundlight.binary_labels import convert_display_label_to_internal |
14 | 14 | from groundlight.config import API_TOKEN_VARIABLE_NAME, API_TOKEN_WEB_URL |
15 | 15 | from groundlight.images import parse_supported_image_types |
16 | | -from groundlight.internalapi import GroundlightApiClient, sanitize_endpoint_url |
| 16 | +from groundlight.internalapi import GroundlightApiClient, NotFoundError, sanitize_endpoint_url |
17 | 17 | from groundlight.optional_imports import Image, np |
18 | 18 |
|
19 | 19 | logger = logging.getLogger("groundlight.sdk") |
@@ -109,25 +109,27 @@ def get_or_create_detector( |
109 | 109 | confidence exists, return it. Otherwise, create a detector with the specified query and |
110 | 110 | config. |
111 | 111 | """ |
112 | | - existing_detector = self.get_detector_by_name(name) |
113 | | - if existing_detector: |
114 | | - # TODO: We may soon allow users to update the retrieved detector's fields. |
115 | | - if existing_detector.query != query: |
116 | | - raise ValueError( |
117 | | - f"Found existing detector with name={name} (id={existing_detector.id}) but the queries don't match." |
118 | | - f" The existing query is '{existing_detector.query}'." |
119 | | - ) |
120 | | - if confidence_threshold is not None and existing_detector.confidence_threshold != confidence_threshold: |
121 | | - raise ValueError( |
122 | | - f"Found existing detector with name={name} (id={existing_detector.id}) but the confidence" |
123 | | - " thresholds don't match. The existing confidence threshold is" |
124 | | - f" {existing_detector.confidence_threshold}." |
125 | | - ) |
126 | | - return existing_detector |
127 | | - |
128 | | - return self.create_detector( |
129 | | - name=name, query=query, confidence_threshold=confidence_threshold, config_name=config_name |
130 | | - ) |
| 112 | + try: |
| 113 | + existing_detector = self.get_detector_by_name(name) |
| 114 | + except NotFoundError: |
| 115 | + logger.debug(f"We could not find a detector with name='{name}'. So we will create a new detector ...") |
| 116 | + return self.create_detector( |
| 117 | + name=name, query=query, confidence_threshold=confidence_threshold, config_name=config_name |
| 118 | + ) |
| 119 | + |
| 120 | + # TODO: We may soon allow users to update the retrieved detector's fields. |
| 121 | + if existing_detector.query != query: |
| 122 | + raise ValueError( |
| 123 | + f"Found existing detector with name={name} (id={existing_detector.id}) but the queries don't match." |
| 124 | + f" The existing query is '{existing_detector.query}'." |
| 125 | + ) |
| 126 | + if confidence_threshold is not None and existing_detector.confidence_threshold != confidence_threshold: |
| 127 | + raise ValueError( |
| 128 | + f"Found existing detector with name={name} (id={existing_detector.id}) but the confidence" |
| 129 | + " thresholds don't match. The existing confidence threshold is" |
| 130 | + f" {existing_detector.confidence_threshold}." |
| 131 | + ) |
| 132 | + return existing_detector |
131 | 133 |
|
132 | 134 | def get_image_query(self, id: str) -> ImageQuery: # pylint: disable=redefined-builtin |
133 | 135 | obj = self.image_queries_api.get_image_query(id=id) |
|
0 commit comments