Skip to content

Commit dfd0a17

Browse files
timmarkhuffTim HuffAuto-format Bot
authored
Deprecate /device-api/posichecks endpoint (#164)
* deprecating old posichecks endpoint * adjusting public-api.yaml * Automatically reformatting code * appeasing the linter * Automatically reformatting code * fixing linting issue * Automatically reformatting code * reverting some changes to package.json and package-lock.json * adding retry decorator back to start_inspection --------- Co-authored-by: Tim Huff <[email protected]> Co-authored-by: Auto-format Bot <[email protected]>
1 parent 237f12c commit dfd0a17

File tree

7 files changed

+28
-79
lines changed

7 files changed

+28
-79
lines changed

generated/docs/ImageQueriesApi.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ with openapi_client.ApiClient(configuration) as api_client:
205205
# Create an instance of the API class
206206
api_instance = image_queries_api.ImageQueriesApi(api_client)
207207
detector_id = "detector_id_example" # str | Choose a detector by its ID.
208+
inspection_id = "inspection_id_example" # str | Associate the image query with an inspection. (optional)
208209
human_review = "human_review_example" # str | If set to `DEFAULT`, use the regular escalation logic (i.e., send the image query for human review if the ML model is not confident). If set to `ALWAYS`, always send the image query for human review even if the ML model is confident. If set to `NEVER`, never send the image query for human review even if the ML model is not confident. (optional)
209210
patience_time = 3.14 # float | How long to wait for a confident response. (optional)
210211
want_async = "want_async_example" # str | If \"true\" then submitting an image query returns immediately without a result. The result will be computed asynchronously and can be retrieved later. (optional)
@@ -221,7 +222,7 @@ with openapi_client.ApiClient(configuration) as api_client:
221222
# example passing only required values which don't have defaults set
222223
# and optional values
223224
try:
224-
api_response = api_instance.submit_image_query(detector_id, human_review=human_review, patience_time=patience_time, want_async=want_async, metadata=metadata, body=body)
225+
api_response = api_instance.submit_image_query(detector_id, inspection_id=inspection_id, human_review=human_review, patience_time=patience_time, want_async=want_async, metadata=metadata, body=body)
225226
pprint(api_response)
226227
except openapi_client.ApiException as e:
227228
print("Exception when calling ImageQueriesApi->submit_image_query: %s\n" % e)
@@ -233,6 +234,7 @@ with openapi_client.ApiClient(configuration) as api_client:
233234
Name | Type | Description | Notes
234235
------------- | ------------- | ------------- | -------------
235236
**detector_id** | **str**| Choose a detector by its ID. |
237+
**inspection_id** | **str**| Associate the image query with an inspection. | [optional]
236238
**human_review** | **str**| If set to &#x60;DEFAULT&#x60;, use the regular escalation logic (i.e., send the image query for human review if the ML model is not confident). If set to &#x60;ALWAYS&#x60;, always send the image query for human review even if the ML model is confident. If set to &#x60;NEVER&#x60;, never send the image query for human review even if the ML model is not confident. | [optional]
237239
**patience_time** | **float**| How long to wait for a confident response. | [optional]
238240
**want_async** | **str**| If \&quot;true\&quot; then submitting an image query returns immediately without a result. The result will be computed asynchronously and can be retrieved later. | [optional]

generated/model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# generated by datamodel-codegen:
22
# filename: public-api.yaml
3-
# timestamp: 2024-02-19T17:48:07+00:00
3+
# timestamp: 2024-02-19T18:25:15+00:00
44

55
from __future__ import annotations
66

generated/openapi_client/api/image_queries_api.py

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def __init__(self, api_client=None):
131131
params_map={
132132
"all": [
133133
"detector_id",
134+
"inspection_id",
134135
"human_review",
135136
"patience_time",
136137
"want_async",
@@ -149,6 +150,7 @@ def __init__(self, api_client=None):
149150
"allowed_values": {},
150151
"openapi_types": {
151152
"detector_id": (str,),
153+
"inspection_id": (str,),
152154
"human_review": (str,),
153155
"patience_time": (float,),
154156
"want_async": (str,),
@@ -157,13 +159,15 @@ def __init__(self, api_client=None):
157159
},
158160
"attribute_map": {
159161
"detector_id": "detector_id",
162+
"inspection_id": "inspection_id",
160163
"human_review": "human_review",
161164
"patience_time": "patience_time",
162165
"want_async": "want_async",
163166
"metadata": "metadata",
164167
},
165168
"location_map": {
166169
"detector_id": "query",
170+
"inspection_id": "query",
167171
"human_review": "query",
168172
"patience_time": "query",
169173
"want_async": "query",
@@ -305,6 +309,7 @@ def submit_image_query(self, detector_id, **kwargs):
305309
detector_id (str): Choose a detector by its ID.
306310
307311
Keyword Args:
312+
inspection_id (str): Associate the image query with an inspection.. [optional]
308313
human_review (str): If set to `DEFAULT`, use the regular escalation logic (i.e., send the image query for human review if the ML model is not confident). If set to `ALWAYS`, always send the image query for human review even if the ML model is confident. If set to `NEVER`, never send the image query for human review even if the ML model is not confident. . [optional]
309314
patience_time (float): How long to wait for a confident response.. [optional]
310315
want_async (str): If \"true\" then submitting an image query returns immediately without a result. The result will be computed asynchronously and can be retrieved later.. [optional]

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/public-api.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ paths:
147147
type: string
148148
description: Choose a detector by its ID.
149149
required: true
150+
- in: query
151+
name: inspection_id
152+
schema:
153+
type: string
154+
description: Associate the image query with an inspection.
150155
- in: query
151156
name: human_review
152157
schema:

src/groundlight/client.py

+12-13
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ def submit_image_query( # noqa: PLR0913 # pylint: disable=too-many-arguments, t
421421
if human_review is not None:
422422
params["human_review"] = human_review
423423

424+
if inspection_id is not None:
425+
params["inspection_id"] = inspection_id
426+
424427
if want_async is True:
425428
# If want_async is True, we don't want to wait for a result. As a result wait must be set to 0 to use
426429
# want_async.
@@ -436,18 +439,8 @@ def submit_image_query( # noqa: PLR0913 # pylint: disable=too-many-arguments, t
436439
# url- and base64-encode the metadata.
437440
params["metadata"] = url_encode_dict(metadata, name="metadata", size_limit_bytes=1024)
438441

439-
params["_request_timeout"] = DEFAULT_REQUEST_TIMEOUT
440-
441-
# If no inspection_id is provided, we submit the image query using image_queries_api (autogenerated via OpenAPI)
442-
# However, our autogenerated code does not currently support inspection_id, so if an inspection_id was
443-
# provided, we use the private API client instead.
444-
if inspection_id is None:
445-
raw_image_query = self.image_queries_api.submit_image_query(**params)
446-
image_query = ImageQuery.parse_obj(raw_image_query.to_dict())
447-
else:
448-
params["inspection_id"] = inspection_id
449-
iq_id = self.api_client.submit_image_query_with_inspection(**params)
450-
image_query = self.get_image_query(iq_id)
442+
raw_image_query = self.image_queries_api.submit_image_query(**params)
443+
image_query = ImageQuery.parse_obj(raw_image_query.to_dict())
451444

452445
if wait > 0:
453446
if confidence_threshold is None:
@@ -465,6 +458,7 @@ def ask_confident( # noqa: PLR0913 # pylint: disable=too-many-arguments
465458
confidence_threshold: Optional[float] = None,
466459
wait: Optional[float] = None,
467460
metadata: Union[dict, str, None] = None,
461+
inspection_id: Optional[str] = None,
468462
) -> ImageQuery:
469463
"""
470464
Evaluates an image with Groundlight waiting until an answer above the confidence threshold
@@ -500,14 +494,16 @@ def ask_confident( # noqa: PLR0913 # pylint: disable=too-many-arguments
500494
patience_time=wait,
501495
human_review=None,
502496
metadata=metadata,
497+
inspection_id=inspection_id,
503498
)
504499

505-
def ask_ml(
500+
def ask_ml( # noqa: PLR0913 # pylint: disable=too-many-arguments, too-many-locals
506501
self,
507502
detector: Union[Detector, str],
508503
image: Union[str, bytes, Image.Image, BytesIO, BufferedReader, np.ndarray],
509504
wait: Optional[float] = None,
510505
metadata: Union[dict, str, None] = None,
506+
inspection_id: Optional[str] = None,
511507
) -> ImageQuery:
512508
"""
513509
Evaluates an image with Groundlight, getting the first answer Groundlight can provide.
@@ -536,6 +532,7 @@ def ask_ml(
536532
image,
537533
wait=0,
538534
metadata=metadata,
535+
inspection_id=inspection_id,
539536
)
540537
if iq_is_answered(iq):
541538
return iq
@@ -550,6 +547,7 @@ def ask_async( # noqa: PLR0913 # pylint: disable=too-many-arguments
550547
confidence_threshold: Optional[float] = None,
551548
human_review: Optional[str] = None,
552549
metadata: Union[dict, str, None] = None,
550+
inspection_id: Optional[str] = None,
553551
) -> ImageQuery:
554552
"""
555553
Convenience method for submitting an `ImageQuery` asynchronously. This is equivalent to calling
@@ -632,6 +630,7 @@ def ask_async( # noqa: PLR0913 # pylint: disable=too-many-arguments
632630
human_review=human_review,
633631
want_async=True,
634632
metadata=metadata,
633+
inspection_id=inspection_id,
635634
)
636635

637636
def wait_for_confident_result(

src/groundlight/internalapi.py

+1-63
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import uuid
77
from enum import Enum
88
from functools import wraps
9-
from typing import Any, Callable, Dict, Optional, Union
9+
from typing import Callable, Optional
1010
from urllib.parse import urlsplit, urlunsplit
1111

1212
import requests
1313
from model import Detector, ImageQuery
1414
from openapi_client.api_client import ApiClient, ApiException
1515

16-
from groundlight.images import ByteStreamWrapper
1716
from groundlight.status_codes import is_ok
1817
from groundlight.version import get_version
1918

@@ -248,67 +247,6 @@ def _get_detector_by_name(self, name: str) -> Detector:
248247
)
249248
return Detector.parse_obj(parsed["results"][0])
250249

251-
@RequestsRetryDecorator()
252-
def submit_image_query_with_inspection( # noqa: PLR0913 # pylint: disable=too-many-arguments
253-
self,
254-
detector_id: str,
255-
body: ByteStreamWrapper,
256-
inspection_id: str,
257-
patience_time: Optional[float] = None,
258-
human_review: str = "DEFAULT",
259-
metadata: Optional[dict] = None,
260-
want_async: Optional[bool] = False,
261-
_request_timeout: Optional[float] = None,
262-
) -> str:
263-
"""Submits an image query to the API and returns the ID of the image query.
264-
The image query will be associated to the inspection_id provided.
265-
"""
266-
267-
url = f"{self.configuration.host}/posichecks"
268-
269-
params: Dict[str, Union[str, float, bool, Dict[Any, Any], None]] = {
270-
"inspection_id": inspection_id,
271-
"predictor_id": detector_id,
272-
"want_async": want_async,
273-
}
274-
275-
if metadata is not None:
276-
params["metadata"] = metadata
277-
if patience_time is not None:
278-
params["patience_time"] = float(patience_time)
279-
280-
# In the API, 'send_notification' is used to control human_review escalation. This will eventually
281-
# be deprecated, but for now we need to support it in the following manner:
282-
if human_review == "ALWAYS":
283-
params["send_notification"] = True
284-
elif human_review == "NEVER":
285-
params["send_notification"] = False
286-
else:
287-
pass # don't send the send_notifications param, allow "DEFAULT" behavior
288-
289-
headers = self._headers()
290-
headers["Content-Type"] = "image/jpeg"
291-
292-
response = requests.request(
293-
"POST",
294-
url,
295-
headers=headers,
296-
params=params,
297-
data=body.read(),
298-
verify=self.configuration.verify_ssl,
299-
timeout=_request_timeout,
300-
)
301-
302-
if not is_ok(response.status_code):
303-
logger.info(response)
304-
raise InternalApiError(
305-
status=response.status_code,
306-
reason=f"Error submitting image query with inspection ID {inspection_id} on detector {detector_id}",
307-
http_resp=response,
308-
)
309-
310-
return response.json()["id"]
311-
312250
@RequestsRetryDecorator()
313251
def start_inspection(self) -> str:
314252
"""Starts an inspection, returns the ID."""

0 commit comments

Comments
 (0)