-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #573 from DagsHub/ls-api-integrations
LS API Integrations
- Loading branch information
Showing
8 changed files
with
171 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from tenacity import retry, wait_fixed, stop_after_attempt, retry_if_exception_type | ||
from json import JSONDecodeError | ||
from typing import Optional | ||
import importlib.util | ||
import semver | ||
|
||
from dagshub.common.api.repo import RepoAPI | ||
from dagshub.auth import get_token | ||
from dagshub.common import config | ||
|
||
|
||
def _use_legacy_client(): | ||
""" | ||
https://github.com/HumanSignal/label-studio/releases/tag/1.13.0, \ | ||
https://github.com/HumanSignal/label-studio/pull/5961; \ | ||
introduces breaking changes; anyone using SDK < 1.0 should use the legacy client. | ||
:meta experimental: | ||
""" | ||
import label_studio_sdk | ||
|
||
return semver.compare("1.0.0", label_studio_sdk.__version__) == 1 | ||
|
||
|
||
@retry(retry=retry_if_exception_type(JSONDecodeError), wait=wait_fixed(3), stop=stop_after_attempt(5)) | ||
def get_label_studio_client( | ||
repo: str, legacy_client: Optional[bool] = None, host: Optional[str] = None, token: Optional[str] = None | ||
): | ||
""" | ||
Creates a `label_studio_sdk.Client / label_studio_sdk.client.LabelStudio \ | ||
<https://labelstud.io/guide/sdk> / \ | ||
https://api.labelstud.io/api-reference/introduction/getting-started`.\ | ||
object to interact with the label studio instance associated with the repository. | ||
Args: | ||
repo: Name of the repo in the format of ``username/reponame`` | ||
legacy_client: if True, returns the older legacy LabelStudio Client. | ||
host: URL of the hosted DagsHub instance. default is ``https://dagshub.com``. | ||
token: (optional, default: None) uses programmatically specified token, \ | ||
if not provided either uses cached token or requests oauth interactively. | ||
Returns: | ||
`label_studio_sdk.Client` / `label_studio_sdk.client.LabelStudio` object | ||
""" | ||
|
||
if importlib.util.find_spec("label_studio_sdk") is None: | ||
raise ModuleNotFoundError("Could not import module label_studio_sdk. Make sure to pip install label_studio_sdk") | ||
|
||
if legacy_client is None: | ||
legacy_client = _use_legacy_client() | ||
|
||
if not host: | ||
host = config.host | ||
|
||
if legacy_client: | ||
from label_studio_sdk import Client as LabelStudio | ||
else: | ||
from label_studio_sdk.client import LabelStudio | ||
|
||
repo_api = RepoAPI(repo, host=host) | ||
kwargs = { | ||
"url" if legacy_client else "base_url": repo_api.label_studio_api_url()[:-4], | ||
"api_key": token if token is not None else get_token(host=host), | ||
} | ||
|
||
return LabelStudio(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
LS Client Documentation | ||
======================= | ||
|
||
.. automodule:: dagshub.ls_client | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters