From fd6990b23abc374e10cba6cdade749447efd32dd Mon Sep 17 00:00:00 2001 From: ryanjball Date: Tue, 16 Jul 2024 20:37:38 +0000 Subject: [PATCH 1/2] api key precedence --- roboflow/__init__.py | 20 +++++++++++------- roboflow/core/project.py | 14 +++++++++---- roboflow/core/version.py | 28 +++++++++++++------------ roboflow/core/workspace.py | 18 +++++++++------- roboflow/roboflowpy.py | 43 +++++++++++++++++++++++++++++++++++++- 5 files changed, 90 insertions(+), 33 deletions(-) diff --git a/roboflow/__init__.py b/roboflow/__init__.py index f31b4347..d608ea03 100644 --- a/roboflow/__init__.py +++ b/roboflow/__init__.py @@ -15,7 +15,7 @@ from roboflow.models import CLIPModel, GazeModel # noqa: F401 from roboflow.util.general import write_line -__version__ = "1.1.34" +__version__ = "1.1.35" def check_key(api_key, model, notebook, num_retries=0): @@ -237,14 +237,18 @@ def workspace(self, the_workspace=None): if the_workspace is None: the_workspace = self.current_workspace - - if self.api_key in DEMO_KEYS: + + if self.api_key: # Check if api_key was passed during __init__ + workspace_api_key = load_roboflow_api_key(the_workspace) + api_key = workspace_api_key or self.api_key + list_projects = rfapi.get_workspace(api_key, the_workspace) + return Workspace(list_projects, api_key, the_workspace, self.model_format) + + elif self.api_key in DEMO_KEYS: return Workspace({}, self.api_key, the_workspace, self.model_format) - workspace_api_key = load_roboflow_api_key(the_workspace) - api_key = workspace_api_key or self.api_key - - list_projects = rfapi.get_workspace(api_key, the_workspace) - return Workspace(list_projects, api_key, the_workspace, self.model_format) + + else: + raise ValueError("A valid API key must be provided.") def project(self, project_name, the_workspace=None): """Function that takes in the name of the project and returns the project object diff --git a/roboflow/core/project.py b/roboflow/core/project.py index 1de9924c..28fdd5b7 100644 --- a/roboflow/core/project.py +++ b/roboflow/core/project.py @@ -53,10 +53,8 @@ def __init__(self, api_key: str, a_project: dict, model_format: Optional[str] = >>> project = rf.workspace().project("PROJECT_ID") """ - if api_key in DEMO_KEYS: - self.__api_key = api_key - self.model_format = model_format - else: + + if api_key: self.__api_key = api_key self.annotation = a_project["annotation"] self.classes = a_project["classes"] @@ -75,6 +73,14 @@ def __init__(self, api_key: str, a_project: dict, model_format: Optional[str] = temp = self.id.rsplit("/") self.__workspace = temp[0] self.__project_name = temp[1] + + elif DEMO_KEYS: + self.__api_key = DEMO_KEYS[0] + self.model_format = model_format + + else: + raise ValueError("A valid API key must be provided.") + def get_version_information(self): """ diff --git a/roboflow/core/version.py b/roboflow/core/version.py index 00359cf6..6288c966 100644 --- a/roboflow/core/version.py +++ b/roboflow/core/version.py @@ -64,19 +64,7 @@ def __init__( """ Initialize a Version object. """ - if api_key in DEMO_KEYS: - if api_key == "coco-128-sample": - self.__api_key = api_key - self.model_format = model_format - self.name = "coco-128" - self.version = "1" - else: - self.__api_key = api_key - self.model_format = model_format - self.name = "chess-pieces-new" - self.version = "23" - self.id = "joseph-nelson/chess-pieces-new" - else: + if api_key: self.__api_key = api_key self.name = name self.version = unwrap_version_id(version_id=version) @@ -144,6 +132,20 @@ def __init__( self.model = KeypointDetectionModel(self.__api_key, self.id, version=version_without_workspace) else: self.model = None + + elif DEMO_KEYS: + api_key = DEMO_KEYS[0] + if api_key == "coco-128-sample": + self.__api_key = api_key + self.model_format = model_format + self.name = "coco-128" + self.version = "1" + else: + self.__api_key = api_key + self.model_format = model_format + self.name = "chess-pieces-new" + self.version = "23" + self.id = "joseph-nelson/chess-pieces-new" def __check_if_generating(self): # check Roboflow API to see if this version is still generating diff --git a/roboflow/core/workspace.py b/roboflow/core/workspace.py index b04f92aa..00a90716 100644 --- a/roboflow/core/workspace.py +++ b/roboflow/core/workspace.py @@ -23,13 +23,11 @@ class Workspace: """ Manage a Roboflow workspace. """ - def __init__(self, info, api_key, default_workspace, model_format): - if api_key in DEMO_KEYS: - self.__api_key = api_key - self.model_format = model_format - self.project_list = [] - else: + + if api_key: + self.__api_key = api_key + workspace_info = info["workspace"] self.name = workspace_info["name"] self.project_list = workspace_info["projects"] @@ -38,8 +36,14 @@ def __init__(self, info, api_key, default_workspace, model_format): self.url = workspace_info["url"] self.model_format = model_format - self.__api_key = api_key + elif DEMO_KEYS: + self.__api_key = DEMO_KEYS[0] + self.model_format = model_format + self.project_list = [] + else: + raise ValueError("A valid API key must be provided.") + def list_projects(self): """ Print all projects in the workspace to the console. diff --git a/roboflow/roboflowpy.py b/roboflow/roboflowpy.py index c0faaa92..c26b8672 100755 --- a/roboflow/roboflowpy.py +++ b/roboflow/roboflowpy.py @@ -113,6 +113,17 @@ def get_workspace(args): print(json.dumps(workspace_json, indent=2)) + +def get_workspace_project_version(args): + #api_key = load_roboflow_api_key(args.workspaceId) + rf = roboflow.Roboflow(args.api_key) + workspace = rf.workspace() + print('workspace',workspace) + project = workspace.project(args.project) + print('project',project) + version = project.version(args.version_number) + print('version',version) + def get_project(args): workspace_url = args.workspace or get_conditional_configuration_variable("RF_WORKSPACE", default=None) api_key = load_roboflow_api_key(workspace_url) @@ -161,6 +172,9 @@ def _argparser(): _add_projects_parser(subparsers) _add_workspaces_parser(subparsers) _add_upload_model_parser(subparsers) + _add_get_workspace_project_version_parser(subparsers) + + return parser @@ -410,7 +424,34 @@ def _add_upload_model_parser(subparsers): ) upload_model_parser.set_defaults(func=upload_model) - +def _add_get_workspace_project_version_parser(subparsers): + workspace_project_version_parser = subparsers.add_parser( + "get_workspace_info", + help="get workspace project version info", + ) + workspace_project_version_parser.add_argument( + "-a", + dest="api_key", + help="api_key", + ) + workspace_project_version_parser.add_argument( + "-w", + dest="workspace", + help="specify a workspace url or id (will use default workspace if not specified)", + ) + workspace_project_version_parser.add_argument( + "-p", + dest="project", + help="project_id to upload the model into", + ) + workspace_project_version_parser.add_argument( + "-v", + dest="version_number", + type=int, + help="version number to upload the model to", + ) + workspace_project_version_parser.set_defaults(func=get_workspace_project_version) + def _add_login_parser(subparsers): login_parser = subparsers.add_parser("login", help="Log in to Roboflow") login_parser.set_defaults(func=login) From 098d96fa04dd16c54870194a2cffb51a02fc9528 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 16 Jul 2024 20:41:18 +0000 Subject: [PATCH 2/2] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- roboflow/__init__.py | 6 +++--- roboflow/core/project.py | 7 +++---- roboflow/core/version.py | 2 +- roboflow/core/workspace.py | 8 ++++---- roboflow/roboflowpy.py | 17 +++++++++-------- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/roboflow/__init__.py b/roboflow/__init__.py index d608ea03..bfb0d133 100644 --- a/roboflow/__init__.py +++ b/roboflow/__init__.py @@ -237,16 +237,16 @@ def workspace(self, the_workspace=None): if the_workspace is None: the_workspace = self.current_workspace - + if self.api_key: # Check if api_key was passed during __init__ workspace_api_key = load_roboflow_api_key(the_workspace) api_key = workspace_api_key or self.api_key list_projects = rfapi.get_workspace(api_key, the_workspace) return Workspace(list_projects, api_key, the_workspace, self.model_format) - + elif self.api_key in DEMO_KEYS: return Workspace({}, self.api_key, the_workspace, self.model_format) - + else: raise ValueError("A valid API key must be provided.") diff --git a/roboflow/core/project.py b/roboflow/core/project.py index 28fdd5b7..198afb33 100644 --- a/roboflow/core/project.py +++ b/roboflow/core/project.py @@ -53,7 +53,7 @@ def __init__(self, api_key: str, a_project: dict, model_format: Optional[str] = >>> project = rf.workspace().project("PROJECT_ID") """ - + if api_key: self.__api_key = api_key self.annotation = a_project["annotation"] @@ -73,14 +73,13 @@ def __init__(self, api_key: str, a_project: dict, model_format: Optional[str] = temp = self.id.rsplit("/") self.__workspace = temp[0] self.__project_name = temp[1] - + elif DEMO_KEYS: self.__api_key = DEMO_KEYS[0] self.model_format = model_format - + else: raise ValueError("A valid API key must be provided.") - def get_version_information(self): """ diff --git a/roboflow/core/version.py b/roboflow/core/version.py index 6288c966..3e0b3218 100644 --- a/roboflow/core/version.py +++ b/roboflow/core/version.py @@ -132,7 +132,7 @@ def __init__( self.model = KeypointDetectionModel(self.__api_key, self.id, version=version_without_workspace) else: self.model = None - + elif DEMO_KEYS: api_key = DEMO_KEYS[0] if api_key == "coco-128-sample": diff --git a/roboflow/core/workspace.py b/roboflow/core/workspace.py index 00a90716..c5f67a3d 100644 --- a/roboflow/core/workspace.py +++ b/roboflow/core/workspace.py @@ -23,11 +23,11 @@ class Workspace: """ Manage a Roboflow workspace. """ + def __init__(self, info, api_key, default_workspace, model_format): - if api_key: - self.__api_key = api_key - + self.__api_key = api_key + workspace_info = info["workspace"] self.name = workspace_info["name"] self.project_list = workspace_info["projects"] @@ -43,7 +43,7 @@ def __init__(self, info, api_key, default_workspace, model_format): else: raise ValueError("A valid API key must be provided.") - + def list_projects(self): """ Print all projects in the workspace to the console. diff --git a/roboflow/roboflowpy.py b/roboflow/roboflowpy.py index c26b8672..cf3280b9 100755 --- a/roboflow/roboflowpy.py +++ b/roboflow/roboflowpy.py @@ -113,17 +113,17 @@ def get_workspace(args): print(json.dumps(workspace_json, indent=2)) - def get_workspace_project_version(args): - #api_key = load_roboflow_api_key(args.workspaceId) + # api_key = load_roboflow_api_key(args.workspaceId) rf = roboflow.Roboflow(args.api_key) workspace = rf.workspace() - print('workspace',workspace) + print("workspace", workspace) project = workspace.project(args.project) - print('project',project) + print("project", project) version = project.version(args.version_number) - print('version',version) - + print("version", version) + + def get_project(args): workspace_url = args.workspace or get_conditional_configuration_variable("RF_WORKSPACE", default=None) api_key = load_roboflow_api_key(workspace_url) @@ -174,7 +174,6 @@ def _argparser(): _add_upload_model_parser(subparsers) _add_get_workspace_project_version_parser(subparsers) - return parser @@ -424,6 +423,7 @@ def _add_upload_model_parser(subparsers): ) upload_model_parser.set_defaults(func=upload_model) + def _add_get_workspace_project_version_parser(subparsers): workspace_project_version_parser = subparsers.add_parser( "get_workspace_info", @@ -451,7 +451,8 @@ def _add_get_workspace_project_version_parser(subparsers): help="version number to upload the model to", ) workspace_project_version_parser.set_defaults(func=get_workspace_project_version) - + + def _add_login_parser(subparsers): login_parser = subparsers.add_parser("login", help="Log in to Roboflow") login_parser.set_defaults(func=login)