trainwave-cli 0.1.5__tar.gz → 0.1.7__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: trainwave-cli
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary:
5
5
  Author: Johan Backman
6
6
  Author-email: johan@trainwave.ai
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "trainwave-cli"
3
- version = "0.1.5"
3
+ version = "0.1.7"
4
4
  description = ""
5
5
  authors = ["Johan Backman <johan@trainwave.ai>"]
6
6
  readme = "README.md"
@@ -89,6 +89,11 @@ class CLIAuthStatus(Enum):
89
89
  SUCCESS = "SUCCESS"
90
90
 
91
91
 
92
+ class CLIConfigSessionStatus(Enum):
93
+ COMPLETE = "complete"
94
+ PENDING = "pending"
95
+
96
+
92
97
  class JobStatus(Enum):
93
98
  LAUNCHING = "LAUNCHING"
94
99
  RUNNING = "RUNNING"
@@ -127,10 +132,29 @@ class Api:
127
132
  raise ValueError(f"Error: {res.text}")
128
133
  return res
129
134
 
130
- async def unauthenticated_request(self, method: str, path: str, **kwargs) -> httpx.Response:
135
+ async def unauthenticated_request(
136
+ self, method: str, path: str, **kwargs
137
+ ) -> httpx.Response:
131
138
  res = await self.client.request(method, path, **kwargs)
132
139
  return self._ensure_no_errors(res)
133
140
 
141
+ async def create_cli_config_session(
142
+ self, existing_config: dict[str, Any]
143
+ ) -> tuple[str, str]:
144
+ res = await self.request(
145
+ "POST", "/api/v1/cli/config_session/", json=existing_config
146
+ )
147
+ json_body = res.json()
148
+ return str(json_body["url"]), str(json_body["token"])
149
+
150
+ async def check_cli_config_session_status(
151
+ self, token: str
152
+ ) -> tuple[CLIConfigSessionStatus, dict[str, Any] | None]:
153
+ res = await self.request("GET", f"/api/v1/cli/{token}/config/")
154
+ self._ensure_no_errors(res)
155
+ json_body = res.json()
156
+ return CLIConfigSessionStatus(json_body["status"]), json_body.get("config")
157
+
134
158
  async def create_cli_auth_session(self) -> tuple[str, str]:
135
159
  res = await self.unauthenticated_request(
136
160
  "POST", "/api/v1/cli/create_session/", json={"name": socket.gethostname()}
@@ -138,7 +162,9 @@ class Api:
138
162
  json_body = res.json()
139
163
  return str(json_body["url"]), str(json_body["token"])
140
164
 
141
- async def check_cli_auth_session_status(self, token: str) -> tuple[CLIAuthStatus, str | None]:
165
+ async def check_cli_auth_session_status(
166
+ self, token: str
167
+ ) -> tuple[CLIAuthStatus, str | None]:
142
168
  res = await self.unauthenticated_request(
143
169
  "POST", f"/api/v1/cli/session_status/", json={"token": token}
144
170
  )
@@ -166,7 +192,9 @@ class Api:
166
192
  async def list_projects(self, organization_id: str) -> list[TrainwaveProject]:
167
193
  res = await self.request("GET", "/api/v1/projects/")
168
194
  res.raise_for_status()
169
- return [from_dict(TrainwaveProject, project) for project in res.json()["results"]]
195
+ return [
196
+ from_dict(TrainwaveProject, project) for project in res.json()["results"]
197
+ ]
170
198
 
171
199
  async def create_project(self, organization_id: str, name: str) -> TrainwaveProject:
172
200
  res = await self.request(
@@ -0,0 +1,92 @@
1
+ import copy
2
+ import time
3
+ import webbrowser
4
+ from typing import Annotated, Any
5
+
6
+ import toml
7
+ import typer
8
+ from beaupy import confirm
9
+ from rich.progress import Progress, SpinnerColumn, TextColumn
10
+ from typer_config.loaders import toml_loader
11
+
12
+ from trainwave_cli.api import Api, CLIConfigSessionStatus
13
+ from trainwave_cli.config.config import config
14
+ from trainwave_cli.utils import async_command, ensure_api_key
15
+
16
+ CONFIG_SESSION_POLLING_TIME: Annotated[int, "minutes"] = 20
17
+ CONFIG_SESSION_POLLING_SLEEP_TIME: Annotated[int, "seconds"] = 5
18
+
19
+ app = typer.Typer()
20
+
21
+
22
+ async def _run_setup_guide(existing_config: dict[str, Any]) -> dict[str, Any]:
23
+ typer.echo("Running setup\n")
24
+ api_client = Api(config.api_key, config.endpoint)
25
+
26
+ _existing_config = copy.deepcopy(existing_config)
27
+ if _existing_config.get("env_vars"):
28
+ vars = _existing_config.pop("env_vars")
29
+ _existing_config["env_vars"] = [{"key": k, "value": v} for k, v in vars.items()]
30
+
31
+ url, session_token = await api_client.create_cli_config_session(_existing_config)
32
+ webbrowser.open_new_tab(url)
33
+
34
+ end_polling_at = time.time() + 60 * CONFIG_SESSION_POLLING_TIME
35
+ with Progress(
36
+ SpinnerColumn(),
37
+ TextColumn("[progress.description]{task.description}"),
38
+ transient=True,
39
+ ) as progress:
40
+ progress.add_task(description="Awaiting config...", total=None)
41
+
42
+ while time.time() < end_polling_at:
43
+ status, new_config = await api_client.check_cli_config_session_status(
44
+ session_token
45
+ )
46
+ if status == CLIConfigSessionStatus.COMPLETE:
47
+ # convert back the env vars
48
+ if new_config and new_config.get("env_vars"):
49
+ vars = new_config.pop("env_vars")
50
+ new_config["env_vars"] = {var["key"]: var["value"] for var in vars}
51
+
52
+ return new_config or {}
53
+
54
+ time.sleep(CONFIG_SESSION_POLLING_SLEEP_TIME)
55
+
56
+ return {}
57
+
58
+
59
+ @app.callback(invoke_without_command=True)
60
+ @async_command
61
+ @ensure_api_key
62
+ async def default(
63
+ config: Annotated[str | None, typer.Argument()] = None,
64
+ accept: Annotated[bool, "Accept overwrite of config"] = False,
65
+ ) -> None:
66
+ config_path = "trainwave.toml"
67
+ if config:
68
+ config_path = config
69
+
70
+ try:
71
+ existing_config = toml_loader(config_path)
72
+ except FileNotFoundError:
73
+ existing_config = {}
74
+
75
+ new_config = await _run_setup_guide(existing_config or {})
76
+
77
+ if new_config:
78
+ confirmed = accept or confirm(
79
+ f"You already have a config file({config_path}). Do you want to overwrite it?"
80
+ )
81
+
82
+ if confirmed:
83
+ with open(config_path, "w") as f:
84
+ # Merge and dump
85
+ toml.dump(existing_config | new_config, f)
86
+
87
+ typer.echo(f"Config file saved to {config_path}.")
88
+ else:
89
+ typer.echo("Config file not saved to file. \n")
90
+
91
+ typer.echo("Config: \n")
92
+ typer.echo(toml.dumps(existing_config | new_config))
@@ -8,7 +8,7 @@ from trainwave_cli.cli import auth, jobs, setup
8
8
  app = typer.Typer()
9
9
  app.add_typer(jobs.app, name="jobs")
10
10
  app.add_typer(auth.app, name="auth")
11
- app.add_typer(setup.app, name="setup")
11
+ app.add_typer(setup.app, name="config")
12
12
 
13
13
 
14
14
  def entrypoint() -> None:
@@ -1,114 +0,0 @@
1
- from typing import Annotated, Any
2
-
3
- import toml
4
- import typer
5
- from beaupy import confirm, prompt, select
6
- from typer_config import conf_callback_factory
7
- from typer_config.decorators import use_config
8
- from typer_config.loaders import toml_loader
9
-
10
- from trainwave_cli.api import Api
11
- from trainwave_cli.config.config import config
12
- from trainwave_cli.utils import async_command, ensure_api_key
13
-
14
- app = typer.Typer()
15
-
16
-
17
- def pyproject_loader(param_value: str) -> dict[str, Any]:
18
- if not param_value: # set a default path to read from
19
- param_value = "trainwave.toml"
20
- try:
21
- pyproject = toml_loader(param_value)
22
- except FileNotFoundError:
23
- return {}
24
- return pyproject
25
-
26
-
27
- pyproject_callback = conf_callback_factory(pyproject_loader)
28
-
29
-
30
- _CREATE_NEW_PROJECT_OPTION = "( Create a new project )"
31
-
32
-
33
- async def _run_setup_guide(
34
- existing_org: str,
35
- existing_project: str,
36
- ) -> dict[str, str | int] | None:
37
- typer.echo("Running setup\n")
38
-
39
- api_client = Api(config.api_key, config.endpoint)
40
-
41
- # Step 1: Select an organization that user is part of
42
- organizations = await api_client.list_organizations()
43
- if not organizations:
44
- typer.echo("You are not part of any organization.\n")
45
- typer.echo("Please visit the Trainwave web app to create an organization.\n")
46
- return
47
-
48
- formatted_org_selection = [f"{org.name}" for org in organizations]
49
-
50
- typer.echo("1. Select which organization to use.\n")
51
- select_org = select(formatted_org_selection)
52
-
53
- selected_org = next(org for org in organizations if org.name == select_org)
54
-
55
- # Step 2: Select or create a project to use
56
-
57
- projects = await api_client.list_projects(selected_org.id)
58
- project_options = [f"{project.name}" for project in projects]
59
- project_options.append(_CREATE_NEW_PROJECT_OPTION)
60
-
61
- typer.echo("2. Select which project to use.\n")
62
- typer.echo(
63
- "A project groups together all the jobs you run and allows you to enforce spending limits and default configurations for all jobs run in the project.\n"
64
- )
65
-
66
- selected_project = select(project_options)
67
-
68
- if selected_project == _CREATE_NEW_PROJECT_OPTION:
69
- project_name = prompt("Enter the name of the new project")
70
- project = await api_client.create_project(selected_org.id, project_name)
71
- else:
72
- project = next(
73
- project for project in projects if project.name == selected_project
74
- )
75
-
76
- typer.echo("Give your job a name. Example: 'Training a LLM'\n")
77
-
78
- name = prompt("Enter the name of the job")
79
-
80
- # TODO: Ask for GPU type and count
81
- # SETUP COMMAND and RUN COMMAND
82
- return {
83
- "organization": selected_org.rid,
84
- "project": project.rid,
85
- "name": name,
86
- }
87
-
88
-
89
- @app.callback(invoke_without_command=True)
90
- @async_command
91
- @use_config(pyproject_callback)
92
- @ensure_api_key
93
- async def default(
94
- organization: Annotated[str, typer.Option()] = "",
95
- project: Annotated[str, typer.Option()] = "",
96
- name: Annotated[str, typer.Option()] = "",
97
- ) -> None:
98
- # Stuff is not set
99
- if not organization:
100
- typer.echo("No config file found.\n")
101
-
102
- if confirm(
103
- "Would you like to create a new Trainwave configuration (trainwave.toml)?"
104
- ):
105
- new_config = await _run_setup_guide(organization, project, name)
106
-
107
- typer.echo(new_config)
108
-
109
- with open("trainwave.toml", "w") as f:
110
- toml.dump(new_config, f)
111
-
112
- # Ask user if they want to switch org or project
113
- else:
114
- pass
File without changes