7
7
import time
8
8
import urllib .parse
9
9
from collections import defaultdict
10
+ from datetime import datetime , timezone
10
11
from types import MappingProxyType
11
12
from typing import Any , Callable , Dict , List , Optional , Set , Union , overload
12
13
13
- import lbox .exceptions
14
14
import requests
15
15
import requests .exceptions
16
16
from google .api_core import retry
17
+ from lbox .exceptions import (
18
+ InternalServerError ,
19
+ LabelboxError ,
20
+ ResourceNotFoundError ,
21
+ TimeoutError ,
22
+ )
17
23
from lbox .request_client import RequestClient
18
24
19
25
from labelbox import __version__ as SDK_VERSION
@@ -111,7 +117,7 @@ def __init__(
111
117
enable_experimental (bool): Indicates whether or not to use experimental features
112
118
app_url (str) : host url for all links to the web app
113
119
Raises:
114
- lbox.exceptions. AuthenticationError: If no `api_key`
120
+ AuthenticationError: If no `api_key`
115
121
is provided as an argument or via the environment
116
122
variable.
117
123
"""
@@ -199,7 +205,7 @@ def upload_file(self, path: str) -> str:
199
205
Returns:
200
206
str, the URL of uploaded data.
201
207
Raises:
202
- lbox.exceptions. LabelboxError: If upload failed.
208
+ LabelboxError: If upload failed.
203
209
"""
204
210
content_type , _ = mimetypes .guess_type (path )
205
211
filename = os .path .basename (path )
@@ -208,9 +214,7 @@ def upload_file(self, path: str) -> str:
208
214
content = f .read (), filename = filename , content_type = content_type
209
215
)
210
216
211
- @retry .Retry (
212
- predicate = retry .if_exception_type (lbox .exceptions .InternalServerError )
213
- )
217
+ @retry .Retry (predicate = retry .if_exception_type (InternalServerError ))
214
218
def upload_data (
215
219
self ,
216
220
content : bytes ,
@@ -230,7 +234,7 @@ def upload_data(
230
234
str, the URL of uploaded data.
231
235
232
236
Raises:
233
- lbox.exceptions. LabelboxError: If upload failed.
237
+ LabelboxError: If upload failed.
234
238
"""
235
239
236
240
request_data = {
@@ -271,18 +275,16 @@ def upload_data(
271
275
272
276
if response .status_code == 502 :
273
277
error_502 = "502 Bad Gateway"
274
- raise lbox . exceptions . InternalServerError (error_502 )
278
+ raise InternalServerError (error_502 )
275
279
elif response .status_code == 503 :
276
- raise lbox . exceptions . InternalServerError (response .text )
280
+ raise InternalServerError (response .text )
277
281
elif response .status_code == 520 :
278
- raise lbox . exceptions . InternalServerError (response .text )
282
+ raise InternalServerError (response .text )
279
283
280
284
try :
281
285
file_data = response .json ().get ("data" , None )
282
286
except ValueError as e : # response is not valid JSON
283
- raise lbox .exceptions .LabelboxError (
284
- "Failed to upload, unknown cause" , e
285
- )
287
+ raise LabelboxError ("Failed to upload, unknown cause" , e )
286
288
287
289
if not file_data or not file_data .get ("uploadFile" , None ):
288
290
try :
@@ -292,9 +294,7 @@ def upload_data(
292
294
)
293
295
except Exception :
294
296
error_msg = "Unknown error"
295
- raise lbox .exceptions .LabelboxError (
296
- "Failed to upload, message: %s" % error_msg
297
- )
297
+ raise LabelboxError ("Failed to upload, message: %s" % error_msg )
298
298
299
299
return file_data ["uploadFile" ]["url" ]
300
300
@@ -307,15 +307,15 @@ def _get_single(self, db_object_type, uid):
307
307
Returns:
308
308
Object of `db_object_type`.
309
309
Raises:
310
- lbox.exceptions. ResourceNotFoundError: If there is no object
310
+ ResourceNotFoundError: If there is no object
311
311
of the given type for the given ID.
312
312
"""
313
313
query_str , params = query .get_single (db_object_type , uid )
314
314
315
315
res = self .execute (query_str , params )
316
316
res = res and res .get (utils .camel_case (db_object_type .type_name ()))
317
317
if res is None :
318
- raise lbox . exceptions . ResourceNotFoundError (db_object_type , params )
318
+ raise ResourceNotFoundError (db_object_type , params )
319
319
else :
320
320
return db_object_type (self , res )
321
321
@@ -329,7 +329,7 @@ def get_project(self, project_id) -> Project:
329
329
Returns:
330
330
The sought Project.
331
331
Raises:
332
- lbox.exceptions. ResourceNotFoundError: If there is no
332
+ ResourceNotFoundError: If there is no
333
333
Project with the given ID.
334
334
"""
335
335
return self ._get_single (Entity .Project , project_id )
@@ -344,7 +344,7 @@ def get_dataset(self, dataset_id) -> Dataset:
344
344
Returns:
345
345
The sought Dataset.
346
346
Raises:
347
- lbox.exceptions. ResourceNotFoundError: If there is no
347
+ ResourceNotFoundError: If there is no
348
348
Dataset with the given ID.
349
349
"""
350
350
return self ._get_single (Entity .Dataset , dataset_id )
@@ -470,7 +470,7 @@ def _create(self, db_object_type, data, extra_params={}):
470
470
)
471
471
472
472
if not res :
473
- raise lbox . exceptions . LabelboxError (
473
+ raise LabelboxError (
474
474
"Failed to create %s" % db_object_type .type_name ()
475
475
)
476
476
res = res ["create%s" % db_object_type .type_name ()]
@@ -528,9 +528,7 @@ def delete_model_config(self, id: str) -> bool:
528
528
params = {"id" : id }
529
529
result = self .execute (query , params )
530
530
if not result :
531
- raise lbox .exceptions .ResourceNotFoundError (
532
- Entity .ModelConfig , params
533
- )
531
+ raise ResourceNotFoundError (Entity .ModelConfig , params )
534
532
return result ["deleteModelConfig" ]["success" ]
535
533
536
534
def create_dataset (
@@ -589,7 +587,7 @@ def create_dataset(
589
587
)
590
588
591
589
if not validation_result ["validateDataset" ]["valid" ]:
592
- raise lbox . exceptions . LabelboxError (
590
+ raise LabelboxError (
593
591
"IAMIntegration was not successfully added to the dataset."
594
592
)
595
593
except Exception as e :
@@ -895,7 +893,7 @@ def get_data_row_by_global_key(self, global_key: str) -> DataRow:
895
893
"""
896
894
res = self .get_data_row_ids_for_global_keys ([global_key ])
897
895
if res ["status" ] != "SUCCESS" :
898
- raise lbox . exceptions . ResourceNotFoundError (
896
+ raise ResourceNotFoundError (
899
897
Entity .DataRow , {global_key : global_key }
900
898
)
901
899
data_row_id = res ["results" ][0 ]
@@ -923,7 +921,7 @@ def get_model(self, model_id) -> Model:
923
921
Returns:
924
922
The sought Model.
925
923
Raises:
926
- lbox.exceptions. ResourceNotFoundError: If there is no
924
+ ResourceNotFoundError: If there is no
927
925
Model with the given ID.
928
926
"""
929
927
return self ._get_single (Entity .Model , model_id )
@@ -1169,7 +1167,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
1169
1167
response = self .connection .delete (endpoint )
1170
1168
1171
1169
if response .status_code != requests .codes .no_content :
1172
- raise lbox . exceptions . LabelboxError (
1170
+ raise LabelboxError (
1173
1171
"Failed to delete the feature schema, message: "
1174
1172
+ str (response .json ()["message" ])
1175
1173
)
@@ -1190,7 +1188,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
1190
1188
response = self .connection .delete (endpoint )
1191
1189
1192
1190
if response .status_code != requests .codes .no_content :
1193
- raise lbox . exceptions . LabelboxError (
1191
+ raise LabelboxError (
1194
1192
"Failed to delete the ontology, message: "
1195
1193
+ str (response .json ()["message" ])
1196
1194
)
@@ -1220,7 +1218,7 @@ def update_feature_schema_title(
1220
1218
if response .status_code == requests .codes .ok :
1221
1219
return self .get_feature_schema (feature_schema_id )
1222
1220
else :
1223
- raise lbox . exceptions . LabelboxError (
1221
+ raise LabelboxError (
1224
1222
"Failed to update the feature schema, message: "
1225
1223
+ str (response .json ()["message" ])
1226
1224
)
@@ -1256,7 +1254,7 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
1256
1254
if response .status_code == requests .codes .ok :
1257
1255
return self .get_feature_schema (response .json ()["schemaId" ])
1258
1256
else :
1259
- raise lbox . exceptions . LabelboxError (
1257
+ raise LabelboxError (
1260
1258
"Failed to upsert the feature schema, message: "
1261
1259
+ str (response .json ()["message" ])
1262
1260
)
@@ -1284,7 +1282,7 @@ def insert_feature_schema_into_ontology(
1284
1282
)
1285
1283
response = self .connection .post (endpoint , json = {"position" : position })
1286
1284
if response .status_code != requests .codes .created :
1287
- raise lbox . exceptions . LabelboxError (
1285
+ raise LabelboxError (
1288
1286
"Failed to insert the feature schema into the ontology, message: "
1289
1287
+ str (response .json ()["message" ])
1290
1288
)
@@ -1309,7 +1307,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
1309
1307
if response .status_code == requests .codes .ok :
1310
1308
return response .json ()
1311
1309
else :
1312
- raise lbox . exceptions . LabelboxError (
1310
+ raise LabelboxError (
1313
1311
"Failed to get unused ontologies, message: "
1314
1312
+ str (response .json ()["message" ])
1315
1313
)
@@ -1334,7 +1332,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
1334
1332
if response .status_code == requests .codes .ok :
1335
1333
return response .json ()
1336
1334
else :
1337
- raise lbox . exceptions . LabelboxError (
1335
+ raise LabelboxError (
1338
1336
"Failed to get unused feature schemas, message: "
1339
1337
+ str (response .json ()["message" ])
1340
1338
)
@@ -1630,12 +1628,12 @@ def _format_failed_rows(
1630
1628
elif (
1631
1629
res ["assignGlobalKeysToDataRowsResult" ]["jobStatus" ] == "FAILED"
1632
1630
):
1633
- raise lbox . exceptions . LabelboxError (
1631
+ raise LabelboxError (
1634
1632
"Job assign_global_keys_to_data_rows failed."
1635
1633
)
1636
1634
current_time = time .time ()
1637
1635
if current_time - start_time > timeout_seconds :
1638
- raise lbox . exceptions . TimeoutError (
1636
+ raise TimeoutError (
1639
1637
"Timed out waiting for assign_global_keys_to_data_rows job to complete."
1640
1638
)
1641
1639
time .sleep (sleep_time )
@@ -1739,12 +1737,10 @@ def _format_failed_rows(
1739
1737
1740
1738
return {"status" : status , "results" : results , "errors" : errors }
1741
1739
elif res ["dataRowsForGlobalKeysResult" ]["jobStatus" ] == "FAILED" :
1742
- raise lbox .exceptions .LabelboxError (
1743
- "Job dataRowsForGlobalKeys failed."
1744
- )
1740
+ raise LabelboxError ("Job dataRowsForGlobalKeys failed." )
1745
1741
current_time = time .time ()
1746
1742
if current_time - start_time > timeout_seconds :
1747
- raise lbox . exceptions . TimeoutError (
1743
+ raise TimeoutError (
1748
1744
"Timed out waiting for get_data_rows_for_global_keys job to complete."
1749
1745
)
1750
1746
time .sleep (sleep_time )
@@ -1843,12 +1839,10 @@ def _format_failed_rows(
1843
1839
1844
1840
return {"status" : status , "results" : results , "errors" : errors }
1845
1841
elif res ["clearGlobalKeysResult" ]["jobStatus" ] == "FAILED" :
1846
- raise lbox .exceptions .LabelboxError (
1847
- "Job clearGlobalKeys failed."
1848
- )
1842
+ raise LabelboxError ("Job clearGlobalKeys failed." )
1849
1843
current_time = time .time ()
1850
1844
if current_time - start_time > timeout_seconds :
1851
- raise lbox . exceptions . TimeoutError (
1845
+ raise TimeoutError (
1852
1846
"Timed out waiting for clear_global_keys job to complete."
1853
1847
)
1854
1848
time .sleep (sleep_time )
@@ -1913,14 +1907,14 @@ def is_feature_schema_archived(
1913
1907
if filtered_feature_schema_nodes :
1914
1908
return bool (filtered_feature_schema_nodes [0 ]["archived" ])
1915
1909
else :
1916
- raise lbox . exceptions . LabelboxError (
1910
+ raise LabelboxError (
1917
1911
"The specified feature schema was not in the ontology."
1918
1912
)
1919
1913
1920
1914
elif response .status_code == 404 :
1921
- raise lbox . exceptions . ResourceNotFoundError (Ontology , ontology_id )
1915
+ raise ResourceNotFoundError (Ontology , ontology_id )
1922
1916
else :
1923
- raise lbox . exceptions . LabelboxError (
1917
+ raise LabelboxError (
1924
1918
"Failed to get the feature schema archived status."
1925
1919
)
1926
1920
@@ -1947,7 +1941,7 @@ def get_model_slice(self, slice_id) -> ModelSlice:
1947
1941
"""
1948
1942
res = self .execute (query_str , {"id" : slice_id })
1949
1943
if res is None or res ["getSavedQuery" ] is None :
1950
- raise lbox . exceptions . ResourceNotFoundError (ModelSlice , slice_id )
1944
+ raise ResourceNotFoundError (ModelSlice , slice_id )
1951
1945
1952
1946
return Entity .ModelSlice (self , res ["getSavedQuery" ])
1953
1947
@@ -1994,7 +1988,7 @@ def delete_feature_schema_from_ontology(
1994
1988
result .deleted = bool (response_json ["deleted" ])
1995
1989
return result
1996
1990
else :
1997
- raise lbox . exceptions . LabelboxError (
1991
+ raise LabelboxError (
1998
1992
"Failed to remove feature schema from ontology, message: "
1999
1993
+ str (response .json ()["message" ])
2000
1994
)
@@ -2022,11 +2016,9 @@ def unarchive_feature_schema_node(
2022
2016
response = self .connection .patch (ontology_endpoint )
2023
2017
if response .status_code == requests .codes .ok :
2024
2018
if not bool (response .json ()["unarchived" ]):
2025
- raise lbox .exceptions .LabelboxError (
2026
- "Failed unarchive the feature schema."
2027
- )
2019
+ raise LabelboxError ("Failed unarchive the feature schema." )
2028
2020
else :
2029
- raise lbox . exceptions . LabelboxError (
2021
+ raise LabelboxError (
2030
2022
"Failed unarchive the feature schema node, message: " ,
2031
2023
response .text ,
2032
2024
)
@@ -2255,7 +2247,7 @@ def get_embedding_by_name(self, name: str) -> Embedding:
2255
2247
for e in embeddings :
2256
2248
if e .name == name :
2257
2249
return e
2258
- raise lbox . exceptions . ResourceNotFoundError (Embedding , dict (name = name ))
2250
+ raise ResourceNotFoundError (Embedding , dict (name = name ))
2259
2251
2260
2252
def upsert_label_feedback (
2261
2253
self , label_id : str , feedback : str , scores : Dict [str , float ]
@@ -2378,7 +2370,7 @@ def get_task_by_id(self, task_id: str) -> Union[Task, DataUpsertTask]:
2378
2370
result = self .execute (query , {"userId" : user .uid , "taskId" : task_id })
2379
2371
data = result .get ("user" , {}).get ("createdTasks" , [])
2380
2372
if not data :
2381
- raise lbox . exceptions . ResourceNotFoundError (
2373
+ raise ResourceNotFoundError (
2382
2374
message = f"The task { task_id } does not exist."
2383
2375
)
2384
2376
task_data = data [0 ]
0 commit comments