nufi-python 1.0.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.
Files changed (52) hide show
  1. nufi/__init__.py +52 -0
  2. nufi/auth.py +364 -0
  3. nufi/cli/__init__.py +0 -0
  4. nufi/cli/commands/__init__.py +1 -0
  5. nufi/cli/commands/auth.py +113 -0
  6. nufi/cli/commands/common.py +198 -0
  7. nufi/cli/commands/compiles.py +507 -0
  8. nufi/cli/commands/custom_evaluation_templates.py +206 -0
  9. nufi/cli/commands/datasets.py +697 -0
  10. nufi/cli/commands/evaluations.py +854 -0
  11. nufi/cli/commands/leaderboards.py +763 -0
  12. nufi/cli/commands/models.py +1347 -0
  13. nufi/cli/commands/monitoring.py +350 -0
  14. nufi/cli/commands/profile.py +123 -0
  15. nufi/cli/commands/projects.py +308 -0
  16. nufi/cli/commands/servings.py +306 -0
  17. nufi/cli/main.py +66 -0
  18. nufi/client.py +103 -0
  19. nufi/config.py +281 -0
  20. nufi/evaluator.py +559 -0
  21. nufi/exceptions.py +58 -0
  22. nufi/http_utils.py +51 -0
  23. nufi/models/__init__.py +185 -0
  24. nufi/models/compiles.py +79 -0
  25. nufi/models/custom_evaluation_templates.py +111 -0
  26. nufi/models/datasets.py +94 -0
  27. nufi/models/evaluations.py +253 -0
  28. nufi/models/leaderboards.py +178 -0
  29. nufi/models/monitoring.py +179 -0
  30. nufi/models/pagination.py +33 -0
  31. nufi/models/projects.py +32 -0
  32. nufi/models/registry.py +138 -0
  33. nufi/models/servings.py +27 -0
  34. nufi/py.typed +1 -0
  35. nufi/resources/__init__.py +34 -0
  36. nufi/resources/base.py +190 -0
  37. nufi/resources/compiles.py +193 -0
  38. nufi/resources/custom_evaluation_templates.py +126 -0
  39. nufi/resources/datasets.py +786 -0
  40. nufi/resources/evaluations.py +428 -0
  41. nufi/resources/leaderboards.py +413 -0
  42. nufi/resources/models.py +726 -0
  43. nufi/resources/monitoring.py +82 -0
  44. nufi/resources/projects.py +114 -0
  45. nufi/resources/servings.py +80 -0
  46. nufi/transport.py +248 -0
  47. nufi_python-1.0.0.dist-info/METADATA +228 -0
  48. nufi_python-1.0.0.dist-info/RECORD +52 -0
  49. nufi_python-1.0.0.dist-info/WHEEL +5 -0
  50. nufi_python-1.0.0.dist-info/entry_points.txt +2 -0
  51. nufi_python-1.0.0.dist-info/licenses/LICENSE +21 -0
  52. nufi_python-1.0.0.dist-info/top_level.txt +1 -0
nufi/__init__.py ADDED
@@ -0,0 +1,52 @@
1
+ from importlib.metadata import version
2
+
3
+ from nufi.client import NuFi
4
+ from nufi.models import (
5
+ ArtifactValidation,
6
+ CompileRun,
7
+ EvaluationMethod,
8
+ EvaluationMethodTemplates,
9
+ EvaluationRun,
10
+ HuggingFaceMapping,
11
+ Leaderboard,
12
+ LeaderboardEntry,
13
+ LeaderboardSubmission,
14
+ ModelArtifact,
15
+ ModelArtifactCreate,
16
+ ModelDetail,
17
+ ModelImport,
18
+ ModelVersion,
19
+ Page,
20
+ Project,
21
+ ProjectAccessToken,
22
+ ProjectAccessTokenCreateResponse,
23
+ RegisteredModel,
24
+ Serving,
25
+ )
26
+
27
+ __version__ = version("nufi-python")
28
+
29
+ __all__ = [
30
+ "__version__",
31
+ "ArtifactValidation",
32
+ "CompileRun",
33
+ "EvaluationMethod",
34
+ "EvaluationMethodTemplates",
35
+ "EvaluationRun",
36
+ "HuggingFaceMapping",
37
+ "Leaderboard",
38
+ "LeaderboardEntry",
39
+ "LeaderboardSubmission",
40
+ "ModelArtifact",
41
+ "ModelArtifactCreate",
42
+ "ModelDetail",
43
+ "ModelImport",
44
+ "ModelVersion",
45
+ "NuFi",
46
+ "Page",
47
+ "Project",
48
+ "ProjectAccessToken",
49
+ "ProjectAccessTokenCreateResponse",
50
+ "RegisteredModel",
51
+ "Serving",
52
+ ]
nufi/auth.py ADDED
@@ -0,0 +1,364 @@
1
+ from __future__ import annotations
2
+
3
+ import base64
4
+ import json
5
+ import os
6
+ import time
7
+ import webbrowser
8
+ from collections.abc import Callable
9
+ from pathlib import Path
10
+ from typing import Any
11
+ from urllib.parse import urlparse
12
+
13
+ import httpx
14
+
15
+ from nufi.config import Profile, get_config_dir, is_valid_profile_name
16
+ from nufi.exceptions import AuthenticationError
17
+ from nufi.http_utils import local_dev_url_and_headers
18
+
19
+ OIDC_SCOPE = "openid offline_access"
20
+ DEVICE_CODE_GRANT = "urn:ietf:params:oauth:grant-type:device_code"
21
+ DEFAULT_HTTP_TIMEOUT_SECONDS = 30.0
22
+ DEFAULT_DEVICE_POLL_INTERVAL_SECONDS = 5
23
+ DEVICE_SLOW_DOWN_INCREMENT_SECONDS = 5
24
+
25
+
26
+ def _timestamp(now: int | Callable[[], int] | None = None) -> int:
27
+ if callable(now):
28
+ return int(now())
29
+ if now is not None:
30
+ return int(now)
31
+ return int(time.time())
32
+
33
+
34
+ class TokenStore:
35
+ def __init__(self, token_dir: Path | None = None):
36
+ if token_dir is None:
37
+ token_dir = Path(
38
+ os.environ.get("NUFI_TOKEN_DIR", get_config_dir() / "tokens")
39
+ )
40
+ self.token_dir = Path(token_dir).expanduser()
41
+
42
+ def path_for(self, profile_name: str) -> Path:
43
+ if not is_valid_profile_name(profile_name):
44
+ raise AuthenticationError(
45
+ "Invalid profile name. Use letters, numbers, dot, underscore, or dash."
46
+ )
47
+ path = (self.token_dir / f"{profile_name}.json").resolve()
48
+ token_dir = self.token_dir.resolve()
49
+ if not path.is_relative_to(token_dir):
50
+ raise AuthenticationError("Invalid profile name.")
51
+ return path
52
+
53
+ def save(
54
+ self,
55
+ profile_name: str,
56
+ token_data: dict[str, Any],
57
+ *,
58
+ now: int | Callable[[], int] | None = None,
59
+ ) -> dict[str, Any]:
60
+ tokens = dict(token_data)
61
+ issued_at = _timestamp(now)
62
+ if "expires_in" in tokens:
63
+ tokens["expires_at"] = issued_at + int(tokens["expires_in"])
64
+ if int(tokens.get("refresh_expires_in") or 0) > 0:
65
+ tokens["refresh_expires_at"] = issued_at + int(tokens["refresh_expires_in"])
66
+
67
+ path = self.path_for(profile_name)
68
+ path.parent.mkdir(parents=True, exist_ok=True)
69
+ path.parent.chmod(0o700)
70
+ tmp_path = path.with_name(f".{path.name}.tmp")
71
+ fd = os.open(tmp_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
72
+ with os.fdopen(fd, "w") as file:
73
+ json.dump(tokens, file, indent=2, sort_keys=True)
74
+ os.replace(tmp_path, path)
75
+ path.chmod(0o600)
76
+ return tokens
77
+
78
+ def load(self, profile_name: str) -> dict[str, Any]:
79
+ path = self.path_for(profile_name)
80
+ if not path.exists():
81
+ raise AuthenticationError(
82
+ f"No token found for profile '{profile_name}'. Run 'nufi login'."
83
+ )
84
+ try:
85
+ return json.loads(path.read_text())
86
+ except json.JSONDecodeError as exc:
87
+ raise AuthenticationError(
88
+ f"Token file for profile '{profile_name}' is invalid."
89
+ ) from exc
90
+
91
+ def delete(self, profile_name: str) -> None:
92
+ self.path_for(profile_name).unlink(missing_ok=True)
93
+
94
+
95
+ def _token_endpoint(profile: Profile) -> str:
96
+ return f"{profile.resolved_keycloak_issuer}/protocol/openid-connect/token"
97
+
98
+
99
+ def _device_endpoint(profile: Profile) -> str:
100
+ return f"{profile.resolved_keycloak_issuer}/protocol/openid-connect/auth/device"
101
+
102
+
103
+ def _request_error_message(
104
+ context: str,
105
+ exc: httpx.RequestError,
106
+ *,
107
+ profile: Profile,
108
+ ) -> str:
109
+ issuer = profile.resolved_keycloak_issuer
110
+ host = urlparse(issuer).hostname
111
+ hint = ""
112
+ if host:
113
+ hint = (
114
+ f" Check DNS or /etc/hosts for '{host}', or recreate the profile with "
115
+ "`nufi profile create <name> --keycloak-issuer <url>`."
116
+ )
117
+ return f"{context}: could not reach Keycloak issuer {issuer}.{hint} Original error: {exc}"
118
+
119
+
120
+ def _post_keycloak(endpoint: str, **kwargs: Any) -> httpx.Response:
121
+ url, headers = local_dev_url_and_headers(endpoint, kwargs.pop("headers", None))
122
+ return httpx.post(url, headers=headers, **kwargs)
123
+
124
+
125
+ def _json_payload(response: httpx.Response, *, context: str) -> dict[str, Any]:
126
+ try:
127
+ payload = response.json()
128
+ except ValueError as exc:
129
+ raise AuthenticationError(f"{context}: HTTP {response.status_code}") from exc
130
+ if not isinstance(payload, dict):
131
+ raise AuthenticationError(f"{context}: invalid response.")
132
+ return payload
133
+
134
+
135
+ def _token_error_message(response: httpx.Response, *, context: str) -> str:
136
+ try:
137
+ payload = response.json()
138
+ except ValueError:
139
+ return f"{context}: HTTP {response.status_code}"
140
+ if isinstance(payload, dict):
141
+ message = payload.get("error_description") or payload.get("error")
142
+ if message:
143
+ return f"{context}: {message}"
144
+ return f"{context}: HTTP {response.status_code}"
145
+
146
+
147
+ def _validated_token_response(
148
+ token_data: dict[str, Any],
149
+ *,
150
+ previous_refresh_token: str | None = None,
151
+ ) -> dict[str, Any]:
152
+ access_token = token_data.get("access_token")
153
+ if not isinstance(access_token, str) or not access_token:
154
+ raise AuthenticationError("Token response is missing access_token.")
155
+
156
+ tokens = dict(token_data)
157
+ refresh_token = tokens.get("refresh_token")
158
+ if refresh_token is None and previous_refresh_token:
159
+ tokens["refresh_token"] = previous_refresh_token
160
+ elif refresh_token is not None and not isinstance(refresh_token, str):
161
+ raise AuthenticationError("Token response has invalid refresh_token.")
162
+ return tokens
163
+
164
+
165
+ def _refresh_access_token(
166
+ profile_name: str,
167
+ profile: Profile,
168
+ refresh_token: str,
169
+ *,
170
+ token_store: TokenStore,
171
+ now: int | Callable[[], int] | None = None,
172
+ ) -> dict[str, Any]:
173
+ try:
174
+ response = _post_keycloak(
175
+ _token_endpoint(profile),
176
+ data={
177
+ "client_id": profile.client_id,
178
+ "grant_type": "refresh_token",
179
+ "refresh_token": refresh_token,
180
+ },
181
+ timeout=DEFAULT_HTTP_TIMEOUT_SECONDS,
182
+ )
183
+ except httpx.RequestError as exc:
184
+ raise AuthenticationError(
185
+ _request_error_message("Token refresh failed", exc, profile=profile)
186
+ ) from exc
187
+
188
+ if response.status_code >= 400:
189
+ raise AuthenticationError(
190
+ _token_error_message(response, context="Token refresh failed")
191
+ )
192
+
193
+ tokens = _validated_token_response(
194
+ _json_payload(response, context="Token refresh failed"),
195
+ previous_refresh_token=refresh_token,
196
+ )
197
+ return token_store.save(profile_name, tokens, now=now)
198
+
199
+
200
+ def get_valid_access_token(
201
+ profile_name: str,
202
+ profile: Profile,
203
+ *,
204
+ token_store: TokenStore | None = None,
205
+ now: int | Callable[[], int] | None = None,
206
+ ) -> str:
207
+ token_store = token_store or TokenStore()
208
+ tokens = token_store.load(profile_name)
209
+ current_time = _timestamp(now)
210
+
211
+ if int(tokens.get("expires_at") or 0) > current_time:
212
+ return str(tokens["access_token"])
213
+
214
+ refresh_token = tokens.get("refresh_token")
215
+ if not refresh_token:
216
+ raise AuthenticationError("Access token is expired. Run 'nufi login'.")
217
+ refresh_expires_at = tokens.get("refresh_expires_at")
218
+ if isinstance(refresh_expires_at, int) and refresh_expires_at <= current_time:
219
+ raise AuthenticationError("Refresh token is expired. Run 'nufi login'.")
220
+
221
+ tokens = _refresh_access_token(
222
+ profile_name,
223
+ profile,
224
+ str(refresh_token),
225
+ token_store=token_store,
226
+ now=current_time,
227
+ )
228
+ return str(tokens["access_token"])
229
+
230
+
231
+ def refresh_access_token(
232
+ profile_name: str,
233
+ profile: Profile,
234
+ *,
235
+ token_store: TokenStore | None = None,
236
+ now: int | Callable[[], int] | None = None,
237
+ ) -> str:
238
+ """Refresh a profile access token after the API rejects it."""
239
+ token_store = token_store or TokenStore()
240
+ tokens = token_store.load(profile_name)
241
+ refresh_token = tokens.get("refresh_token")
242
+ if not refresh_token:
243
+ raise AuthenticationError("No refresh token is available. Run 'nufi login'.")
244
+ current_time = _timestamp(now)
245
+ refresh_expires_at = tokens.get("refresh_expires_at")
246
+ if isinstance(refresh_expires_at, int) and refresh_expires_at <= current_time:
247
+ raise AuthenticationError("Refresh token is expired. Run 'nufi login'.")
248
+ refreshed = _refresh_access_token(
249
+ profile_name,
250
+ profile,
251
+ str(refresh_token),
252
+ token_store=token_store,
253
+ now=current_time,
254
+ )
255
+ return str(refreshed["access_token"])
256
+
257
+
258
+ def perform_device_login(
259
+ profile_name: str,
260
+ profile: Profile,
261
+ *,
262
+ token_store: TokenStore | None = None,
263
+ open_browser: bool = True,
264
+ sleep: Callable[[int], None] = time.sleep,
265
+ show_verification: Callable[[dict[str, Any]], None] | None = None,
266
+ now: int | Callable[[], int] | None = None,
267
+ ) -> dict[str, Any]:
268
+ token_store = token_store or TokenStore()
269
+ try:
270
+ device_response = _post_keycloak(
271
+ _device_endpoint(profile),
272
+ data={"client_id": profile.client_id, "scope": OIDC_SCOPE},
273
+ timeout=DEFAULT_HTTP_TIMEOUT_SECONDS,
274
+ )
275
+ except httpx.RequestError as exc:
276
+ raise AuthenticationError(
277
+ _request_error_message(
278
+ "Device authorization failed",
279
+ exc,
280
+ profile=profile,
281
+ )
282
+ ) from exc
283
+
284
+ if device_response.status_code >= 400:
285
+ raise AuthenticationError(
286
+ _token_error_message(device_response, context="Device authorization failed")
287
+ )
288
+
289
+ device_data = _json_payload(device_response, context="Device authorization failed")
290
+ verification_uri = (
291
+ device_data.get("verification_uri_complete")
292
+ or device_data.get("verification_uri")
293
+ or ""
294
+ )
295
+ if show_verification:
296
+ show_verification(device_data)
297
+ if open_browser and verification_uri:
298
+ webbrowser.open(str(verification_uri))
299
+
300
+ interval = int(device_data.get("interval") or DEFAULT_DEVICE_POLL_INTERVAL_SECONDS)
301
+ started_at = _timestamp(now)
302
+ expires_in = int(device_data.get("expires_in", 600))
303
+ deadline = started_at + expires_in if expires_in > 0 else started_at
304
+ device_code = str(device_data["device_code"])
305
+
306
+ while True:
307
+ current_time = _timestamp(now)
308
+ if current_time >= deadline:
309
+ raise AuthenticationError("Device login expired. Run 'nufi login' again.")
310
+ sleep(min(interval, max(1, deadline - current_time)))
311
+ try:
312
+ response = _post_keycloak(
313
+ _token_endpoint(profile),
314
+ data={
315
+ "client_id": profile.client_id,
316
+ "grant_type": DEVICE_CODE_GRANT,
317
+ "device_code": device_code,
318
+ },
319
+ timeout=DEFAULT_HTTP_TIMEOUT_SECONDS,
320
+ )
321
+ except httpx.RequestError as exc:
322
+ raise AuthenticationError(
323
+ _request_error_message("Login failed", exc, profile=profile)
324
+ ) from exc
325
+
326
+ payload = _json_payload(response, context="Login failed")
327
+ if response.status_code == 200:
328
+ return token_store.save(
329
+ profile_name,
330
+ _validated_token_response(payload),
331
+ now=now,
332
+ )
333
+
334
+ error = payload.get("error")
335
+ if error == "authorization_pending":
336
+ continue
337
+ if error == "slow_down":
338
+ interval += DEVICE_SLOW_DOWN_INCREMENT_SECONDS
339
+ continue
340
+ raise AuthenticationError(
341
+ f"Login failed: {payload.get('error_description') or error or response.text}"
342
+ )
343
+
344
+
345
+ def logout(profile_name: str, *, token_store: TokenStore | None = None) -> None:
346
+ (token_store or TokenStore()).delete(profile_name)
347
+
348
+
349
+ def get_token_claims(
350
+ profile_name: str, *, token_store: TokenStore | None = None
351
+ ) -> dict[str, Any]:
352
+ token = (token_store or TokenStore()).load(profile_name).get("access_token")
353
+ if not isinstance(token, str) or token.count(".") < 2:
354
+ raise AuthenticationError("Access token is not a JWT.")
355
+ payload = token.split(".")[1]
356
+ payload += "=" * (-len(payload) % 4)
357
+ try:
358
+ decoded = base64.urlsafe_b64decode(payload.encode())
359
+ claims = json.loads(decoded)
360
+ except (ValueError, json.JSONDecodeError) as exc:
361
+ raise AuthenticationError("Access token claims could not be decoded.") from exc
362
+ if not isinstance(claims, dict):
363
+ raise AuthenticationError("Access token claims are invalid.")
364
+ return claims
nufi/cli/__init__.py ADDED
File without changes
@@ -0,0 +1 @@
1
+ """Typer command modules for the NuFi CLI."""
@@ -0,0 +1,113 @@
1
+ import os
2
+ from uuid import UUID
3
+
4
+ import typer
5
+ from rich.console import Console
6
+
7
+ from nufi import auth, config
8
+ from nufi.auth import perform_device_login
9
+ from nufi.cli.commands import common
10
+ from nufi.exceptions import AuthenticationError, ConfigurationError
11
+
12
+ console = Console()
13
+
14
+
15
+ def _resolve(profile_name: str | None) -> tuple[str, config.Profile]:
16
+ return config.resolve_profile(profile_name)
17
+
18
+
19
+ def login(
20
+ profile_name: str | None = typer.Option(None, "--profile", help="Profile name."),
21
+ open_browser: bool = typer.Option(
22
+ True,
23
+ "--browser/--no-browser",
24
+ help="Open the verification URL in a browser.",
25
+ ),
26
+ ):
27
+ def show_verification(device_data: dict[str, object]) -> None:
28
+ verification_uri = (
29
+ device_data.get("verification_uri_complete")
30
+ or device_data.get("verification_uri")
31
+ or "-"
32
+ )
33
+ user_code = device_data.get("user_code") or "-"
34
+ console.print(f"Open: {verification_uri}")
35
+ console.print(f"Code: {user_code}")
36
+
37
+ try:
38
+ resolved_name, profile = _resolve(profile_name)
39
+ perform_device_login(
40
+ resolved_name,
41
+ profile,
42
+ open_browser=open_browser,
43
+ show_verification=show_verification,
44
+ )
45
+ except (AuthenticationError, ConfigurationError) as exc:
46
+ console.print(f"[bold red]Error:[/] {exc}")
47
+ raise typer.Exit(code=1) from exc
48
+ console.print(f"[bold green]Logged in:[/] {resolved_name}")
49
+
50
+
51
+ def logout(
52
+ profile_name: str | None = typer.Option(None, "--profile", help="Profile name."),
53
+ ):
54
+ try:
55
+ resolved_name, _ = _resolve(profile_name)
56
+ auth.logout(resolved_name)
57
+ except (AuthenticationError, ConfigurationError) as exc:
58
+ console.print(f"[bold red]Error:[/] {exc}")
59
+ raise typer.Exit(code=1) from exc
60
+ console.print(f"[bold green]Logged out:[/] {resolved_name}")
61
+
62
+
63
+ def whoami(
64
+ profile_name: str | None = typer.Option(None, "--profile", help="Profile name."),
65
+ ):
66
+ try:
67
+ resolved_name, _ = _resolve(profile_name)
68
+ if auth.TokenStore().path_for(resolved_name).exists():
69
+ claims = auth.get_token_claims(resolved_name)
70
+ else:
71
+ token = os.environ.get("NUFI_TOKEN", "").strip()
72
+ if not token:
73
+ auth.TokenStore().load(resolved_name)
74
+ client = common.make_client(profile_name)
75
+ try:
76
+ projects = client.projects.list()
77
+ finally:
78
+ client.close()
79
+ if len(projects) != 1:
80
+ raise AuthenticationError(
81
+ "NUFI_TOKEN did not resolve to one bound project."
82
+ )
83
+ prefix, separator, _ = token.partition(".")
84
+ token_id = prefix.removeprefix("nufi_proj_")
85
+ if (
86
+ not separator
87
+ or not prefix.startswith("nufi_proj_")
88
+ or str(UUID(token_id)) != token_id
89
+ ):
90
+ raise AuthenticationError(
91
+ "NUFI_TOKEN is not a NuFi project access token."
92
+ )
93
+ project = projects[0]
94
+ console.print("Authentication: project token")
95
+ console.print(f"Token ID: {token_id}")
96
+ console.print(f"Project: {project.name} ({project.id})")
97
+ return
98
+ except (AuthenticationError, ConfigurationError) as exc:
99
+ console.print(f"[bold red]Error:[/] {exc}")
100
+ raise typer.Exit(code=1) from exc
101
+ except ValueError as exc:
102
+ console.print(
103
+ "[bold red]Error:[/] NUFI_TOKEN is not a NuFi project access token."
104
+ )
105
+ raise typer.Exit(code=1) from exc
106
+ except Exception as exc:
107
+ common.handle_cli_error(exc, verbose=False)
108
+
109
+ username = claims.get("preferred_username") or claims.get("sub") or "-"
110
+ email = claims.get("email") or "-"
111
+ console.print(f"Profile: {resolved_name}")
112
+ console.print(f"Username: {username}")
113
+ console.print(f"Email: {email}")