code-loader 1.0.43__tar.gz → 1.0.43a0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {code_loader-1.0.43 → code_loader-1.0.43a0}/PKG-INFO +3 -1
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/__init__.py +1 -0
- code_loader-1.0.43a0/code_loader/rt_api/__init__.py +12 -0
- code_loader-1.0.43a0/code_loader/rt_api/api_client.py +107 -0
- code_loader-1.0.43a0/code_loader/rt_api/cli_config_utils.py +41 -0
- code_loader-1.0.43a0/code_loader/rt_api/epoch.py +63 -0
- code_loader-1.0.43a0/code_loader/rt_api/experiment.py +47 -0
- code_loader-1.0.43a0/code_loader/rt_api/experiment_context.py +10 -0
- code_loader-1.0.43a0/code_loader/rt_api/types.py +25 -0
- code_loader-1.0.43a0/code_loader/rt_api/utils.py +34 -0
- code_loader-1.0.43a0/code_loader/rt_api/workingspace_config_utils.py +27 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/pyproject.toml +3 -1
- {code_loader-1.0.43 → code_loader-1.0.43a0}/LICENSE +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/README.md +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/contract/__init__.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/contract/datasetclasses.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/contract/enums.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/contract/exceptions.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/contract/responsedataclasses.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/contract/visualizer_classes.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/inner_leap_binder/__init__.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/inner_leap_binder/leapbinder.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/leaploader.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/utils.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/visualizers/__init__.py +0 -0
- {code_loader-1.0.43 → code_loader-1.0.43a0}/code_loader/visualizers/default_visualizers.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: code-loader
|
3
|
-
Version: 1.0.
|
3
|
+
Version: 1.0.43a0
|
4
4
|
Summary:
|
5
5
|
Home-page: https://github.com/tensorleap/code-loader
|
6
6
|
License: MIT
|
@@ -16,6 +16,8 @@ Classifier: Programming Language :: Python :: 3.11
|
|
16
16
|
Requires-Dist: matplotlib (>=3.3,<3.4)
|
17
17
|
Requires-Dist: numpy (>=1.22.3,<2.0.0)
|
18
18
|
Requires-Dist: psutil (>=5.9.5,<6.0.0)
|
19
|
+
Requires-Dist: pyyaml (>=6.0.2,<7.0.0)
|
20
|
+
Requires-Dist: requests (>=2.32.3,<3.0.0)
|
19
21
|
Project-URL: Repository, https://github.com/tensorleap/code-loader
|
20
22
|
Description-Content-Type: text/markdown
|
21
23
|
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
from code_loader.rt_api.experiment_context import ExperimentContext
|
3
|
+
from code_loader.rt_api.epoch import Epoch
|
4
|
+
from code_loader.rt_api.experiment import Experiment
|
5
|
+
from code_loader.rt_api.api_client import ApiClient
|
6
|
+
|
7
|
+
__all__ = [
|
8
|
+
'ExperimentContext',
|
9
|
+
'Epoch',
|
10
|
+
'Experiment',
|
11
|
+
'ApiClient'
|
12
|
+
]
|
@@ -0,0 +1,107 @@
|
|
1
|
+
|
2
|
+
from dataclasses import dataclass
|
3
|
+
from typing import Dict, List, Optional
|
4
|
+
from code_loader.rt_api.types import ApiMetrics
|
5
|
+
from code_loader.rt_api.utils import join_url, to_dict_no_none
|
6
|
+
from .cli_config_utils import get_auth_config
|
7
|
+
import requests
|
8
|
+
|
9
|
+
|
10
|
+
@dataclass
|
11
|
+
class StartExperimentRequest:
|
12
|
+
projectId: str
|
13
|
+
experimentName: str
|
14
|
+
description: str
|
15
|
+
removeUntaggedUploadedModels: bool = True
|
16
|
+
codeIntegrationVersionId: Optional[str] = None
|
17
|
+
|
18
|
+
@dataclass
|
19
|
+
class StartExperimentResponse:
|
20
|
+
projectId: str
|
21
|
+
versionId: str
|
22
|
+
experimentId: str
|
23
|
+
|
24
|
+
@dataclass
|
25
|
+
class GetUploadModelSignedUrlRequest:
|
26
|
+
epoch: int
|
27
|
+
experimentId: str
|
28
|
+
versionId: str
|
29
|
+
projectId: str
|
30
|
+
fileType: str
|
31
|
+
origin: Optional[str] = None
|
32
|
+
|
33
|
+
@dataclass
|
34
|
+
class GetUploadModelSignedUrlResponse:
|
35
|
+
url: str
|
36
|
+
fileName: str
|
37
|
+
|
38
|
+
@dataclass
|
39
|
+
class AddExternalEpochDataRequest:
|
40
|
+
projectId: str
|
41
|
+
experimentId: str
|
42
|
+
epoch: int
|
43
|
+
metrics: ApiMetrics
|
44
|
+
force: bool = False
|
45
|
+
|
46
|
+
@dataclass
|
47
|
+
class TagModelRequest:
|
48
|
+
projectId: str
|
49
|
+
experimentId: str
|
50
|
+
epoch: int
|
51
|
+
tags: List[str]
|
52
|
+
|
53
|
+
@dataclass
|
54
|
+
class SetExperimentNotesRequest:
|
55
|
+
projectId: str
|
56
|
+
experimentId: str
|
57
|
+
notes: Dict[str, any] # type: ignore[valid-type]
|
58
|
+
|
59
|
+
class ApiClient:
|
60
|
+
def __init__(self, url: Optional[str] = None, token: Optional[str] = None):
|
61
|
+
if url is None or token is None:
|
62
|
+
configAuth = get_auth_config()
|
63
|
+
if configAuth is None:
|
64
|
+
raise Exception("No auth config found, either provide url and token or use `leap auth [url] [token]` to setup a config file")
|
65
|
+
url = configAuth.api_url
|
66
|
+
token = configAuth.api_key
|
67
|
+
|
68
|
+
self.url = url
|
69
|
+
self.token = token
|
70
|
+
|
71
|
+
def __add_auth(self, headers: Dict[str, str]) -> Dict[str, str]:
|
72
|
+
headers['Authorization'] = f'Bearer {self.token}'
|
73
|
+
return headers
|
74
|
+
|
75
|
+
def __post(self, post_path: str, data: any, headers: Dict[str, str] = {})-> requests.Response: # type: ignore[valid-type]
|
76
|
+
headers = self.__add_auth(headers)
|
77
|
+
if 'Content-Type' not in headers:
|
78
|
+
headers['Content-Type'] = 'application/json'
|
79
|
+
url = join_url(self.url, post_path)
|
80
|
+
json_data = to_dict_no_none(data)
|
81
|
+
return requests.post(url, json=json_data, headers=headers)
|
82
|
+
|
83
|
+
def __check_response(self, response: requests.Response)-> None:
|
84
|
+
if response.status_code >= 400:
|
85
|
+
raise Exception(f"Error: {response.status_code} {response.text}")
|
86
|
+
|
87
|
+
def start_experiment(self, data: StartExperimentRequest) -> StartExperimentResponse:
|
88
|
+
response = self.__post('/versions/startExperiment', data)
|
89
|
+
self.__check_response(response)
|
90
|
+
return StartExperimentResponse(**response.json())
|
91
|
+
|
92
|
+
def get_attach_model_upload_signed_url(self, data: GetUploadModelSignedUrlRequest)-> GetUploadModelSignedUrlResponse:
|
93
|
+
response = self.__post('/versions/getUploadModelSignedUrl', data)
|
94
|
+
self.__check_response(response)
|
95
|
+
return GetUploadModelSignedUrlResponse(**response.json())
|
96
|
+
|
97
|
+
def add_external_epoch_data(self, data: AddExternalEpochDataRequest)-> None:
|
98
|
+
response = self.__post('/externalepochdata/addExternalEpochData', data)
|
99
|
+
self.__check_response(response)
|
100
|
+
|
101
|
+
def tag_model(self, data: TagModelRequest)-> None:
|
102
|
+
response = self.__post('/versions/tagModel', data)
|
103
|
+
self.__check_response(response)
|
104
|
+
|
105
|
+
def set_experiment_notes(self, data: SetExperimentNotesRequest)-> None:
|
106
|
+
response = self.__post('/versions/setExperimentNotes', data)
|
107
|
+
self.__check_response(response)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
from dataclasses import dataclass
|
2
|
+
import os
|
3
|
+
from typing import Dict, Optional
|
4
|
+
import yaml
|
5
|
+
|
6
|
+
@dataclass
|
7
|
+
class AuthConfig:
|
8
|
+
api_url: str
|
9
|
+
api_key: str
|
10
|
+
|
11
|
+
@dataclass
|
12
|
+
class Config:
|
13
|
+
current_env: str
|
14
|
+
envs: Dict[str, AuthConfig]
|
15
|
+
|
16
|
+
def get_cli_conf_path()-> str:
|
17
|
+
cli_conf_path = os.getenv("TL_CLI_CONFIG_FILE") or os.path.join(os.path.expanduser("~"), ".config/tensorleap/config.yaml")
|
18
|
+
return cli_conf_path
|
19
|
+
|
20
|
+
def get_cli_conf_file() -> Optional[Config]:
|
21
|
+
cli_conf_path = get_cli_conf_path()
|
22
|
+
if not os.path.exists(cli_conf_path):
|
23
|
+
return None
|
24
|
+
with open(cli_conf_path) as f:
|
25
|
+
y = yaml.safe_load(f)
|
26
|
+
envs_dict = y.get("envs")
|
27
|
+
if envs_dict is None:
|
28
|
+
return None
|
29
|
+
envs = dict()
|
30
|
+
for k, v in envs_dict.items():
|
31
|
+
envs[k] = AuthConfig(**v)
|
32
|
+
return Config(envs=envs, current_env=y["current_env"])
|
33
|
+
|
34
|
+
def get_auth_config() -> Optional[AuthConfig]:
|
35
|
+
cli_conf = get_cli_conf_file()
|
36
|
+
if cli_conf is None or cli_conf.current_env not in cli_conf.envs:
|
37
|
+
return None
|
38
|
+
return cli_conf.envs[cli_conf.current_env]
|
39
|
+
|
40
|
+
|
41
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
|
2
|
+
from typing import List, Optional
|
3
|
+
from code_loader.rt_api import ExperimentContext
|
4
|
+
from code_loader.rt_api.types import Metrics
|
5
|
+
from code_loader.rt_api.utils import to_api_metric_value, upload_file
|
6
|
+
from .api_client import AddExternalEpochDataRequest, GetUploadModelSignedUrlRequest, TagModelRequest
|
7
|
+
|
8
|
+
|
9
|
+
class Epoch:
|
10
|
+
def __init__(self, ctx: ExperimentContext, epoch: int):
|
11
|
+
self.experiment = ExperimentContext
|
12
|
+
self.epoch = epoch
|
13
|
+
self.metrics: Metrics = {}
|
14
|
+
self.ctx = ctx
|
15
|
+
|
16
|
+
def add_metric(self, name: str, value: float)-> None:
|
17
|
+
self.metrics[name] = value
|
18
|
+
|
19
|
+
def set_metrics(self, metrics: Metrics)-> None:
|
20
|
+
self.metrics = metrics
|
21
|
+
|
22
|
+
def upload_model(self, modelFilePath: str)-> None:
|
23
|
+
allowed_extensions = ["h5", "onnx"]
|
24
|
+
modelExtension = modelFilePath.split(".")[-1]
|
25
|
+
if modelExtension not in allowed_extensions:
|
26
|
+
raise Exception(f"Model file extension not allowed. Allowed extensions are {allowed_extensions}")
|
27
|
+
url = self.ctx.client.get_attach_model_upload_signed_url(GetUploadModelSignedUrlRequest(
|
28
|
+
epoch=self.epoch,
|
29
|
+
experimentId=self.ctx.experiment_id,
|
30
|
+
versionId=self.ctx.version_id,
|
31
|
+
projectId=self.ctx.project_id,
|
32
|
+
fileType=modelExtension
|
33
|
+
))
|
34
|
+
print(f"Uploading epoch({self.epoch}) model file")
|
35
|
+
upload_file(url.url, modelFilePath)
|
36
|
+
print("Model file uploaded")
|
37
|
+
|
38
|
+
def tag_model(self, tags: List[str])-> None:
|
39
|
+
print(f"Tagging epoch({self.epoch}) model")
|
40
|
+
self.ctx.client.tag_model(TagModelRequest(
|
41
|
+
experimentId=self.ctx.experiment_id,
|
42
|
+
projectId=self.ctx.project_id,
|
43
|
+
epoch=self.epoch,
|
44
|
+
tags=tags
|
45
|
+
))
|
46
|
+
|
47
|
+
def save(self, modelFilePath: Optional[str] = None, tags: List[str] = ['latest'])-> None:
|
48
|
+
if modelFilePath is not None:
|
49
|
+
self.upload_model(modelFilePath)
|
50
|
+
|
51
|
+
print(f"Add metrics for epoch({self.epoch}) model")
|
52
|
+
api_metrics ={
|
53
|
+
key: to_api_metric_value(value) for key, value in self.metrics.items()
|
54
|
+
}
|
55
|
+
self.ctx.client.add_external_epoch_data(AddExternalEpochDataRequest(
|
56
|
+
experimentId=self.ctx.experiment_id,
|
57
|
+
projectId=self.ctx.project_id,
|
58
|
+
epoch=self.epoch,
|
59
|
+
metrics=api_metrics
|
60
|
+
))
|
61
|
+
if modelFilePath is not None and len(tags) > 0:
|
62
|
+
self.tag_model(tags)
|
63
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
from typing import Dict, List, Optional
|
3
|
+
from code_loader.rt_api import Epoch, ExperimentContext
|
4
|
+
from code_loader.rt_api.types import Metrics
|
5
|
+
from code_loader.rt_api.workingspace_config_utils import load_workspace_config
|
6
|
+
from .api_client import SetExperimentNotesRequest, StartExperimentRequest, ApiClient
|
7
|
+
|
8
|
+
|
9
|
+
class Experiment:
|
10
|
+
def __init__(self, ctx: ExperimentContext):
|
11
|
+
self.ctx = ctx
|
12
|
+
|
13
|
+
def start_epoch(self, epoch: int) -> Epoch:
|
14
|
+
return Epoch(self.ctx, epoch)
|
15
|
+
|
16
|
+
def add_epoch(self, epoch: int, metrics: Optional[Metrics] = None, model_path: Optional[str] = None, tags: List[str] = ['latest'])-> None:
|
17
|
+
epoch_o = self.start_epoch(epoch)
|
18
|
+
if metrics is not None:
|
19
|
+
epoch_o.set_metrics(metrics)
|
20
|
+
epoch_o.save(model_path, tags)
|
21
|
+
|
22
|
+
def set_notes(self, notes: Dict[str, any])-> None: # type: ignore[valid-type]
|
23
|
+
print(f"Setting experiment({self.ctx.experiment_id}) notes")
|
24
|
+
self.ctx.client.set_experiment_notes(SetExperimentNotesRequest(
|
25
|
+
experimentId=self.ctx.experiment_id,
|
26
|
+
projectId=self.ctx.project_id,
|
27
|
+
notes=notes
|
28
|
+
))
|
29
|
+
|
30
|
+
@staticmethod
|
31
|
+
def Start(experimentName: str, description: str, working_dir: Optional[str] = None, client: Optional[ApiClient] = None) -> 'Experiment':
|
32
|
+
|
33
|
+
if client is None:
|
34
|
+
client = ApiClient()
|
35
|
+
|
36
|
+
workspace_config = load_workspace_config(working_dir)
|
37
|
+
if workspace_config is None or workspace_config.projectId is None:
|
38
|
+
raise Exception("No leap workspace config found or projectId is missing, make sure you are in a leap workspace directory or provide a working_dir")
|
39
|
+
|
40
|
+
result = client.start_experiment(StartExperimentRequest(
|
41
|
+
projectId=workspace_config.projectId,
|
42
|
+
experimentName=experimentName,
|
43
|
+
description=description,
|
44
|
+
codeIntegrationVersionId=workspace_config.codeIntegrationId
|
45
|
+
))
|
46
|
+
ctx = ExperimentContext(client, result.projectId, result.versionId, result.experimentId)
|
47
|
+
return Experiment(ctx)
|
@@ -0,0 +1,25 @@
|
|
1
|
+
|
2
|
+
from dataclasses import dataclass
|
3
|
+
from typing import Dict, Union
|
4
|
+
|
5
|
+
|
6
|
+
MetricValue = Union[str, float]
|
7
|
+
Metrics = Dict[str, MetricValue]
|
8
|
+
|
9
|
+
@dataclass
|
10
|
+
class NumericMetricValue:
|
11
|
+
value: float
|
12
|
+
type: str = "number"
|
13
|
+
|
14
|
+
@dataclass
|
15
|
+
class StringMetricValue:
|
16
|
+
value: str
|
17
|
+
type: str = "string"
|
18
|
+
|
19
|
+
@dataclass
|
20
|
+
class ImageMetricValue:
|
21
|
+
value: str
|
22
|
+
type: str = "image"
|
23
|
+
|
24
|
+
ApiMetricValue = Union[NumericMetricValue, StringMetricValue, ImageMetricValue]
|
25
|
+
ApiMetrics = Dict[str, ApiMetricValue]
|
@@ -0,0 +1,34 @@
|
|
1
|
+
from dataclasses import asdict, is_dataclass
|
2
|
+
from typing import Dict
|
3
|
+
from urllib.parse import urljoin
|
4
|
+
from code_loader.rt_api.types import ApiMetricValue, MetricValue, NumericMetricValue, StringMetricValue
|
5
|
+
import requests
|
6
|
+
|
7
|
+
def upload_file(url: str, file_path: str)-> None:
|
8
|
+
with open(file_path, "rb") as f:
|
9
|
+
requests.put(url, data=f, timeout=12_000)
|
10
|
+
|
11
|
+
def to_dict_no_none(data: any)-> Dict[str, any]: # type: ignore[valid-type]
|
12
|
+
if is_dataclass(data):
|
13
|
+
data = asdict(data)
|
14
|
+
if isinstance(data, dict):
|
15
|
+
return {k: to_dict_no_none(v) for k, v in data.items() if v is not None}
|
16
|
+
elif isinstance(data, list):
|
17
|
+
return [to_dict_no_none(item) for item in data if item is not None]
|
18
|
+
else:
|
19
|
+
return data
|
20
|
+
|
21
|
+
def join_url(base_url: str, post_path: str)-> str:
|
22
|
+
if not base_url.endswith('/'):
|
23
|
+
base_url += '/'
|
24
|
+
if post_path.startswith('/'):
|
25
|
+
post_path = post_path[1:]
|
26
|
+
return urljoin(base_url, post_path)
|
27
|
+
|
28
|
+
def to_api_metric_value(value: MetricValue) -> ApiMetricValue:
|
29
|
+
if isinstance(value, float) or isinstance(value, int):
|
30
|
+
return NumericMetricValue(value=value)
|
31
|
+
elif isinstance(value, str):
|
32
|
+
return StringMetricValue(value=value)
|
33
|
+
else:
|
34
|
+
raise Exception(f"Unsupported metric value type: {type(value)}")
|
@@ -0,0 +1,27 @@
|
|
1
|
+
from dataclasses import dataclass
|
2
|
+
from os import path
|
3
|
+
import os
|
4
|
+
from typing import List, Optional
|
5
|
+
import yaml
|
6
|
+
|
7
|
+
|
8
|
+
@dataclass
|
9
|
+
class LocalProjectConfig:
|
10
|
+
codeIntegrationId: Optional[str] = None
|
11
|
+
projectId: Optional[str] = None
|
12
|
+
secretId: Optional[str] = None
|
13
|
+
secretManagerId: Optional[str] = None
|
14
|
+
entryFile: Optional[str] = None
|
15
|
+
includePatterns: Optional[List[str]] = None
|
16
|
+
|
17
|
+
# Loading workspace configuration from leap.yaml
|
18
|
+
def load_workspace_config(workspace_dir: Optional[str] = None) -> Optional[LocalProjectConfig]:
|
19
|
+
if workspace_dir is None:
|
20
|
+
workspace_dir = os.getcwd()
|
21
|
+
elif not path.isabs(workspace_dir):
|
22
|
+
workspace_dir = path.join(os.getcwd(), workspace_dir)
|
23
|
+
|
24
|
+
file_path = path.join(workspace_dir, "leap.yaml")
|
25
|
+
with open(file_path) as f:
|
26
|
+
y = yaml.safe_load(f)
|
27
|
+
return LocalProjectConfig(**y)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "code-loader"
|
3
|
-
version = "1.0.
|
3
|
+
version = "1.0.43a0"
|
4
4
|
description = ""
|
5
5
|
authors = ["dorhar <doron.harnoy@tensorleap.ai>"]
|
6
6
|
license = "MIT"
|
@@ -16,6 +16,8 @@ python = ">=3.8,<3.12"
|
|
16
16
|
numpy = "^1.22.3"
|
17
17
|
psutil = "^5.9.5"
|
18
18
|
matplotlib = ">=3.3,<3.4"
|
19
|
+
requests = "^2.32.3"
|
20
|
+
pyyaml = "^6.0.2"
|
19
21
|
|
20
22
|
[tool.poetry.dev-dependencies]
|
21
23
|
pytest = "^7.1.1"
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|