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 )
@@ -209,7 +215,7 @@ def upload_file(self, path: str) -> str:
209
215
)
210
216
211
217
@retry .Retry (
212
- predicate = retry .if_exception_type (lbox . exceptions . InternalServerError )
218
+ predicate = retry .if_exception_type (InternalServerError )
213
219
)
214
220
def upload_data (
215
221
self ,
@@ -230,7 +236,7 @@ def upload_data(
230
236
str, the URL of uploaded data.
231
237
232
238
Raises:
233
- lbox.exceptions. LabelboxError: If upload failed.
239
+ LabelboxError: If upload failed.
234
240
"""
235
241
236
242
request_data = {
@@ -271,16 +277,16 @@ def upload_data(
271
277
272
278
if response .status_code == 502 :
273
279
error_502 = "502 Bad Gateway"
274
- raise lbox . exceptions . InternalServerError (error_502 )
280
+ raise InternalServerError (error_502 )
275
281
elif response .status_code == 503 :
276
- raise lbox . exceptions . InternalServerError (response .text )
282
+ raise InternalServerError (response .text )
277
283
elif response .status_code == 520 :
278
- raise lbox . exceptions . InternalServerError (response .text )
284
+ raise InternalServerError (response .text )
279
285
280
286
try :
281
287
file_data = response .json ().get ("data" , None )
282
288
except ValueError as e : # response is not valid JSON
283
- raise lbox . exceptions . LabelboxError (
289
+ raise LabelboxError (
284
290
"Failed to upload, unknown cause" , e
285
291
)
286
292
@@ -292,7 +298,7 @@ def upload_data(
292
298
)
293
299
except Exception :
294
300
error_msg = "Unknown error"
295
- raise lbox . exceptions . LabelboxError (
301
+ raise LabelboxError (
296
302
"Failed to upload, message: %s" % error_msg
297
303
)
298
304
@@ -307,15 +313,15 @@ def _get_single(self, db_object_type, uid):
307
313
Returns:
308
314
Object of `db_object_type`.
309
315
Raises:
310
- lbox.exceptions. ResourceNotFoundError: If there is no object
316
+ ResourceNotFoundError: If there is no object
311
317
of the given type for the given ID.
312
318
"""
313
319
query_str , params = query .get_single (db_object_type , uid )
314
320
315
321
res = self .execute (query_str , params )
316
322
res = res and res .get (utils .camel_case (db_object_type .type_name ()))
317
323
if res is None :
318
- raise lbox . exceptions . ResourceNotFoundError (db_object_type , params )
324
+ raise ResourceNotFoundError (db_object_type , params )
319
325
else :
320
326
return db_object_type (self , res )
321
327
@@ -329,7 +335,7 @@ def get_project(self, project_id) -> Project:
329
335
Returns:
330
336
The sought Project.
331
337
Raises:
332
- lbox.exceptions. ResourceNotFoundError: If there is no
338
+ ResourceNotFoundError: If there is no
333
339
Project with the given ID.
334
340
"""
335
341
return self ._get_single (Entity .Project , project_id )
@@ -344,7 +350,7 @@ def get_dataset(self, dataset_id) -> Dataset:
344
350
Returns:
345
351
The sought Dataset.
346
352
Raises:
347
- lbox.exceptions. ResourceNotFoundError: If there is no
353
+ ResourceNotFoundError: If there is no
348
354
Dataset with the given ID.
349
355
"""
350
356
return self ._get_single (Entity .Dataset , dataset_id )
@@ -470,7 +476,7 @@ def _create(self, db_object_type, data, extra_params={}):
470
476
)
471
477
472
478
if not res :
473
- raise lbox . exceptions . LabelboxError (
479
+ raise LabelboxError (
474
480
"Failed to create %s" % db_object_type .type_name ()
475
481
)
476
482
res = res ["create%s" % db_object_type .type_name ()]
@@ -528,7 +534,7 @@ def delete_model_config(self, id: str) -> bool:
528
534
params = {"id" : id }
529
535
result = self .execute (query , params )
530
536
if not result :
531
- raise lbox . exceptions . ResourceNotFoundError (
537
+ raise ResourceNotFoundError (
532
538
Entity .ModelConfig , params
533
539
)
534
540
return result ["deleteModelConfig" ]["success" ]
@@ -589,7 +595,7 @@ def create_dataset(
589
595
)
590
596
591
597
if not validation_result ["validateDataset" ]["valid" ]:
592
- raise lbox . exceptions . LabelboxError (
598
+ raise LabelboxError (
593
599
"IAMIntegration was not successfully added to the dataset."
594
600
)
595
601
except Exception as e :
@@ -895,7 +901,7 @@ def get_data_row_by_global_key(self, global_key: str) -> DataRow:
895
901
"""
896
902
res = self .get_data_row_ids_for_global_keys ([global_key ])
897
903
if res ["status" ] != "SUCCESS" :
898
- raise lbox . exceptions . ResourceNotFoundError (
904
+ raise ResourceNotFoundError (
899
905
Entity .DataRow , {global_key : global_key }
900
906
)
901
907
data_row_id = res ["results" ][0 ]
@@ -923,7 +929,7 @@ def get_model(self, model_id) -> Model:
923
929
Returns:
924
930
The sought Model.
925
931
Raises:
926
- lbox.exceptions. ResourceNotFoundError: If there is no
932
+ ResourceNotFoundError: If there is no
927
933
Model with the given ID.
928
934
"""
929
935
return self ._get_single (Entity .Model , model_id )
@@ -1169,7 +1175,7 @@ def delete_unused_feature_schema(self, feature_schema_id: str) -> None:
1169
1175
response = self .connection .delete (endpoint )
1170
1176
1171
1177
if response .status_code != requests .codes .no_content :
1172
- raise lbox . exceptions . LabelboxError (
1178
+ raise LabelboxError (
1173
1179
"Failed to delete the feature schema, message: "
1174
1180
+ str (response .json ()["message" ])
1175
1181
)
@@ -1190,7 +1196,7 @@ def delete_unused_ontology(self, ontology_id: str) -> None:
1190
1196
response = self .connection .delete (endpoint )
1191
1197
1192
1198
if response .status_code != requests .codes .no_content :
1193
- raise lbox . exceptions . LabelboxError (
1199
+ raise LabelboxError (
1194
1200
"Failed to delete the ontology, message: "
1195
1201
+ str (response .json ()["message" ])
1196
1202
)
@@ -1220,7 +1226,7 @@ def update_feature_schema_title(
1220
1226
if response .status_code == requests .codes .ok :
1221
1227
return self .get_feature_schema (feature_schema_id )
1222
1228
else :
1223
- raise lbox . exceptions . LabelboxError (
1229
+ raise LabelboxError (
1224
1230
"Failed to update the feature schema, message: "
1225
1231
+ str (response .json ()["message" ])
1226
1232
)
@@ -1256,7 +1262,7 @@ def upsert_feature_schema(self, feature_schema: Dict) -> FeatureSchema:
1256
1262
if response .status_code == requests .codes .ok :
1257
1263
return self .get_feature_schema (response .json ()["schemaId" ])
1258
1264
else :
1259
- raise lbox . exceptions . LabelboxError (
1265
+ raise LabelboxError (
1260
1266
"Failed to upsert the feature schema, message: "
1261
1267
+ str (response .json ()["message" ])
1262
1268
)
@@ -1284,7 +1290,7 @@ def insert_feature_schema_into_ontology(
1284
1290
)
1285
1291
response = self .connection .post (endpoint , json = {"position" : position })
1286
1292
if response .status_code != requests .codes .created :
1287
- raise lbox . exceptions . LabelboxError (
1293
+ raise LabelboxError (
1288
1294
"Failed to insert the feature schema into the ontology, message: "
1289
1295
+ str (response .json ()["message" ])
1290
1296
)
@@ -1309,7 +1315,7 @@ def get_unused_ontologies(self, after: str = None) -> List[str]:
1309
1315
if response .status_code == requests .codes .ok :
1310
1316
return response .json ()
1311
1317
else :
1312
- raise lbox . exceptions . LabelboxError (
1318
+ raise LabelboxError (
1313
1319
"Failed to get unused ontologies, message: "
1314
1320
+ str (response .json ()["message" ])
1315
1321
)
@@ -1334,7 +1340,7 @@ def get_unused_feature_schemas(self, after: str = None) -> List[str]:
1334
1340
if response .status_code == requests .codes .ok :
1335
1341
return response .json ()
1336
1342
else :
1337
- raise lbox . exceptions . LabelboxError (
1343
+ raise LabelboxError (
1338
1344
"Failed to get unused feature schemas, message: "
1339
1345
+ str (response .json ()["message" ])
1340
1346
)
@@ -1630,12 +1636,12 @@ def _format_failed_rows(
1630
1636
elif (
1631
1637
res ["assignGlobalKeysToDataRowsResult" ]["jobStatus" ] == "FAILED"
1632
1638
):
1633
- raise lbox . exceptions . LabelboxError (
1639
+ raise LabelboxError (
1634
1640
"Job assign_global_keys_to_data_rows failed."
1635
1641
)
1636
1642
current_time = time .time ()
1637
1643
if current_time - start_time > timeout_seconds :
1638
- raise lbox . exceptions . TimeoutError (
1644
+ raise TimeoutError (
1639
1645
"Timed out waiting for assign_global_keys_to_data_rows job to complete."
1640
1646
)
1641
1647
time .sleep (sleep_time )
@@ -1739,12 +1745,12 @@ def _format_failed_rows(
1739
1745
1740
1746
return {"status" : status , "results" : results , "errors" : errors }
1741
1747
elif res ["dataRowsForGlobalKeysResult" ]["jobStatus" ] == "FAILED" :
1742
- raise lbox . exceptions . LabelboxError (
1748
+ raise LabelboxError (
1743
1749
"Job dataRowsForGlobalKeys failed."
1744
1750
)
1745
1751
current_time = time .time ()
1746
1752
if current_time - start_time > timeout_seconds :
1747
- raise lbox . exceptions . TimeoutError (
1753
+ raise TimeoutError (
1748
1754
"Timed out waiting for get_data_rows_for_global_keys job to complete."
1749
1755
)
1750
1756
time .sleep (sleep_time )
@@ -1843,12 +1849,12 @@ def _format_failed_rows(
1843
1849
1844
1850
return {"status" : status , "results" : results , "errors" : errors }
1845
1851
elif res ["clearGlobalKeysResult" ]["jobStatus" ] == "FAILED" :
1846
- raise lbox . exceptions . LabelboxError (
1852
+ raise LabelboxError (
1847
1853
"Job clearGlobalKeys failed."
1848
1854
)
1849
1855
current_time = time .time ()
1850
1856
if current_time - start_time > timeout_seconds :
1851
- raise lbox . exceptions . TimeoutError (
1857
+ raise TimeoutError (
1852
1858
"Timed out waiting for clear_global_keys job to complete."
1853
1859
)
1854
1860
time .sleep (sleep_time )
@@ -1913,14 +1919,14 @@ def is_feature_schema_archived(
1913
1919
if filtered_feature_schema_nodes :
1914
1920
return bool (filtered_feature_schema_nodes [0 ]["archived" ])
1915
1921
else :
1916
- raise lbox . exceptions . LabelboxError (
1922
+ raise LabelboxError (
1917
1923
"The specified feature schema was not in the ontology."
1918
1924
)
1919
1925
1920
1926
elif response .status_code == 404 :
1921
- raise lbox . exceptions . ResourceNotFoundError (Ontology , ontology_id )
1927
+ raise ResourceNotFoundError (Ontology , ontology_id )
1922
1928
else :
1923
- raise lbox . exceptions . LabelboxError (
1929
+ raise LabelboxError (
1924
1930
"Failed to get the feature schema archived status."
1925
1931
)
1926
1932
@@ -1947,7 +1953,7 @@ def get_model_slice(self, slice_id) -> ModelSlice:
1947
1953
"""
1948
1954
res = self .execute (query_str , {"id" : slice_id })
1949
1955
if res is None or res ["getSavedQuery" ] is None :
1950
- raise lbox . exceptions . ResourceNotFoundError (ModelSlice , slice_id )
1956
+ raise ResourceNotFoundError (ModelSlice , slice_id )
1951
1957
1952
1958
return Entity .ModelSlice (self , res ["getSavedQuery" ])
1953
1959
@@ -1994,7 +2000,7 @@ def delete_feature_schema_from_ontology(
1994
2000
result .deleted = bool (response_json ["deleted" ])
1995
2001
return result
1996
2002
else :
1997
- raise lbox . exceptions . LabelboxError (
2003
+ raise LabelboxError (
1998
2004
"Failed to remove feature schema from ontology, message: "
1999
2005
+ str (response .json ()["message" ])
2000
2006
)
@@ -2022,11 +2028,11 @@ def unarchive_feature_schema_node(
2022
2028
response = self .connection .patch (ontology_endpoint )
2023
2029
if response .status_code == requests .codes .ok :
2024
2030
if not bool (response .json ()["unarchived" ]):
2025
- raise lbox . exceptions . LabelboxError (
2031
+ raise LabelboxError (
2026
2032
"Failed unarchive the feature schema."
2027
2033
)
2028
2034
else :
2029
- raise lbox . exceptions . LabelboxError (
2035
+ raise LabelboxError (
2030
2036
"Failed unarchive the feature schema node, message: " ,
2031
2037
response .text ,
2032
2038
)
@@ -2255,7 +2261,7 @@ def get_embedding_by_name(self, name: str) -> Embedding:
2255
2261
for e in embeddings :
2256
2262
if e .name == name :
2257
2263
return e
2258
- raise lbox . exceptions . ResourceNotFoundError (Embedding , dict (name = name ))
2264
+ raise ResourceNotFoundError (Embedding , dict (name = name ))
2259
2265
2260
2266
def upsert_label_feedback (
2261
2267
self , label_id : str , feedback : str , scores : Dict [str , float ]
@@ -2378,7 +2384,7 @@ def get_task_by_id(self, task_id: str) -> Union[Task, DataUpsertTask]:
2378
2384
result = self .execute (query , {"userId" : user .uid , "taskId" : task_id })
2379
2385
data = result .get ("user" , {}).get ("createdTasks" , [])
2380
2386
if not data :
2381
- raise lbox . exceptions . ResourceNotFoundError (
2387
+ raise ResourceNotFoundError (
2382
2388
message = f"The task { task_id } does not exist."
2383
2389
)
2384
2390
task_data = data [0 ]
0 commit comments