Skip to content

Commit ebcbf42

Browse files
authored
Send inference data for ETL and tracking (#156)
* Send inference data for ETL and tracking * Fix mypy error
1 parent 884ac2c commit ebcbf42

13 files changed

+37
-33
lines changed

landingai/predict.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
SegmentationPrediction,
2525
)
2626
from landingai.exceptions import HttpResponse, RateLimitExceededError
27-
from landingai.telemetry import get_runtime_environment_info, is_running_in_pytest
27+
from landingai.telemetry import get_runtime_environment_info
2828
from landingai.timer import Timer
2929
from landingai.utils import load_api_credential, serialize_image
3030

@@ -68,7 +68,11 @@ def __init__(
6868

6969
self._endpoint_id = endpoint_id
7070
self._api_credential = load_api_credential(api_key)
71-
headers = self._build_default_headers(self._api_credential)
71+
extra_x_event = {
72+
"endpoint_id": self._endpoint_id,
73+
"model_type": "fast_and_easy",
74+
}
75+
headers = self._build_default_headers(self._api_credential, extra_x_event)
7276
self._session = _create_session(Predictor._url, self._num_retry, headers)
7377

7478
def _check_connectivity(
@@ -93,11 +97,22 @@ def _check_connectivity(
9397
sock.close()
9498
return result == 0
9599

96-
def _build_default_headers(self, api_key: APIKey) -> Dict[str, str]:
100+
def _build_default_headers(
101+
self, api_key: APIKey, extra_x_event: Optional[Dict[str, str]] = None
102+
) -> Dict[str, str]:
97103
"""Build the HTTP headers for the request to the Cloud inference endpoint(s)."""
104+
tracked_properties = get_runtime_environment_info()
105+
if extra_x_event:
106+
tracked_properties.update(extra_x_event)
107+
tracking_data = {
108+
"event": "inference",
109+
"action": "POST",
110+
"properties": tracked_properties,
111+
}
98112
return {
99113
"contentType": "multipart/form-data",
100114
"apikey": api_key.api_key,
115+
"X-event": json.dumps(tracking_data),
101116
}
102117

103118
@retry(
@@ -133,17 +148,15 @@ def predict(
133148
"""
134149
buffer_bytes = serialize_image(image)
135150
files = {"file": buffer_bytes}
136-
payload = {
151+
query_params = {
137152
"endpoint_id": self._endpoint_id,
138-
"device_type": "pylib",
139153
}
140-
_add_default_query_params(payload)
141154
data = {"metadata": metadata.json()} if metadata else None
142155
return _do_inference(
143156
self._session,
144157
Predictor._url,
145158
files,
146-
payload,
159+
query_params,
147160
_CloudExtractor,
148161
data=data,
149162
)
@@ -173,7 +186,10 @@ def __init__(
173186
"""
174187
self._threshold = threshold
175188
self._api_credential = load_api_credential(api_key)
176-
headers = self._build_default_headers(self._api_credential)
189+
extra_x_event = {
190+
"model_type": "ocr",
191+
}
192+
headers = self._build_default_headers(self._api_credential, extra_x_event)
177193
self._session = _create_session(Predictor._url, self._num_retry, headers)
178194

179195
@retry(
@@ -223,13 +239,11 @@ def predict( # type: ignore
223239
if rois := kwargs.get("regions_of_interest", []):
224240
data["rois"] = serialize_rois(rois, mode)
225241

226-
payload: Dict[str, Any] = {"device_type": "pylib"}
227-
_add_default_query_params(payload)
228242
preds = _do_inference(
229243
self._session,
230244
OcrPredictor._url,
231245
files,
232-
payload,
246+
{},
233247
_OcrExtractor,
234248
data=data,
235249
)
@@ -802,13 +816,3 @@ def _do_inference(
802816
response.raise_for_status()
803817
json_dict = response.json()
804818
return extractor_class.extract_prediction(json_dict)
805-
806-
807-
def _add_default_query_params(payload: Dict[str, Any]) -> None:
808-
"""Add default query params to the payload for tracking and analytics purpose."""
809-
env_info = get_runtime_environment_info()
810-
payload["runtime"] = env_info["runtime"]
811-
if is_running_in_pytest():
812-
# Don't add extra query params if pytest is running, otherwise it will fail some unit tests
813-
return
814-
payload["lib_version"] = env_info["lib_version"]

tests/data/responses/default_vp_model_response.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ responses:
2525
content_type: text/plain
2626
method: POST
2727
status: 200
28-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=63035608-9d24-4342-8042-e4b08e084fde&device_type=pylib&runtime=pytest
28+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=63035608-9d24-4342-8042-e4b08e084fde

tests/data/responses/v1_predict_status_300.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ responses:
55
content_type: text/plain
66
method: POST
77
status: 300
8-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43&device_type=pylib&runtime=pytest
8+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43

tests/data/responses/v1_predict_status_400.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ responses:
66
content_type: text/plain
77
method: POST
88
status: 400
9-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4&device_type=pylib&runtime=pytest
9+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4

tests/data/responses/v1_predict_status_401.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ responses:
66
content_type: text/plain
77
method: POST
88
status: 401
9-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43&device_type=pylib&runtime=pytest
9+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43

tests/data/responses/v1_predict_status_403.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ responses:
66
content_type: text/plain
77
method: POST
88
status: 403
9-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=dfa79692-75eb-4a48-b02e-b273751adbae&device_type=pylib&runtime=pytest
9+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=dfa79692-75eb-4a48-b02e-b273751adbae

tests/data/responses/v1_predict_status_404.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ responses:
77
content_type: text/plain
88
method: POST
99
status: 404
10-
url: https://predict.app.landing.ai/v0/foo?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43&device_type=pylib&runtime=pytest
10+
url: https://predict.app.landing.ai/v0/foo?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43

tests/data/responses/v1_predict_status_422.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ responses:
55
content_type: text/plain
66
method: POST
77
status: 422
8-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=12345&device_type=pylib&runtime=pytest
8+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=12345

tests/data/responses/v1_predict_status_429.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ responses:
55
content_type: text/plain
66
method: POST
77
status: 429
8-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43&device_type=pylib&runtime=pytest
8+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43

tests/data/responses/v1_predict_status_500.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ responses:
55
content_type: text/plain
66
method: POST
77
status: 500
8-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4&device_type=pylib&runtime=pytest
8+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4

tests/data/responses/v1_predict_status_503.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ responses:
55
content_type: text/plain
66
method: POST
77
status: 503
8-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4&device_type=pylib&runtime=pytest
8+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4

tests/data/responses/v1_predict_status_504.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ responses:
55
content_type: text/plain
66
method: POST
77
status: 504
8-
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4&device_type=pylib&runtime=pytest
8+
url: https://predict.app.landing.ai/inference/v1/predict?endpoint_id=db90b68d-cbfd-4a9c-8dc2-ebc4c3f6e5a4

tests/unit/landingai/test_predict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def test_predict_matching_expected_request_body():
229229
img = Image.open(img_path)
230230
expected_request_files = {"file": _read_image_as_jpeg_bytes(img_path)}
231231
responses.post(
232-
url="https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43&device_type=pylib&runtime=pytest",
232+
url="https://predict.app.landing.ai/inference/v1/predict?endpoint_id=8fc1bc53-c5c1-4154-8cc1-a08f2e17ba43",
233233
match=[multipart_matcher(expected_request_files)],
234234
json={
235235
"backbonetype": None,

0 commit comments

Comments
 (0)