Skip to content

Commit e41aa66

Browse files
author
Matt Sokoloff
committed
abstract annotation import. Format
1 parent 6d1ddb2 commit e41aa66

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,30 @@ pip3 install -r requirements.txt
5656
Labelbox uses API keys to validate requests. You can create and manage API keys on [Labelbox](https://app.labelbox.com/account/api-keys). Pass your API key as an environment variable. Then, import and initialize the API Client.
5757

5858
```
59-
user@machine:~$ export LABELBOX_TEST_API_KEY_LOCAL="<your local api key here>"
59+
user@machine:~$ export LABELBOX_API_KEY="<your local api key here>"
6060
user@machine:~$ python3
6161
6262
from labelbox import Client
63-
client = Client(api_key="your_key_here", endpoint="http://localhost:8080/_gql")
63+
client = Client()
64+
```
65+
* Update api_key and endpoint if not using the production cloud deployment
66+
```
67+
# On prem
68+
client = Client( endpoint = "<local deployment>")
69+
70+
# Local
71+
client = Client(api_key=os.environ['LABELBOX_TEST_API_KEY_LOCAL'], endpoint="http://localhost:8080/graphql")
72+
73+
# Staging
74+
client = Client(api_key=os.environ['LABELBOX_TEST_API_KEY_LOCAL'], endpoint="https://staging-api.labelbox.com/graphql")
6475
```
6576

6677
## Contribution
6778
Please consult `CONTRIB.md`
6879

6980
## Testing
7081
1. Update the `Makefile` with your `local`, `staging`, `prod` API key. Ensure that docker has been installed on your system. Make sure the key is not from a free tier account.
71-
2. To test on `local`:
82+
2. To test on `local`:
7283
```
7384
user@machine:~$ export LABELBOX_TEST_API_KEY_LOCAL="<your local api key here>"
7485
make test-local # with an optional flag: PATH_TO_TEST=tests/integration/...etc LABELBOX_TEST_API_KEY_LOCAL=specify_here_or_export_me

labelbox/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def check_errors(keywords, *path):
192192
if obj in keywords:
193193
return error
194194
return None
195-
195+
196196
def get_error_status_code(error):
197197
return error["extensions"]["exception"]["status"]
198198

labelbox/schema/annotation_import.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
from typing import Any, Dict, List, Union
21
import functools
3-
import os
42
import json
5-
import time
63
import logging
4+
import os
5+
import time
6+
from abc import ABC, abstractmethod
7+
from typing import Any, Dict, List, Union
78

89
import backoff
910
import ndjson
1011
import requests
1112

1213
import labelbox
13-
from labelbox.schema.enums import AnnotationImportState
14+
from labelbox.orm import query
1415
from labelbox.orm.db_object import DbObject
1516
from labelbox.orm.model import Field, Relationship
16-
from labelbox.orm import query
17+
from labelbox.schema.enums import AnnotationImportState
1718

1819
NDJSON_MIME_TYPE = "application/x-ndjson"
1920
logger = logging.getLogger(__name__)
2021

2122

22-
class AnnotationImport(DbObject):
23+
class AnnotationImport(DbObject, ABC):
2324
name = Field.String("name")
2425
state = Field.Enum(AnnotationImportState, "state")
2526
input_file_url = Field.String("input_file_url")
@@ -122,6 +123,10 @@ def _fetch_remote_ndjson(self, url: str) -> List[Dict[str, Any]]:
122123
def _make_file_name(parent_id: str, name: str) -> str:
123124
return f"{parent_id}__{name}.ndjson"
124125

126+
@abstractmethod
127+
def from_name(cls, client, parent, name):
128+
...
129+
125130
def refresh(self) -> None:
126131
"""Synchronizes values of all fields with the database.
127132
"""
@@ -240,11 +245,10 @@ def create_from_url(cls, client: "labelbox.Client", model_run_id: str,
240245
parent_id=model_run_id,
241246
name=name,
242247
url=url)
243-
248+
244249
@classmethod
245-
def from_name(
246-
cls, client: "labelbox.Client", model_run_id: str,
247-
name: str) -> "MEAPredictionImport":
250+
def from_name(cls, client: "labelbox.Client", model_run_id: str,
251+
name: str) -> "MEAPredictionImport":
248252

249253
query_str = """query getModelErrorAnalysisPredictionImportPyApi($modelRunId : ID!, $name: String!) {
250254
modelErrorAnalysisPredictionImport(
@@ -256,8 +260,9 @@ def from_name(
256260
"name": name,
257261
}
258262
response = client.execute(query_str, params)
259-
if response is None:
260-
raise labelbox.exceptions.ResourceNotFoundError(MEAPredictionImport, params)
263+
if response is None:
264+
raise labelbox.exceptions.ResourceNotFoundError(
265+
MEAPredictionImport, params)
261266

262267
return cls(client, response["modelErrorAnalysisPredictionImport"])
263268

@@ -293,9 +298,8 @@ def create_from_url(cls, client: "labelbox.Client", project_id: str,
293298
url=url)
294299

295300
@classmethod
296-
def from_name(
297-
cls, client: "labelbox.Client", project_id: str,
298-
name: str) -> "MALPredictionImport":
301+
def from_name(cls, client: "labelbox.Client", project_id: str,
302+
name: str) -> "MALPredictionImport":
299303

300304
query_str = """query getModelAssistedLabelingPredictionImportPyApi($projectId : ID!, $name: String!) {
301305
modelAssistedLabelingPredictionImport(
@@ -307,7 +311,8 @@ def from_name(
307311
"name": name,
308312
}
309313
response = client.execute(query_str, params)
310-
if response is None:
311-
raise labelbox.exceptions.ResourceNotFoundError(MALPredictionImport, params)
314+
if response is None:
315+
raise labelbox.exceptions.ResourceNotFoundError(
316+
MALPredictionImport, params)
312317

313318
return cls(client, response["modelAssistedLabelingPredictionImport"])

0 commit comments

Comments
 (0)