|
|
|
@@ -1,5 +1,5 @@ |
|
|
|
import os |
|
|
|
import numpy as np |
|
|
|
import uuid |
|
|
|
import yaml |
|
|
|
import json |
|
|
|
import atexit |
|
|
|
@@ -7,6 +7,7 @@ import zipfile |
|
|
|
import hashlib |
|
|
|
import requests |
|
|
|
import tempfile |
|
|
|
import numpy as np |
|
|
|
from enum import Enum |
|
|
|
from tqdm import tqdm |
|
|
|
from typing import Union, List |
|
|
|
@@ -309,31 +310,44 @@ class LearnwareClient: |
|
|
|
|
|
|
|
return semantic_conf[key.value]["Values"] |
|
|
|
|
|
|
|
def load_learnware(self, learnware_file: Union[str, List[str]], load_option: str = "conda_env"): |
|
|
|
"""Load learnware |
|
|
|
def load_learnware(self, learnware_path: Union[str, List[str]] = None, learnware_id: Union[str, List[str]] = None, runnable_option: str = None): |
|
|
|
"""Load learnware by learnware zip file or learnware id (zip file has higher priority) |
|
|
|
|
|
|
|
Parameters |
|
|
|
---------- |
|
|
|
learnware_file : Union[str, List[str]] |
|
|
|
learnware_path : Union[str, List[str]] |
|
|
|
learnware zip path or learnware zip path list |
|
|
|
load_option : str |
|
|
|
the option for loading learnwares |
|
|
|
- "normal": load learnware without installing environment |
|
|
|
- "conda_env": load learnware with installing conda virtual environment |
|
|
|
learnware_id : Union[str, List[str]] |
|
|
|
learnware id or learnware id list |
|
|
|
runnable_option : str |
|
|
|
the option for instantiating learnwares |
|
|
|
- "normal": instantiate learnware without installing environment |
|
|
|
- "conda_env": instantiate learnware with installing conda virtual environment |
|
|
|
|
|
|
|
Returns |
|
|
|
------- |
|
|
|
Learnware |
|
|
|
The contructed learnware object or object list |
|
|
|
""" |
|
|
|
if load_option not in ["normal", "conda_env"]: |
|
|
|
raise ValueError(f"load_option must be one of ['normal', 'conda_env'], but got {load_option}") |
|
|
|
if runnable_option is not None and runnable_option not in ["normal", "conda_env"]: |
|
|
|
raise logger.warning(f"runnable_option must be one of ['normal', 'conda_env'], but got {runnable_option}") |
|
|
|
|
|
|
|
if learnware_path is None and learnware_id is None: |
|
|
|
raise ValueError("Requires one of learnware_path or learnware_id") |
|
|
|
|
|
|
|
def _get_learnware_obj(learnware_zippath): |
|
|
|
def _get_learnware_by_id(_learnware_id): |
|
|
|
self.tempdir_list.append(tempfile.TemporaryDirectory(prefix="learnware_")) |
|
|
|
tempdir = self.tempdir_list[-1].name |
|
|
|
zip_path = os.path.join(tempdir, f"{str(uuid.uuid4())}.zip") |
|
|
|
self.download_learnware(_learnware_id, zip_path) |
|
|
|
return zip_path, _get_learnware_by_path(zip_path, tempdir=tempdir) |
|
|
|
|
|
|
|
def _get_learnware_by_path(_learnware_zippath, tempdir=None): |
|
|
|
if tempdir is None: |
|
|
|
self.tempdir_list.append(tempfile.TemporaryDirectory(prefix="learnware_")) |
|
|
|
tempdir = self.tempdir_list[-1].name |
|
|
|
|
|
|
|
with zipfile.ZipFile(learnware_zippath, "r") as z_file: |
|
|
|
with zipfile.ZipFile(_learnware_zippath, "r") as z_file: |
|
|
|
z_file.extractall(tempdir) |
|
|
|
|
|
|
|
yaml_file = C.learnware_folder_config["yaml_file"] |
|
|
|
@@ -354,22 +368,36 @@ class LearnwareClient: |
|
|
|
semantic_specification = json.load(fin) |
|
|
|
|
|
|
|
return learnware.get_learnware_from_dirpath(learnware_id, semantic_specification, tempdir) |
|
|
|
|
|
|
|
if isinstance(learnware_file, str): |
|
|
|
zip_paths = [learnware_file] |
|
|
|
elif isinstance(learnware_file, list): |
|
|
|
zip_paths = learnware_file |
|
|
|
|
|
|
|
learnware_list = [] |
|
|
|
for zip_path in zip_paths: |
|
|
|
learnware_obj = _get_learnware_obj(zip_path) |
|
|
|
if load_option == "normal": |
|
|
|
learnware_obj.instantiate_model() |
|
|
|
learnware_list.append(learnware_obj) |
|
|
|
zip_paths = [] |
|
|
|
if learnware_path is not None: |
|
|
|
if isinstance(learnware_path, str): |
|
|
|
zip_paths = [learnware_path] |
|
|
|
elif isinstance(learnware_path, list): |
|
|
|
zip_paths = learnware_path |
|
|
|
|
|
|
|
for zip_path in zip_paths: |
|
|
|
learnware_obj = _get_learnware_by_path(zip_path) |
|
|
|
learnware_list.append(learnware_obj) |
|
|
|
elif learnware_id is not None: |
|
|
|
if isinstance(learnware_id, str): |
|
|
|
id_list = [learnware_id] |
|
|
|
elif isinstance(learnware_id, list): |
|
|
|
id_list = learnware_id |
|
|
|
|
|
|
|
for idx in id_list: |
|
|
|
zip_path, learnware_obj = _get_learnware_by_id(idx) |
|
|
|
zip_paths.append(zip_path) |
|
|
|
learnware_list.append(learnware_obj) |
|
|
|
|
|
|
|
if load_option == "conda_env": |
|
|
|
env_container = LearnwaresContainer(learnware_list, zip_paths) |
|
|
|
learnware_list = env_container.get_learnware_list_with_container() |
|
|
|
if runnable_option is not None: |
|
|
|
if runnable_option == "normal": |
|
|
|
for i in range(len(learnware_list)): |
|
|
|
learnware_list[i].instantiate_model() |
|
|
|
elif runnable_option == "conda_env": |
|
|
|
env_container = LearnwaresContainer(learnware_list, zip_paths) |
|
|
|
learnware_list = env_container.get_learnware_list_with_container() |
|
|
|
|
|
|
|
if len(learnware_list) == 1: |
|
|
|
return learnware_list[0] |
|
|
|
|