From 28e436777d498b214cbfc52f41c40a3791df0985 Mon Sep 17 00:00:00 2001 From: GoldenAnpu Date: Thu, 15 Aug 2024 00:09:52 +0200 Subject: [PATCH] Add Workflow --- config.json | 4 ++-- dev_requirements.txt | 2 +- src/functions.py | 3 ++- src/main.py | 7 ++++++- src/workflow.py | 30 ++++++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/workflow.py diff --git a/config.json b/config.json index 10ee7d7..9f1e379 100644 --- a/config.json +++ b/config.json @@ -7,7 +7,7 @@ "images", "export" ], - "docker_image": "supervisely/import-export:0.0.5", + "docker_image": "supervisely/import-export:6.73.157", "icon": "https://user-images.githubusercontent.com/115161827/203991592-de444a0d-3d7c-4c26-b575-6fa4ac020635.jpg", "icon_cover": true, "poster": "https://user-images.githubusercontent.com/115161827/203993840-5a170216-d7a2-4e45-a74a-4b4c856e5b2f.jpg", @@ -19,7 +19,7 @@ ] }, "min_agent_version": "6.7.4", - "min_instance_version": "6.5.51", + "min_instance_version": "6.10.0", "entrypoint": "python -m uvicorn src.main:app --host 0.0.0.0 --port 8000", "port": 8000 } \ No newline at end of file diff --git a/dev_requirements.txt b/dev_requirements.txt index 41ad398..e1cfa9b 100644 --- a/dev_requirements.txt +++ b/dev_requirements.txt @@ -1,2 +1,2 @@ black==22.6.0 -supervisely==6.72.55 +supervisely==6.73.157 diff --git a/src/functions.py b/src/functions.py index 0c9c5cc..c7d0144 100644 --- a/src/functions.py +++ b/src/functions.py @@ -87,7 +87,7 @@ def label_to_ro_bbox(label: sly.Label, project_meta: sly.ProjectMeta): return ann_line -def upload_project_to_tf(api, project): +def upload_project_to_tf(api: sly.Api, project: sly.ProjectInfo) -> sly.api.file_api.FileInfo: full_archive_name = f"{str(project.id)}_{project.name}.tar" result_archive = os.path.join(STORAGE_DIR, full_archive_name) sly.fs.archive_directory(PROJECT_DIR, result_archive) @@ -118,3 +118,4 @@ def _print_progress(monitor, upload_progress): api.task.set_output_archive( TASK_ID, file_info.id, full_archive_name, file_url=file_info.storage_path ) + return file_info diff --git a/src/main.py b/src/main.py index 29a65d2..873672d 100644 --- a/src/main.py +++ b/src/main.py @@ -10,6 +10,8 @@ upload_project_to_tf, ) from src.globals import DATASET_ID, PROJECT_DIR, PROJECT_ID, api, app +import src.workflow as w + project = api.project.get_info_by_id(id=PROJECT_ID) project_meta = sly.ProjectMeta.from_json(data=api.project.get_meta(id=PROJECT_ID)) @@ -17,8 +19,10 @@ if DATASET_ID is not None: datasets = [api.dataset.get_info_by_id(id=DATASET_ID)] + w.workflow_input(api, DATASET_ID, type="dataset") else: datasets = api.dataset.get_list(project_id=PROJECT_ID) + w.workflow_input(api, PROJECT_ID, type="project") progress_ds = sly.Progress(message="Exporting datasets", total_cnt=len(datasets)) for dataset in datasets: dataset_dir = os.path.join(PROJECT_DIR, dataset.name) @@ -56,5 +60,6 @@ progress_img.iters_done_report(len(batch_img_ids)) progress_ds.iter_done_report() -upload_project_to_tf(api, project) +file_info = upload_project_to_tf(api, project) +w.workflow_output(api, file_info) app.shutdown() diff --git a/src/workflow.py b/src/workflow.py new file mode 100644 index 0000000..b0bdd42 --- /dev/null +++ b/src/workflow.py @@ -0,0 +1,30 @@ +# This module contains the functions that are used to configure the input and output of the workflow for the current app. + +import supervisely as sly +from typing import Union, Literal + +def workflow_input(api: sly.Api, id: Union[int, str], type: Literal["project", "dataset"]): + if type == "project": + api.app.workflow.add_input_project(int(id)) + sly.logger.debug(f"Workflow: Input project - {id}") + elif type == "dataset": + api.app.workflow.add_input_dataset(int(id)) + sly.logger.debug(f"Workflow: Input dataset - {id}") + +def workflow_output(api: sly.Api, file: Union[int, sly.api.file_api.FileInfo]): + try: + if isinstance(file, int): + file = api.file.get_info_by_id(file) + relation_settings = sly.WorkflowSettings( + title=file.name, + icon="archive", + icon_color="#33c94c", + icon_bg_color="#d9f7e4", + url=f"/files/{file.id}/true/?teamId={file.team_id}", + url_title="Download", + ) + meta = sly.WorkflowMeta(relation_settings=relation_settings) + api.app.workflow.add_output_file(file, meta=meta) + sly.logger.debug(f"Workflow: Output file - {file}") + except Exception as e: + sly.logger.debug(f"Failed to add output to the workflow: {repr(e)}") \ No newline at end of file