Skip to content

SDK v2 development #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 38 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d6d6dec
initial commit v2.0
fatihkurtoglu Mar 26, 2021
8889dbd
v2.0 development pt2
fatihkurtoglu Mar 29, 2021
7373239
minor fixes
fatihkurtoglu Mar 29, 2021
7cb5a17
readme v2 and updates to objects
fatihkurtoglu Mar 29, 2021
327db4e
readme updates
fatihkurtoglu Mar 29, 2021
d04b5fa
readme enhanced with samples
fatihkurtoglu Mar 29, 2021
f42f5c1
updates to gitignore, setup and manifest files
fatihkurtoglu Mar 30, 2021
5791fd5
pytest updated
fatihkurtoglu Mar 30, 2021
3e08bca
docstring updates in model files
fatihkurtoglu Mar 30, 2021
107ada7
exception structure and pylint recommendations
fatihkurtoglu Mar 30, 2021
49f19f8
pylint configuration
fatihkurtoglu Mar 30, 2021
8bc75ed
re-formatting by black
fatihkurtoglu Mar 30, 2021
04a6f01
fixed setup to add urllib3 requirement
fatihkurtoglu Mar 30, 2021
7ae04eb
fixed docstring
fatihkurtoglu Mar 30, 2021
ee7cafc
updates to project model, update_project method
fatihkurtoglu Mar 30, 2021
c7f4521
updates to readme and migration guide
fatihkurtoglu Mar 30, 2021
ccc4dff
typo in migration guide
fatihkurtoglu Mar 30, 2021
cde7cd1
updates to migration guide
fatihkurtoglu Mar 30, 2021
7691314
pre-commit configuration
fatihkurtoglu Mar 31, 2021
5478aa4
readme whitespace changes
fatihkurtoglu Mar 31, 2021
39283f7
pre-commit formatting updates
fatihkurtoglu Mar 31, 2021
7d8c231
pre-commit settings updated
fatihkurtoglu Mar 31, 2021
c785f56
global http parameters
fatihkurtoglu Mar 31, 2021
c995778
minor updates to pytest and batch
fatihkurtoglu Mar 31, 2021
67a3116
update pytest
fatihkurtoglu Apr 1, 2021
00cac12
make project params optional in creation
fatihkurtoglu Apr 1, 2021
697f9ee
pre-commit updates
fatihkurtoglu Apr 1, 2021
5df8782
changes to pre-commit
fatihkurtoglu Apr 1, 2021
1dbe25d
pre-commit updates
fatihkurtoglu Apr 1, 2021
aa52f5c
updated readme
fatihkurtoglu Apr 1, 2021
afdbd02
readme update
fatihkurtoglu Apr 1, 2021
9067831
readme updated with icons
fatihkurtoglu Apr 1, 2021
65f216c
automated publish to pypi
fatihkurtoglu Apr 1, 2021
3b7938a
version update guide
fatihkurtoglu Apr 1, 2021
a105982
updated documentation
fatihkurtoglu Apr 1, 2021
cf018aa
Updated developer_guide
fatihkurtoglu Apr 1, 2021
2118ffd
method updates
fatihkurtoglu Apr 5, 2021
4ad0951
updated documentation
fatihkurtoglu Apr 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
*.pyc
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
/build/
/dist/
/*.egg-info
.tox
.cache
/.vscode/
*.egg
*.eggs
*.egg-info/
MANIFEST

# For Visual Studio Code
.vscode/

# Mac
.DS_Store
/build/

# Unit test / coverage reports
.[nt]ox/
htmlcov/
.coverage
.coverage.*
.*cache
nosetests.xml
coverage.xml
*.cover
41 changes: 41 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
default_language_version:
python: python3.6
default_stages: [commit]

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- id: check-yaml
- id: check-case-conflict
- repo: https://github.com/pycqa/isort
rev: 5.8.0
hooks:
- id: isort
name: isort
args: ["--profile", "black"]
- repo: https://github.com/psf/black
rev: 20.8b1
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.8.4
hooks:
- id: flake8
- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: python
types: [python]
files: scaleapi/
additional_dependencies:
- 'pylint>=2.7.4'
- 'requests>=2.25.0'
- 'urllib3>=1.26.0'
- 'pytest>=6.2.2'
language_version: python3.6
8 changes: 8 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[MASTER]
disable=
missing-module-docstring,
too-few-public-methods,
too-many-locals,
too-many-arguments,
too-many-instance-attributes,
invalid-name,
5 changes: 0 additions & 5 deletions MANIFEST

This file was deleted.

156 changes: 99 additions & 57 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
=====================
*********************
Scale AI | Python SDK
=====================
*********************

If you use earlier versions of the SDK, please refer to `v1.0.4 documentation <https://github.com/scaleapi/scaleapi-python-client/blob/release-1.0.4/README.rst>`_.

If you are migrating from earlier versions to v2, please refer to `Migration Guide to v2 <https://github.com/scaleapi/scaleapi-python-client/blob/master/docs/migration_guide.md>`_.

|pic1| |pic2| |pic3|

.. |pic1| image:: https://pepy.tech/badge/scaleapi/month
:alt: Downloads
:target: https://pepy.tech/project/scaleapi
.. |pic2| image:: https://img.shields.io/pypi/pyversions/scaleapi.svg
:alt: Supported Versions
:target: https://pypi.org/project/scaleapi
.. |pic3| image:: https://img.shields.io/github/contributors/scaleapi/scaleapi-python-client.svg
:alt: Contributors
:target: https://github.com/scaleapi/scaleapi-python-client/graphs/contributors

Installation
____________
Expand All @@ -9,8 +25,6 @@ ____________

$ pip install --upgrade scaleapi

Note: We strongly suggest using `scaleapi` with Python version 2.7.9 or greater due to SSL issues with prior versions.

Usage
_____

Expand All @@ -23,11 +37,11 @@ Tasks
_____

Most of these methods will return a `scaleapi.Task` object, which will contain information
about the json response (task_id, status, etc.).
about the json response (task_id, status, params, response etc.).

Any parameter available in `Scale's API documentation`__ can be passed as an argument option with the corresponding type.

__ https://docs.scale.com/reference#task-object
__ https://docs.scale.com/reference#tasks-object-overview

The following endpoints for tasks are available:

Expand All @@ -38,15 +52,18 @@ This method can be used for any Scale supported task type using the following fo

.. code-block:: python

client.create_{{Task Type}}_task(...)
client.create_task(TaskType, ...task parameters...)

Passing in the applicable values into the function definition. The applicable fields and further information for each task type can be found in `Scale's API documentation`__.

__ https://docs.scale.com/reference#general-image-annotation
__ https://docs.scale.com/reference

.. code-block:: python

client.create_imageannotation_task(
from scaleapi.tasks import TaskType

client.create_task(
TaskType.ImageAnnotation,
project = 'test_project',
callback_url = "http://www.example.com/callback",
instruction= "Draw a box around each baby cow and big cow.",
Expand All @@ -61,46 +78,49 @@ __ https://docs.scale.com/reference#general-image-annotation
}
)

Retrieve task
^^^^^^^^^^^^^
Retrieve a task
^^^^^^^^^^^^^^^

Retrieve a task given its id. Check out `Scale's API documentation`__ for more information.

__ https://docs.scale.com/reference#retrieve-tasks

.. code-block :: python

task = client.fetch_task('asdfasdfasdfasdfasdfasdf')
print(task.status) // Task status ('pending', 'completed', 'error', 'canceled')
print(task.response) // If task is complete
task = client.get_task('30553edd0b6a93f8f05f0fee')
print(task.status) # Task status ('pending', 'completed', 'error', 'canceled')
print(task.response) # If task is complete

List Tasks
^^^^^^^^^^

Retrieve a list of tasks, with optional filter by start and end date/time. Paginated with `next_token`. The return value is a `scaleapi.Tasklist`, which acts as a list, but also has fields for the total number of tasks, the limit and offset, and whether or not there's more. Check out `Scale's API documentation`__ for more information.
Retrieve a list of `Task` objects, with optional filters for: `project_name, batch_name, type, status, review_status, unique_id, completed_after, completed_before, updated_after, updated_before, created_after, created_before` and `tags`.

This method is a generator and yields tasks. It can be wrapped in a `list` statement if a Task list is needed.

Check out `Scale's API documentation`__ for more information.

__ https://docs.scale.com/reference#list-multiple-tasks

.. code-block :: python

next_token = None;
counter = 0
all_tasks =[]
while True:
tasks = client.tasks(
start_time = "2020-09-08",
end_time = "2021-01-01",
customer_review_status = "accepted",
next_token = next_token,
)
for task in tasks:
counter += 1
print('Downloading Task %s | %s' % (counter, task.task_id))
all_tasks.append(task.__dict__['param_dict'])
next_token = tasks.next_token
if next_token is None:
break
print(all_tasks)
from scaleapi.tasks import TaskReviewStatus, TaskStatus

tasks = client.tasks_all(
project_name = "My Project",
created_after = "2020-09-08",
completed_before = "2021-04-01",
status = TaskStatus.Completed,
review_status = TaskReviewStatus.Accepted
)

for task in tasks:
# Download task or do something!
print(task.task_id)

# Alternative for accessing as a Task list
task_list = list(tasks)
print(f"{len(task_list))} tasks retrieved")

Cancel Task
^^^^^^^^^^^
Expand Down Expand Up @@ -153,6 +173,11 @@ __ https://docs.scale.com/reference#batch-status

client.batch_status(batch_name = 'batch_name_01_07_2021')

# Alternative via Batch.get_status()
batch = client.get_batch('batch_name_01_07_2021')
batch.get_status() # Refreshes tasks_{status} attributes of Batch
print(batch.tasks_pending, batch.tasks_completed)

Retrieve Batch
^^^^^^^^^^^^^^

Expand All @@ -167,27 +192,29 @@ __ https://docs.scale.com/reference#batch-retrieval
List Batches
^^^^^^^^^^^^

Retrieve a list of Batches. Check out `Scale's API documentation`__ for more information.
Retrieve a list of Batches. Optional parameters are `project_name, batch_status, created_after, created_before`.

Check out `Scale's API documentation`__ for more information.

__ https://docs.scale.com/reference#batch-list

.. code-block :: python

next_token = None;
from scaleapi.batches import BatchStatus

batches = client.batches_all(
batch_status=BatchStatus.Completed,
created_after = "2020-09-08"
)

counter = 0
all_batchs =[]
while True:
batches = client.list_batches(
status = "completed"
)
for batch in batches:
counter += 1
print('Downloading Batch %s | %s | %s' % (counter, batch.name, batch.param_dict['status']))
all_batchs.append(batch.__dict__['param_dict'])
next_token = batches.next_token
if next_token is None:
break
print(all_batchs)
for batch in batches:
counter += 1
print(f'Downloading batch {counter} | {batch.name} | {batch.project}')

# Alternative for accessing as a Batch list
batch_list = list(batches)
print(f"{len(batch_list))} batches retrieved")

Projects
________
Expand Down Expand Up @@ -221,7 +248,7 @@ __ https://docs.scale.com/reference#project-retrieval
List Projects
^^^^^^^^^^^^^

This function does not take any arguments. Retrieve a list of every Project.
This function does not take any arguments. Retrieve a list of every Project.
Check out `Scale's API documentation`__ for more information.

__ https://docs.scale.com/reference#batch-list
Expand All @@ -232,7 +259,7 @@ __ https://docs.scale.com/reference#batch-list
projects = client.projects()
for project in projects:
counter += 1
print('Downloading project %s | %s | %s' % (counter, project['name'], project['type']))
print(f'Downloading project {counter} | {project.name} | { project.type}')

Update Project
^^^^^^^^^^^^^^
Expand All @@ -245,23 +272,38 @@ __ https://docs.scale.com/reference#project-update-parameters

data = client.update_project(
project_name='test_project',
pathc = false,
patch = false,
instruction='update: Please label all the stuff',
)

Error handling
______________

If something went wrong while making API calls, then exceptions will be raised automatically
as a `scaleapi.ScaleException` or `scaleapi.ScaleInvalidRequest` runtime error. For example:
as a `ScaleException` parent type and child exceptions:

- ``ScaleInvalidRequest``: 400 - Bad Request -- The request was unacceptable, often due to missing a required parameter.
- ``ScaleUnauthorized``: 401 - Unauthorized -- No valid API key provided.
- ``ScaleNotEnabled``: 402 - Not enabled -- Please contact sales@scaleapi.com before creating this type of task.
- ``ScaleResourceNotFound``: 404 - Not Found -- The requested resource doesn't exist.
- ``ScaleDuplicateTask``: 409 - Conflict -- The provided idempotency key or unique_id is already in use for a different request.
- ``ScaleTooManyRequests``: 429 - Too Many Requests -- Too many requests hit the API too quickly.
- ``ScaleInternalError``: 500 - Internal Server Error -- We had a problem with our server. Try again later
- ``ScaleTimeoutError``: 504 - Server Timeout Error -- Try again later.

Check out `Scale's API documentation <https://docs.scale.com/reference#errors>`_ for more details.

For example:

.. code-block:: python

try
client.create_categorization_task('Some parameters are missing.')
except scaleapi.ValidationError as e:
print(e.code) # 400
print(e.message) # missing param X
from scaleapi.exceptions import ScaleException

try:
client.create_task(TaskType.TextCollection, attachment='Some parameters are missing.')
except ScaleException as err:
print(err.code) # 400
print(err.message) # Parameters is invalid, reason: "attachments" is required

Troubleshooting
_______________
Expand Down
6 changes: 6 additions & 0 deletions docs/dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
black>=19.10b0
flake8>=3.8.4
pre-commit==2.11.1
isort>=5.7.0
pytest>=6.2.2
pylint>=2.7.2
Loading