trainwave-cli 0.1.0__py3-none-any.whl
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.
- trainwave_cli/__init__.py +0 -0
- trainwave_cli/api.py +174 -0
- trainwave_cli/cli/auth.py +108 -0
- trainwave_cli/cli/jobs.py +190 -0
- trainwave_cli/cli/offers.py +35 -0
- trainwave_cli/config/config.py +53 -0
- trainwave_cli/io.py +56 -0
- trainwave_cli/main.py +20 -0
- trainwave_cli/utils.py +62 -0
- trainwave_cli/xceptions.py +6 -0
- trainwave_cli-0.1.0.dist-info/METADATA +29 -0
- trainwave_cli-0.1.0.dist-info/RECORD +14 -0
- trainwave_cli-0.1.0.dist-info/WHEEL +4 -0
- trainwave_cli-0.1.0.dist-info/entry_points.txt +3 -0
|
File without changes
|
trainwave_cli/api.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import socket
|
|
3
|
+
import tempfile
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from enum import Enum
|
|
6
|
+
from http import HTTPStatus
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import aiofiles
|
|
11
|
+
import aiohttp
|
|
12
|
+
import httpx
|
|
13
|
+
from tqdm import tqdm
|
|
14
|
+
|
|
15
|
+
from trainwave_cli.utils import from_dict
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class CloudOffer:
|
|
20
|
+
cpus: int
|
|
21
|
+
cpu_type: str
|
|
22
|
+
memory_mb: int
|
|
23
|
+
compliance_soc2: bool
|
|
24
|
+
gpu_type: str
|
|
25
|
+
gpu_memory_mb: int
|
|
26
|
+
gpus: int
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass
|
|
30
|
+
class Job:
|
|
31
|
+
id: str
|
|
32
|
+
rid: str
|
|
33
|
+
state: str
|
|
34
|
+
s3_url: str
|
|
35
|
+
project: str
|
|
36
|
+
upload_url: str
|
|
37
|
+
cloud_offer: CloudOffer
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class TrainWaveUser:
|
|
42
|
+
id: str
|
|
43
|
+
rid: str
|
|
44
|
+
first_name: str
|
|
45
|
+
last_name: str
|
|
46
|
+
email: str
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class CLIAuthStatus(Enum):
|
|
50
|
+
NOT_FOUND = "NOT_FOUND"
|
|
51
|
+
NOT_COMPLETED = "NOT_COMPLETED"
|
|
52
|
+
SUCCESS = "SUCCESS"
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class JobStatus(Enum):
|
|
56
|
+
LAUNCHING = "LAUNCHING"
|
|
57
|
+
RUNNING = "RUNNING"
|
|
58
|
+
ERROR = "ERROR"
|
|
59
|
+
|
|
60
|
+
@classmethod
|
|
61
|
+
def from_str(cls, provider: str) -> "JobStatus | None":
|
|
62
|
+
if provider.upper() not in JobStatus.__members__:
|
|
63
|
+
return None
|
|
64
|
+
return cls(provider.upper())
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class Api:
|
|
68
|
+
def __init__(
|
|
69
|
+
self,
|
|
70
|
+
api_key: str | None,
|
|
71
|
+
endpoint: str,
|
|
72
|
+
project: str = "",
|
|
73
|
+
):
|
|
74
|
+
verify_ssl = "trainwave.dev" not in endpoint
|
|
75
|
+
self.client = httpx.AsyncClient(base_url=endpoint, verify=verify_ssl)
|
|
76
|
+
self.api_key = api_key
|
|
77
|
+
self.project = project
|
|
78
|
+
|
|
79
|
+
async def request(self, method, path, **kwargs):
|
|
80
|
+
headers = kwargs.pop("headers", {})
|
|
81
|
+
headers["X-Api-Key"] = self.api_key
|
|
82
|
+
res = await self.client.request(method, path, headers=headers, **kwargs)
|
|
83
|
+
self._ensure_no_errors(res)
|
|
84
|
+
return res
|
|
85
|
+
|
|
86
|
+
def _ensure_no_errors(self, res: httpx.Response) -> httpx.Response:
|
|
87
|
+
if res.status_code >= HTTPStatus.BAD_REQUEST.value:
|
|
88
|
+
raise ValueError(f"Error: {res.text}")
|
|
89
|
+
return res
|
|
90
|
+
|
|
91
|
+
async def unauthenticated_request(self, method: str, path: str, **kwargs) -> httpx.Response:
|
|
92
|
+
res = await self.client.request(method, path, **kwargs)
|
|
93
|
+
return self._ensure_no_errors(res)
|
|
94
|
+
|
|
95
|
+
async def create_cli_auth_session(self) -> tuple[str, str]:
|
|
96
|
+
res = await self.unauthenticated_request(
|
|
97
|
+
"POST", "/api/v1/cli/create_session/", json={"name": socket.gethostname()}
|
|
98
|
+
)
|
|
99
|
+
json_body = res.json()
|
|
100
|
+
return str(json_body["url"]), str(json_body["token"])
|
|
101
|
+
|
|
102
|
+
async def check_cli_auth_session_status(self, token: str) -> tuple[CLIAuthStatus, str | None]:
|
|
103
|
+
res = await self.unauthenticated_request(
|
|
104
|
+
"POST", f"/api/v1/cli/session_status/", json={"token": token}
|
|
105
|
+
)
|
|
106
|
+
self._ensure_no_errors(res)
|
|
107
|
+
|
|
108
|
+
if res.status_code == HTTPStatus.ACCEPTED.value:
|
|
109
|
+
return CLIAuthStatus.NOT_COMPLETED, None
|
|
110
|
+
|
|
111
|
+
api_token = res.json().get("api_token")
|
|
112
|
+
return CLIAuthStatus.SUCCESS, api_token
|
|
113
|
+
|
|
114
|
+
async def check_api_key(self) -> bool:
|
|
115
|
+
res = await self.request("GET", "/api/v1/organizations/")
|
|
116
|
+
if res.status_code != HTTPStatus.OK.value:
|
|
117
|
+
return False
|
|
118
|
+
if len(res.json()) > 0:
|
|
119
|
+
return True
|
|
120
|
+
return False
|
|
121
|
+
|
|
122
|
+
async def get_myself(self) -> TrainWaveUser:
|
|
123
|
+
res = await self.request("GET", "/api/v1/users/me/")
|
|
124
|
+
res.raise_for_status()
|
|
125
|
+
json_body = res.json()
|
|
126
|
+
return TrainWaveUser(
|
|
127
|
+
id=json_body.get("id"),
|
|
128
|
+
rid=json_body.get("rid"),
|
|
129
|
+
first_name=json_body.get("first_name"),
|
|
130
|
+
last_name=json_body.get("last_name"),
|
|
131
|
+
email=json_body.get("email"),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
async def create_job(self, config: dict[str, Any]) -> Job:
|
|
135
|
+
res = await self.request(
|
|
136
|
+
"POST",
|
|
137
|
+
"api/v1/jobs/",
|
|
138
|
+
json={
|
|
139
|
+
"project": self.project,
|
|
140
|
+
"config": config,
|
|
141
|
+
},
|
|
142
|
+
)
|
|
143
|
+
return from_dict(Job, res.json())
|
|
144
|
+
|
|
145
|
+
async def job_status(self, job_id: str) -> JobStatus | None:
|
|
146
|
+
res = await self.request("GET", f"/api/v1/jobs/{job_id}/")
|
|
147
|
+
return JobStatus.from_str(res.json()["state"])
|
|
148
|
+
|
|
149
|
+
async def cancel_job(self, job_id: str) -> None:
|
|
150
|
+
await self.request("POST", f"/api/v1/jobs/{job_id}/cancel/", json={})
|
|
151
|
+
|
|
152
|
+
async def code_submission(self, job: Job) -> None:
|
|
153
|
+
await self.request("POST", f"/api/v1/jobs/{job.id}/code_submission/")
|
|
154
|
+
|
|
155
|
+
async def upload_code(self, tarball: Path, presigned_url: str) -> None:
|
|
156
|
+
size = tarball.stat().st_size
|
|
157
|
+
|
|
158
|
+
progress_bar = tqdm(total=size, unit="B", unit_scale=True, desc="Uploading")
|
|
159
|
+
|
|
160
|
+
async def file_chunk_iterator(filename):
|
|
161
|
+
async with aiofiles.open(filename, "rb") as file:
|
|
162
|
+
while True:
|
|
163
|
+
chunk = await file.read(64 * 1024)
|
|
164
|
+
if not chunk:
|
|
165
|
+
break
|
|
166
|
+
progress_bar.update(len(chunk))
|
|
167
|
+
yield chunk
|
|
168
|
+
|
|
169
|
+
async with aiohttp.ClientSession() as session:
|
|
170
|
+
headers = {"Content-Type": "application/gzip", "Content-Length": str(size)}
|
|
171
|
+
response = await session.put(
|
|
172
|
+
presigned_url, headers=headers, data=file_chunk_iterator(tarball)
|
|
173
|
+
)
|
|
174
|
+
response.raise_for_status()
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import time
|
|
2
|
+
import webbrowser
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
|
|
5
|
+
import typer
|
|
6
|
+
from loguru import logger
|
|
7
|
+
from rich.progress import Progress, SpinnerColumn, TextColumn
|
|
8
|
+
|
|
9
|
+
from trainwave_cli.api import Api, CLIAuthStatus
|
|
10
|
+
from trainwave_cli.config.config import config, config_manager
|
|
11
|
+
from trainwave_cli.utils import async_command, ensure_api_key
|
|
12
|
+
|
|
13
|
+
app = typer.Typer()
|
|
14
|
+
|
|
15
|
+
_BOLD = "\033[1m"
|
|
16
|
+
_RESET = "\033[0;0m"
|
|
17
|
+
|
|
18
|
+
LOGIN_POLLING_TIME: Annotated[int, "minutes"] = 5
|
|
19
|
+
LOGIN_POLLING_SLEEP_TIME: Annotated[int, "seconds"] = 5
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@app.command()
|
|
23
|
+
@async_command
|
|
24
|
+
async def login() -> None:
|
|
25
|
+
api_client = Api(None, config.endpoint)
|
|
26
|
+
session_url, session_token = await api_client.create_cli_auth_session()
|
|
27
|
+
|
|
28
|
+
print("Opening your browser to complete the login.")
|
|
29
|
+
print(f"{_BOLD}URL:{_RESET} {session_url} \n")
|
|
30
|
+
|
|
31
|
+
webbrowser.open_new_tab(session_url)
|
|
32
|
+
|
|
33
|
+
api_token: str | None = None
|
|
34
|
+
end_polling_at = time.time() + 60 * LOGIN_POLLING_TIME
|
|
35
|
+
|
|
36
|
+
with Progress(
|
|
37
|
+
SpinnerColumn(),
|
|
38
|
+
TextColumn("[progress.description]{task.description}"),
|
|
39
|
+
transient=True,
|
|
40
|
+
) as progress:
|
|
41
|
+
progress.add_task(description="Awaiting login...", total=None)
|
|
42
|
+
|
|
43
|
+
while time.time() < end_polling_at:
|
|
44
|
+
status, api_token = await api_client.check_cli_auth_session_status(
|
|
45
|
+
session_token
|
|
46
|
+
)
|
|
47
|
+
if status == CLIAuthStatus.SUCCESS:
|
|
48
|
+
break
|
|
49
|
+
|
|
50
|
+
time.sleep(LOGIN_POLLING_SLEEP_TIME)
|
|
51
|
+
|
|
52
|
+
if api_token:
|
|
53
|
+
# Test the token
|
|
54
|
+
api = Api(api_token, config.endpoint)
|
|
55
|
+
user = await api.get_myself()
|
|
56
|
+
|
|
57
|
+
print("✅ Success!")
|
|
58
|
+
print(f"Logged in as: {_BOLD}{user.email}{_RESET}")
|
|
59
|
+
|
|
60
|
+
config.api_key = api_token
|
|
61
|
+
config_manager.save()
|
|
62
|
+
else:
|
|
63
|
+
print("❌ Something went wrong. Try again.")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@app.command()
|
|
67
|
+
@async_command
|
|
68
|
+
@ensure_api_key
|
|
69
|
+
async def logout() -> None:
|
|
70
|
+
config_manager.delete()
|
|
71
|
+
logger.info("Logged out")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@app.command()
|
|
75
|
+
@async_command
|
|
76
|
+
@ensure_api_key
|
|
77
|
+
async def whoami() -> None:
|
|
78
|
+
api = Api(config.api_key, config.endpoint)
|
|
79
|
+
res = await api.get_myself()
|
|
80
|
+
print(res.email)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@app.command()
|
|
84
|
+
@async_command
|
|
85
|
+
async def set_token(api_key: str):
|
|
86
|
+
api = Api(api_key, config.endpoint)
|
|
87
|
+
logger.info("Checking API key...")
|
|
88
|
+
if await api.check_api_key():
|
|
89
|
+
logger.info("API key is valid!")
|
|
90
|
+
config.api_key = api_key
|
|
91
|
+
config_manager.save()
|
|
92
|
+
logger.info("Done!")
|
|
93
|
+
else:
|
|
94
|
+
logger.error("API key is invalid")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@app.command()
|
|
98
|
+
@async_command
|
|
99
|
+
@ensure_api_key
|
|
100
|
+
async def token() -> None:
|
|
101
|
+
print(config.api_key)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@app.command()
|
|
105
|
+
@async_command
|
|
106
|
+
async def set_endpoint(endpoint: str) -> None:
|
|
107
|
+
config.endpoint = endpoint
|
|
108
|
+
config_manager.save()
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import sys
|
|
4
|
+
import traceback
|
|
5
|
+
from dataclasses import asdict
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Annotated, Optional
|
|
9
|
+
from urllib.parse import urlparse
|
|
10
|
+
|
|
11
|
+
import pytimeparse
|
|
12
|
+
import typer
|
|
13
|
+
import websockets
|
|
14
|
+
from loguru import logger
|
|
15
|
+
from tabulate import tabulate
|
|
16
|
+
from typer_config.decorators import use_toml_config
|
|
17
|
+
|
|
18
|
+
from trainwave_cli.api import Api
|
|
19
|
+
from trainwave_cli.config.config import config
|
|
20
|
+
from trainwave_cli.io import create_tarball
|
|
21
|
+
from trainwave_cli.utils import async_command, ensure_api_key, truncate_string
|
|
22
|
+
|
|
23
|
+
app = typer.Typer()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@app.command()
|
|
27
|
+
@async_command
|
|
28
|
+
@use_toml_config(default_value="trainwave.toml")
|
|
29
|
+
@ensure_api_key
|
|
30
|
+
async def launch( # noqa
|
|
31
|
+
# Basic job parameters
|
|
32
|
+
name: Annotated[str, typer.Option()],
|
|
33
|
+
project: Annotated[str, typer.Option()],
|
|
34
|
+
setup_command: Annotated[str, typer.Option()],
|
|
35
|
+
run_command: Annotated[str, typer.Option()],
|
|
36
|
+
# Optional params
|
|
37
|
+
expires: Annotated[str, typer.Option()] = "",
|
|
38
|
+
env_vars={}, # dict[str, str] not supported by typer yet
|
|
39
|
+
exclude_gitignore: Annotated[bool, typer.Option()] = True,
|
|
40
|
+
exclude_regex: Optional[Annotated[str, typer.Option()]] = None,
|
|
41
|
+
# Harware parameters
|
|
42
|
+
image: Annotated[str, typer.Option()] = "",
|
|
43
|
+
hdd_size_mb: Annotated[int, typer.Option()] = 20,
|
|
44
|
+
memory_mb: Annotated[int, typer.Option()] = 0,
|
|
45
|
+
cpus: Annotated[int, typer.Option()] = 0,
|
|
46
|
+
gpus: Annotated[int, typer.Option()] = 0,
|
|
47
|
+
gpu_type: Annotated[str, typer.Option()] = "",
|
|
48
|
+
) -> None:
|
|
49
|
+
api_client = Api(config.api_key, config.endpoint, project)
|
|
50
|
+
# Interpolate environe variables
|
|
51
|
+
env_vars = eval(env_vars)
|
|
52
|
+
for k, v in env_vars.items():
|
|
53
|
+
if v.startswith("${"):
|
|
54
|
+
interpolated = os.getenv(v[2:-1])
|
|
55
|
+
if interpolated is None:
|
|
56
|
+
logger.warning(f"Environment variable {v} not found")
|
|
57
|
+
else:
|
|
58
|
+
env_vars[k] = interpolated
|
|
59
|
+
|
|
60
|
+
conf = {
|
|
61
|
+
"name": name,
|
|
62
|
+
"image": image,
|
|
63
|
+
"setup_command": setup_command,
|
|
64
|
+
"run_command": run_command,
|
|
65
|
+
"hdd_size_mb": hdd_size_mb,
|
|
66
|
+
"memory_mb": memory_mb,
|
|
67
|
+
"cpus": cpus,
|
|
68
|
+
"gpus": gpus,
|
|
69
|
+
"gpu_type": gpu_type,
|
|
70
|
+
"env_vars": env_vars,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if expires != "":
|
|
74
|
+
expires_time = pytimeparse.parse(expires)
|
|
75
|
+
if expires_time is None:
|
|
76
|
+
logger.warning(f"Expires cannot be used, defaulting to nothing")
|
|
77
|
+
else:
|
|
78
|
+
expires_at = int(datetime.now(timezone.utc).timestamp()) + int(expires_time)
|
|
79
|
+
conf["expires_at"] = expires_at
|
|
80
|
+
|
|
81
|
+
def render(k, v) -> str:
|
|
82
|
+
match k:
|
|
83
|
+
case "env_vars":
|
|
84
|
+
return ",".join(v.keys())
|
|
85
|
+
case "expires_at":
|
|
86
|
+
return datetime.fromtimestamp(v).strftime("%Y-%m-%d %H:%M:%S")
|
|
87
|
+
case _:
|
|
88
|
+
return truncate_string(str(conf[k]), 50)
|
|
89
|
+
|
|
90
|
+
tab_keys = list(conf.keys())
|
|
91
|
+
tab_values = {
|
|
92
|
+
"name": tab_keys,
|
|
93
|
+
"value": [render(k, conf[k]) for k in tab_keys],
|
|
94
|
+
}
|
|
95
|
+
logger.info(tabulate(tab_values, headers="keys"))
|
|
96
|
+
logger.info("\n")
|
|
97
|
+
|
|
98
|
+
tarball = None
|
|
99
|
+
try:
|
|
100
|
+
job = await api_client.create_job(conf)
|
|
101
|
+
tarball = create_tarball(Path().cwd(), exclude_gitignore, exclude_regex)
|
|
102
|
+
await api_client.upload_code(Path(tarball.name), job.upload_url)
|
|
103
|
+
await api_client.code_submission(job)
|
|
104
|
+
|
|
105
|
+
co = asdict(job.cloud_offer)
|
|
106
|
+
tab_keys = list(co.keys())
|
|
107
|
+
tab_values = {
|
|
108
|
+
"name": tab_keys,
|
|
109
|
+
"value": [truncate_string(str(co[k]), 50) for k in tab_keys],
|
|
110
|
+
}
|
|
111
|
+
logger.info("\nMatched machine\n" + tabulate(tab_values, headers="keys") + "\n")
|
|
112
|
+
logger.info(f"Job created: {job.rid} ({job.id})")
|
|
113
|
+
except Exception as e:
|
|
114
|
+
traceback.print_exc()
|
|
115
|
+
logger.error(f"An error occurred: {e}")
|
|
116
|
+
finally:
|
|
117
|
+
if tarball:
|
|
118
|
+
tarball.close()
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@app.command()
|
|
122
|
+
@async_command
|
|
123
|
+
@use_toml_config(default_value="trainwave.toml")
|
|
124
|
+
@ensure_api_key
|
|
125
|
+
async def status(
|
|
126
|
+
project: Annotated[str, typer.Option()],
|
|
127
|
+
job_id: str,
|
|
128
|
+
) -> None:
|
|
129
|
+
api_client = Api(config.api_key, config.endpoint, project)
|
|
130
|
+
logger.info(await api_client.job_status(job_id))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
@app.command()
|
|
134
|
+
@async_command
|
|
135
|
+
@use_toml_config(default_value="trainwave.toml")
|
|
136
|
+
@ensure_api_key
|
|
137
|
+
async def cancel(
|
|
138
|
+
project: Annotated[str, typer.Option()],
|
|
139
|
+
job_id: str,
|
|
140
|
+
):
|
|
141
|
+
api_client = Api(config.api_key, config.endpoint, project)
|
|
142
|
+
await api_client.cancel_job(job_id)
|
|
143
|
+
logger.info(f"Cancelled job: {job_id}")
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@app.command()
|
|
147
|
+
@async_command
|
|
148
|
+
@ensure_api_key
|
|
149
|
+
async def logs(
|
|
150
|
+
job_id: str,
|
|
151
|
+
) -> None:
|
|
152
|
+
logger.remove()
|
|
153
|
+
logger.add(
|
|
154
|
+
sys.stdout,
|
|
155
|
+
format="<green>{extra[log_time]}</green> | <white>{message}</white>",
|
|
156
|
+
colorize=True,
|
|
157
|
+
serialize=False,
|
|
158
|
+
level="DEBUG",
|
|
159
|
+
enqueue=True,
|
|
160
|
+
catch=True,
|
|
161
|
+
)
|
|
162
|
+
parsed = urlparse(config.endpoint)
|
|
163
|
+
scheme = "wss" if parsed.scheme == "https" else "ws"
|
|
164
|
+
port = ""
|
|
165
|
+
if parsed.port:
|
|
166
|
+
port = f":{parsed.port}"
|
|
167
|
+
uri = f"{scheme}://{parsed.hostname}{port}/ws/logs/{job_id}/?api_key={config.api_key}"
|
|
168
|
+
|
|
169
|
+
def format_ts(ts: str | None) -> str:
|
|
170
|
+
if ts is None:
|
|
171
|
+
return ""
|
|
172
|
+
timestamp = int(ts) / 1e9
|
|
173
|
+
dt = datetime.fromtimestamp(timestamp)
|
|
174
|
+
return dt.strftime("%Y-%m-%d %H:%M:%S.%f")
|
|
175
|
+
|
|
176
|
+
ssl_context = None
|
|
177
|
+
if "trainwave.dev" in uri:
|
|
178
|
+
import ssl
|
|
179
|
+
|
|
180
|
+
ssl_context = ssl.SSLContext()
|
|
181
|
+
ssl_context.verify_mode = ssl.CERT_NONE
|
|
182
|
+
opts = {"ssl": ssl_context} if ssl_context else {}
|
|
183
|
+
|
|
184
|
+
async with websockets.connect(uri, **opts) as websocket:
|
|
185
|
+
try:
|
|
186
|
+
while response := await websocket.recv():
|
|
187
|
+
data = json.loads(response)
|
|
188
|
+
logger.info(data["message"], log_time=format_ts(data.get("ts")))
|
|
189
|
+
except websockets.exceptions.ConnectionClosedOK:
|
|
190
|
+
pass
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import typer
|
|
2
|
+
from beaupy import confirm, prompt, select, select_multiple
|
|
3
|
+
from rich.console import Console
|
|
4
|
+
|
|
5
|
+
from trainwave_cli.api import Api
|
|
6
|
+
from trainwave_cli.config.config import config
|
|
7
|
+
from trainwave_cli.utils import async_command, ensure_api_key
|
|
8
|
+
|
|
9
|
+
app = typer.Typer()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
HARDWARE_OPTIONS = [
|
|
13
|
+
"GPU",
|
|
14
|
+
"CPU",
|
|
15
|
+
]
|
|
16
|
+
CURSOR = "➡️"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@app.command()
|
|
20
|
+
@async_command
|
|
21
|
+
@ensure_api_key
|
|
22
|
+
async def list():
|
|
23
|
+
api_client = Api(config.api_key, config.endpoint)
|
|
24
|
+
offers = await api_client.offers()
|
|
25
|
+
|
|
26
|
+
console = Console()
|
|
27
|
+
console.print("Select Hardware")
|
|
28
|
+
hw = select(HARDWARE_OPTIONS, cursor=CURSOR)
|
|
29
|
+
|
|
30
|
+
if hw == "GPU":
|
|
31
|
+
|
|
32
|
+
else:
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
from dataclasses import asdict, dataclass
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import ClassVar
|
|
4
|
+
|
|
5
|
+
import toml
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class Config:
|
|
10
|
+
endpoint: str = "http://localhost:8000" # TODO: Change this
|
|
11
|
+
api_key: str = ""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ConfigManager:
|
|
15
|
+
CONFIG_DIR = Path.home() / ".config" / "trainwave"
|
|
16
|
+
CONFIG_PATH = CONFIG_DIR / "config.toml"
|
|
17
|
+
DEFAULT_CONF: ClassVar[dict[str, str]] = {"api_key": ""}
|
|
18
|
+
|
|
19
|
+
def __init__(self) -> None:
|
|
20
|
+
self.config = Config()
|
|
21
|
+
self._ensure_config_dir()
|
|
22
|
+
self.read()
|
|
23
|
+
|
|
24
|
+
def save(self) -> None:
|
|
25
|
+
if self.config is None:
|
|
26
|
+
raise ValueError("Config is not initialized")
|
|
27
|
+
data = asdict(self.config)
|
|
28
|
+
with open(self.CONFIG_PATH, "w") as f:
|
|
29
|
+
toml.dump(data, f)
|
|
30
|
+
self.read()
|
|
31
|
+
|
|
32
|
+
def delete(self) -> None:
|
|
33
|
+
self.CONFIG_PATH.unlink(missing_ok=True)
|
|
34
|
+
|
|
35
|
+
def read(self) -> None:
|
|
36
|
+
if not self.CONFIG_PATH.exists():
|
|
37
|
+
self._setup_config()
|
|
38
|
+
with open(self.CONFIG_PATH) as f:
|
|
39
|
+
data = toml.load(f)
|
|
40
|
+
self.config = Config(
|
|
41
|
+
**{k: v for k, v in data.items() if k in Config.__dataclass_fields__.keys()},
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
def _setup_config(self) -> None:
|
|
45
|
+
with open(self.CONFIG_PATH, "w") as f:
|
|
46
|
+
toml.dump(self.DEFAULT_CONF, f)
|
|
47
|
+
|
|
48
|
+
def _ensure_config_dir(self) -> None:
|
|
49
|
+
self.CONFIG_DIR.mkdir(exist_ok=True, parents=True)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
config_manager = ConfigManager()
|
|
53
|
+
config = config_manager.config
|
trainwave_cli/io.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
import tarfile
|
|
4
|
+
import tempfile
|
|
5
|
+
import typing
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
import pathspec
|
|
9
|
+
from loguru import logger
|
|
10
|
+
from tqdm import tqdm
|
|
11
|
+
|
|
12
|
+
WARN_FILE_SIZE = 50 * 1024 * 1024 # 100 MB
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_all_files_in_dir(directory):
|
|
16
|
+
file_list = []
|
|
17
|
+
for root, dirs, files in os.walk(directory):
|
|
18
|
+
for file in files:
|
|
19
|
+
file_list.append(os.path.join(root, file))
|
|
20
|
+
return file_list
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def create_tarball(
|
|
24
|
+
source_dir: Path, exclude_gitignore: bool, exclude_regex: str | None
|
|
25
|
+
) -> typing.IO[typing.Any]:
|
|
26
|
+
temp = tempfile.NamedTemporaryFile(suffix=".tar")
|
|
27
|
+
spec = None
|
|
28
|
+
|
|
29
|
+
if exclude_gitignore and (source_dir / ".gitignore").exists():
|
|
30
|
+
with open(source_dir / ".gitignore") as gitignore:
|
|
31
|
+
spec_src = gitignore.read()
|
|
32
|
+
spec = pathspec.PathSpec.from_lines(
|
|
33
|
+
pathspec.patterns.gitwildmatch.GitWildMatchPattern, spec_src.splitlines()
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
files = set(get_all_files_in_dir(source_dir))
|
|
37
|
+
if spec is not None:
|
|
38
|
+
files -= set(spec.match_files(files))
|
|
39
|
+
|
|
40
|
+
# Run exclude regex
|
|
41
|
+
if exclude_regex is not None:
|
|
42
|
+
files = {file for file in files if not re.search(exclude_regex, file)}
|
|
43
|
+
|
|
44
|
+
total_size = sum(os.path.getsize(file) for file in files)
|
|
45
|
+
with tarfile.open(temp.name, "w:gz") as tar:
|
|
46
|
+
with tqdm(
|
|
47
|
+
total=total_size, unit="B", unit_scale=True, desc="Creating archive"
|
|
48
|
+
) as progress_bar:
|
|
49
|
+
for file in files:
|
|
50
|
+
if os.path.getsize(file) > WARN_FILE_SIZE:
|
|
51
|
+
logger.warning(f"{file} is larger than {WARN_FILE_SIZE/(1024*1024)}MB")
|
|
52
|
+
arcname = os.path.relpath(file, source_dir)
|
|
53
|
+
tar.add(file, arcname=arcname)
|
|
54
|
+
progress_bar.update(os.path.getsize(file))
|
|
55
|
+
|
|
56
|
+
return temp
|
trainwave_cli/main.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
import typer
|
|
4
|
+
from loguru import logger
|
|
5
|
+
|
|
6
|
+
from trainwave_cli.cli import auth, jobs
|
|
7
|
+
|
|
8
|
+
app = typer.Typer()
|
|
9
|
+
app.add_typer(jobs.app, name="jobs")
|
|
10
|
+
app.add_typer(auth.app, name="auth")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def entrypoint() -> None:
|
|
14
|
+
logger.remove()
|
|
15
|
+
logger.add(sys.stdout, colorize=True, format="<level>{message}</level>")
|
|
16
|
+
app()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
if __name__ == "__main__":
|
|
20
|
+
app()
|
trainwave_cli/utils.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from dataclasses import fields, is_dataclass
|
|
3
|
+
from functools import wraps
|
|
4
|
+
from typing import Type, TypeVar
|
|
5
|
+
|
|
6
|
+
import typer
|
|
7
|
+
|
|
8
|
+
from trainwave_cli.config.config import config
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def has_running_event_loop() -> bool:
|
|
12
|
+
try:
|
|
13
|
+
asyncio.get_running_loop()
|
|
14
|
+
return True
|
|
15
|
+
except RuntimeError:
|
|
16
|
+
# No running event loop in this thread
|
|
17
|
+
return False
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def async_command(f):
|
|
21
|
+
@wraps(f)
|
|
22
|
+
def wrapper(*args, **kwargs):
|
|
23
|
+
if has_running_event_loop():
|
|
24
|
+
return f(*args, **kwargs)
|
|
25
|
+
return asyncio.run(f(*args, **kwargs))
|
|
26
|
+
|
|
27
|
+
return wrapper
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def ensure_api_key(f):
|
|
31
|
+
@wraps(f)
|
|
32
|
+
def wrapper(*args, **kwargs):
|
|
33
|
+
if config.api_key in {"", None}:
|
|
34
|
+
print(
|
|
35
|
+
"No access token found. Please run `trainwave auth login` to login to your account."
|
|
36
|
+
)
|
|
37
|
+
raise typer.Exit(1)
|
|
38
|
+
return f(*args, **kwargs)
|
|
39
|
+
|
|
40
|
+
return wrapper
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def truncate_string(s: str, max_len: int) -> str:
|
|
44
|
+
if len(s) <= max_len:
|
|
45
|
+
return s
|
|
46
|
+
return s[: max_len - 3] + "..."
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
T = TypeVar("T")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def from_dict(data_class: Type[T], data: dict) -> T:
|
|
53
|
+
"""Recursively constructs a dataclass from a nested dictionary, skipping unmatched fields."""
|
|
54
|
+
field_names = {f.name for f in fields(data_class)}
|
|
55
|
+
filtered_data = {k: v for k, v in data.items() if k in field_names}
|
|
56
|
+
|
|
57
|
+
for key in filtered_data:
|
|
58
|
+
field_type = next(f for f in fields(data_class) if f.name == key).type
|
|
59
|
+
if is_dataclass(field_type):
|
|
60
|
+
filtered_data[key] = from_dict(field_type, filtered_data[key])
|
|
61
|
+
|
|
62
|
+
return data_class(**filtered_data)
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: trainwave-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary:
|
|
5
|
+
Author: Johan Backman
|
|
6
|
+
Author-email: johan@trainwave.ai
|
|
7
|
+
Requires-Python: >=3.10,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Requires-Dist: aiofiles (>=24.1.0,<25.0.0)
|
|
13
|
+
Requires-Dist: aiohttp (>=3.10,<4.0)
|
|
14
|
+
Requires-Dist: boto3 (>=1.30)
|
|
15
|
+
Requires-Dist: httpx (==0.27.0)
|
|
16
|
+
Requires-Dist: loguru (>=0.7.2,<0.8.0)
|
|
17
|
+
Requires-Dist: pathspec (>=0.12.1,<0.13.0)
|
|
18
|
+
Requires-Dist: pytimeparse (>=1.1.8,<2.0.0)
|
|
19
|
+
Requires-Dist: tabulate (>=0.9.0,<0.10.0)
|
|
20
|
+
Requires-Dist: toml (>=0.10.2,<0.11.0)
|
|
21
|
+
Requires-Dist: tqdm (>=4.66.4,<5.0.0)
|
|
22
|
+
Requires-Dist: typer (>=0.12.3,<0.13.0)
|
|
23
|
+
Requires-Dist: typer-config[all] (>=1.4.0,<2.0.0)
|
|
24
|
+
Requires-Dist: websockets (>=12.0,<13.0)
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# TrainWave cli
|
|
28
|
+
|
|
29
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
trainwave_cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
trainwave_cli/api.py,sha256=G7DqqebXs_G1mnZln1WKOChFfherrwqNP8sMTyHlo8I,5254
|
|
3
|
+
trainwave_cli/cli/auth.py,sha256=M1z9PFscWFzLKPOr4B87jsvE2BsQ-MNSbze7-_usxmU,2742
|
|
4
|
+
trainwave_cli/cli/jobs.py,sha256=xHUeq9LpKxaeUJ2aJLGhhcZOMnzqv6x7euOVqK2NYTE,5910
|
|
5
|
+
trainwave_cli/cli/offers.py,sha256=FltYQuZ4QGhHLdI-PQpvveCung9ux1xusaxR1I7mRpY,661
|
|
6
|
+
trainwave_cli/config/config.py,sha256=vNbF8SglvT2iuUEqdAz5LLse75U7cEmPkZLeVlDkxaM,1461
|
|
7
|
+
trainwave_cli/io.py,sha256=8F_MhqMWx91Ztzzf1gzl1R1oz2a9bW3tK5No-wFfzxI,1775
|
|
8
|
+
trainwave_cli/main.py,sha256=g9bJYEk_rreM2GMlAfcm5VtUz6G9jHILKHFDBQVb4pA,362
|
|
9
|
+
trainwave_cli/utils.py,sha256=2PfqdyTa8CdsEAX5lFpHXKhyIFLcb4peuNNGFuyUx2w,1620
|
|
10
|
+
trainwave_cli/xceptions.py,sha256=42wPK7ib0hLzSvzqLMHdWRWx81hpsl7no9h32Ssgj8I,116
|
|
11
|
+
trainwave_cli-0.1.0.dist-info/METADATA,sha256=VZ6z85AG9mxPolzQ4K3H1T5SEvclYuo5qGAUi8GbGaU,929
|
|
12
|
+
trainwave_cli-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
13
|
+
trainwave_cli-0.1.0.dist-info/entry_points.txt,sha256=6NDgl5S7Igb09TPdoUBcI_B1Lj2pB59NAfs2IcnMDOk,54
|
|
14
|
+
trainwave_cli-0.1.0.dist-info/RECORD,,
|