abidlabs HF Staff commited on
Commit
9ffec07
·
verified ·
1 Parent(s): b444fec

Upload folder using huggingface_hub

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +2 -0
  2. trackio/.DS_Store +0 -0
  3. trackio/CHANGELOG.md +242 -0
  4. trackio/__init__.py +1095 -0
  5. trackio/_vendor/__init__.py +0 -0
  6. trackio/_vendor/gradio_exceptions.py +6 -0
  7. trackio/_vendor/networking.py +81 -0
  8. trackio/_vendor/tunneling.py +191 -0
  9. trackio/alerts.py +184 -0
  10. trackio/api.py +93 -0
  11. trackio/apple_gpu.py +253 -0
  12. trackio/asgi_app.py +483 -0
  13. trackio/assets/badge.png +3 -0
  14. trackio/assets/trackio_logo_dark.png +0 -0
  15. trackio/assets/trackio_logo_light.png +0 -0
  16. trackio/assets/trackio_logo_old.png +3 -0
  17. trackio/assets/trackio_logo_type_dark.png +0 -0
  18. trackio/assets/trackio_logo_type_dark_transparent.png +0 -0
  19. trackio/assets/trackio_logo_type_light.png +0 -0
  20. trackio/assets/trackio_logo_type_light_transparent.png +0 -0
  21. trackio/bucket_storage.py +152 -0
  22. trackio/cli.py +1326 -0
  23. trackio/cli_helpers.py +204 -0
  24. trackio/commit_scheduler.py +310 -0
  25. trackio/context_vars.py +21 -0
  26. trackio/deploy.py +1107 -0
  27. trackio/dummy_commit_scheduler.py +19 -0
  28. trackio/exceptions.py +2 -0
  29. trackio/frontend/dist/assets/index-BgSTPqpC.css +1 -0
  30. trackio/frontend/dist/assets/index-Djj_n7T6.js +0 -0
  31. trackio/frontend/dist/index.html +14 -0
  32. trackio/frontend/eslint.config.js +42 -0
  33. trackio/frontend/index.html +13 -0
  34. trackio/frontend_server.py +64 -0
  35. trackio/gpu.py +381 -0
  36. trackio/histogram.py +71 -0
  37. trackio/imports.py +300 -0
  38. trackio/launch.py +202 -0
  39. trackio/launch_utils.py +33 -0
  40. trackio/markdown.py +21 -0
  41. trackio/mcp_setup.py +156 -0
  42. trackio/media/__init__.py +27 -0
  43. trackio/media/audio.py +167 -0
  44. trackio/media/image.py +84 -0
  45. trackio/media/media.py +79 -0
  46. trackio/media/utils.py +60 -0
  47. trackio/media/video.py +246 -0
  48. trackio/package.json +6 -0
  49. trackio/py.typed +0 -0
  50. trackio/remote_client.py +242 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ trackio/assets/badge.png filter=lfs diff=lfs merge=lfs -text
37
+ trackio/assets/trackio_logo_old.png filter=lfs diff=lfs merge=lfs -text
trackio/.DS_Store ADDED
Binary file (8.2 kB). View file
 
trackio/CHANGELOG.md ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # trackio
2
+
3
+ ## 0.23.0
4
+
5
+ ### Features
6
+
7
+ - [#494](https://github.com/gradio-app/trackio/pull/494) [`e8a897d`](https://github.com/gradio-app/trackio/commit/e8a897d2266d9b2558f72d768b0b21f4d0a8781b) - Add a settings/CLI page to Trackio. Thanks @abidlabs!
8
+ - [#481](https://github.com/gradio-app/trackio/pull/481) [`882647e`](https://github.com/gradio-app/trackio/commit/882647ec1599cf04500d03b5ca75ddc2733682e2) - Add multi-GPU system metrics support. Thanks @Saba9!
9
+ - [#485](https://github.com/gradio-app/trackio/pull/485) [`46a3cc3`](https://github.com/gradio-app/trackio/commit/46a3cc3758719e171417612efee102a487e71ebd) - Fix/remove flaky E2E space tests. Thanks @abidlabs!
10
+ - [#501](https://github.com/gradio-app/trackio/pull/501) [`06ea885`](https://github.com/gradio-app/trackio/commit/06ea8852f5e40ab3f1cf629a0a01af5c17f847a1) - Fix SQLite corruption on bucket-mounted Spaces. Thanks @abidlabs!
11
+ - [#496](https://github.com/gradio-app/trackio/pull/496) [`af23d74`](https://github.com/gradio-app/trackio/commit/af23d74438b146c4a3512ace15ea984656e943ed) - Prevent trackio errors from crashing the user's training loop. Thanks @abidlabs!
12
+
13
+ ## 0.22.0
14
+
15
+ ### Features
16
+
17
+ - [#484](https://github.com/gradio-app/trackio/pull/484) [`cc05ada`](https://github.com/gradio-app/trackio/commit/cc05ada8e89773f3a894af99b801ef680f64418f) - Fix duplicate columns in parquet export. Thanks @abidlabs!
18
+ - [#487](https://github.com/gradio-app/trackio/pull/487) [`853f764`](https://github.com/gradio-app/trackio/commit/853f7646a70d12633afaa4f69db86425aa665413) - Relax `PIL` dependency and remove `plotly` as it's no longer used. Thanks @abidlabs!
19
+
20
+ ## 0.21.2
21
+
22
+ ### Features
23
+
24
+ - [#482](https://github.com/gradio-app/trackio/pull/482) [`f62180a`](https://github.com/gradio-app/trackio/commit/f62180a0218bc99a259d5ca110a0384a6cae11c8) - Use server-side bucket copy when freezing Spaces. Thanks @abidlabs!
25
+
26
+ ## 0.21.1
27
+
28
+ ### Features
29
+
30
+ - [#475](https://github.com/gradio-app/trackio/pull/475) [`fcb476c`](https://github.com/gradio-app/trackio/commit/fcb476cd37a40923e9679aaf966f41d582a878a8) - Tweaks. Thanks @abidlabs!
31
+ - [#477](https://github.com/gradio-app/trackio/pull/477) [`7d52dfd`](https://github.com/gradio-app/trackio/commit/7d52dfdce5b6eff6a34501a6d5a620220663cf09) - Fix `.sync()` and add `.freeze()` as a separate methods. Thanks @abidlabs!
32
+
33
+ ## 0.21.0
34
+
35
+ ### Features
36
+
37
+ - [#467](https://github.com/gradio-app/trackio/pull/467) [`f357deb`](https://github.com/gradio-app/trackio/commit/f357debf78957e4c1f2b901bee4f77cf397298b4) - Allow logged metrics as x-axis choices. Thanks @abidlabs!
38
+ - [#474](https://github.com/gradio-app/trackio/pull/474) [`655673d`](https://github.com/gradio-app/trackio/commit/655673d4c6b7c8b7ee8f87f2589f2dbbc3d2ef91) - Fix file descriptor leak from `sqlite3.connect`. Thanks @abidlabs!
39
+ - [#470](https://github.com/gradio-app/trackio/pull/470) [`bea8c9d`](https://github.com/gradio-app/trackio/commit/bea8c9dcae0b59d071b6c779c97ee525c9bbf6e7) - Restores tooltips to line plots and fixes the call to uses TTL instead of OAuth. Thanks @abidlabs!
40
+ - [#471](https://github.com/gradio-app/trackio/pull/471) [`246fce0`](https://github.com/gradio-app/trackio/commit/246fce0a01619e1c2c538c67b3e460883334d500) - Deprecate dataset backend in favor of buckets. Thanks @abidlabs!
41
+ - [#465](https://github.com/gradio-app/trackio/pull/465) [`3e11174`](https://github.com/gradio-app/trackio/commit/3e1117438bb8168b802245a33059affa558ae519) - Use HF buckets as backend. Thanks @abidlabs!
42
+ - [#469](https://github.com/gradio-app/trackio/pull/469) [`915d170`](https://github.com/gradio-app/trackio/commit/915d17045133172b59195acfdcc70709229668aa) - Make static Spaces work with Buckets and also allow conversion from Gradio SDK to Static Spaces. Thanks @abidlabs!
43
+
44
+ ## 0.20.2
45
+
46
+ ### Features
47
+
48
+ - [#464](https://github.com/gradio-app/trackio/pull/464) [`c89ebb3`](https://github.com/gradio-app/trackio/commit/c89ebb3b50f695bc7f16cbc6f46dce86f79a01e9) - Improve rendering of curves. Thanks @abidlabs!
49
+ - [#462](https://github.com/gradio-app/trackio/pull/462) [`9160b78`](https://github.com/gradio-app/trackio/commit/9160b78ff6f258f0b87a4f34a24e7d0b5dfbf2fb) - Refactor plot title to display only the metric name without the path. Thanks @qgallouedec!
50
+
51
+ ## 0.20.1
52
+
53
+ ### Features
54
+
55
+ - [#454](https://github.com/gradio-app/trackio/pull/454) [`22881db`](https://github.com/gradio-app/trackio/commit/22881dbbbb6b81197a00a19853771007093d61e4) - Bar chart single point. Thanks @abidlabs!
56
+ - [#455](https://github.com/gradio-app/trackio/pull/455) [`f8db51a`](https://github.com/gradio-app/trackio/commit/f8db51a20ca61ef703f3f2c2ee1ebd9c4f239cf2) - Adds a static Trackio mode via `trackio.sync(sdk="static")` and support for the `TRACKIO_SPACE_ID` environment variable. Thanks @abidlabs!
57
+
58
+ ## 0.20.0
59
+
60
+ ### Features
61
+
62
+ - [#450](https://github.com/gradio-app/trackio/pull/450) [`b0571ef`](https://github.com/gradio-app/trackio/commit/b0571ef6207a1ce346696f858ad2b7b584dd194f) - Use Svelte source for Gradio components directly in Trackio dashboard. Thanks @abidlabs!
63
+
64
+ ## 0.19.0
65
+
66
+ ### Features
67
+
68
+ - [#445](https://github.com/gradio-app/trackio/pull/445) [`cef4a58`](https://github.com/gradio-app/trackio/commit/cef4a583cb76f4091fc6c0e5783124ee84f8e243) - Add remote HF Space support to CLI. Thanks @abidlabs!
69
+ - [#444](https://github.com/gradio-app/trackio/pull/444) [`358f2a9`](https://github.com/gradio-app/trackio/commit/358f2a9ca238ee8b90b5a8c96220da287e0698fb) - Fix alerts placeholder flashing on reports page. Thanks @abidlabs!
70
+
71
+ ## 0.18.0
72
+
73
+ ### Features
74
+
75
+ - [#435](https://github.com/gradio-app/trackio/pull/435) [`4a47112`](https://github.com/gradio-app/trackio/commit/4a471128e18a39e45fad48a67fd711c5ae9e4aed) - feat: allow hiding section header accordions. Thanks @Saba9!
76
+ - [#439](https://github.com/gradio-app/trackio/pull/439) [`18e9650`](https://github.com/gradio-app/trackio/commit/18e96503d5a3a7cf926e92782d457e23c19942bd) - Add alerts with webhooks, CLI, and documentation. Thanks @abidlabs!
77
+ - [#438](https://github.com/gradio-app/trackio/pull/438) [`0875ccd`](https://github.com/gradio-app/trackio/commit/0875ccd3d8a41b1376f64030f21cfe8cdcc73b05) - Add "share this view" functionality. Thanks @qgallouedec!
78
+ - [#409](https://github.com/gradio-app/trackio/pull/409) [`9282403`](https://github.com/gradio-app/trackio/commit/9282403d8896d48679b0f888208a7ba5bdd4271a) - Add Apple Silicon GPU and system monitoring support. Thanks @znation!
79
+ - [#434](https://github.com/gradio-app/trackio/pull/434) [`4193223`](https://github.com/gradio-app/trackio/commit/41932230a3a2e1c16405dba08ecba5a42f11d1a8) - fix: table slider crash. Thanks @Saba9!
80
+
81
+ ### Fixes
82
+
83
+ - [#441](https://github.com/gradio-app/trackio/pull/441) [`3a2d11d`](https://github.com/gradio-app/trackio/commit/3a2d11dab0b4b37c925abc30ef84b0e2910321ee) - preserve x-axis step when toggling run checkboxes. Thanks @Saba9!
84
+
85
+ ## 0.17.0
86
+
87
+ ### Features
88
+
89
+ - [#428](https://github.com/gradio-app/trackio/pull/428) [`f7dd1ce`](https://github.com/gradio-app/trackio/commit/f7dd1ce2dc8a1936f9983467fcbcf93bfef01e09) - feat: add ability to rename runs. Thanks @Saba9!
90
+ - [#437](https://github.com/gradio-app/trackio/pull/437) [`2727c0b`](https://github.com/gradio-app/trackio/commit/2727c0b0755f48f7f186162ea45185c98f6b5516) - Add markdown reports across Trackio. Thanks @abidlabs!
91
+ - [#427](https://github.com/gradio-app/trackio/pull/427) [`5aeb9ed`](https://github.com/gradio-app/trackio/commit/5aeb9edcfd2068d309d9d64f172dcbcc327be1ab) - Make Trackio logging much more robust. Thanks @abidlabs!
92
+
93
+ ## 0.16.1
94
+
95
+ ### Features
96
+
97
+ - [#431](https://github.com/gradio-app/trackio/pull/431) [`c7ce55b`](https://github.com/gradio-app/trackio/commit/c7ce55b14dd5eb0c2165fb15df17dd60721c9325) - Lazy load the UI when trackio is imported. Thanks @abidlabs!
98
+
99
+ ## 0.16.0
100
+
101
+ ### Features
102
+
103
+ - [#426](https://github.com/gradio-app/trackio/pull/426) [`ead4dc8`](https://github.com/gradio-app/trackio/commit/ead4dc8e74ee2d8e47d61bca0a7668456acf49be) - Fix redundant double rendering of group checkboxes. Thanks @abidlabs!
104
+ - [#413](https://github.com/gradio-app/trackio/pull/413) [`39c4750`](https://github.com/gradio-app/trackio/commit/39c4750951d554ba6eb4d58847c6bb444b2891a8) - Check `dist-packages` when checking for source installation. Thanks @sergiopaniego!
105
+ - [#423](https://github.com/gradio-app/trackio/pull/423) [`2e52ab3`](https://github.com/gradio-app/trackio/commit/2e52ab303e3041718a6a56fbf84d0848aca9ad67) - Fix legend outline visibility issue. Thanks @Raghunath-Balaji!
106
+ - [#407](https://github.com/gradio-app/trackio/pull/407) [`c8a384d`](https://github.com/gradio-app/trackio/commit/c8a384ddfe5a295cecf862a26178d40e48acb424) - Fix pytests that were failling locally on MacOS. Thanks @abidlabs!
107
+ - [#405](https://github.com/gradio-app/trackio/pull/405) [`35aae4e`](https://github.com/gradio-app/trackio/commit/35aae4e3aa3e2b2888887528478b9dc6a9808bda) - Add conditional padding for HF Space dashboard when not in iframe. Thanks @znation!
108
+
109
+ ## 0.15.0
110
+
111
+ ### Features
112
+
113
+ - [#397](https://github.com/gradio-app/trackio/pull/397) [`6b38ad0`](https://github.com/gradio-app/trackio/commit/6b38ad02e5d73a0df49c4eede7e91331282ece04) - Adds `--host` cli option support. Thanks @abidlabs!
114
+ - [#396](https://github.com/gradio-app/trackio/pull/396) [`4a4d1ab`](https://github.com/gradio-app/trackio/commit/4a4d1ab85e63d923132a3fa7afa5d90e16431bec) - Fix run selection issue. Thanks @abidlabs!
115
+ - [#394](https://github.com/gradio-app/trackio/pull/394) [`c47a3a3`](https://github.com/gradio-app/trackio/commit/c47a3a31f8c4b83bce1aa7fc22eeba3d9021ad3d) - Add wandb-compatible API for trackio. Thanks @abidlabs!
116
+ - [#378](https://github.com/gradio-app/trackio/pull/378) [`b02046a`](https://github.com/gradio-app/trackio/commit/b02046a5b0dad7c9854e099a87f884afba4aecb2) - Add JSON export button for line plots and upgrade gradio dependency. Thanks @JamshedAli18!
117
+
118
+ ## 0.14.2
119
+
120
+ ### Features
121
+
122
+ - [#386](https://github.com/gradio-app/trackio/pull/386) [`f9452cd`](https://github.com/gradio-app/trackio/commit/f9452cdb8f0819368f3610f7ac0ed08957305275) - Fixing some issues related to deployed Trackio Spaces. Thanks @abidlabs!
123
+
124
+ ## 0.14.1
125
+
126
+ ### Features
127
+
128
+ - [#382](https://github.com/gradio-app/trackio/pull/382) [`44fe9bb`](https://github.com/gradio-app/trackio/commit/44fe9bb264fb2aafb0ec302ff15227c045819a2c) - Fix app file path when Trackio is not installed from source. Thanks @abidlabs!
129
+ - [#380](https://github.com/gradio-app/trackio/pull/380) [`c3f4cff`](https://github.com/gradio-app/trackio/commit/c3f4cff74bc5676e812773d8571454894fcdc7cc) - Add CLI commands for querying projects, runs, and metrics. Thanks @abidlabs!
130
+
131
+ ## 0.14.0
132
+
133
+ ### Features
134
+
135
+ - [#377](https://github.com/gradio-app/trackio/pull/377) [`5c5015b`](https://github.com/gradio-app/trackio/commit/5c5015b68c85c5de51111dad983f735c27b9a05f) - fixed wrapping issue in Runs table. Thanks @gaganchapa!
136
+ - [#374](https://github.com/gradio-app/trackio/pull/374) [`388e26b`](https://github.com/gradio-app/trackio/commit/388e26b9e9f24cd7ad203affe9b709be885b3d24) - Save Optimized Parquet files. Thanks @lhoestq!
137
+ - [#371](https://github.com/gradio-app/trackio/pull/371) [`fbace9c`](https://github.com/gradio-app/trackio/commit/fbace9cd7732c166f34d268f54b05bb06846cc5d) - Add GPU metrics logging. Thanks @kashif!
138
+ - [#367](https://github.com/gradio-app/trackio/pull/367) [`862840c`](https://github.com/gradio-app/trackio/commit/862840c13e30fc960cbee5b9eac4d3c25beba9de) - Add option to only show latest run, and fix the double logo issue. Thanks @abidlabs!
139
+
140
+ ## 0.13.1
141
+
142
+ ### Features
143
+
144
+ - [#369](https://github.com/gradio-app/trackio/pull/369) [`767e9fe`](https://github.com/gradio-app/trackio/commit/767e9fe095d7c6ed102016caf927c1517fb8618c) - tiny pr removing unnecessary code. Thanks @abidlabs!
145
+
146
+ ## 0.13.0
147
+
148
+ ### Features
149
+
150
+ - [#358](https://github.com/gradio-app/trackio/pull/358) [`073715d`](https://github.com/gradio-app/trackio/commit/073715d1caf8282f68890117f09c3ac301205312) - Improvements to `trackio.sync()`. Thanks @abidlabs!
151
+
152
+ ## 0.12.0
153
+
154
+ ### Features
155
+
156
+ - [#357](https://github.com/gradio-app/trackio/pull/357) [`02ba815`](https://github.com/gradio-app/trackio/commit/02ba815358060f1966052de051a5bdb09702920e) - Redesign media and tables to show up on separate page. Thanks @abidlabs!
157
+ - [#359](https://github.com/gradio-app/trackio/pull/359) [`08fe9c9`](https://github.com/gradio-app/trackio/commit/08fe9c9ddd7fe99ee811555fdfb62df9ab88e939) - docs: Improve docstrings. Thanks @qgallouedec!
158
+
159
+ ## 0.11.0
160
+
161
+ ### Features
162
+
163
+ - [#355](https://github.com/gradio-app/trackio/pull/355) [`ea51f49`](https://github.com/gradio-app/trackio/commit/ea51f4954922f21be76ef828700420fe9a912c4b) - Color code run checkboxes and match with plot lines. Thanks @abidlabs!
164
+ - [#353](https://github.com/gradio-app/trackio/pull/353) [`8abe691`](https://github.com/gradio-app/trackio/commit/8abe6919aeefe21fc7a23af814883efbb037c21f) - Remove show_api from demo.launch. Thanks @sergiopaniego!
165
+ - [#351](https://github.com/gradio-app/trackio/pull/351) [`8a8957e`](https://github.com/gradio-app/trackio/commit/8a8957e530dd7908d1fef7f2df030303f808101f) - Add `trackio.save()`. Thanks @abidlabs!
166
+
167
+ ## 0.10.0
168
+
169
+ ### Features
170
+
171
+ - [#305](https://github.com/gradio-app/trackio/pull/305) [`e64883a`](https://github.com/gradio-app/trackio/commit/e64883a51f7b8b93f7d48b8afe55acdb62238b71) - bump to gradio 6.0, make `trackio` compatible, and fix related issues. Thanks @abidlabs!
172
+
173
+ ## 0.9.1
174
+
175
+ ### Features
176
+
177
+ - [#344](https://github.com/gradio-app/trackio/pull/344) [`7e01024`](https://github.com/gradio-app/trackio/commit/7e010241d9a34794e0ce0dc19c1a6f0cf94ba856) - Avoid redundant calls to /whoami-v2. Thanks @Wauplin!
178
+
179
+ ## 0.9.0
180
+
181
+ ### Features
182
+
183
+ - [#343](https://github.com/gradio-app/trackio/pull/343) [`51bea30`](https://github.com/gradio-app/trackio/commit/51bea30f2877adff8e6497466d3a799400a0a049) - Sync offline projects to Hugging Face spaces. Thanks @candemircan!
184
+ - [#341](https://github.com/gradio-app/trackio/pull/341) [`4fd841f`](https://github.com/gradio-app/trackio/commit/4fd841fa190e15071b02f6fba7683ef4f393a654) - Adds a basic UI test to `trackio`. Thanks @abidlabs!
185
+ - [#339](https://github.com/gradio-app/trackio/pull/339) [`011d91b`](https://github.com/gradio-app/trackio/commit/011d91bb6ae266516fd250a349285670a8049d05) - Allow customzing the trackio color palette. Thanks @abidlabs!
186
+
187
+ ## 0.8.1
188
+
189
+ ### Features
190
+
191
+ - [#336](https://github.com/gradio-app/trackio/pull/336) [`5f9f51d`](https://github.com/gradio-app/trackio/commit/5f9f51dac8677f240d7c42c3e3b2660a22aee138) - Support a list of `Trackio.Image` in a `trackio.Table` cell. Thanks @abidlabs!
192
+
193
+ ## 0.8.0
194
+
195
+ ### Features
196
+
197
+ - [#331](https://github.com/gradio-app/trackio/pull/331) [`2c02d0f`](https://github.com/gradio-app/trackio/commit/2c02d0fd0a5824160528782402bb0dd4083396d5) - Truncate table string values that are greater than 250 characters (configuirable via env variable). Thanks @abidlabs!
198
+ - [#324](https://github.com/gradio-app/trackio/pull/324) [`50b2122`](https://github.com/gradio-app/trackio/commit/50b2122e7965ac82a72e6cb3b7d048bc10a2a6b1) - Add log y-axis functionality to UI. Thanks @abidlabs!
199
+ - [#326](https://github.com/gradio-app/trackio/pull/326) [`61dc1f4`](https://github.com/gradio-app/trackio/commit/61dc1f40af2f545f8e70395ddf0dbb8aee6b60d5) - Fix: improve table rendering for metrics in Trackio Dashboard. Thanks @vigneshwaran!
200
+ - [#328](https://github.com/gradio-app/trackio/pull/328) [`6857cbb`](https://github.com/gradio-app/trackio/commit/6857cbbe557a59a4642f210ec42566d108294e63) - Support trackio.Table with trackio.Image columns. Thanks @abidlabs!
201
+ - [#323](https://github.com/gradio-app/trackio/pull/323) [`6857cbb`](https://github.com/gradio-app/trackio/commit/6857cbbe557a59a4642f210ec42566d108294e63) - add Trackio client implementations in Go, Rust, and JS. Thanks @vaibhav-research!
202
+
203
+ ## 0.7.0
204
+
205
+ ### Features
206
+
207
+ - [#277](https://github.com/gradio-app/trackio/pull/277) [`db35601`](https://github.com/gradio-app/trackio/commit/db35601b9c023423c4654c9909b8ab73e58737de) - fix: make grouped runs view reflect live updates. Thanks @Saba9!
208
+ - [#320](https://github.com/gradio-app/trackio/pull/320) [`24ae739`](https://github.com/gradio-app/trackio/commit/24ae73969b09fb3126acd2f91647cdfbf8cf72a1) - Add additional query parms for xmin, xmax, and smoothing. Thanks @abidlabs!
209
+ - [#270](https://github.com/gradio-app/trackio/pull/270) [`cd1dfc3`](https://github.com/gradio-app/trackio/commit/cd1dfc3dc641b4499ac6d4a1b066fa8e2b52c57b) - feature: add support for logging audio. Thanks @Saba9!
210
+
211
+ ## 0.6.0
212
+
213
+ ### Features
214
+
215
+ - [#309](https://github.com/gradio-app/trackio/pull/309) [`1df2353`](https://github.com/gradio-app/trackio/commit/1df23534d6c01938c8db9c0f584ffa23e8d6021d) - Add histogram support with wandb-compatible API. Thanks @abidlabs!
216
+ - [#315](https://github.com/gradio-app/trackio/pull/315) [`76ba060`](https://github.com/gradio-app/trackio/commit/76ba06055dc43ca8f03b79f3e72d761949bd19a8) - Add guards to avoid silent fails. Thanks @Xmaster6y!
217
+ - [#313](https://github.com/gradio-app/trackio/pull/313) [`a606b3e`](https://github.com/gradio-app/trackio/commit/a606b3e1c5edf3d4cf9f31bd50605226a5a1c5d0) - No longer prevent certain keys from being used. Instead, dunderify them to prevent collisions with internal usage. Thanks @abidlabs!
218
+ - [#317](https://github.com/gradio-app/trackio/pull/317) [`27370a5`](https://github.com/gradio-app/trackio/commit/27370a595d0dbdf7eebbe7159d2ba778f039da44) - quick fixes for trackio.histogram. Thanks @abidlabs!
219
+ - [#312](https://github.com/gradio-app/trackio/pull/312) [`aa0f3bf`](https://github.com/gradio-app/trackio/commit/aa0f3bf372e7a0dd592a38af699c998363830eeb) - Fix video logging by adding TRACKIO_DIR to allowed_paths. Thanks @abidlabs!
220
+
221
+ ## 0.5.3
222
+
223
+ ### Features
224
+
225
+ - [#300](https://github.com/gradio-app/trackio/pull/300) [`5e4cacf`](https://github.com/gradio-app/trackio/commit/5e4cacf2e7ce527b4ce60de3a5bc05d2c02c77fb) - Adds more environment variables to allow customization of Trackio dashboard. Thanks @abidlabs!
226
+
227
+ ## 0.5.2
228
+
229
+ ### Features
230
+
231
+ - [#293](https://github.com/gradio-app/trackio/pull/293) [`64afc28`](https://github.com/gradio-app/trackio/commit/64afc28d3ea1dfd821472dc6bf0b8ed35a9b74be) - Ensures that the TRACKIO_DIR environment variable is respected. Thanks @abidlabs!
232
+ - [#287](https://github.com/gradio-app/trackio/pull/287) [`cd3e929`](https://github.com/gradio-app/trackio/commit/cd3e9294320949e6b8b829239069a43d5d7ff4c1) - fix(sqlite): unify .sqlite extension, allow export when DBs exist, clean WAL sidecars on import. Thanks @vaibhav-research!
233
+
234
+ ### Fixes
235
+
236
+ - [#291](https://github.com/gradio-app/trackio/pull/291) [`3b5adc3`](https://github.com/gradio-app/trackio/commit/3b5adc3d1f452dbab7a714d235f4974782f93730) - Fix the wheel build. Thanks @pngwn!
237
+
238
+ ## 0.5.1
239
+
240
+ ### Fixes
241
+
242
+ - [#278](https://github.com/gradio-app/trackio/pull/278) [`314c054`](https://github.com/gradio-app/trackio/commit/314c05438007ddfea3383e06fd19143e27468e2d) - Fix row orientation of metrics plots. Thanks @abidlabs!
trackio/__init__.py ADDED
@@ -0,0 +1,1095 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import atexit
2
+ import glob
3
+ import json
4
+ import logging
5
+ import os
6
+ import shutil
7
+ import warnings
8
+ import webbrowser
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ import huggingface_hub
13
+ from gradio_client import handle_file
14
+ from huggingface_hub import SpaceStorage
15
+ from huggingface_hub.errors import LocalTokenNotFoundError
16
+
17
+ from trackio import context_vars, deploy, utils
18
+ from trackio.alerts import AlertLevel
19
+ from trackio.api import Api
20
+ from trackio.apple_gpu import apple_gpu_available
21
+ from trackio.apple_gpu import log_apple_gpu as _log_apple_gpu
22
+ from trackio.deploy import freeze, sync
23
+ from trackio.gpu import gpu_available
24
+ from trackio.gpu import log_gpu as _log_nvidia_gpu
25
+ from trackio.histogram import Histogram
26
+ from trackio.imports import import_csv, import_tf_events
27
+ from trackio.launch import launch_trackio_dashboard
28
+ from trackio.markdown import Markdown
29
+ from trackio.media import (
30
+ TrackioAudio,
31
+ TrackioImage,
32
+ TrackioVideo,
33
+ get_project_media_path,
34
+ )
35
+ from trackio.remote_client import RemoteClient
36
+ from trackio.run import Run
37
+ from trackio.server import TrackioDashboardApp, build_starlette_app_only
38
+ from trackio.sqlite_storage import SQLiteStorage
39
+ from trackio.table import Table
40
+ from trackio.typehints import UploadEntry
41
+ from trackio.utils import TRACKIO_DIR, TRACKIO_LOGO_DIR, _emit_nonfatal_warning
42
+
43
+ logging.getLogger("httpx").setLevel(logging.WARNING)
44
+
45
+ __version__ = json.loads(Path(__file__).parent.joinpath("package.json").read_text())[
46
+ "version"
47
+ ]
48
+
49
+
50
+ class _TupleNoPrint(tuple):
51
+ def __repr__(self) -> str:
52
+ return ""
53
+
54
+
55
+ __all__ = [
56
+ "init",
57
+ "log",
58
+ "log_system",
59
+ "log_gpu",
60
+ "finish",
61
+ "alert",
62
+ "AlertLevel",
63
+ "show",
64
+ "sync",
65
+ "freeze",
66
+ "delete_project",
67
+ "import_csv",
68
+ "import_tf_events",
69
+ "save",
70
+ "Image",
71
+ "Video",
72
+ "Audio",
73
+ "Table",
74
+ "Histogram",
75
+ "Markdown",
76
+ "Api",
77
+ "TRACKIO_LOGO_DIR",
78
+ ]
79
+
80
+ Audio = TrackioAudio
81
+ Image = TrackioImage
82
+ Video = TrackioVideo
83
+
84
+
85
+ config = {}
86
+
87
+ _atexit_registered = False
88
+ _projects_notified_auto_log_hw: set[str] = set()
89
+
90
+
91
+ def _cleanup_current_run():
92
+ run = context_vars.current_run.get()
93
+ if run is not None:
94
+ try:
95
+ run.finish()
96
+ except Exception:
97
+ pass
98
+
99
+
100
+ def _safe_get_runs_for_init(
101
+ project: str,
102
+ space_id: str | None,
103
+ server_base_url: str | None,
104
+ write_token: str | None,
105
+ resume: str,
106
+ remote_client: RemoteClient | None = None,
107
+ check_existing_for_never: bool = False,
108
+ ) -> list[str]:
109
+ if space_id is not None:
110
+ if resume == "never" and not check_existing_for_never:
111
+ return []
112
+ try:
113
+ client = remote_client or RemoteClient(
114
+ space_id,
115
+ hf_token=huggingface_hub.utils.get_token(),
116
+ verbose=False,
117
+ )
118
+ runs = client.predict(project=project, api_name="/get_runs_for_project")
119
+ return runs if isinstance(runs, list) else []
120
+ except Exception as e:
121
+ _emit_nonfatal_warning(
122
+ f"trackio.init() could not inspect existing runs for project '{project}' on Space '{space_id}': {e}. Continuing without resume metadata."
123
+ )
124
+ return []
125
+ if server_base_url is not None:
126
+ if resume == "never" and not check_existing_for_never:
127
+ return []
128
+ try:
129
+ client = remote_client or RemoteClient(
130
+ server_base_url,
131
+ hf_token=None,
132
+ write_token=write_token,
133
+ verbose=False,
134
+ )
135
+ runs = client.predict(project=project, api_name="/get_runs_for_project")
136
+ return runs if isinstance(runs, list) else []
137
+ except Exception as e:
138
+ _emit_nonfatal_warning(
139
+ f"trackio.init() could not inspect existing runs for project '{project}' on self-hosted server '{server_base_url}': {e}. Continuing without resume metadata."
140
+ )
141
+ return []
142
+ try:
143
+ return SQLiteStorage.get_runs(project)
144
+ except Exception as e:
145
+ _emit_nonfatal_warning(
146
+ f"trackio.init() could not inspect existing runs for project '{project}': {e}. Continuing without resume metadata."
147
+ )
148
+ return []
149
+
150
+
151
+ def _safe_get_latest_run_for_init(
152
+ project: str,
153
+ name: str,
154
+ space_id: str | None = None,
155
+ server_base_url: str | None = None,
156
+ write_token: str | None = None,
157
+ remote_client: RemoteClient | None = None,
158
+ ) -> dict | None:
159
+ if space_id is not None:
160
+ try:
161
+ client = remote_client or RemoteClient(
162
+ space_id,
163
+ hf_token=huggingface_hub.utils.get_token(),
164
+ verbose=False,
165
+ )
166
+ runs = client.predict(project=project, api_name="/get_runs_for_project")
167
+ if not isinstance(runs, list):
168
+ return None
169
+ matches = [r for r in runs if isinstance(r, dict) and r.get("name") == name]
170
+ if not matches:
171
+ return None
172
+ matches.sort(key=lambda r: r.get("created_at") or "", reverse=True)
173
+ return matches[0]
174
+ except Exception as e:
175
+ _emit_nonfatal_warning(
176
+ f"trackio.init() could not inspect existing runs for project '{project}' on Space '{space_id}': {e}. Continuing without resume metadata."
177
+ )
178
+ return None
179
+ if server_base_url is not None:
180
+ try:
181
+ client = remote_client or RemoteClient(
182
+ server_base_url,
183
+ hf_token=None,
184
+ write_token=write_token,
185
+ verbose=False,
186
+ )
187
+ runs = client.predict(project=project, api_name="/get_runs_for_project")
188
+ if not isinstance(runs, list):
189
+ return None
190
+ matches = [r for r in runs if isinstance(r, dict) and r.get("name") == name]
191
+ if not matches:
192
+ return None
193
+ matches.sort(key=lambda r: r.get("created_at") or "", reverse=True)
194
+ return matches[0]
195
+ except Exception as e:
196
+ _emit_nonfatal_warning(
197
+ f"trackio.init() could not inspect existing runs for project '{project}' on self-hosted server '{server_base_url}': {e}. Continuing without resume metadata."
198
+ )
199
+ return None
200
+ try:
201
+ return SQLiteStorage.get_latest_run_record_by_name(project, name)
202
+ except Exception as e:
203
+ _emit_nonfatal_warning(
204
+ f"trackio.init() could not inspect existing runs for project '{project}': {e}. Continuing without resume metadata."
205
+ )
206
+ return None
207
+
208
+
209
+ def _safe_get_last_step_for_init(
210
+ project: str,
211
+ run_name: str,
212
+ space_id: str | None,
213
+ server_base_url: str | None,
214
+ write_token: str | None,
215
+ resumed: bool,
216
+ run_id: str | None = None,
217
+ remote_client: RemoteClient | None = None,
218
+ ) -> int | None:
219
+ if not resumed:
220
+ return None
221
+ if space_id is not None:
222
+ try:
223
+ client = remote_client or RemoteClient(
224
+ space_id,
225
+ hf_token=huggingface_hub.utils.get_token(),
226
+ verbose=False,
227
+ )
228
+ summary_kwargs: dict[str, Any] = {
229
+ "project": project,
230
+ "api_name": "/get_run_summary",
231
+ }
232
+ if run_id is not None:
233
+ summary_kwargs["run_id"] = run_id
234
+ else:
235
+ summary_kwargs["run"] = run_name
236
+ summary = client.predict(**summary_kwargs)
237
+ if isinstance(summary, dict):
238
+ last_step = summary.get("last_step")
239
+ return last_step if isinstance(last_step, int) else None
240
+ return None
241
+ except Exception as e:
242
+ _emit_nonfatal_warning(
243
+ f"trackio.init() could not recover the previous step for run '{run_name}' on Space '{space_id}': {e}. Continuing from step 0."
244
+ )
245
+ return None
246
+ if server_base_url is not None:
247
+ try:
248
+ client = remote_client or RemoteClient(
249
+ server_base_url,
250
+ hf_token=None,
251
+ write_token=write_token,
252
+ verbose=False,
253
+ )
254
+ summary_kwargs = {
255
+ "project": project,
256
+ "api_name": "/get_run_summary",
257
+ }
258
+ if run_id is not None:
259
+ summary_kwargs["run_id"] = run_id
260
+ else:
261
+ summary_kwargs["run"] = run_name
262
+ summary = client.predict(**summary_kwargs)
263
+ if isinstance(summary, dict):
264
+ last_step = summary.get("last_step")
265
+ return last_step if isinstance(last_step, int) else None
266
+ return None
267
+ except Exception as e:
268
+ _emit_nonfatal_warning(
269
+ f"trackio.init() could not recover the previous step for run '{run_name}' on self-hosted server '{server_base_url}': {e}. Continuing from step 0."
270
+ )
271
+ return None
272
+ try:
273
+ return SQLiteStorage.get_max_step_for_run(project, run_name, run_id=run_id)
274
+ except Exception as e:
275
+ _emit_nonfatal_warning(
276
+ f"trackio.init() could not recover the previous step for run '{run_name}': {e}. Continuing from step 0."
277
+ )
278
+ return None
279
+
280
+
281
+ def init(
282
+ project: str,
283
+ name: str | None = None,
284
+ group: str | None = None,
285
+ space_id: str | None = None,
286
+ server_url: str | None = None,
287
+ space_storage: SpaceStorage | None = None,
288
+ dataset_id: str | None = None,
289
+ bucket_id: str | None = None,
290
+ config: dict | None = None,
291
+ resume: str = "never",
292
+ settings: Any = None,
293
+ private: bool | None = None,
294
+ embed: bool = True,
295
+ auto_log_gpu: bool | None = None,
296
+ gpu_log_interval: float = 10.0,
297
+ webhook_url: str | None = None,
298
+ webhook_min_level: AlertLevel | str | None = None,
299
+ ) -> Run:
300
+ """
301
+ Creates a new Trackio project and returns a [`Run`] object.
302
+
303
+ Args:
304
+ project (`str`):
305
+ The name of the project (can be an existing project to continue tracking or
306
+ a new project to start tracking from scratch).
307
+ name (`str`, *optional*):
308
+ The name of the run (if not provided, a default name will be generated).
309
+ group (`str`, *optional*):
310
+ The name of the group which this run belongs to in order to help organize
311
+ related runs together. You can toggle the entire group's visibilitiy in the
312
+ dashboard.
313
+ space_id (`str`, *optional*):
314
+ If provided, the project will be logged to a Hugging Face Space instead of
315
+ a local directory. Should be a complete Space name like
316
+ `"username/reponame"` or `"orgname/reponame"`, or just `"reponame"` in which
317
+ case the Space will be created in the currently-logged-in Hugging Face
318
+ user's namespace. If the Space does not exist, it will be created. If the
319
+ Space already exists, the project will be logged to it. Can also be set
320
+ via the `TRACKIO_SPACE_ID` environment variable. You cannot log to a
321
+ Space that has been **frozen** (converted to the static SDK); use
322
+ ``trackio.sync(..., sdk="static")`` only after you are done logging.
323
+ Takes precedence over `server_url` and `TRACKIO_SERVER_URL` when more than
324
+ one is set.
325
+ server_url (`str`, *optional*):
326
+ Base URL of a self-hosted Trackio server (``http://`` or ``https://``), or the
327
+ write-access URL from ``trackio.show()`` which may include a ``write_token`` query
328
+ parameter. The client sends that token on each request (``X-Trackio-Write-Token``);
329
+ you can also set ``TRACKIO_WRITE_TOKEN`` instead of embedding the token in the URL.
330
+ When set, metrics are sent to that server over HTTP instead of creating or syncing
331
+ to a Hugging Face Space. Can also be set via the ``TRACKIO_SERVER_URL`` environment
332
+ variable. Ignored when ``space_id`` or ``TRACKIO_SPACE_ID`` is set.
333
+ space_storage ([`~huggingface_hub.SpaceStorage`], *optional*):
334
+ Choice of persistent storage tier.
335
+ dataset_id (`str`, *optional*):
336
+ Deprecated. Use `bucket_id` instead.
337
+ bucket_id (`str`, *optional*):
338
+ The ID of the Hugging Face Bucket to use for metric persistence. By default,
339
+ when a `space_id` is provided and `bucket_id` is not explicitly set, a
340
+ bucket is auto-generated from the space_id. Buckets provide
341
+ S3-like storage without git overhead - the SQLite database is stored directly
342
+ via `hf-mount` in the Space. Specify a Bucket with name like
343
+ `"username/bucketname"` or just `"bucketname"`.
344
+ config (`dict`, *optional*):
345
+ A dictionary of configuration options. Provided for compatibility with
346
+ `wandb.init()`.
347
+ resume (`str`, *optional*, defaults to `"never"`):
348
+ Controls how to handle resuming a run. Can be one of:
349
+
350
+ - `"must"`: Must resume the run with the given name, raises error if run
351
+ doesn't exist
352
+ - `"allow"`: Resume the run if it exists, otherwise create a new run
353
+ - `"never"`: Never resume a run, always create a new one
354
+ private (`bool`, *optional*):
355
+ Whether to make the Space private. If None (default), the repo will be
356
+ public unless the organization's default is private. This value is ignored
357
+ if the repo already exists.
358
+ settings (`Any`, *optional*):
359
+ Not used. Provided for compatibility with `wandb.init()`.
360
+ embed (`bool`, *optional*, defaults to `True`):
361
+ If running inside a Jupyter/Colab notebook, whether the dashboard should
362
+ automatically be embedded in the cell when trackio.init() is called. For
363
+ local runs, this launches a local Trackio dashboard and embeds it. For Space runs,
364
+ this embeds the Space URL. In Colab, the local dashboard will be accessible
365
+ via a public share URL when `share=True`.
366
+ auto_log_gpu (`bool` or `None`, *optional*, defaults to `None`):
367
+ Controls automatic GPU metrics logging. If `None` (default), GPU logging
368
+ is automatically enabled when `nvidia-ml-py` is installed and an NVIDIA
369
+ GPU or Apple M series is detected. Set to `True` to force enable or
370
+ `False` to disable.
371
+ gpu_log_interval (`float`, *optional*, defaults to `10.0`):
372
+ The interval in seconds between automatic GPU metric logs.
373
+ Only used when `auto_log_gpu=True`.
374
+ webhook_url (`str`, *optional*):
375
+ A webhook URL to POST alert payloads to when `trackio.alert()` is
376
+ called. Supports Slack and Discord webhook URLs natively (payloads
377
+ are formatted automatically). Can also be set via the
378
+ `TRACKIO_WEBHOOK_URL` environment variable. Individual alerts can
379
+ override this URL by passing `webhook_url` to `trackio.alert()`.
380
+ webhook_min_level (`AlertLevel` or `str`, *optional*):
381
+ Minimum alert level that should trigger webhook delivery.
382
+ For example, `AlertLevel.WARN` sends only `WARN` and `ERROR`
383
+ alerts to the webhook destination. Can also be set via
384
+ `TRACKIO_WEBHOOK_MIN_LEVEL`.
385
+ Returns:
386
+ `Run`: A [`Run`] object that can be used to log metrics and finish the run.
387
+ """
388
+ if settings is not None:
389
+ _emit_nonfatal_warning(
390
+ "* Warning: settings is not used. Provided for compatibility with wandb.init(). Please create an issue at: https://github.com/gradio-app/trackio/issues if you need a specific feature implemented."
391
+ )
392
+
393
+ bucket_id_was_explicit = bucket_id is not None
394
+ space_id, server_url = utils.resolve_space_id_and_server_url(space_id, server_url)
395
+ if bucket_id is None and utils.on_spaces():
396
+ bucket_id = os.environ.get("TRACKIO_BUCKET_ID")
397
+ if server_url is not None and not server_url.startswith(("http://", "https://")):
398
+ raise ValueError(
399
+ f"`server_url` must be a full URL starting with http:// or https://, got: {server_url!r}"
400
+ )
401
+ server_base_url: str | None = None
402
+ write_token_resolved: str | None = None
403
+ if server_url is not None:
404
+ server_base_url, tok = utils.parse_trackio_server_url(server_url)
405
+ write_token_resolved = tok or os.environ.get("TRACKIO_WRITE_TOKEN")
406
+ if not write_token_resolved:
407
+ raise ValueError(
408
+ "Self-hosted logging requires a write token: add write_token to the server URL, "
409
+ "or set the TRACKIO_WRITE_TOKEN environment variable."
410
+ )
411
+ if server_url is not None and (dataset_id is not None or bucket_id is not None):
412
+ raise ValueError(
413
+ "`dataset_id` and `bucket_id` are Hugging Face Spaces concepts and are not "
414
+ "compatible with `server_url`. Configure storage on the self-hosted server."
415
+ )
416
+ if space_id is None and dataset_id is not None:
417
+ raise ValueError("Must provide a `space_id` when `dataset_id` is provided.")
418
+ if dataset_id is not None and bucket_id is not None:
419
+ raise ValueError("Cannot provide both `dataset_id` and `bucket_id`.")
420
+ try:
421
+ space_id, dataset_id, bucket_id = utils.preprocess_space_and_dataset_ids(
422
+ space_id, dataset_id, bucket_id
423
+ )
424
+ if (
425
+ space_id is not None
426
+ and dataset_id is None
427
+ and bucket_id is not None
428
+ and not bucket_id_was_explicit
429
+ and not utils.on_spaces()
430
+ ):
431
+ bucket_id = deploy.resolve_auto_bucket_id(space_id, bucket_id)
432
+ except LocalTokenNotFoundError as e:
433
+ raise LocalTokenNotFoundError(
434
+ f"You must be logged in to Hugging Face locally when `space_id` is provided to deploy to a Space. {e}"
435
+ ) from e
436
+
437
+ if space_id is None and bucket_id is not None:
438
+ _emit_nonfatal_warning(
439
+ "trackio.init() has `bucket_id` set but `space_id` is None: metrics will be logged "
440
+ "locally only. Pass `space_id` to create or use a Hugging Face Space, which will be "
441
+ "attached to the Hugging Face Bucket.",
442
+ UserWarning,
443
+ stacklevel=2,
444
+ )
445
+
446
+ if space_id is not None:
447
+ deploy.raise_if_space_is_frozen_for_logging(space_id)
448
+
449
+ remote_source = space_id or server_base_url
450
+
451
+ url = context_vars.current_server.get()
452
+
453
+ if remote_source is not None:
454
+ if url is None:
455
+ url = remote_source
456
+ context_vars.current_server.set(url)
457
+ if space_id is not None:
458
+ context_vars.current_space_id.set(space_id)
459
+ context_vars.current_server_write_token.set(None)
460
+ elif server_base_url is not None:
461
+ context_vars.current_server_write_token.set(write_token_resolved)
462
+
463
+ _should_embed_local = False
464
+
465
+ if (
466
+ context_vars.current_project.get() is None
467
+ or context_vars.current_project.get() != project
468
+ ):
469
+ print(f"* Trackio project initialized: {project}")
470
+
471
+ if bucket_id is not None:
472
+ if utils.on_spaces():
473
+ os.environ["TRACKIO_BUCKET_ID"] = bucket_id
474
+ bucket_url = f"https://huggingface.co/buckets/{bucket_id}"
475
+ print(
476
+ f"* Trackio metrics will be synced to Hugging Face Bucket: {bucket_url}"
477
+ )
478
+ elif dataset_id is not None:
479
+ if utils.on_spaces():
480
+ os.environ["TRACKIO_DATASET_ID"] = dataset_id
481
+ print(
482
+ f"* Trackio metrics will be synced to Hugging Face Dataset: {dataset_id}"
483
+ )
484
+ if remote_source is None:
485
+ print(f"* Trackio metrics logged to: {TRACKIO_DIR}")
486
+ _should_embed_local = embed and utils.is_in_notebook()
487
+ if not _should_embed_local:
488
+ utils.print_dashboard_instructions(project)
489
+ elif server_base_url is not None:
490
+ print(
491
+ f"* Trackio metrics will be sent to self-hosted server: {server_base_url}"
492
+ )
493
+ if utils.is_in_notebook() and embed:
494
+ utils.embed_url_in_notebook(server_base_url)
495
+ else:
496
+ try:
497
+ deploy.create_space_if_not_exists(
498
+ space_id,
499
+ space_storage,
500
+ dataset_id,
501
+ bucket_id,
502
+ private,
503
+ )
504
+ user_name, space_name = space_id.split("/")
505
+ space_url = deploy.SPACE_HOST_URL.format(
506
+ user_name=user_name, space_name=space_name
507
+ )
508
+ if utils.is_in_notebook() and embed:
509
+ utils.embed_url_in_notebook(space_url)
510
+ except Exception as e:
511
+ _emit_nonfatal_warning(
512
+ f"trackio.init() could not prepare Space '{space_id}': {e}. Logging will continue in local fallback mode until the Space is reachable."
513
+ )
514
+ context_vars.current_project.set(project)
515
+
516
+ remote_client = None
517
+ if space_id is not None:
518
+ try:
519
+ remote_client = RemoteClient(
520
+ space_id,
521
+ hf_token=huggingface_hub.utils.get_token(),
522
+ verbose=False,
523
+ )
524
+ except Exception as e:
525
+ _emit_nonfatal_warning(
526
+ f"trackio.init() could not create a remote client for Space '{space_id}': {e}. Continuing with local fallback metadata lookups."
527
+ )
528
+ elif server_base_url is not None:
529
+ try:
530
+ remote_client = RemoteClient(
531
+ server_base_url,
532
+ hf_token=None,
533
+ write_token=write_token_resolved,
534
+ verbose=False,
535
+ )
536
+ except Exception as e:
537
+ _emit_nonfatal_warning(
538
+ f"trackio.init() could not create a remote client for '{server_base_url}': {e}. Continuing with local fallback metadata lookups."
539
+ )
540
+
541
+ existing_run_records = _safe_get_runs_for_init(
542
+ project,
543
+ space_id,
544
+ server_base_url,
545
+ write_token_resolved,
546
+ resume,
547
+ remote_client=remote_client,
548
+ check_existing_for_never=name is not None,
549
+ )
550
+ existing_runs = [
551
+ r["name"] if isinstance(r, dict) else r for r in existing_run_records
552
+ ]
553
+
554
+ existing_run = (
555
+ _safe_get_latest_run_for_init(
556
+ project,
557
+ name,
558
+ space_id=space_id,
559
+ server_base_url=server_base_url,
560
+ write_token=write_token_resolved,
561
+ remote_client=remote_client,
562
+ )
563
+ if name is not None
564
+ else None
565
+ )
566
+ resolved_run_id = None
567
+
568
+ if resume == "must":
569
+ if name is None:
570
+ raise ValueError("Must provide a run name when resume='must'")
571
+ if existing_run is None:
572
+ raise ValueError(f"Run '{name}' does not exist in project '{project}'")
573
+ resumed = True
574
+ resolved_run_id = existing_run["id"]
575
+ elif resume == "allow":
576
+ resumed = existing_run is not None
577
+ if resumed:
578
+ resolved_run_id = existing_run["id"]
579
+ elif resume == "never":
580
+ resumed = False
581
+ else:
582
+ raise ValueError("resume must be one of: 'must', 'allow', or 'never'")
583
+
584
+ initial_last_step = (
585
+ _safe_get_last_step_for_init(
586
+ project,
587
+ name,
588
+ space_id,
589
+ server_base_url,
590
+ write_token_resolved,
591
+ resumed,
592
+ run_id=resolved_run_id,
593
+ remote_client=remote_client,
594
+ )
595
+ if name is not None
596
+ else None
597
+ )
598
+
599
+ if auto_log_gpu is None:
600
+ nvidia_available = gpu_available()
601
+ apple_available = apple_gpu_available()
602
+ auto_log_gpu = nvidia_available or apple_available
603
+ if project not in _projects_notified_auto_log_hw:
604
+ if nvidia_available:
605
+ print("* NVIDIA GPU detected, enabling automatic GPU metrics logging")
606
+ elif apple_available:
607
+ print(
608
+ "* Apple Silicon detected, enabling automatic system metrics logging"
609
+ )
610
+ if nvidia_available or apple_available:
611
+ _projects_notified_auto_log_hw.add(project)
612
+
613
+ run = Run(
614
+ url=url,
615
+ project=project,
616
+ client=None,
617
+ name=name,
618
+ run_id=resolved_run_id,
619
+ group=group,
620
+ config=config,
621
+ space_id=space_id,
622
+ server_base_url=server_base_url,
623
+ write_token=write_token_resolved,
624
+ existing_runs=existing_runs,
625
+ initial_last_step=initial_last_step,
626
+ auto_log_gpu=auto_log_gpu,
627
+ gpu_log_interval=gpu_log_interval,
628
+ webhook_url=webhook_url,
629
+ webhook_min_level=webhook_min_level,
630
+ )
631
+
632
+ if space_id is not None:
633
+ try:
634
+ SQLiteStorage.set_project_metadata(project, "space_id", space_id)
635
+ except Exception as e:
636
+ _emit_nonfatal_warning(
637
+ f"trackio.init() could not persist Space metadata for project '{project}': {e}. Logging will continue."
638
+ )
639
+ try:
640
+ if SQLiteStorage.has_pending_data(project):
641
+ run._has_local_buffer = True
642
+ except Exception as e:
643
+ _emit_nonfatal_warning(
644
+ f"trackio.init() could not inspect pending buffered data for project '{project}': {e}. Logging will continue."
645
+ )
646
+
647
+ global _atexit_registered
648
+ if not _atexit_registered:
649
+ atexit.register(_cleanup_current_run)
650
+ _atexit_registered = True
651
+
652
+ if resumed:
653
+ print(f"* Resumed existing run: {run.name}")
654
+ else:
655
+ print(f"* Created new run: {run.name}")
656
+
657
+ context_vars.current_run.set(run)
658
+ globals()["config"] = run.config
659
+
660
+ if _should_embed_local:
661
+ try:
662
+ show(project=project, open_browser=False, block_thread=False)
663
+ except Exception as e:
664
+ _emit_nonfatal_warning(
665
+ f"trackio.init() could not auto-launch the dashboard: {e}. Logging will continue."
666
+ )
667
+
668
+ return run
669
+
670
+
671
+ def log(metrics: dict, step: int | None = None) -> None:
672
+ """
673
+ Logs metrics to the current run.
674
+
675
+ Args:
676
+ metrics (`dict`):
677
+ A dictionary of metrics to log.
678
+ step (`int`, *optional*):
679
+ The step number. If not provided, the step will be incremented
680
+ automatically.
681
+ """
682
+ run = context_vars.current_run.get()
683
+ if run is None:
684
+ raise RuntimeError("Call trackio.init() before trackio.log().")
685
+ run.log(
686
+ metrics=metrics,
687
+ step=step,
688
+ )
689
+
690
+
691
+ def log_system(metrics: dict) -> None:
692
+ """
693
+ Logs system metrics (GPU, etc.) to the current run using timestamps instead of steps.
694
+
695
+ Args:
696
+ metrics (`dict`):
697
+ A dictionary of system metrics to log.
698
+ """
699
+ run = context_vars.current_run.get()
700
+ if run is None:
701
+ raise RuntimeError("Call trackio.init() before trackio.log_system().")
702
+ run.log_system(metrics=metrics)
703
+
704
+
705
+ def log_gpu(run: Run | None = None, device: int | None = None) -> dict:
706
+ """
707
+ Log GPU metrics to the current or specified run as system metrics.
708
+ Automatically detects whether an NVIDIA or Apple GPU is available and calls
709
+ the appropriate logging method.
710
+
711
+ Args:
712
+ run: Optional Run instance. If None, uses current run from context.
713
+ device: CUDA device index to collect metrics from (NVIDIA GPUs only).
714
+ If None, collects from all GPUs visible to this process.
715
+ This parameter is ignored for Apple GPUs.
716
+
717
+ Returns:
718
+ dict: The GPU metrics that were logged.
719
+
720
+ Example:
721
+ ```python
722
+ import trackio
723
+
724
+ run = trackio.init(project="my-project")
725
+ trackio.log({"loss": 0.5})
726
+ trackio.log_gpu()
727
+ trackio.log_gpu(device=0)
728
+ ```
729
+ """
730
+ if run is None:
731
+ run = context_vars.current_run.get()
732
+ if run is None:
733
+ raise RuntimeError("Call trackio.init() before trackio.log_gpu().")
734
+
735
+ if gpu_available():
736
+ return _log_nvidia_gpu(run=run, device=device)
737
+ elif apple_gpu_available():
738
+ return _log_apple_gpu(run=run)
739
+ else:
740
+ _emit_nonfatal_warning(
741
+ "No GPU detected. Install nvidia-ml-py for NVIDIA GPU support "
742
+ "or psutil for Apple Silicon support."
743
+ )
744
+ return {}
745
+
746
+
747
+ def finish():
748
+ """
749
+ Finishes the current run.
750
+ """
751
+ run = context_vars.current_run.get()
752
+ if run is None:
753
+ raise RuntimeError("Call trackio.init() before trackio.finish().")
754
+ try:
755
+ run.finish()
756
+ finally:
757
+ context_vars.current_run.set(None)
758
+
759
+
760
+ def alert(
761
+ title: str,
762
+ text: str | None = None,
763
+ level: AlertLevel = AlertLevel.WARN,
764
+ webhook_url: str | None = None,
765
+ ) -> None:
766
+ """
767
+ Fires an alert immediately on the current run. The alert is printed to the
768
+ terminal, stored in the database, and displayed in the dashboard. If a
769
+ webhook URL is configured (via `trackio.init()`, the `TRACKIO_WEBHOOK_URL`
770
+ environment variable, or the `webhook_url` parameter here), the alert is
771
+ also POSTed to that URL.
772
+
773
+ Args:
774
+ title (`str`):
775
+ A short title for the alert.
776
+ text (`str`, *optional*):
777
+ A longer description with details about the alert.
778
+ level (`AlertLevel`, *optional*, defaults to `AlertLevel.WARN`):
779
+ The severity level. One of `AlertLevel.INFO`, `AlertLevel.WARN`,
780
+ or `AlertLevel.ERROR`.
781
+ webhook_url (`str`, *optional*):
782
+ A webhook URL to send this specific alert to. Overrides any
783
+ URL set in `trackio.init()` or the `TRACKIO_WEBHOOK_URL`
784
+ environment variable. Supports Slack and Discord webhook
785
+ URLs natively.
786
+ """
787
+ run = context_vars.current_run.get()
788
+ if run is None:
789
+ raise RuntimeError("Call trackio.init() before trackio.alert().")
790
+ run.alert(title=title, text=text, level=level, webhook_url=webhook_url)
791
+
792
+
793
+ def delete_project(project: str, force: bool = False) -> bool:
794
+ """
795
+ Deletes a project by removing its local SQLite database.
796
+
797
+ Args:
798
+ project (`str`):
799
+ The name of the project to delete.
800
+ force (`bool`, *optional*, defaults to `False`):
801
+ If `True`, deletes the project without prompting for confirmation.
802
+ If `False`, prompts the user to confirm before deleting.
803
+
804
+ Returns:
805
+ `bool`: `True` if the project was deleted, `False` otherwise.
806
+ """
807
+ db_path = SQLiteStorage.get_project_db_path(project)
808
+
809
+ if not db_path.exists():
810
+ print(f"* Project '{project}' does not exist.")
811
+ return False
812
+
813
+ if not force:
814
+ response = input(
815
+ f"Are you sure you want to delete project '{project}'? "
816
+ f"This will permanently delete all runs and metrics. (y/N): "
817
+ )
818
+ if response.lower() not in ["y", "yes"]:
819
+ print("* Deletion cancelled.")
820
+ return False
821
+
822
+ try:
823
+ db_path.unlink()
824
+
825
+ for suffix in ("-wal", "-shm"):
826
+ sidecar = Path(str(db_path) + suffix)
827
+ if sidecar.exists():
828
+ sidecar.unlink()
829
+
830
+ print(f"* Project '{project}' has been deleted.")
831
+ return True
832
+ except Exception as e:
833
+ print(f"* Error deleting project '{project}': {e}")
834
+ return False
835
+
836
+
837
+ def save(
838
+ glob_str: str | Path,
839
+ project: str | None = None,
840
+ ) -> str:
841
+ """
842
+ Saves files to a project (not linked to a specific run). If Trackio is running
843
+ locally, the file(s) will be copied to the project's files directory. If Trackio is
844
+ running in a Space, the file(s) will be uploaded to the Space's files directory.
845
+
846
+ Args:
847
+ glob_str (`str` or `Path`):
848
+ The file path or glob pattern to save. Can be a single file or a pattern
849
+ matching multiple files (e.g., `"*.py"`, `"models/**/*.pth"`).
850
+ project (`str`, *optional*):
851
+ The name of the project to save files to. If not provided, uses the current
852
+ project from `trackio.init()`. If no project is initialized, raises an
853
+ error.
854
+
855
+ Returns:
856
+ `str`: The path where the file(s) were saved (project's files directory).
857
+
858
+ Example:
859
+ ```python
860
+ import trackio
861
+
862
+ trackio.init(project="my-project")
863
+ trackio.save("config.yaml")
864
+ trackio.save("models/*.pth")
865
+ ```
866
+ """
867
+ if project is None:
868
+ project = context_vars.current_project.get()
869
+ if project is None:
870
+ raise RuntimeError(
871
+ "No project specified. Either call trackio.init() first or provide a "
872
+ "project parameter to trackio.save()."
873
+ )
874
+
875
+ glob_str = Path(glob_str)
876
+ base_path = Path.cwd().resolve()
877
+
878
+ matched_files = []
879
+ if glob_str.is_file():
880
+ matched_files = [glob_str.resolve()]
881
+ else:
882
+ pattern = str(glob_str)
883
+ if not glob_str.is_absolute():
884
+ pattern = str((Path.cwd() / glob_str).resolve())
885
+ matched_files = [
886
+ Path(f).resolve()
887
+ for f in glob.glob(pattern, recursive=True)
888
+ if Path(f).is_file()
889
+ ]
890
+
891
+ if not matched_files:
892
+ raise ValueError(f"No files found matching pattern: {glob_str}")
893
+
894
+ current_run = context_vars.current_run.get()
895
+ is_local = (
896
+ current_run._is_local
897
+ if current_run is not None
898
+ else (
899
+ context_vars.current_space_id.get() is None
900
+ and context_vars.current_server.get() is None
901
+ )
902
+ )
903
+
904
+ if is_local:
905
+ for file_path in matched_files:
906
+ try:
907
+ relative_to_base = file_path.relative_to(base_path)
908
+ except ValueError:
909
+ relative_to_base = Path(file_path.name)
910
+
911
+ if current_run is not None:
912
+ current_run._queue_upload(
913
+ file_path,
914
+ step=None,
915
+ relative_path=str(relative_to_base.parent),
916
+ use_run_name=False,
917
+ )
918
+ else:
919
+ media_path = get_project_media_path(
920
+ project=project,
921
+ run=None,
922
+ step=None,
923
+ relative_path=str(relative_to_base),
924
+ )
925
+ shutil.copy(str(file_path), str(media_path))
926
+ else:
927
+ url = context_vars.current_server.get()
928
+
929
+ upload_entries = []
930
+ for file_path in matched_files:
931
+ try:
932
+ relative_to_base = file_path.relative_to(base_path)
933
+ except ValueError:
934
+ relative_to_base = Path(file_path.name)
935
+
936
+ if current_run is not None:
937
+ current_run._queue_upload(
938
+ file_path,
939
+ step=None,
940
+ relative_path=str(relative_to_base.parent),
941
+ use_run_name=False,
942
+ )
943
+ else:
944
+ upload_entry: UploadEntry = {
945
+ "project": project,
946
+ "run": None,
947
+ "step": None,
948
+ "relative_path": str(relative_to_base),
949
+ "uploaded_file": handle_file(file_path),
950
+ }
951
+ upload_entries.append(upload_entry)
952
+
953
+ if upload_entries:
954
+ if url is None:
955
+ raise RuntimeError(
956
+ "No server available. Call trackio.init() before trackio.save() to start the server."
957
+ )
958
+
959
+ try:
960
+ wt = context_vars.current_server_write_token.get()
961
+ if wt is not None:
962
+ client = RemoteClient(
963
+ url,
964
+ hf_token=None,
965
+ write_token=wt,
966
+ httpx_kwargs={"timeout": 90},
967
+ )
968
+ else:
969
+ client = RemoteClient(
970
+ url,
971
+ hf_token=huggingface_hub.utils.get_token(),
972
+ httpx_kwargs={"timeout": 90},
973
+ )
974
+ client.predict(
975
+ api_name="/bulk_upload_media",
976
+ uploads=upload_entries,
977
+ hf_token=huggingface_hub.utils.get_token() if wt is None else None,
978
+ )
979
+ except Exception as e:
980
+ _emit_nonfatal_warning(
981
+ f"Failed to upload files: {e}. "
982
+ "Files may not be available in the dashboard."
983
+ )
984
+
985
+ return str(utils.MEDIA_DIR / project / "files")
986
+
987
+
988
+ def show(
989
+ project: str | None = None,
990
+ *,
991
+ theme: Any = None,
992
+ mcp_server: bool | None = None,
993
+ footer: bool = True,
994
+ color_palette: list[str] | None = None,
995
+ open_browser: bool = True,
996
+ block_thread: bool | None = None,
997
+ host: str | None = None,
998
+ share: bool | None = None,
999
+ server_port: int | None = None,
1000
+ ):
1001
+ """
1002
+ Launches the Trackio dashboard.
1003
+
1004
+ Args:
1005
+ project (`str`, *optional*):
1006
+ The name of the project whose runs to show. If not provided, all projects
1007
+ will be shown and the user can select one.
1008
+ theme (`Any`, *optional*):
1009
+ Ignored. Kept for backward compatibility; Trackio no longer uses Gradio themes.
1010
+ mcp_server (`bool`, *optional*):
1011
+ If `True`, the dashboard exposes an MCP server at `/mcp` when the optional
1012
+ `trackio[mcp]` dependency is installed. If `None` (default), the
1013
+ `GRADIO_MCP_SERVER` environment variable is used (e.g. on Spaces).
1014
+ footer (`bool`, *optional*, defaults to `True`):
1015
+ Whether to include `footer=false` in the write-token URL when `False`.
1016
+ This can also be controlled via the `footer` query parameter in the URL.
1017
+ color_palette (`list[str]`, *optional*):
1018
+ A list of hex color codes to use for plot lines. If not provided, the
1019
+ `TRACKIO_COLOR_PALETTE` environment variable will be used (comma-separated
1020
+ hex codes), or if that is not set, the default color palette will be used.
1021
+ Example: `['#FF0000', '#00FF00', '#0000FF']`
1022
+ open_browser (`bool`, *optional*, defaults to `True`):
1023
+ If `True` and not in a notebook, a new browser tab will be opened with the
1024
+ dashboard. If `False`, the browser will not be opened.
1025
+ block_thread (`bool`, *optional*):
1026
+ If `True`, the main thread will be blocked until the dashboard is closed.
1027
+ If `None` (default behavior), then the main thread will not be blocked if the
1028
+ dashboard is launched in a notebook, otherwise the main thread will be blocked.
1029
+ host (`str`, *optional*):
1030
+ The host to bind the server to. If not provided, defaults to `'127.0.0.1'`
1031
+ (localhost only). Set to `'0.0.0.0'` to allow remote access.
1032
+ share (`bool`, *optional*):
1033
+ If `True`, creates a temporary public URL (Gradio-compatible tunnel). On Colab
1034
+ or hosted notebooks, defaults to `True` unless overridden.
1035
+ server_port (`int`, *optional*):
1036
+ Port to bind. If not set, scans from `GRADIO_SERVER_PORT` (default 7860).
1037
+
1038
+ Returns:
1039
+ `app`: The dashboard handle (`.close()` stops the server).
1040
+ `url`: The local URL of the dashboard.
1041
+ `share_url`: The public share URL, if any.
1042
+ `full_url`: The full URL including the write token (share URL when sharing, else local).
1043
+ """
1044
+ if theme is not None and theme != "default":
1045
+ warnings.warn(
1046
+ "The theme argument is ignored; Trackio no longer depends on Gradio themes.",
1047
+ UserWarning,
1048
+ stacklevel=2,
1049
+ )
1050
+
1051
+ if color_palette is not None:
1052
+ os.environ["TRACKIO_COLOR_PALETTE"] = ",".join(color_palette)
1053
+
1054
+ _mcp_server = (
1055
+ mcp_server
1056
+ if mcp_server is not None
1057
+ else os.environ.get("GRADIO_MCP_SERVER", "False") == "True"
1058
+ )
1059
+
1060
+ starlette_app, wt = build_starlette_app_only(mcp_server=_mcp_server)
1061
+ local_url, share_url, _local_api_url, uv_server = launch_trackio_dashboard(
1062
+ starlette_app,
1063
+ server_name=host,
1064
+ server_port=server_port,
1065
+ share=share,
1066
+ mcp_server=_mcp_server,
1067
+ quiet=True,
1068
+ )
1069
+ server = TrackioDashboardApp(starlette_app, uv_server, wt)
1070
+
1071
+ base_root = (share_url or local_url).rstrip("/")
1072
+ base_url = base_root + "/"
1073
+ dashboard_url = base_url
1074
+ if project:
1075
+ dashboard_url += f"?project={project}"
1076
+ full_url = utils.get_full_url(
1077
+ base_root,
1078
+ project=project,
1079
+ write_token=wt,
1080
+ footer=footer,
1081
+ )
1082
+
1083
+ if not utils.is_in_notebook():
1084
+ print(f"\033[1m\033[38;5;208m* Trackio UI launched at: {dashboard_url}\033[0m")
1085
+ utils.print_write_token_instructions(full_url)
1086
+ if open_browser:
1087
+ webbrowser.open(full_url)
1088
+ block_thread = block_thread if block_thread is not None else True
1089
+ else:
1090
+ utils.embed_url_in_notebook(full_url)
1091
+ block_thread = block_thread if block_thread is not None else False
1092
+
1093
+ if block_thread:
1094
+ utils.block_main_thread_until_keyboard_interrupt()
1095
+ return _TupleNoPrint((server, local_url, share_url, full_url))
trackio/_vendor/__init__.py ADDED
File without changes
trackio/_vendor/gradio_exceptions.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ class ChecksumMismatchError(Exception):
2
+ pass
3
+
4
+
5
+ class ShareCertificateWriteError(Exception):
6
+ pass
trackio/_vendor/networking.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import time
5
+ import warnings
6
+ from pathlib import Path
7
+ from urllib.parse import urlparse, urlunparse
8
+
9
+ import httpx
10
+
11
+ from trackio._vendor.gradio_exceptions import ShareCertificateWriteError
12
+ from trackio._vendor.tunneling import CERTIFICATE_PATH, Tunnel
13
+
14
+ GRADIO_API_SERVER = "https://api.gradio.app/v3/tunnel-request"
15
+ GRADIO_SHARE_SERVER_ADDRESS = os.getenv("GRADIO_SHARE_SERVER_ADDRESS")
16
+
17
+
18
+ def setup_tunnel(
19
+ local_host: str,
20
+ local_port: int,
21
+ share_token: str,
22
+ share_server_address: str | None,
23
+ share_server_tls_certificate: str | None,
24
+ ) -> str:
25
+ share_server_address = (
26
+ GRADIO_SHARE_SERVER_ADDRESS
27
+ if share_server_address is None
28
+ else share_server_address
29
+ )
30
+ if share_server_address is None:
31
+ try:
32
+ response = httpx.get(GRADIO_API_SERVER, timeout=30)
33
+ payload = response.json()[0]
34
+ remote_host, remote_port = payload["host"], int(payload["port"])
35
+ certificate = payload["root_ca"]
36
+ except Exception as e:
37
+ raise RuntimeError(
38
+ "Could not get share link from Gradio API Server."
39
+ ) from e
40
+ try:
41
+ Path(CERTIFICATE_PATH).parent.mkdir(parents=True, exist_ok=True)
42
+ with open(CERTIFICATE_PATH, "w") as f:
43
+ f.write(certificate)
44
+ except Exception as e:
45
+ raise ShareCertificateWriteError(
46
+ f"{e}. This can happen if the current working directory is read-only."
47
+ ) from e
48
+ share_server_tls_certificate = CERTIFICATE_PATH
49
+
50
+ else:
51
+ remote_host, remote_port = share_server_address.split(":")
52
+ remote_port = int(remote_port)
53
+ tunnel = Tunnel(
54
+ remote_host,
55
+ remote_port,
56
+ local_host,
57
+ local_port,
58
+ share_token,
59
+ share_server_tls_certificate,
60
+ )
61
+ address = tunnel.start_tunnel()
62
+ return address
63
+
64
+
65
+ def url_ok(url: str) -> bool:
66
+ try:
67
+ for _ in range(5):
68
+ with warnings.catch_warnings():
69
+ warnings.filterwarnings("ignore")
70
+ r = httpx.head(url, timeout=3, verify=False)
71
+ if r.status_code in (200, 401, 302, 303, 307):
72
+ return True
73
+ time.sleep(0.500)
74
+ except (ConnectionError, httpx.ConnectError, httpx.TimeoutException):
75
+ return False
76
+ return False
77
+
78
+
79
+ def normalize_share_url(share_url: str, share_server_protocol: str) -> str:
80
+ parsed_url = urlparse(share_url)
81
+ return urlunparse((share_server_protocol,) + parsed_url[1:])
trackio/_vendor/tunneling.py ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import atexit
2
+ import hashlib
3
+ import os
4
+ import platform
5
+ import re
6
+ import stat
7
+ import subprocess
8
+ import sys
9
+ import time
10
+ from pathlib import Path
11
+
12
+ import httpx
13
+ from huggingface_hub.constants import HF_HOME
14
+
15
+ from trackio._vendor.gradio_exceptions import ChecksumMismatchError
16
+
17
+ VERSION = "0.3"
18
+ CURRENT_TUNNELS: list["Tunnel"] = []
19
+
20
+ machine = platform.machine()
21
+ if machine == "x86_64":
22
+ machine = "amd64"
23
+ elif machine == "aarch64":
24
+ machine = "arm64"
25
+
26
+ BINARY_REMOTE_NAME = f"frpc_{platform.system().lower()}_{machine.lower()}"
27
+ EXTENSION = ".exe" if os.name == "nt" else ""
28
+ BINARY_URL = f"https://cdn-media.huggingface.co/frpc-gradio-{VERSION}/{BINARY_REMOTE_NAME}{EXTENSION}"
29
+
30
+ CHECKSUMS = {
31
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_windows_amd64.exe": "14bc0ea470be5d67d79a07412bd21de8a0a179c6ac1116d7764f68e942dc9ceb",
32
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_amd64": "c791d1f047b41ff5885772fc4bf20b797c6059bbd82abb9e31de15e55d6a57c4",
33
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_arm64": "823ced25104de6dc3c9f4798dbb43f20e681207279e6ab89c40e2176ccbf70cd",
34
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_darwin_amd64": "930f8face3365810ce16689da81b7d1941fda4466225a7bbcbced9a2916a6e15",
35
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_darwin_arm64": "dfac50c690aca459ed5158fad8bfbe99f9282baf4166cf7c410a6673fbc1f327",
36
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_arm": "4b563beb2e36c448cc688174e20b53af38dc1ff2b5e362d4ddd1401f2affbfb7",
37
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_freebsd_386": "cb0a56c764ecf96dd54ed601d240c564f060ee4e58202d65ffca17c1a51ce19c",
38
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_freebsd_amd64": "516d9e6903513869a011ddcd1ec206167ad1eb5dd6640d21057acc258edecbbb",
39
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_386": "4c2f2a48cd71571498c0ac8a4d42a055f22cb7f14b4b5a2b0d584220fd60a283",
40
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_mips": "b309ecd594d4f0f7f33e556a80d4b67aef9319c00a8334648a618e56b23cb9e0",
41
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_mips64": "0372ef5505baa6f3b64c6295a86541b24b7b0dbe4ef28b344992e21f47624b7b",
42
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_riscv64": "1658eed7e8c14ea76e1d95749d58441ce24147c3d559381832c725c29cfc3df3",
43
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_mipsle": "a2aaba16961d3372b79bd7a28976fcd0f0bbaebc2b50d5a7a71af2240747960f",
44
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_windows_386.exe": "721b90550195a83e15f2176d8f85a48d5a25822757cb872e9723d4bccc4e5bb6",
45
+ "https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_linux_mips64le": "796481edd609f31962b45cc0ab4c9798d040205ae3bf354ed1b72fb432d796b8",
46
+ }
47
+
48
+ CHUNK_SIZE = 128
49
+
50
+ BINARY_FILENAME = f"{BINARY_REMOTE_NAME}_v{VERSION}"
51
+ BINARY_FOLDER = Path(HF_HOME) / "trackio" / "frpc"
52
+ BINARY_PATH = str(BINARY_FOLDER / BINARY_FILENAME)
53
+
54
+ TUNNEL_TIMEOUT_SECONDS = 30
55
+ TUNNEL_ERROR_MESSAGE = (
56
+ "Could not create share URL. "
57
+ "Please check the appended log from frpc for more information:"
58
+ )
59
+
60
+ CERTIFICATE_PATH = ".trackio/certificate.pem"
61
+
62
+
63
+ class Tunnel:
64
+ def __init__(
65
+ self,
66
+ remote_host: str,
67
+ remote_port: int,
68
+ local_host: str,
69
+ local_port: int,
70
+ share_token: str,
71
+ share_server_tls_certificate: str | None,
72
+ ):
73
+ self.proc = None
74
+ self.url = None
75
+ self.remote_host = remote_host
76
+ self.remote_port = remote_port
77
+ self.local_host = local_host
78
+ self.local_port = local_port
79
+ self.share_token = share_token
80
+ self.share_server_tls_certificate = share_server_tls_certificate
81
+
82
+ @staticmethod
83
+ def download_binary():
84
+ if not Path(BINARY_PATH).exists():
85
+ Path(BINARY_FOLDER).mkdir(parents=True, exist_ok=True)
86
+ resp = httpx.get(BINARY_URL, timeout=30)
87
+
88
+ if resp.status_code == 403:
89
+ raise OSError(
90
+ f"Cannot set up a share link as this platform is incompatible. Please "
91
+ f"create a GitHub issue with information about your platform: {platform.uname()}"
92
+ )
93
+
94
+ resp.raise_for_status()
95
+
96
+ with open(BINARY_PATH, "wb") as file:
97
+ file.write(resp.content)
98
+ st = os.stat(BINARY_PATH)
99
+ os.chmod(BINARY_PATH, st.st_mode | stat.S_IEXEC)
100
+
101
+ if BINARY_URL in CHECKSUMS:
102
+ sha = hashlib.sha256()
103
+ with open(BINARY_PATH, "rb") as f:
104
+ for chunk in iter(lambda: f.read(CHUNK_SIZE * sha.block_size), b""):
105
+ sha.update(chunk)
106
+ calculated_hash = sha.hexdigest()
107
+
108
+ if calculated_hash != CHECKSUMS[BINARY_URL]:
109
+ raise ChecksumMismatchError()
110
+
111
+ def start_tunnel(self) -> str:
112
+ self.download_binary()
113
+ self.url = self._start_tunnel(BINARY_PATH)
114
+ return self.url
115
+
116
+ def kill(self):
117
+ if self.proc is not None:
118
+ print(f"Killing tunnel {self.local_host}:{self.local_port} <> {self.url}")
119
+ self.proc.terminate()
120
+ self.proc = None
121
+
122
+ def _start_tunnel(self, binary: str) -> str:
123
+ CURRENT_TUNNELS.append(self)
124
+ command = [
125
+ binary,
126
+ "http",
127
+ "-n",
128
+ self.share_token,
129
+ "-l",
130
+ str(self.local_port),
131
+ "-i",
132
+ self.local_host,
133
+ "--uc",
134
+ "--sd",
135
+ "random",
136
+ "--ue",
137
+ "--server_addr",
138
+ f"{self.remote_host}:{self.remote_port}",
139
+ "--disable_log_color",
140
+ ]
141
+ if self.share_server_tls_certificate is not None:
142
+ command.extend(
143
+ [
144
+ "--tls_enable",
145
+ "--tls_trusted_ca_file",
146
+ self.share_server_tls_certificate,
147
+ ]
148
+ )
149
+ self.proc = subprocess.Popen(
150
+ command, stdout=subprocess.PIPE, stderr=subprocess.PIPE
151
+ )
152
+ atexit.register(self.kill)
153
+ return self._read_url_from_tunnel_stream()
154
+
155
+ def _read_url_from_tunnel_stream(self) -> str:
156
+ start_timestamp = time.time()
157
+
158
+ log = []
159
+ url = ""
160
+
161
+ def _raise_tunnel_error():
162
+ log_text = "\n".join(log)
163
+ print(log_text, file=sys.stderr)
164
+ raise ValueError(f"{TUNNEL_ERROR_MESSAGE}\n{log_text}")
165
+
166
+ while url == "":
167
+ if time.time() - start_timestamp >= TUNNEL_TIMEOUT_SECONDS:
168
+ _raise_tunnel_error()
169
+
170
+ assert self.proc is not None # noqa: S101
171
+ if self.proc.stdout is None:
172
+ continue
173
+
174
+ line = self.proc.stdout.readline()
175
+ line = line.decode("utf-8")
176
+
177
+ if line == "":
178
+ continue
179
+
180
+ log.append(line.strip())
181
+
182
+ if "start proxy success" in line:
183
+ result = re.search("start proxy success: (.+)\n", line)
184
+ if result is None:
185
+ _raise_tunnel_error()
186
+ else:
187
+ url = result.group(1)
188
+ elif "login to server failed" in line:
189
+ _raise_tunnel_error()
190
+
191
+ return url
trackio/alerts.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import logging
3
+ import ssl
4
+ import urllib.request
5
+ from enum import Enum
6
+
7
+ try:
8
+ import certifi
9
+
10
+ _SSL_CONTEXT = ssl.create_default_context(cafile=certifi.where())
11
+ except ImportError:
12
+ _SSL_CONTEXT = None
13
+
14
+ logger = logging.getLogger(__name__)
15
+
16
+
17
+ class AlertLevel(str, Enum):
18
+ INFO = "info"
19
+ WARN = "warn"
20
+ ERROR = "error"
21
+
22
+
23
+ ALERT_LEVEL_ORDER = {
24
+ AlertLevel.INFO: 0,
25
+ AlertLevel.WARN: 1,
26
+ AlertLevel.ERROR: 2,
27
+ }
28
+
29
+ ALERT_COLORS = {
30
+ AlertLevel.INFO: "\033[94m",
31
+ AlertLevel.WARN: "\033[93m",
32
+ AlertLevel.ERROR: "\033[91m",
33
+ }
34
+ RESET_COLOR = "\033[0m"
35
+
36
+ LEVEL_EMOJI = {
37
+ AlertLevel.INFO: "ℹ️",
38
+ AlertLevel.WARN: "⚠️",
39
+ AlertLevel.ERROR: "🚨",
40
+ }
41
+
42
+
43
+ def format_alert_terminal(
44
+ level: AlertLevel, title: str, text: str | None, step: int | None
45
+ ) -> str:
46
+ color = ALERT_COLORS.get(level, "")
47
+ step_str = f" (step {step})" if step is not None else ""
48
+ if text:
49
+ return f"{color}[TRACKIO {level.value.upper()}]{RESET_COLOR} {title}: {text}{step_str}"
50
+ return f"{color}[TRACKIO {level.value.upper()}]{RESET_COLOR} {title}{step_str}"
51
+
52
+
53
+ def _is_slack_url(url: str) -> bool:
54
+ return "hooks.slack.com" in url
55
+
56
+
57
+ def _is_discord_url(url: str) -> bool:
58
+ return "discord.com/api/webhooks" in url or "discordapp.com/api/webhooks" in url
59
+
60
+
61
+ def _build_slack_payload(
62
+ level: AlertLevel,
63
+ title: str,
64
+ text: str | None,
65
+ project: str,
66
+ run: str,
67
+ step: int | None,
68
+ ) -> dict:
69
+ emoji = LEVEL_EMOJI.get(level, "")
70
+ step_str = f" • Step {step}" if step is not None else ""
71
+ header = f"{emoji} *[{level.value.upper()}] {title}*"
72
+ context = f"Project: {project} • Run: {run}{step_str}"
73
+ blocks = [
74
+ {"type": "section", "text": {"type": "mrkdwn", "text": header}},
75
+ ]
76
+ if text:
77
+ blocks.append({"type": "section", "text": {"type": "mrkdwn", "text": text}})
78
+ blocks.append(
79
+ {"type": "context", "elements": [{"type": "mrkdwn", "text": context}]}
80
+ )
81
+ return {"blocks": blocks}
82
+
83
+
84
+ def _build_discord_payload(
85
+ level: AlertLevel,
86
+ title: str,
87
+ text: str | None,
88
+ project: str,
89
+ run: str,
90
+ step: int | None,
91
+ ) -> dict:
92
+ color_map = {
93
+ AlertLevel.INFO: 3447003,
94
+ AlertLevel.WARN: 16776960,
95
+ AlertLevel.ERROR: 15158332,
96
+ }
97
+ emoji = LEVEL_EMOJI.get(level, "")
98
+ step_str = f" • Step {step}" if step is not None else ""
99
+ embed = {
100
+ "title": f"{emoji} [{level.value.upper()}] {title}",
101
+ "color": color_map.get(level, 0),
102
+ "footer": {"text": f"Project: {project} • Run: {run}{step_str}"},
103
+ }
104
+ if text:
105
+ embed["description"] = text
106
+ return {"embeds": [embed]}
107
+
108
+
109
+ def _build_generic_payload(
110
+ level: AlertLevel,
111
+ title: str,
112
+ text: str | None,
113
+ project: str,
114
+ run: str,
115
+ step: int | None,
116
+ timestamp: str | None,
117
+ ) -> dict:
118
+ return {
119
+ "level": level.value,
120
+ "title": title,
121
+ "text": text,
122
+ "project": project,
123
+ "run": run,
124
+ "step": step,
125
+ "timestamp": timestamp,
126
+ }
127
+
128
+
129
+ def parse_alert_level(level: AlertLevel | str) -> AlertLevel:
130
+ if isinstance(level, AlertLevel):
131
+ return level
132
+ normalized = level.lower().strip()
133
+ try:
134
+ return AlertLevel(normalized)
135
+ except ValueError as e:
136
+ allowed = ", ".join(lvl.value for lvl in AlertLevel)
137
+ raise ValueError(
138
+ f"Invalid alert level '{level}'. Expected one of: {allowed}."
139
+ ) from e
140
+
141
+
142
+ def resolve_webhook_min_level(
143
+ webhook_min_level: AlertLevel | str | None,
144
+ ) -> AlertLevel | None:
145
+ if webhook_min_level is None:
146
+ return None
147
+ return parse_alert_level(webhook_min_level)
148
+
149
+
150
+ def should_send_webhook(
151
+ level: AlertLevel, webhook_min_level: AlertLevel | None
152
+ ) -> bool:
153
+ if webhook_min_level is None:
154
+ return True
155
+ return ALERT_LEVEL_ORDER[level] >= ALERT_LEVEL_ORDER[webhook_min_level]
156
+
157
+
158
+ def send_webhook(
159
+ url: str,
160
+ level: AlertLevel,
161
+ title: str,
162
+ text: str | None,
163
+ project: str,
164
+ run: str,
165
+ step: int | None,
166
+ timestamp: str | None = None,
167
+ ) -> None:
168
+ if _is_slack_url(url):
169
+ payload = _build_slack_payload(level, title, text, project, run, step)
170
+ elif _is_discord_url(url):
171
+ payload = _build_discord_payload(level, title, text, project, run, step)
172
+ else:
173
+ payload = _build_generic_payload(
174
+ level, title, text, project, run, step, timestamp
175
+ )
176
+
177
+ data = json.dumps(payload).encode("utf-8")
178
+ req = urllib.request.Request(
179
+ url, data=data, headers={"Content-Type": "application/json"}
180
+ )
181
+ try:
182
+ urllib.request.urlopen(req, timeout=10, context=_SSL_CONTEXT)
183
+ except Exception as e:
184
+ logger.warning(f"Failed to send webhook to {url}: {e}")
trackio/api.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Iterator
2
+
3
+ from trackio.sqlite_storage import SQLiteStorage
4
+
5
+
6
+ class Run:
7
+ def __init__(self, project: str, name: str, run_id: str | None = None):
8
+ self.project = project
9
+ self.name = name
10
+ self._id = run_id or name
11
+ self._config = None
12
+
13
+ @property
14
+ def id(self) -> str:
15
+ return self._id
16
+
17
+ @property
18
+ def config(self) -> dict | None:
19
+ if self._config is None:
20
+ self._config = SQLiteStorage.get_run_config(
21
+ self.project, self.name, run_id=self.id
22
+ )
23
+ return self._config
24
+
25
+ def alerts(self, level: str | None = None, since: str | None = None) -> list[dict]:
26
+ return SQLiteStorage.get_alerts(
27
+ self.project, run_name=self.name, run_id=self.id, level=level, since=since
28
+ )
29
+
30
+ def delete(self) -> bool:
31
+ return SQLiteStorage.delete_run(self.project, self.name, run_id=self.id)
32
+
33
+ def move(self, new_project: str) -> bool:
34
+ success = SQLiteStorage.move_run(self.project, self.name, new_project)
35
+ if success:
36
+ self.project = new_project
37
+ return success
38
+
39
+ def rename(self, new_name: str) -> "Run":
40
+ SQLiteStorage.rename_run(self.project, self.name, new_name)
41
+ self.name = new_name
42
+ return self
43
+
44
+ def __repr__(self) -> str:
45
+ return f"<Run {self.name} in project {self.project}>"
46
+
47
+
48
+ class Runs:
49
+ def __init__(self, project: str):
50
+ self.project = project
51
+ self._runs = None
52
+
53
+ def _load_runs(self):
54
+ if self._runs is None:
55
+ records = SQLiteStorage.get_run_records(self.project)
56
+ self._runs = [
57
+ Run(self.project, str(record["name"]), run_id=str(record["id"]))
58
+ for record in records
59
+ ]
60
+
61
+ def __iter__(self) -> Iterator[Run]:
62
+ self._load_runs()
63
+ return iter(self._runs)
64
+
65
+ def __getitem__(self, index: int) -> Run:
66
+ self._load_runs()
67
+ return self._runs[index]
68
+
69
+ def __len__(self) -> int:
70
+ self._load_runs()
71
+ return len(self._runs)
72
+
73
+ def __repr__(self) -> str:
74
+ self._load_runs()
75
+ return f"<Runs project={self.project} count={len(self._runs)}>"
76
+
77
+
78
+ class Api:
79
+ def runs(self, project: str) -> Runs:
80
+ if not SQLiteStorage.get_project_db_path(project).exists():
81
+ raise ValueError(f"Project '{project}' does not exist")
82
+ return Runs(project)
83
+
84
+ def alerts(
85
+ self,
86
+ project: str,
87
+ run: str | None = None,
88
+ level: str | None = None,
89
+ since: str | None = None,
90
+ ) -> list[dict]:
91
+ if not SQLiteStorage.get_project_db_path(project).exists():
92
+ raise ValueError(f"Project '{project}' does not exist")
93
+ return SQLiteStorage.get_alerts(project, run_name=run, level=level, since=since)
trackio/apple_gpu.py ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import platform
2
+ import subprocess
3
+ import sys
4
+ import threading
5
+ import warnings
6
+ from typing import TYPE_CHECKING, Any
7
+
8
+ if TYPE_CHECKING:
9
+ from trackio.run import Run
10
+
11
+ psutil: Any = None
12
+ PSUTIL_AVAILABLE = False
13
+ _monitor_lock = threading.Lock()
14
+
15
+
16
+ def _ensure_psutil():
17
+ global PSUTIL_AVAILABLE, psutil
18
+ if PSUTIL_AVAILABLE:
19
+ return psutil
20
+ try:
21
+ import psutil as _psutil
22
+
23
+ psutil = _psutil
24
+ PSUTIL_AVAILABLE = True
25
+ return psutil
26
+ except ImportError:
27
+ raise ImportError(
28
+ "psutil is required for Apple Silicon monitoring. "
29
+ "Install it with: pip install psutil"
30
+ )
31
+
32
+
33
+ def is_apple_silicon() -> bool:
34
+ """Check if running on Apple Silicon (M1/M2/M3/M4)."""
35
+ if platform.system() != "Darwin":
36
+ return False
37
+
38
+ try:
39
+ result = subprocess.run(
40
+ ["sysctl", "-n", "machdep.cpu.brand_string"],
41
+ capture_output=True,
42
+ text=True,
43
+ timeout=1,
44
+ )
45
+ cpu_brand = result.stdout.strip()
46
+ return "Apple" in cpu_brand
47
+ except Exception:
48
+ return False
49
+
50
+
51
+ def get_gpu_info() -> dict[str, Any]:
52
+ """Get Apple GPU information using ioreg."""
53
+ try:
54
+ result = subprocess.run(
55
+ ["ioreg", "-r", "-d", "1", "-w", "0", "-c", "IOAccelerator"],
56
+ capture_output=True,
57
+ text=True,
58
+ timeout=2,
59
+ )
60
+
61
+ if result.returncode == 0 and result.stdout:
62
+ lines = result.stdout.strip().split("\n")
63
+ for line in lines:
64
+ if "IOAccelerator" in line and "class" in line:
65
+ return {"detected": True, "type": "Apple GPU"}
66
+ else:
67
+ print("Error collecting Apple GPU info. ioreg stdout was:", file=sys.stderr)
68
+ print(result.stdout, file=sys.stderr)
69
+ print("ioreg stderr was:", file=sys.stderr)
70
+ print(result.stderr, file=sys.stderr)
71
+
72
+ result = subprocess.run(
73
+ ["system_profiler", "SPDisplaysDataType"],
74
+ capture_output=True,
75
+ text=True,
76
+ timeout=3,
77
+ )
78
+
79
+ if result.returncode == 0 and "Apple" in result.stdout:
80
+ for line in result.stdout.split("\n"):
81
+ if "Chipset Model:" in line:
82
+ model = line.split(":")[-1].strip()
83
+ return {"detected": True, "type": model}
84
+
85
+ except Exception:
86
+ pass
87
+
88
+ return {"detected": False}
89
+
90
+
91
+ def apple_gpu_available() -> bool:
92
+ """
93
+ Check if Apple GPU monitoring is available.
94
+
95
+ Returns True if running on Apple Silicon (M-series chips) and psutil is installed.
96
+ """
97
+ try:
98
+ _ensure_psutil()
99
+ return is_apple_silicon()
100
+ except ImportError:
101
+ return False
102
+ except Exception:
103
+ return False
104
+
105
+
106
+ def collect_apple_metrics() -> dict:
107
+ """
108
+ Collect system metrics for Apple Silicon.
109
+
110
+ Returns:
111
+ Dictionary of system metrics including CPU, memory, and GPU info.
112
+ """
113
+ if not PSUTIL_AVAILABLE:
114
+ try:
115
+ _ensure_psutil()
116
+ except ImportError:
117
+ return {}
118
+
119
+ metrics = {}
120
+
121
+ try:
122
+ cpu_percent = psutil.cpu_percent(interval=0.1, percpu=False)
123
+ metrics["cpu/utilization"] = cpu_percent
124
+ except Exception:
125
+ pass
126
+
127
+ try:
128
+ cpu_percents = psutil.cpu_percent(interval=0.1, percpu=True)
129
+ for i, percent in enumerate(cpu_percents):
130
+ metrics[f"cpu/{i}/utilization"] = percent
131
+ except Exception:
132
+ pass
133
+
134
+ try:
135
+ cpu_freq = psutil.cpu_freq()
136
+ if cpu_freq:
137
+ metrics["cpu/frequency"] = cpu_freq.current
138
+ if cpu_freq.max > 0:
139
+ metrics["cpu/frequency_max"] = cpu_freq.max
140
+ except Exception:
141
+ pass
142
+
143
+ try:
144
+ mem = psutil.virtual_memory()
145
+ metrics["memory/used"] = mem.used / (1024**3)
146
+ metrics["memory/total"] = mem.total / (1024**3)
147
+ metrics["memory/available"] = mem.available / (1024**3)
148
+ metrics["memory/percent"] = mem.percent
149
+ except Exception:
150
+ pass
151
+
152
+ try:
153
+ swap = psutil.swap_memory()
154
+ metrics["swap/used"] = swap.used / (1024**3)
155
+ metrics["swap/total"] = swap.total / (1024**3)
156
+ metrics["swap/percent"] = swap.percent
157
+ except Exception:
158
+ pass
159
+
160
+ try:
161
+ sensors_temps = psutil.sensors_temperatures()
162
+ if sensors_temps:
163
+ for name, entries in sensors_temps.items():
164
+ for i, entry in enumerate(entries):
165
+ label = entry.label or f"{name}_{i}"
166
+ metrics[f"temp/{label}"] = entry.current
167
+ except Exception:
168
+ pass
169
+
170
+ gpu_info = get_gpu_info()
171
+ if gpu_info.get("detected"):
172
+ metrics["gpu/detected"] = 1
173
+ if "type" in gpu_info:
174
+ pass
175
+
176
+ return metrics
177
+
178
+
179
+ class AppleGpuMonitor:
180
+ def __init__(self, run: "Run", interval: float = 10.0):
181
+ self._run = run
182
+ self._interval = interval
183
+ self._stop_flag = threading.Event()
184
+ self._thread: "threading.Thread | None" = None
185
+
186
+ def start(self):
187
+ if not is_apple_silicon():
188
+ warnings.warn(
189
+ "auto_log_gpu=True but not running on Apple Silicon. "
190
+ "Apple GPU logging disabled."
191
+ )
192
+ return
193
+
194
+ if not PSUTIL_AVAILABLE:
195
+ try:
196
+ _ensure_psutil()
197
+ except ImportError:
198
+ warnings.warn(
199
+ "auto_log_gpu=True but psutil not installed. "
200
+ "Install with: pip install psutil"
201
+ )
202
+ return
203
+
204
+ self._thread = threading.Thread(target=self._monitor_loop, daemon=True)
205
+ self._thread.start()
206
+
207
+ def stop(self):
208
+ self._stop_flag.set()
209
+ if self._thread is not None:
210
+ self._thread.join(timeout=2.0)
211
+
212
+ def _monitor_loop(self):
213
+ while not self._stop_flag.is_set():
214
+ try:
215
+ metrics = collect_apple_metrics()
216
+ if metrics:
217
+ self._run.log_system(metrics)
218
+ except Exception:
219
+ pass
220
+
221
+ self._stop_flag.wait(timeout=self._interval)
222
+
223
+
224
+ def log_apple_gpu(run: "Run | None" = None) -> dict:
225
+ """
226
+ Log Apple Silicon system metrics to the current or specified run.
227
+
228
+ Args:
229
+ run: Optional Run instance. If None, uses current run from context.
230
+
231
+ Returns:
232
+ dict: The system metrics that were logged.
233
+
234
+ Example:
235
+ ```python
236
+ import trackio
237
+
238
+ run = trackio.init(project="my-project")
239
+ trackio.log({"loss": 0.5})
240
+ trackio.log_apple_gpu()
241
+ ```
242
+ """
243
+ from trackio import context_vars
244
+
245
+ if run is None:
246
+ run = context_vars.current_run.get()
247
+ if run is None:
248
+ raise RuntimeError("Call trackio.init() before trackio.log_apple_gpu().")
249
+
250
+ metrics = collect_apple_metrics()
251
+ if metrics:
252
+ run.log_system(metrics)
253
+ return metrics
trackio/asgi_app.py ADDED
@@ -0,0 +1,483 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import inspect
4
+ import json
5
+ import logging
6
+ import math
7
+ import secrets
8
+ import tempfile
9
+ import threading
10
+ from pathlib import Path
11
+ from typing import Any, get_args, get_origin
12
+ from urllib.parse import unquote
13
+
14
+ from starlette.applications import Starlette
15
+ from starlette.requests import Request
16
+ from starlette.responses import FileResponse, JSONResponse, Response, StreamingResponse
17
+ from starlette.routing import Route
18
+
19
+ from trackio.exceptions import TrackioAPIError
20
+ from trackio.remote_client import HTTP_API_VERSION
21
+ from trackio.utils import on_spaces
22
+
23
+ logger = logging.getLogger("trackio.asgi_app")
24
+
25
+ _PACKAGE_JSON_PATH = Path(__file__).parent / "package.json"
26
+ _TRACKIO_PACKAGE_VERSION = json.loads(_PACKAGE_JSON_PATH.read_text())["version"]
27
+
28
+
29
+ def _normalize_allowed_file_roots(
30
+ allowed_file_roots: list[str | Path] | None,
31
+ ) -> tuple[Path, ...]:
32
+ roots = []
33
+ for root in allowed_file_roots or []:
34
+ roots.append(Path(root).resolve())
35
+ return tuple(roots)
36
+
37
+
38
+ def _is_allowed_file_path(path: Path, allowed_roots: tuple[Path, ...]) -> bool:
39
+ resolved_path = path.resolve(strict=False)
40
+ for root in allowed_roots:
41
+ try:
42
+ resolved_path.relative_to(root)
43
+ return True
44
+ except ValueError:
45
+ continue
46
+ return False
47
+
48
+
49
+ def _json_safe(data: Any) -> Any:
50
+ if data is None or isinstance(data, (str, bool, int)):
51
+ return data
52
+ if isinstance(data, float):
53
+ return data if math.isfinite(data) else None
54
+ if isinstance(data, dict):
55
+ return {k: _json_safe(v) for k, v in data.items()}
56
+ if isinstance(data, (list, tuple)):
57
+ return [_json_safe(v) for v in data]
58
+ if hasattr(data, "item"):
59
+ try:
60
+ return _json_safe(data.item())
61
+ except Exception:
62
+ pass
63
+ return str(data)
64
+
65
+
66
+ def register_uploaded_temp_file(request: Request, file_path: str | Path) -> None:
67
+ resolved_path = Path(file_path).resolve(strict=False)
68
+ with request.app.state.uploaded_temp_files_lock:
69
+ request.app.state.uploaded_temp_files.add(resolved_path)
70
+
71
+
72
+ def consume_uploaded_temp_file(request: Request, file_data: Any) -> Path:
73
+ file_path = file_data.get("path") if isinstance(file_data, dict) else None
74
+ if not isinstance(file_path, str) or not file_path:
75
+ raise TrackioAPIError("Expected uploaded file metadata with a valid path.")
76
+
77
+ resolved_path = Path(file_path).resolve(strict=False)
78
+ with request.app.state.uploaded_temp_files_lock:
79
+ if resolved_path not in request.app.state.uploaded_temp_files:
80
+ raise TrackioAPIError(
81
+ "Uploaded file was not created by this Trackio server."
82
+ )
83
+ request.app.state.uploaded_temp_files.remove(resolved_path)
84
+
85
+ if not resolved_path.is_file():
86
+ raise TrackioAPIError("Uploaded file is missing.")
87
+
88
+ return resolved_path
89
+
90
+
91
+ def _invoke_handler(
92
+ fn: Any,
93
+ request: Request,
94
+ args: list[Any] | None = None,
95
+ kwargs: dict[str, Any] | None = None,
96
+ ) -> Any:
97
+ sig = inspect.signature(fn)
98
+ params = list(sig.parameters.values())
99
+ positional_args: list[Any] = []
100
+ keyword_args: dict[str, Any] = {}
101
+ args = list(args or [])
102
+ kwargs = dict(kwargs or {})
103
+ data_index = 0
104
+
105
+ for param in params:
106
+ if param.name == "request":
107
+ keyword_args["request"] = request
108
+ elif param.kind == inspect.Parameter.VAR_POSITIONAL:
109
+ positional_args.extend(args[data_index:])
110
+ data_index = len(args)
111
+ elif param.kind == inspect.Parameter.VAR_KEYWORD:
112
+ keyword_args.update(kwargs)
113
+ kwargs.clear()
114
+ elif param.name in kwargs:
115
+ keyword_args[param.name] = kwargs.pop(param.name)
116
+ elif param.kind in (
117
+ inspect.Parameter.POSITIONAL_ONLY,
118
+ inspect.Parameter.POSITIONAL_OR_KEYWORD,
119
+ ) and data_index < len(args):
120
+ positional_args.append(args[data_index])
121
+ data_index += 1
122
+ elif param.default is inspect.Signature.empty and param.kind not in (
123
+ inspect.Parameter.VAR_POSITIONAL,
124
+ inspect.Parameter.VAR_KEYWORD,
125
+ ):
126
+ raise TrackioAPIError(f"Missing required parameter: {param.name}")
127
+
128
+ return fn(*positional_args, **keyword_args)
129
+
130
+
131
+ async def version_handler(request: Request) -> Response:
132
+ mcp_enabled = bool(getattr(request.app.state, "mcp_enabled", False))
133
+ return JSONResponse(
134
+ {
135
+ "version": _TRACKIO_PACKAGE_VERSION,
136
+ "api_version": HTTP_API_VERSION,
137
+ "api_transport": "http",
138
+ "mcp_enabled": mcp_enabled,
139
+ "mcp_path": "/mcp" if mcp_enabled else None,
140
+ }
141
+ )
142
+
143
+
144
+ def _json_schema_and_python_type(annotation: Any) -> tuple[dict[str, Any], str]:
145
+ if annotation is inspect.Parameter.empty:
146
+ return {"type": "object"}, "Any"
147
+ origin = get_origin(annotation)
148
+ args = get_args(annotation)
149
+ if origin is not None and args:
150
+ non_none = tuple(a for a in args if a is not type(None))
151
+ if len(non_none) == 1 and len(args) > 1:
152
+ return _json_schema_and_python_type(non_none[0])
153
+ if origin is list:
154
+ inner, py_inner = _json_schema_and_python_type(
155
+ args[0] if args else inspect.Parameter.empty
156
+ )
157
+ return {"type": "array", "items": inner}, f"list[{py_inner}]"
158
+ if origin is dict:
159
+ return {"type": "object"}, "dict"
160
+ if annotation in (str, bytes):
161
+ return {"type": "string"}, "str"
162
+ if annotation is int:
163
+ return {"type": "integer"}, "int"
164
+ if annotation is float:
165
+ return {"type": "number"}, "float"
166
+ if annotation is bool:
167
+ return {"type": "boolean"}, "bool"
168
+ name = getattr(annotation, "__name__", None)
169
+ if name:
170
+ return {"type": "object"}, name
171
+ return {"type": "object"}, "Any"
172
+
173
+
174
+ def build_gradio_api_info(api_registry: dict[str, Any]) -> dict[str, Any]:
175
+ named_endpoints: dict[str, Any] = {}
176
+ for name in sorted(api_registry.keys()):
177
+ fn = api_registry[name]
178
+ if not callable(fn):
179
+ continue
180
+ sig = inspect.signature(fn)
181
+ parameters: list[dict[str, Any]] = []
182
+ for pname, param in sig.parameters.items():
183
+ if pname == "request":
184
+ continue
185
+ jtype, pytype = _json_schema_and_python_type(param.annotation)
186
+ has_default = param.default is not inspect.Parameter.empty
187
+ parameters.append(
188
+ {
189
+ "label": pname,
190
+ "parameter_name": pname,
191
+ "parameter_has_default": has_default,
192
+ "parameter_default": None if not has_default else param.default,
193
+ "type": jtype,
194
+ "python_type": {"type": pytype, "description": ""},
195
+ "component": "Api",
196
+ "example_input": None,
197
+ }
198
+ )
199
+ ret_ann = sig.return_annotation
200
+ if ret_ann is inspect.Signature.empty:
201
+ ret_ann = Any
202
+ rjtype, rpytype = _json_schema_and_python_type(ret_ann)
203
+ returns = [
204
+ {
205
+ "label": "result",
206
+ "type": rjtype,
207
+ "python_type": {"type": rpytype, "description": ""},
208
+ "component": "Api",
209
+ }
210
+ ]
211
+ named_endpoints[f"/{name}"] = {
212
+ "parameters": parameters,
213
+ "returns": returns,
214
+ "api_visibility": "public",
215
+ }
216
+ return {"named_endpoints": named_endpoints, "unnamed_endpoints": {}}
217
+
218
+
219
+ _MAX_GRADIO_CALL_EVENTS = 256
220
+
221
+
222
+ def _hf_token_value_is_unset(value: Any) -> bool:
223
+ if value is None:
224
+ return True
225
+ if isinstance(value, str) and value.strip() == "":
226
+ return True
227
+ return False
228
+
229
+
230
+ def _authorization_bearer_token(request: Request) -> str | None:
231
+ auth = request.headers.get("authorization") or request.headers.get("Authorization")
232
+ if not auth:
233
+ return None
234
+ parts = auth.split()
235
+ if len(parts) != 2 or parts[0].lower() != "bearer":
236
+ return None
237
+ tok = parts[1].strip()
238
+ return tok or None
239
+
240
+
241
+ def _maybe_apply_hf_token_from_authorization(
242
+ request: Request, fn: Any, args: list[Any], kwargs: dict[str, Any]
243
+ ) -> None:
244
+ if not on_spaces():
245
+ return
246
+ token = _authorization_bearer_token(request)
247
+ if not token:
248
+ return
249
+ sig = inspect.signature(fn)
250
+ if "hf_token" not in sig.parameters:
251
+ return
252
+ params = [p for p in sig.parameters.values() if p.name != "request"]
253
+ names = [p.name for p in params]
254
+ if "hf_token" not in names:
255
+ return
256
+ idx = names.index("hf_token")
257
+ if "hf_token" in kwargs:
258
+ if _hf_token_value_is_unset(kwargs["hf_token"]):
259
+ kwargs["hf_token"] = token
260
+ return
261
+ if idx < len(args):
262
+ if _hf_token_value_is_unset(args[idx]):
263
+ args[idx] = token
264
+ return
265
+ kwargs["hf_token"] = token
266
+
267
+
268
+ def _store_gradio_call_result(
269
+ request: Request, event_id: str, api_name: str, data: Any
270
+ ) -> None:
271
+ with request.app.state.gradio_call_events_lock:
272
+ d = request.app.state.gradio_call_events
273
+ while len(d) >= _MAX_GRADIO_CALL_EVENTS:
274
+ d.pop(next(iter(d)))
275
+ d[event_id] = {"api_name": api_name, "data": data}
276
+
277
+
278
+ async def run_api_request(request: Request, api_name: str) -> Response:
279
+ api_registry = request.app.state.api_registry
280
+ fn = api_registry.get(api_name)
281
+ if fn is None:
282
+ return JSONResponse({"error": f"Unknown API: {api_name}"}, status_code=404)
283
+
284
+ try:
285
+ body = await request.json()
286
+ except Exception:
287
+ body = {}
288
+
289
+ args: list[Any] = []
290
+ kwargs: dict[str, Any] = {}
291
+ if isinstance(body, dict):
292
+ if "args" in body or "kwargs" in body:
293
+ args = body.get("args") or []
294
+ kwargs = body.get("kwargs") or {}
295
+ elif "data" in body and isinstance(body["data"], list):
296
+ args = body["data"]
297
+ else:
298
+ kwargs = body
299
+ elif isinstance(body, list):
300
+ args = body
301
+ elif body is not None:
302
+ args = [body]
303
+
304
+ if not isinstance(args, list):
305
+ args = [args]
306
+ if not isinstance(kwargs, dict):
307
+ kwargs = {}
308
+
309
+ _maybe_apply_hf_token_from_authorization(request, fn, args, kwargs)
310
+
311
+ try:
312
+ result = _invoke_handler(fn, request, args=args, kwargs=kwargs)
313
+ return JSONResponse({"data": _json_safe(result)})
314
+ except TrackioAPIError as e:
315
+ return JSONResponse({"error": str(e)}, status_code=400)
316
+ except Exception as e:
317
+ return JSONResponse({"error": str(e)}, status_code=500)
318
+
319
+
320
+ async def api_handler(request: Request) -> Response:
321
+ return await run_api_request(request, request.path_params["api_name"])
322
+
323
+
324
+ async def gradio_api_info_handler(request: Request) -> Response:
325
+ return JSONResponse(
326
+ build_gradio_api_info(request.app.state.api_registry),
327
+ headers={"Cache-Control": "no-store"},
328
+ )
329
+
330
+
331
+ async def gradio_call_post_handler(request: Request) -> Response:
332
+ api_name = request.path_params["api_name"]
333
+ resp = await run_api_request(request, api_name)
334
+ if resp.status_code != 200:
335
+ return resp
336
+ body = json.loads(bytes(resp.body).decode())
337
+ event_id = secrets.token_urlsafe(16)
338
+ _store_gradio_call_result(request, event_id, api_name, body["data"])
339
+ return JSONResponse({"event_id": event_id})
340
+
341
+
342
+ async def gradio_call_poll_handler(request: Request) -> Response:
343
+ api_name = request.path_params["api_name"]
344
+ event_id = request.path_params["event_id"]
345
+ with request.app.state.gradio_call_events_lock:
346
+ event = request.app.state.gradio_call_events.pop(event_id, None)
347
+ if event is None:
348
+ logger.info("gradio_api poll: unknown or expired event_id")
349
+ return JSONResponse({"error": "Unknown or expired event_id"}, status_code=404)
350
+ if event.get("api_name") != api_name:
351
+ logger.info(
352
+ "gradio_api poll: api_name mismatch (path=%r, stored=%r)",
353
+ api_name,
354
+ event.get("api_name"),
355
+ )
356
+ with request.app.state.gradio_call_events_lock:
357
+ d = request.app.state.gradio_call_events
358
+ while len(d) >= _MAX_GRADIO_CALL_EVENTS:
359
+ d.pop(next(iter(d)))
360
+ d[event_id] = event
361
+ return JSONResponse({"error": "Unknown or expired event_id"}, status_code=404)
362
+
363
+ data = event["data"]
364
+ payload = json.dumps(_json_safe([data]))
365
+
366
+ async def sse() -> Any:
367
+ yield f"event: complete\ndata: {payload}\n\n"
368
+
369
+ return StreamingResponse(
370
+ sse(),
371
+ media_type="text/event-stream",
372
+ headers={
373
+ "Cache-Control": "no-store",
374
+ "X-Accel-Buffering": "no",
375
+ },
376
+ )
377
+
378
+
379
+ async def upload_handler(request: Request) -> Response:
380
+ form = await request.form()
381
+ uploads = form.getlist("files")
382
+ saved_paths = []
383
+ for upload in uploads:
384
+ suffix = Path(getattr(upload, "filename", "") or "").suffix
385
+ with tempfile.NamedTemporaryFile(
386
+ delete=False,
387
+ prefix="trackio-upload-",
388
+ suffix=suffix,
389
+ ) as tmp:
390
+ tmp.write(await upload.read())
391
+ register_uploaded_temp_file(request, tmp.name)
392
+ saved_paths.append(tmp.name)
393
+ return JSONResponse({"paths": saved_paths})
394
+
395
+
396
+ async def gradio_upload_alias_handler(request: Request) -> Response:
397
+ return await upload_handler(request)
398
+
399
+
400
+ async def file_handler(request: Request) -> Response:
401
+ fs_path = request.query_params.get("path")
402
+ if fs_path is None:
403
+ return Response("Missing path", status_code=400)
404
+ fp = Path(unquote(fs_path)).resolve(strict=False)
405
+ allowed_roots = getattr(request.app.state, "allowed_file_roots", ())
406
+ if fp.is_file() and _is_allowed_file_path(fp, allowed_roots):
407
+ return FileResponse(str(fp))
408
+ return Response("Not found", status_code=404)
409
+
410
+
411
+ def create_trackio_starlette_app(
412
+ oauth_routes: list[Route],
413
+ api_registry: dict[str, Any],
414
+ extra_routes: list[Any] | None = None,
415
+ mcp_lifespan: Any = None,
416
+ mcp_enabled: bool = False,
417
+ allowed_file_roots: list[str | Path] | None = None,
418
+ ) -> Starlette:
419
+ routes: list[Any] = list(oauth_routes)
420
+ routes.extend(
421
+ [
422
+ Route("/version", endpoint=version_handler, methods=["GET"]),
423
+ Route("/api/upload", endpoint=upload_handler, methods=["POST"]),
424
+ Route("/api/{api_name:str}", endpoint=api_handler, methods=["POST"]),
425
+ Route("/file", endpoint=file_handler, methods=["GET"]),
426
+ ]
427
+ )
428
+ if on_spaces():
429
+ routes.extend(
430
+ [
431
+ Route(
432
+ "/gradio_api/info",
433
+ endpoint=gradio_api_info_handler,
434
+ methods=["GET"],
435
+ ),
436
+ Route(
437
+ "/gradio_api/info/",
438
+ endpoint=gradio_api_info_handler,
439
+ methods=["GET"],
440
+ ),
441
+ Route(
442
+ "/gradio_api/upload",
443
+ endpoint=gradio_upload_alias_handler,
444
+ methods=["POST"],
445
+ ),
446
+ Route(
447
+ "/gradio_api/upload/",
448
+ endpoint=gradio_upload_alias_handler,
449
+ methods=["POST"],
450
+ ),
451
+ Route(
452
+ "/gradio_api/call/{api_name:str}",
453
+ endpoint=gradio_call_post_handler,
454
+ methods=["POST"],
455
+ ),
456
+ Route(
457
+ "/gradio_api/call/{api_name:str}/",
458
+ endpoint=gradio_call_post_handler,
459
+ methods=["POST"],
460
+ ),
461
+ Route(
462
+ "/gradio_api/call/{api_name:str}/{event_id:str}",
463
+ endpoint=gradio_call_poll_handler,
464
+ methods=["GET"],
465
+ ),
466
+ Route(
467
+ "/gradio_api/call/{api_name:str}/{event_id:str}/",
468
+ endpoint=gradio_call_poll_handler,
469
+ methods=["GET"],
470
+ ),
471
+ ]
472
+ )
473
+ routes.extend(extra_routes or [])
474
+ app = Starlette(routes=routes, lifespan=mcp_lifespan)
475
+ app.state.api_registry = api_registry
476
+ app.state.mcp_enabled = mcp_enabled
477
+ app.state.allowed_file_roots = _normalize_allowed_file_roots(allowed_file_roots)
478
+ app.state.uploaded_temp_files = set()
479
+ app.state.uploaded_temp_files_lock = threading.Lock()
480
+ if on_spaces():
481
+ app.state.gradio_call_events = {}
482
+ app.state.gradio_call_events_lock = threading.Lock()
483
+ return app
trackio/assets/badge.png ADDED

Git LFS Details

  • SHA256: 206b7847247e83279f498510a2760338a03116bb5141a658d71ec14429f9ea9e
  • Pointer size: 131 Bytes
  • Size of remote file: 170 kB
trackio/assets/trackio_logo_dark.png ADDED
trackio/assets/trackio_logo_light.png ADDED
trackio/assets/trackio_logo_old.png ADDED

Git LFS Details

  • SHA256: 3922c4d1e465270ad4d8abb12023f3beed5d9f7f338528a4c0ac21dcf358a1c8
  • Pointer size: 131 Bytes
  • Size of remote file: 487 kB
trackio/assets/trackio_logo_type_dark.png ADDED
trackio/assets/trackio_logo_type_dark_transparent.png ADDED
trackio/assets/trackio_logo_type_light.png ADDED
trackio/assets/trackio_logo_type_light_transparent.png ADDED
trackio/bucket_storage.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ import tempfile
3
+ from pathlib import Path
4
+
5
+ import huggingface_hub
6
+ from huggingface_hub import copy_files, sync_bucket
7
+
8
+ from trackio.sqlite_storage import SQLiteStorage
9
+ from trackio.utils import MEDIA_DIR, TRACKIO_DIR
10
+
11
+
12
+ def create_bucket_if_not_exists(bucket_id: str, private: bool | None = None) -> None:
13
+ huggingface_hub.create_bucket(bucket_id, private=private, exist_ok=True)
14
+
15
+
16
+ def _list_bucket_file_paths(bucket_id: str, prefix: str | None = None) -> list[str]:
17
+ items = huggingface_hub.list_bucket_tree(bucket_id, prefix=prefix, recursive=True)
18
+ return [
19
+ item.path
20
+ for item in items
21
+ if getattr(item, "type", None) == "file" and getattr(item, "path", None)
22
+ ]
23
+
24
+
25
+ def download_bucket_to_trackio_dir(bucket_id: str) -> None:
26
+ TRACKIO_DIR.mkdir(parents=True, exist_ok=True)
27
+ sync_bucket(
28
+ source=f"hf://buckets/{bucket_id}/trackio",
29
+ dest=str(TRACKIO_DIR),
30
+ quiet=True,
31
+ )
32
+
33
+
34
+ def upload_project_to_bucket(project: str, bucket_id: str) -> None:
35
+ db_path = SQLiteStorage.get_project_db_path(project)
36
+ if not db_path.exists():
37
+ raise FileNotFoundError(f"No database found for project '{project}'")
38
+
39
+ with SQLiteStorage._get_connection(
40
+ db_path, configure_pragmas=False, row_factory=None
41
+ ) as conn:
42
+ conn.execute("PRAGMA wal_checkpoint(TRUNCATE)")
43
+
44
+ files_to_add = [(str(db_path), f"trackio/{db_path.name}")]
45
+
46
+ media_dir = MEDIA_DIR / project
47
+ if media_dir.exists():
48
+ for media_file in media_dir.rglob("*"):
49
+ if media_file.is_file():
50
+ rel = media_file.relative_to(TRACKIO_DIR)
51
+ files_to_add.append((str(media_file), f"trackio/{rel}"))
52
+
53
+ huggingface_hub.batch_bucket_files(bucket_id, add=files_to_add)
54
+
55
+
56
+ def _download_db_from_bucket(
57
+ project: str, bucket_id: str, dest_path: Path | None = None
58
+ ) -> bool:
59
+ db_filename = SQLiteStorage.get_project_db_filename(project)
60
+ remote_path = f"trackio/{db_filename}"
61
+ local_path = dest_path or SQLiteStorage.get_project_db_path(project)
62
+ local_path.parent.mkdir(parents=True, exist_ok=True)
63
+ try:
64
+ huggingface_hub.download_bucket_files(
65
+ bucket_id,
66
+ files=[(remote_path, str(local_path))],
67
+ token=huggingface_hub.utils.get_token(),
68
+ )
69
+ return local_path.exists()
70
+ except Exception:
71
+ return False
72
+
73
+
74
+ def _local_db_has_data(project: str) -> bool:
75
+ db_path = SQLiteStorage.get_project_db_path(project)
76
+ if not db_path.exists() or db_path.stat().st_size == 0:
77
+ return False
78
+ try:
79
+ with SQLiteStorage._get_connection(
80
+ db_path, configure_pragmas=False, row_factory=None
81
+ ) as conn:
82
+ count = conn.execute("SELECT COUNT(*) FROM metrics").fetchone()[0]
83
+ return count > 0
84
+ except Exception:
85
+ return False
86
+
87
+
88
+ def _export_and_upload_static(
89
+ project: str,
90
+ dest_bucket_id: str,
91
+ db_path: Path,
92
+ media_dir: Path | None = None,
93
+ ) -> None:
94
+ with tempfile.TemporaryDirectory() as tmp_dir:
95
+ output_dir = Path(tmp_dir)
96
+ SQLiteStorage.export_for_static_space(
97
+ project, output_dir, db_path_override=db_path
98
+ )
99
+
100
+ if media_dir and media_dir.exists():
101
+ shutil.copytree(media_dir, output_dir / "media")
102
+
103
+ files_to_add = []
104
+ for f in output_dir.rglob("*"):
105
+ if f.is_file():
106
+ rel = f.relative_to(output_dir)
107
+ files_to_add.append((str(f), str(rel)))
108
+
109
+ huggingface_hub.batch_bucket_files(dest_bucket_id, add=files_to_add)
110
+
111
+
112
+ def _copy_project_media_between_buckets(
113
+ source_bucket_id: str, dest_bucket_id: str, project: str
114
+ ) -> None:
115
+ source_media_prefix = f"trackio/media/{project}/"
116
+ media_to_copy = _list_bucket_file_paths(
117
+ source_bucket_id, prefix=source_media_prefix
118
+ )
119
+ if not media_to_copy:
120
+ return
121
+
122
+ copy_files(
123
+ f"hf://buckets/{source_bucket_id}/{source_media_prefix}",
124
+ f"hf://buckets/{dest_bucket_id}/media/",
125
+ )
126
+
127
+
128
+ def upload_project_to_bucket_for_static(project: str, bucket_id: str) -> None:
129
+ if not _local_db_has_data(project):
130
+ _download_db_from_bucket(project, bucket_id)
131
+
132
+ db_path = SQLiteStorage.get_project_db_path(project)
133
+ _export_and_upload_static(project, bucket_id, db_path, MEDIA_DIR / project)
134
+
135
+
136
+ def export_from_bucket_for_static(
137
+ source_bucket_id: str,
138
+ dest_bucket_id: str,
139
+ project: str,
140
+ ) -> None:
141
+ with tempfile.TemporaryDirectory() as work_dir:
142
+ work_path = Path(work_dir)
143
+ db_path = work_path / SQLiteStorage.get_project_db_filename(project)
144
+
145
+ if not _download_db_from_bucket(project, source_bucket_id, dest_path=db_path):
146
+ raise FileNotFoundError(
147
+ f"Could not download database for project '{project}' "
148
+ f"from bucket '{source_bucket_id}'."
149
+ )
150
+
151
+ _export_and_upload_static(project, dest_bucket_id, db_path)
152
+ _copy_project_media_between_buckets(source_bucket_id, dest_bucket_id, project)
trackio/cli.py ADDED
@@ -0,0 +1,1326 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import os
3
+
4
+ from trackio import freeze, show, sync
5
+ from trackio.cli_helpers import (
6
+ error_exit,
7
+ format_alerts,
8
+ format_json,
9
+ format_list,
10
+ format_metric_values,
11
+ format_project_summary,
12
+ format_query_result,
13
+ format_run_summary,
14
+ format_snapshot,
15
+ format_system_metric_names,
16
+ format_system_metrics,
17
+ )
18
+ from trackio.markdown import Markdown
19
+ from trackio.server import get_project_summary, get_run_summary
20
+ from trackio.sqlite_storage import SQLiteStorage
21
+
22
+
23
+ def _get_space(args):
24
+ return getattr(args, "space", None)
25
+
26
+
27
+ def _get_remote(args):
28
+ from trackio.remote_client import RemoteClient
29
+
30
+ space = _get_space(args)
31
+ if not space:
32
+ return None
33
+ hf_token = getattr(args, "hf_token", None)
34
+ return RemoteClient(space, hf_token=hf_token)
35
+
36
+
37
+ def _handle_status():
38
+ print("Reading local Trackio projects...\n")
39
+ projects = SQLiteStorage.get_projects()
40
+ if not projects:
41
+ print("No Trackio projects found.")
42
+ return
43
+
44
+ local_projects = []
45
+ synced_projects = []
46
+ unsynced_projects = []
47
+
48
+ for project in projects:
49
+ space_id = SQLiteStorage.get_space_id(project)
50
+ if space_id is None:
51
+ local_projects.append(project)
52
+ elif SQLiteStorage.has_pending_data(project):
53
+ unsynced_projects.append(project)
54
+ else:
55
+ synced_projects.append(project)
56
+
57
+ print("Finished reading Trackio projects")
58
+ if local_projects:
59
+ print(f" * {len(local_projects)} local trackio project(s) [OK]")
60
+ if synced_projects:
61
+ print(f" * {len(synced_projects)} trackio project(s) synced to Spaces [OK]")
62
+ if unsynced_projects:
63
+ print(
64
+ f" * {len(unsynced_projects)} trackio project(s) with unsynced changes [WARNING]:"
65
+ )
66
+ for p in unsynced_projects:
67
+ print(f" - {p}")
68
+
69
+ if unsynced_projects:
70
+ print(
71
+ f"\nRun `trackio sync --project {unsynced_projects[0]}` to sync. "
72
+ "Or run `trackio sync --all` to sync all unsynced changes."
73
+ )
74
+
75
+
76
+ def _handle_sync(args):
77
+ from trackio.deploy import sync_incremental
78
+
79
+ if args.sync_all and args.project:
80
+ error_exit("Cannot use --all and --project together.")
81
+ if not args.sync_all and not args.project:
82
+ error_exit("Must provide either --project or --all.")
83
+
84
+ if args.sync_all:
85
+ projects = SQLiteStorage.get_projects()
86
+ synced_any = False
87
+ for project in projects:
88
+ space_id = SQLiteStorage.get_space_id(project)
89
+ if space_id and SQLiteStorage.has_pending_data(project):
90
+ sync_incremental(
91
+ project, space_id, private=args.private, pending_only=True
92
+ )
93
+ synced_any = True
94
+ if not synced_any:
95
+ print("No projects with unsynced data found.")
96
+ else:
97
+ space_id = args.space_id
98
+ if space_id is None:
99
+ space_id = SQLiteStorage.get_space_id(args.project)
100
+ sync(
101
+ project=args.project,
102
+ space_id=space_id,
103
+ private=args.private,
104
+ force=args.force,
105
+ sdk=args.sdk,
106
+ )
107
+
108
+
109
+ def _extract_reports(
110
+ run: str, logs: list[dict], report_name: str | None = None
111
+ ) -> list[dict]:
112
+ reports = []
113
+ for log in logs:
114
+ timestamp = log.get("timestamp")
115
+ step = log.get("step")
116
+ for key, value in log.items():
117
+ if report_name is not None and key != report_name:
118
+ continue
119
+ if isinstance(value, dict) and value.get("_type") == Markdown.TYPE:
120
+ content = value.get("_value")
121
+ if isinstance(content, str):
122
+ reports.append(
123
+ {
124
+ "run": run,
125
+ "report": key,
126
+ "step": step,
127
+ "timestamp": timestamp,
128
+ "content": content,
129
+ }
130
+ )
131
+ return reports
132
+
133
+
134
+ def _handle_query(args):
135
+ remote = _get_remote(args)
136
+ try:
137
+ if remote:
138
+ result = remote.predict(args.project, args.sql, api_name="/query_project")
139
+ else:
140
+ result = SQLiteStorage.query_project(args.project, args.sql)
141
+ except FileNotFoundError as e:
142
+ error_exit(str(e))
143
+ except ValueError as e:
144
+ error_exit(str(e))
145
+
146
+ if args.json:
147
+ print(format_json(result))
148
+ else:
149
+ print(format_query_result(result))
150
+
151
+
152
+ def main():
153
+ parser = argparse.ArgumentParser(description="Trackio CLI")
154
+ parser.add_argument(
155
+ "--space",
156
+ required=False,
157
+ help="HF Space ID (e.g. 'user/space') or Space URL to query remotely.",
158
+ )
159
+ parser.add_argument(
160
+ "--hf-token",
161
+ required=False,
162
+ help="HF token for accessing private Spaces.",
163
+ )
164
+ subparsers = parser.add_subparsers(dest="command")
165
+
166
+ ui_parser = subparsers.add_parser(
167
+ "show", help="Show the Trackio dashboard UI for a project"
168
+ )
169
+ ui_parser.add_argument(
170
+ "--project", required=False, help="Project name to show in the dashboard"
171
+ )
172
+ ui_parser.add_argument(
173
+ "--theme",
174
+ required=False,
175
+ default="default",
176
+ help="A Gradio Theme to use for the dashboard instead of the default, can be a built-in theme (e.g. 'soft', 'citrus'), or a theme from the Hub (e.g. 'gstaff/xkcd').",
177
+ )
178
+ ui_parser.add_argument(
179
+ "--mcp-server",
180
+ action="store_true",
181
+ help="Enable MCP server functionality. The Trackio dashboard will be set up as an MCP server and certain functions will be exposed as MCP tools.",
182
+ )
183
+ ui_parser.add_argument(
184
+ "--footer",
185
+ action="store_true",
186
+ default=True,
187
+ help="Show the Gradio footer. Use --no-footer to hide it.",
188
+ )
189
+ ui_parser.add_argument(
190
+ "--no-footer",
191
+ dest="footer",
192
+ action="store_false",
193
+ help="Hide the Gradio footer.",
194
+ )
195
+ ui_parser.add_argument(
196
+ "--color-palette",
197
+ required=False,
198
+ help="Comma-separated list of hex color codes for plot lines (e.g. '#FF0000,#00FF00,#0000FF'). If not provided, the TRACKIO_COLOR_PALETTE environment variable will be used, or the default palette if not set.",
199
+ )
200
+ ui_parser.add_argument(
201
+ "--host",
202
+ required=False,
203
+ help="Host to bind the server to (e.g. '0.0.0.0' for remote access). If not provided, defaults to '127.0.0.1' (localhost only).",
204
+ )
205
+
206
+ subparsers.add_parser(
207
+ "status",
208
+ help="Show the status of all local Trackio projects, including sync status.",
209
+ )
210
+
211
+ sync_parser = subparsers.add_parser(
212
+ "sync",
213
+ help="Sync a local project's database to a Hugging Face Space. If the Space does not exist, it will be created.",
214
+ )
215
+ sync_parser.add_argument(
216
+ "--project",
217
+ required=False,
218
+ help="The name of the local project.",
219
+ )
220
+ sync_parser.add_argument(
221
+ "--space-id",
222
+ required=False,
223
+ help="The Hugging Face Space ID where the project will be synced (e.g. username/space_id). If not provided, uses the previously-configured Space.",
224
+ )
225
+ sync_parser.add_argument(
226
+ "--all",
227
+ action="store_true",
228
+ dest="sync_all",
229
+ help="Sync all projects that have unsynced data to their configured Spaces.",
230
+ )
231
+ sync_parser.add_argument(
232
+ "--private",
233
+ action="store_true",
234
+ help="Make the Hugging Face Space private if creating a new Space. By default, the repo will be public unless the organization's default is private. This value is ignored if the repo already exists.",
235
+ )
236
+ sync_parser.add_argument(
237
+ "--force",
238
+ action="store_true",
239
+ help="Overwrite the existing database without prompting for confirmation.",
240
+ )
241
+ sync_parser.add_argument(
242
+ "--sdk",
243
+ choices=["gradio", "static"],
244
+ default="gradio",
245
+ help="The type of Space to deploy. 'gradio' (default) deploys a live Gradio server. 'static' deploys a static Space that reads from an HF Bucket.",
246
+ )
247
+
248
+ freeze_parser = subparsers.add_parser(
249
+ "freeze",
250
+ help="Create a one-time static Space snapshot from a project's data.",
251
+ )
252
+ freeze_parser.add_argument(
253
+ "--space-id",
254
+ required=True,
255
+ help="The source Gradio Space ID (e.g. username/space_id).",
256
+ )
257
+ freeze_parser.add_argument(
258
+ "--project",
259
+ required=True,
260
+ help="The name of the project to freeze into a static snapshot.",
261
+ )
262
+ freeze_parser.add_argument(
263
+ "--new-space-id",
264
+ required=False,
265
+ help="The Space ID for the new static Space. Defaults to {space_id}_static.",
266
+ )
267
+ freeze_parser.add_argument(
268
+ "--private",
269
+ action="store_true",
270
+ help="Make the new static Space private.",
271
+ )
272
+
273
+ list_parser = subparsers.add_parser(
274
+ "list",
275
+ help="List projects, runs, or metrics",
276
+ )
277
+ list_subparsers = list_parser.add_subparsers(dest="list_type", required=True)
278
+
279
+ list_projects_parser = list_subparsers.add_parser(
280
+ "projects",
281
+ help="List all projects",
282
+ )
283
+ list_projects_parser.add_argument(
284
+ "--json",
285
+ action="store_true",
286
+ help="Output in JSON format",
287
+ )
288
+
289
+ list_runs_parser = list_subparsers.add_parser(
290
+ "runs",
291
+ help="List runs for a project",
292
+ )
293
+ list_runs_parser.add_argument(
294
+ "--project",
295
+ required=True,
296
+ help="Project name",
297
+ )
298
+ list_runs_parser.add_argument(
299
+ "--json",
300
+ action="store_true",
301
+ help="Output in JSON format",
302
+ )
303
+
304
+ list_metrics_parser = list_subparsers.add_parser(
305
+ "metrics",
306
+ help="List metrics for a run",
307
+ )
308
+ list_metrics_parser.add_argument(
309
+ "--project",
310
+ required=True,
311
+ help="Project name",
312
+ )
313
+ list_metrics_parser.add_argument(
314
+ "--run",
315
+ required=True,
316
+ help="Run name",
317
+ )
318
+ list_metrics_parser.add_argument(
319
+ "--json",
320
+ action="store_true",
321
+ help="Output in JSON format",
322
+ )
323
+
324
+ list_system_metrics_parser = list_subparsers.add_parser(
325
+ "system-metrics",
326
+ help="List system metrics for a run",
327
+ )
328
+ list_system_metrics_parser.add_argument(
329
+ "--project",
330
+ required=True,
331
+ help="Project name",
332
+ )
333
+ list_system_metrics_parser.add_argument(
334
+ "--run",
335
+ required=True,
336
+ help="Run name",
337
+ )
338
+ list_system_metrics_parser.add_argument(
339
+ "--json",
340
+ action="store_true",
341
+ help="Output in JSON format",
342
+ )
343
+
344
+ list_alerts_parser = list_subparsers.add_parser(
345
+ "alerts",
346
+ help="List alerts for a project or run",
347
+ )
348
+ list_alerts_parser.add_argument(
349
+ "--project",
350
+ required=True,
351
+ help="Project name",
352
+ )
353
+ list_alerts_parser.add_argument(
354
+ "--run",
355
+ required=False,
356
+ help="Run name (optional)",
357
+ )
358
+ list_alerts_parser.add_argument(
359
+ "--level",
360
+ required=False,
361
+ help="Filter by alert level (info, warn, error)",
362
+ )
363
+ list_alerts_parser.add_argument(
364
+ "--json",
365
+ action="store_true",
366
+ help="Output in JSON format",
367
+ )
368
+ list_alerts_parser.add_argument(
369
+ "--since",
370
+ required=False,
371
+ help="Only show alerts after this ISO 8601 timestamp",
372
+ )
373
+
374
+ list_reports_parser = list_subparsers.add_parser(
375
+ "reports",
376
+ help="List markdown reports for a project or run",
377
+ )
378
+ list_reports_parser.add_argument(
379
+ "--project",
380
+ required=True,
381
+ help="Project name",
382
+ )
383
+ list_reports_parser.add_argument(
384
+ "--run",
385
+ required=False,
386
+ help="Run name (optional)",
387
+ )
388
+ list_reports_parser.add_argument(
389
+ "--json",
390
+ action="store_true",
391
+ help="Output in JSON format",
392
+ )
393
+
394
+ get_parser = subparsers.add_parser(
395
+ "get",
396
+ help="Get project, run, or metric information",
397
+ )
398
+ get_subparsers = get_parser.add_subparsers(dest="get_type", required=True)
399
+
400
+ get_project_parser = get_subparsers.add_parser(
401
+ "project",
402
+ help="Get project summary",
403
+ )
404
+ get_project_parser.add_argument(
405
+ "--project",
406
+ required=True,
407
+ help="Project name",
408
+ )
409
+ get_project_parser.add_argument(
410
+ "--json",
411
+ action="store_true",
412
+ help="Output in JSON format",
413
+ )
414
+
415
+ get_run_parser = get_subparsers.add_parser(
416
+ "run",
417
+ help="Get run summary",
418
+ )
419
+ get_run_parser.add_argument(
420
+ "--project",
421
+ required=True,
422
+ help="Project name",
423
+ )
424
+ get_run_parser.add_argument(
425
+ "--run",
426
+ required=True,
427
+ help="Run name",
428
+ )
429
+ get_run_parser.add_argument(
430
+ "--json",
431
+ action="store_true",
432
+ help="Output in JSON format",
433
+ )
434
+
435
+ get_metric_parser = get_subparsers.add_parser(
436
+ "metric",
437
+ help="Get metric values for a run",
438
+ )
439
+ get_metric_parser.add_argument(
440
+ "--project",
441
+ required=True,
442
+ help="Project name",
443
+ )
444
+ get_metric_parser.add_argument(
445
+ "--run",
446
+ required=True,
447
+ help="Run name",
448
+ )
449
+ get_metric_parser.add_argument(
450
+ "--metric",
451
+ required=True,
452
+ help="Metric name",
453
+ )
454
+ get_metric_parser.add_argument(
455
+ "--step",
456
+ type=int,
457
+ required=False,
458
+ help="Get metric at exactly this step",
459
+ )
460
+ get_metric_parser.add_argument(
461
+ "--around",
462
+ type=int,
463
+ required=False,
464
+ help="Get metrics around this step (use with --window)",
465
+ )
466
+ get_metric_parser.add_argument(
467
+ "--at-time",
468
+ required=False,
469
+ help="Get metrics around this ISO 8601 timestamp (use with --window)",
470
+ )
471
+ get_metric_parser.add_argument(
472
+ "--window",
473
+ type=int,
474
+ required=False,
475
+ default=10,
476
+ help="Window size: ±steps for --around, ±seconds for --at-time (default: 10)",
477
+ )
478
+ get_metric_parser.add_argument(
479
+ "--json",
480
+ action="store_true",
481
+ help="Output in JSON format",
482
+ )
483
+
484
+ get_snapshot_parser = get_subparsers.add_parser(
485
+ "snapshot",
486
+ help="Get all metrics at/around a step or timestamp",
487
+ )
488
+ get_snapshot_parser.add_argument(
489
+ "--project",
490
+ required=True,
491
+ help="Project name",
492
+ )
493
+ get_snapshot_parser.add_argument(
494
+ "--run",
495
+ required=True,
496
+ help="Run name",
497
+ )
498
+ get_snapshot_parser.add_argument(
499
+ "--step",
500
+ type=int,
501
+ required=False,
502
+ help="Get all metrics at exactly this step",
503
+ )
504
+ get_snapshot_parser.add_argument(
505
+ "--around",
506
+ type=int,
507
+ required=False,
508
+ help="Get all metrics around this step (use with --window)",
509
+ )
510
+ get_snapshot_parser.add_argument(
511
+ "--at-time",
512
+ required=False,
513
+ help="Get all metrics around this ISO 8601 timestamp (use with --window)",
514
+ )
515
+ get_snapshot_parser.add_argument(
516
+ "--window",
517
+ type=int,
518
+ required=False,
519
+ default=10,
520
+ help="Window size: ±steps for --around, ±seconds for --at-time (default: 10)",
521
+ )
522
+ get_snapshot_parser.add_argument(
523
+ "--json",
524
+ action="store_true",
525
+ help="Output in JSON format",
526
+ )
527
+
528
+ get_system_metric_parser = get_subparsers.add_parser(
529
+ "system-metric",
530
+ help="Get system metric values for a run",
531
+ )
532
+ get_system_metric_parser.add_argument(
533
+ "--project",
534
+ required=True,
535
+ help="Project name",
536
+ )
537
+ get_system_metric_parser.add_argument(
538
+ "--run",
539
+ required=True,
540
+ help="Run name",
541
+ )
542
+ get_system_metric_parser.add_argument(
543
+ "--metric",
544
+ required=False,
545
+ help="System metric name (optional, if not provided returns all system metrics)",
546
+ )
547
+ get_system_metric_parser.add_argument(
548
+ "--json",
549
+ action="store_true",
550
+ help="Output in JSON format",
551
+ )
552
+
553
+ get_alerts_parser = get_subparsers.add_parser(
554
+ "alerts",
555
+ help="Get alerts for a project or run",
556
+ )
557
+ get_alerts_parser.add_argument(
558
+ "--project",
559
+ required=True,
560
+ help="Project name",
561
+ )
562
+ get_alerts_parser.add_argument(
563
+ "--run",
564
+ required=False,
565
+ help="Run name (optional)",
566
+ )
567
+ get_alerts_parser.add_argument(
568
+ "--level",
569
+ required=False,
570
+ help="Filter by alert level (info, warn, error)",
571
+ )
572
+ get_alerts_parser.add_argument(
573
+ "--json",
574
+ action="store_true",
575
+ help="Output in JSON format",
576
+ )
577
+ get_alerts_parser.add_argument(
578
+ "--since",
579
+ required=False,
580
+ help="Only show alerts after this ISO 8601 timestamp",
581
+ )
582
+
583
+ get_report_parser = get_subparsers.add_parser(
584
+ "report",
585
+ help="Get markdown report entries for a run",
586
+ )
587
+ get_report_parser.add_argument(
588
+ "--project",
589
+ required=True,
590
+ help="Project name",
591
+ )
592
+ get_report_parser.add_argument(
593
+ "--run",
594
+ required=True,
595
+ help="Run name",
596
+ )
597
+ get_report_parser.add_argument(
598
+ "--report",
599
+ required=True,
600
+ help="Report metric name",
601
+ )
602
+ get_report_parser.add_argument(
603
+ "--json",
604
+ action="store_true",
605
+ help="Output in JSON format",
606
+ )
607
+
608
+ query_parser = subparsers.add_parser(
609
+ "query",
610
+ help="Run a read-only SQL query against a project database",
611
+ )
612
+ query_subparsers = query_parser.add_subparsers(dest="query_type", required=True)
613
+ query_project_parser = query_subparsers.add_parser(
614
+ "project",
615
+ help="Run a read-only SQL query against a project's SQLite database",
616
+ )
617
+ query_project_parser.add_argument(
618
+ "--project",
619
+ required=True,
620
+ help="Project name",
621
+ )
622
+ query_project_parser.add_argument(
623
+ "--sql",
624
+ required=True,
625
+ help="Read-only SQL query to execute",
626
+ )
627
+ query_project_parser.add_argument(
628
+ "--json",
629
+ action="store_true",
630
+ help="Output in JSON format",
631
+ )
632
+
633
+ skills_parser = subparsers.add_parser(
634
+ "skills",
635
+ help="Manage Trackio skills for AI coding assistants",
636
+ )
637
+ skills_subparsers = skills_parser.add_subparsers(
638
+ dest="skills_action", required=True
639
+ )
640
+ skills_add_parser = skills_subparsers.add_parser(
641
+ "add",
642
+ help="Download and install the Trackio skill for an AI assistant",
643
+ )
644
+ skills_add_parser.add_argument(
645
+ "--cursor",
646
+ action="store_true",
647
+ help="Install for Cursor",
648
+ )
649
+ skills_add_parser.add_argument(
650
+ "--claude",
651
+ action="store_true",
652
+ help="Install for Claude Code",
653
+ )
654
+ skills_add_parser.add_argument(
655
+ "--codex",
656
+ action="store_true",
657
+ help="Install for Codex",
658
+ )
659
+ skills_add_parser.add_argument(
660
+ "--opencode",
661
+ action="store_true",
662
+ help="Install for OpenCode",
663
+ )
664
+ skills_add_parser.add_argument(
665
+ "--global",
666
+ dest="global_",
667
+ action="store_true",
668
+ help="Install globally (user-level) instead of in the current project directory",
669
+ )
670
+ skills_add_parser.add_argument(
671
+ "--dest",
672
+ type=str,
673
+ required=False,
674
+ help="Install into a custom destination (path to skills directory)",
675
+ )
676
+ skills_add_parser.add_argument(
677
+ "--force",
678
+ action="store_true",
679
+ help="Overwrite existing skill if it already exists",
680
+ )
681
+
682
+ args, unknown_args = parser.parse_known_args()
683
+ if unknown_args:
684
+ trailing_global_parser = argparse.ArgumentParser(add_help=False)
685
+ trailing_global_parser.add_argument("--space", required=False)
686
+ trailing_global_parser.add_argument("--hf-token", required=False)
687
+ trailing_globals, remaining_unknown = trailing_global_parser.parse_known_args(
688
+ unknown_args
689
+ )
690
+ if remaining_unknown:
691
+ parser.error(f"unrecognized arguments: {' '.join(remaining_unknown)}")
692
+ if trailing_globals.space is not None:
693
+ args.space = trailing_globals.space
694
+ if trailing_globals.hf_token is not None:
695
+ args.hf_token = trailing_globals.hf_token
696
+
697
+ if args.command in ("show", "status", "sync", "freeze", "skills") and _get_space(
698
+ args
699
+ ):
700
+ error_exit(
701
+ f"The '{args.command}' command does not support --space (remote mode)."
702
+ )
703
+
704
+ if args.command == "show":
705
+ color_palette = None
706
+ if args.color_palette:
707
+ color_palette = [color.strip() for color in args.color_palette.split(",")]
708
+ show(
709
+ project=args.project,
710
+ theme=args.theme,
711
+ mcp_server=args.mcp_server,
712
+ footer=args.footer,
713
+ color_palette=color_palette,
714
+ host=args.host,
715
+ )
716
+ elif args.command == "status":
717
+ _handle_status()
718
+ elif args.command == "sync":
719
+ _handle_sync(args)
720
+ elif args.command == "freeze":
721
+ freeze(
722
+ space_id=args.space_id,
723
+ project=args.project,
724
+ new_space_id=args.new_space_id,
725
+ private=args.private,
726
+ )
727
+ elif args.command == "list":
728
+ remote = _get_remote(args)
729
+ if args.list_type == "projects":
730
+ if remote:
731
+ projects = remote.predict(api_name="/get_all_projects")
732
+ else:
733
+ projects = SQLiteStorage.get_projects()
734
+ if args.json:
735
+ print(format_json({"projects": projects}))
736
+ else:
737
+ print(format_list(projects, "Projects"))
738
+ elif args.list_type == "runs":
739
+ if remote:
740
+ run_records = remote.predict(
741
+ args.project, api_name="/get_runs_for_project"
742
+ )
743
+ runs = [r["name"] if isinstance(r, dict) else r for r in run_records]
744
+ else:
745
+ db_path = SQLiteStorage.get_project_db_path(args.project)
746
+ if not db_path.exists():
747
+ error_exit(f"Project '{args.project}' not found.")
748
+ runs = SQLiteStorage.get_runs(args.project)
749
+ if args.json:
750
+ print(format_json({"project": args.project, "runs": runs}))
751
+ else:
752
+ print(format_list(runs, f"Runs in '{args.project}'"))
753
+ elif args.list_type == "metrics":
754
+ if remote:
755
+ metrics = remote.predict(
756
+ args.project, args.run, api_name="/get_metrics_for_run"
757
+ )
758
+ else:
759
+ db_path = SQLiteStorage.get_project_db_path(args.project)
760
+ if not db_path.exists():
761
+ error_exit(f"Project '{args.project}' not found.")
762
+ runs = SQLiteStorage.get_runs(args.project)
763
+ if args.run not in runs:
764
+ error_exit(
765
+ f"Run '{args.run}' not found in project '{args.project}'."
766
+ )
767
+ metrics = SQLiteStorage.get_all_metrics_for_run(args.project, args.run)
768
+ if args.json:
769
+ print(
770
+ format_json(
771
+ {"project": args.project, "run": args.run, "metrics": metrics}
772
+ )
773
+ )
774
+ else:
775
+ print(
776
+ format_list(
777
+ metrics, f"Metrics for '{args.run}' in '{args.project}'"
778
+ )
779
+ )
780
+ elif args.list_type == "system-metrics":
781
+ if remote:
782
+ system_metrics = remote.predict(
783
+ args.project, args.run, api_name="/get_system_metrics_for_run"
784
+ )
785
+ else:
786
+ db_path = SQLiteStorage.get_project_db_path(args.project)
787
+ if not db_path.exists():
788
+ error_exit(f"Project '{args.project}' not found.")
789
+ runs = SQLiteStorage.get_runs(args.project)
790
+ if args.run not in runs:
791
+ error_exit(
792
+ f"Run '{args.run}' not found in project '{args.project}'."
793
+ )
794
+ system_metrics = SQLiteStorage.get_all_system_metrics_for_run(
795
+ args.project, args.run
796
+ )
797
+ if args.json:
798
+ print(
799
+ format_json(
800
+ {
801
+ "project": args.project,
802
+ "run": args.run,
803
+ "system_metrics": system_metrics,
804
+ }
805
+ )
806
+ )
807
+ else:
808
+ print(format_system_metric_names(system_metrics))
809
+ elif args.list_type == "alerts":
810
+ if remote:
811
+ alerts = remote.predict(
812
+ args.project,
813
+ args.run,
814
+ args.level,
815
+ args.since,
816
+ api_name="/get_alerts",
817
+ )
818
+ else:
819
+ db_path = SQLiteStorage.get_project_db_path(args.project)
820
+ if not db_path.exists():
821
+ error_exit(f"Project '{args.project}' not found.")
822
+ alerts = SQLiteStorage.get_alerts(
823
+ args.project,
824
+ run_name=args.run,
825
+ level=args.level,
826
+ since=args.since,
827
+ )
828
+ if args.json:
829
+ print(
830
+ format_json(
831
+ {
832
+ "project": args.project,
833
+ "run": args.run,
834
+ "level": args.level,
835
+ "since": args.since,
836
+ "alerts": alerts,
837
+ }
838
+ )
839
+ )
840
+ else:
841
+ print(format_alerts(alerts))
842
+ elif args.list_type == "reports":
843
+ if remote:
844
+ run_records = remote.predict(
845
+ args.project, api_name="/get_runs_for_project"
846
+ )
847
+ runs = [r["name"] if isinstance(r, dict) else r for r in run_records]
848
+ else:
849
+ db_path = SQLiteStorage.get_project_db_path(args.project)
850
+ if not db_path.exists():
851
+ error_exit(f"Project '{args.project}' not found.")
852
+ runs = SQLiteStorage.get_runs(args.project)
853
+ if args.run and args.run not in runs:
854
+ error_exit(f"Run '{args.run}' not found in project '{args.project}'.")
855
+
856
+ target_runs = [args.run] if args.run else runs
857
+ all_reports = []
858
+ for run_name in target_runs:
859
+ if remote:
860
+ logs = remote.predict(args.project, run_name, api_name="/get_logs")
861
+ else:
862
+ logs = SQLiteStorage.get_logs(args.project, run_name)
863
+ all_reports.extend(_extract_reports(run_name, logs))
864
+
865
+ if args.json:
866
+ print(
867
+ format_json(
868
+ {
869
+ "project": args.project,
870
+ "run": args.run,
871
+ "reports": all_reports,
872
+ }
873
+ )
874
+ )
875
+ else:
876
+ report_lines = [
877
+ f"{entry['run']} | {entry['report']} | step={entry['step']} | {entry['timestamp']}"
878
+ for entry in all_reports
879
+ ]
880
+ if args.run:
881
+ print(
882
+ format_list(
883
+ report_lines,
884
+ f"Reports for '{args.run}' in '{args.project}'",
885
+ )
886
+ )
887
+ else:
888
+ print(format_list(report_lines, f"Reports in '{args.project}'"))
889
+ elif args.command == "get":
890
+ remote = _get_remote(args)
891
+ if args.get_type == "project":
892
+ if remote:
893
+ summary = remote.predict(args.project, api_name="/get_project_summary")
894
+ else:
895
+ db_path = SQLiteStorage.get_project_db_path(args.project)
896
+ if not db_path.exists():
897
+ error_exit(f"Project '{args.project}' not found.")
898
+ summary = get_project_summary(args.project)
899
+ if args.json:
900
+ print(format_json(summary))
901
+ else:
902
+ print(format_project_summary(summary))
903
+ elif args.get_type == "run":
904
+ if remote:
905
+ summary = remote.predict(
906
+ args.project, args.run, api_name="/get_run_summary"
907
+ )
908
+ else:
909
+ db_path = SQLiteStorage.get_project_db_path(args.project)
910
+ if not db_path.exists():
911
+ error_exit(f"Project '{args.project}' not found.")
912
+ runs = SQLiteStorage.get_runs(args.project)
913
+ if args.run not in runs:
914
+ error_exit(
915
+ f"Run '{args.run}' not found in project '{args.project}'."
916
+ )
917
+ summary = get_run_summary(args.project, args.run)
918
+ if args.json:
919
+ print(format_json(summary))
920
+ else:
921
+ print(format_run_summary(summary))
922
+ elif args.get_type == "metric":
923
+ at_time = getattr(args, "at_time", None)
924
+ if remote:
925
+ values = remote.predict(
926
+ args.project,
927
+ args.run,
928
+ args.metric,
929
+ args.step,
930
+ args.around,
931
+ at_time,
932
+ args.window,
933
+ api_name="/get_metric_values",
934
+ )
935
+ else:
936
+ db_path = SQLiteStorage.get_project_db_path(args.project)
937
+ if not db_path.exists():
938
+ error_exit(f"Project '{args.project}' not found.")
939
+ runs = SQLiteStorage.get_runs(args.project)
940
+ if args.run not in runs:
941
+ error_exit(
942
+ f"Run '{args.run}' not found in project '{args.project}'."
943
+ )
944
+ metrics = SQLiteStorage.get_all_metrics_for_run(args.project, args.run)
945
+ if args.metric not in metrics:
946
+ error_exit(
947
+ f"Metric '{args.metric}' not found in run '{args.run}' of project '{args.project}'."
948
+ )
949
+ values = SQLiteStorage.get_metric_values(
950
+ args.project,
951
+ args.run,
952
+ args.metric,
953
+ step=args.step,
954
+ around_step=args.around,
955
+ at_time=at_time,
956
+ window=args.window,
957
+ )
958
+ if args.json:
959
+ print(
960
+ format_json(
961
+ {
962
+ "project": args.project,
963
+ "run": args.run,
964
+ "metric": args.metric,
965
+ "values": values,
966
+ }
967
+ )
968
+ )
969
+ else:
970
+ print(format_metric_values(values))
971
+ elif args.get_type == "snapshot":
972
+ if not args.step and not args.around and not getattr(args, "at_time", None):
973
+ error_exit(
974
+ "Provide --step, --around (with --window), or --at-time (with --window)."
975
+ )
976
+ at_time = getattr(args, "at_time", None)
977
+ if remote:
978
+ snapshot = remote.predict(
979
+ args.project,
980
+ args.run,
981
+ args.step,
982
+ args.around,
983
+ at_time,
984
+ args.window,
985
+ api_name="/get_snapshot",
986
+ )
987
+ else:
988
+ db_path = SQLiteStorage.get_project_db_path(args.project)
989
+ if not db_path.exists():
990
+ error_exit(f"Project '{args.project}' not found.")
991
+ runs = SQLiteStorage.get_runs(args.project)
992
+ if args.run not in runs:
993
+ error_exit(
994
+ f"Run '{args.run}' not found in project '{args.project}'."
995
+ )
996
+ snapshot = SQLiteStorage.get_snapshot(
997
+ args.project,
998
+ args.run,
999
+ step=args.step,
1000
+ around_step=args.around,
1001
+ at_time=at_time,
1002
+ window=args.window,
1003
+ )
1004
+ if args.json:
1005
+ result = {
1006
+ "project": args.project,
1007
+ "run": args.run,
1008
+ "metrics": snapshot,
1009
+ }
1010
+ if args.step is not None:
1011
+ result["step"] = args.step
1012
+ if args.around is not None:
1013
+ result["around"] = args.around
1014
+ result["window"] = args.window
1015
+ if at_time is not None:
1016
+ result["at_time"] = at_time
1017
+ result["window"] = args.window
1018
+ print(format_json(result))
1019
+ else:
1020
+ print(format_snapshot(snapshot))
1021
+ elif args.get_type == "system-metric":
1022
+ if remote:
1023
+ system_metrics = remote.predict(
1024
+ args.project, args.run, api_name="/get_system_logs"
1025
+ )
1026
+ if args.metric:
1027
+ all_system_metric_names = remote.predict(
1028
+ args.project,
1029
+ args.run,
1030
+ api_name="/get_system_metrics_for_run",
1031
+ )
1032
+ if args.metric not in all_system_metric_names:
1033
+ error_exit(
1034
+ f"System metric '{args.metric}' not found in run '{args.run}' of project '{args.project}'."
1035
+ )
1036
+ filtered_metrics = [
1037
+ {
1038
+ k: v
1039
+ for k, v in entry.items()
1040
+ if k == "timestamp" or k == args.metric
1041
+ }
1042
+ for entry in system_metrics
1043
+ if args.metric in entry
1044
+ ]
1045
+ if args.json:
1046
+ print(
1047
+ format_json(
1048
+ {
1049
+ "project": args.project,
1050
+ "run": args.run,
1051
+ "metric": args.metric,
1052
+ "values": filtered_metrics,
1053
+ }
1054
+ )
1055
+ )
1056
+ else:
1057
+ print(format_system_metrics(filtered_metrics))
1058
+ else:
1059
+ if args.json:
1060
+ print(
1061
+ format_json(
1062
+ {
1063
+ "project": args.project,
1064
+ "run": args.run,
1065
+ "system_metrics": system_metrics,
1066
+ }
1067
+ )
1068
+ )
1069
+ else:
1070
+ print(format_system_metrics(system_metrics))
1071
+ else:
1072
+ db_path = SQLiteStorage.get_project_db_path(args.project)
1073
+ if not db_path.exists():
1074
+ error_exit(f"Project '{args.project}' not found.")
1075
+ runs = SQLiteStorage.get_runs(args.project)
1076
+ if args.run not in runs:
1077
+ error_exit(
1078
+ f"Run '{args.run}' not found in project '{args.project}'."
1079
+ )
1080
+ if args.metric:
1081
+ system_metrics = SQLiteStorage.get_system_logs(
1082
+ args.project, args.run
1083
+ )
1084
+ all_system_metric_names = (
1085
+ SQLiteStorage.get_all_system_metrics_for_run(
1086
+ args.project, args.run
1087
+ )
1088
+ )
1089
+ if args.metric not in all_system_metric_names:
1090
+ error_exit(
1091
+ f"System metric '{args.metric}' not found in run '{args.run}' of project '{args.project}'."
1092
+ )
1093
+ filtered_metrics = [
1094
+ {
1095
+ k: v
1096
+ for k, v in entry.items()
1097
+ if k == "timestamp" or k == args.metric
1098
+ }
1099
+ for entry in system_metrics
1100
+ if args.metric in entry
1101
+ ]
1102
+ if args.json:
1103
+ print(
1104
+ format_json(
1105
+ {
1106
+ "project": args.project,
1107
+ "run": args.run,
1108
+ "metric": args.metric,
1109
+ "values": filtered_metrics,
1110
+ }
1111
+ )
1112
+ )
1113
+ else:
1114
+ print(format_system_metrics(filtered_metrics))
1115
+ else:
1116
+ system_metrics = SQLiteStorage.get_system_logs(
1117
+ args.project, args.run
1118
+ )
1119
+ if args.json:
1120
+ print(
1121
+ format_json(
1122
+ {
1123
+ "project": args.project,
1124
+ "run": args.run,
1125
+ "system_metrics": system_metrics,
1126
+ }
1127
+ )
1128
+ )
1129
+ else:
1130
+ print(format_system_metrics(system_metrics))
1131
+ elif args.get_type == "alerts":
1132
+ if remote:
1133
+ alerts = remote.predict(
1134
+ args.project,
1135
+ args.run,
1136
+ args.level,
1137
+ args.since,
1138
+ api_name="/get_alerts",
1139
+ )
1140
+ else:
1141
+ db_path = SQLiteStorage.get_project_db_path(args.project)
1142
+ if not db_path.exists():
1143
+ error_exit(f"Project '{args.project}' not found.")
1144
+ alerts = SQLiteStorage.get_alerts(
1145
+ args.project,
1146
+ run_name=args.run,
1147
+ level=args.level,
1148
+ since=args.since,
1149
+ )
1150
+ if args.json:
1151
+ print(
1152
+ format_json(
1153
+ {
1154
+ "project": args.project,
1155
+ "run": args.run,
1156
+ "level": args.level,
1157
+ "since": args.since,
1158
+ "alerts": alerts,
1159
+ }
1160
+ )
1161
+ )
1162
+ else:
1163
+ print(format_alerts(alerts))
1164
+ elif args.get_type == "report":
1165
+ if remote:
1166
+ logs = remote.predict(args.project, args.run, api_name="/get_logs")
1167
+ else:
1168
+ db_path = SQLiteStorage.get_project_db_path(args.project)
1169
+ if not db_path.exists():
1170
+ error_exit(f"Project '{args.project}' not found.")
1171
+ runs = SQLiteStorage.get_runs(args.project)
1172
+ if args.run not in runs:
1173
+ error_exit(
1174
+ f"Run '{args.run}' not found in project '{args.project}'."
1175
+ )
1176
+ logs = SQLiteStorage.get_logs(args.project, args.run)
1177
+
1178
+ reports = _extract_reports(args.run, logs, report_name=args.report)
1179
+ if not reports:
1180
+ error_exit(
1181
+ f"Report '{args.report}' not found in run '{args.run}' of project '{args.project}'."
1182
+ )
1183
+
1184
+ if args.json:
1185
+ print(
1186
+ format_json(
1187
+ {
1188
+ "project": args.project,
1189
+ "run": args.run,
1190
+ "report": args.report,
1191
+ "values": reports,
1192
+ }
1193
+ )
1194
+ )
1195
+ else:
1196
+ output = []
1197
+ for idx, entry in enumerate(reports, start=1):
1198
+ output.append(
1199
+ f"Entry {idx} | step={entry['step']} | timestamp={entry['timestamp']}"
1200
+ )
1201
+ output.append(entry["content"])
1202
+ if idx < len(reports):
1203
+ output.append("-" * 80)
1204
+ print("\n".join(output))
1205
+ elif args.command == "query":
1206
+ if args.query_type == "project":
1207
+ _handle_query(args)
1208
+ elif args.command == "skills":
1209
+ if args.skills_action == "add":
1210
+ _handle_skills_add(args)
1211
+ else:
1212
+ parser.print_help()
1213
+
1214
+
1215
+ def _handle_skills_add(args):
1216
+ import shutil
1217
+ from pathlib import Path
1218
+
1219
+ try:
1220
+ from huggingface_hub.cli.skills import (
1221
+ CENTRAL_GLOBAL,
1222
+ CENTRAL_LOCAL,
1223
+ GLOBAL_TARGETS,
1224
+ LOCAL_TARGETS,
1225
+ )
1226
+ except (ImportError, ModuleNotFoundError):
1227
+ error_exit(
1228
+ "The 'trackio skills' command requires huggingface_hub >= 1.4.0.\n"
1229
+ "Please upgrade: pip install --upgrade huggingface_hub"
1230
+ )
1231
+
1232
+ SKILL_ID = "trackio"
1233
+ GITHUB_RAW = "https://raw.githubusercontent.com/gradio-app/trackio/main"
1234
+ SKILL_PREFIX = ".agents/skills/trackio"
1235
+ SKILL_FILES = [
1236
+ "SKILL.md",
1237
+ "alerts.md",
1238
+ "logging_metrics.md",
1239
+ "retrieving_metrics.md",
1240
+ "storage_schema.md",
1241
+ ]
1242
+
1243
+ if not (args.cursor or args.claude or args.codex or args.opencode or args.dest):
1244
+ error_exit(
1245
+ "Pick a destination via --cursor, --claude, --codex, --opencode, or --dest."
1246
+ )
1247
+
1248
+ def download(url: str) -> str:
1249
+ from huggingface_hub.utils import get_session
1250
+
1251
+ try:
1252
+ response = get_session().get(url)
1253
+ response.raise_for_status()
1254
+ except Exception as e:
1255
+ error_exit(
1256
+ f"Failed to download {url}\n{e}\n\n"
1257
+ "Make sure you have internet access. The skill files are fetched from "
1258
+ "the Trackio GitHub repository."
1259
+ )
1260
+ return response.text
1261
+
1262
+ def remove_existing(path: Path, force: bool):
1263
+ if not (path.exists() or path.is_symlink()):
1264
+ return
1265
+ if not force:
1266
+ error_exit(
1267
+ f"Skill already exists at {path}.\nRe-run with --force to overwrite."
1268
+ )
1269
+ if path.is_dir() and not path.is_symlink():
1270
+ shutil.rmtree(path)
1271
+ else:
1272
+ path.unlink()
1273
+
1274
+ def install_to(skills_dir: Path, force: bool) -> Path:
1275
+ skills_dir = skills_dir.expanduser().resolve()
1276
+ skills_dir.mkdir(parents=True, exist_ok=True)
1277
+ dest = skills_dir / SKILL_ID
1278
+ remove_existing(dest, force)
1279
+ dest.mkdir()
1280
+ for fname in SKILL_FILES:
1281
+ content = download(f"{GITHUB_RAW}/{SKILL_PREFIX}/{fname}")
1282
+ (dest / fname).write_text(content, encoding="utf-8")
1283
+ return dest
1284
+
1285
+ def create_symlink(
1286
+ agent_skills_dir: Path, central_skill_path: Path, force: bool
1287
+ ) -> Path:
1288
+ agent_skills_dir = agent_skills_dir.expanduser().resolve()
1289
+ agent_skills_dir.mkdir(parents=True, exist_ok=True)
1290
+ link_path = agent_skills_dir / SKILL_ID
1291
+ remove_existing(link_path, force)
1292
+ link_path.symlink_to(os.path.relpath(central_skill_path, agent_skills_dir))
1293
+ return link_path
1294
+
1295
+ global_targets = {**GLOBAL_TARGETS, "cursor": Path("~/.cursor/skills")}
1296
+ local_targets = {**LOCAL_TARGETS, "cursor": Path(".cursor/skills")}
1297
+ targets_dict = global_targets if args.global_ else local_targets
1298
+
1299
+ if args.dest:
1300
+ if args.cursor or args.claude or args.codex or args.opencode or args.global_:
1301
+ error_exit("--dest cannot be combined with agent flags or --global.")
1302
+ skill_dest = install_to(Path(args.dest), args.force)
1303
+ print(f"Installed '{SKILL_ID}' to {skill_dest}")
1304
+ return
1305
+
1306
+ agent_targets = []
1307
+ if args.cursor:
1308
+ agent_targets.append(targets_dict["cursor"])
1309
+ if args.claude:
1310
+ agent_targets.append(targets_dict["claude"])
1311
+ if args.codex:
1312
+ agent_targets.append(targets_dict["codex"])
1313
+ if args.opencode:
1314
+ agent_targets.append(targets_dict["opencode"])
1315
+
1316
+ central_path = CENTRAL_GLOBAL if args.global_ else CENTRAL_LOCAL
1317
+ central_skill_path = install_to(central_path, args.force)
1318
+ print(f"Installed '{SKILL_ID}' to central location: {central_skill_path}")
1319
+
1320
+ for agent_target in agent_targets:
1321
+ link_path = create_symlink(agent_target, central_skill_path, args.force)
1322
+ print(f"Created symlink: {link_path}")
1323
+
1324
+
1325
+ if __name__ == "__main__":
1326
+ main()
trackio/cli_helpers.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import sys
3
+ from typing import Any
4
+
5
+
6
+ def format_json(data: Any) -> str:
7
+ """Format data as JSON."""
8
+ return json.dumps(data, indent=2)
9
+
10
+
11
+ def format_list(items: list[str], title: str | None = None) -> str:
12
+ """Format a list of items in human-readable format."""
13
+ if not items:
14
+ return f"No {title.lower() if title else 'items'} found."
15
+
16
+ output = []
17
+ if title:
18
+ output.append(f"{title}:")
19
+
20
+ for item in items:
21
+ output.append(f" - {item}")
22
+
23
+ return "\n".join(output)
24
+
25
+
26
+ def format_project_summary(summary: dict) -> str:
27
+ """Format project summary in human-readable format."""
28
+ output = [f"Project: {summary['project']}"]
29
+ output.append(f"Number of runs: {summary['num_runs']}")
30
+
31
+ if summary["runs"]:
32
+ output.append("\nRuns:")
33
+ for run in summary["runs"]:
34
+ output.append(f" - {run}")
35
+ else:
36
+ output.append("\nNo runs found.")
37
+
38
+ if summary.get("last_activity"):
39
+ output.append(f"\nLast activity (max step): {summary['last_activity']}")
40
+
41
+ return "\n".join(output)
42
+
43
+
44
+ def format_run_summary(summary: dict) -> str:
45
+ """Format run summary in human-readable format."""
46
+ output = [f"Project: {summary['project']}"]
47
+ output.append(f"Run: {summary['run']}")
48
+ output.append(f"Number of logs: {summary['num_logs']}")
49
+
50
+ if summary.get("last_step") is not None:
51
+ output.append(f"Last step: {summary['last_step']}")
52
+
53
+ if summary.get("metrics"):
54
+ output.append("\nMetrics:")
55
+ for metric in summary["metrics"]:
56
+ output.append(f" - {metric}")
57
+ else:
58
+ output.append("\nNo metrics found.")
59
+
60
+ config = summary.get("config")
61
+ if config:
62
+ output.append("\nConfig:")
63
+ config_display = {k: v for k, v in config.items() if not k.startswith("_")}
64
+ if config_display:
65
+ for key, value in config_display.items():
66
+ output.append(f" {key}: {value}")
67
+ else:
68
+ output.append(" (no config)")
69
+ else:
70
+ output.append("\nConfig: (no config)")
71
+
72
+ return "\n".join(output)
73
+
74
+
75
+ def format_metric_values(values: list[dict]) -> str:
76
+ """Format metric values in human-readable format."""
77
+ if not values:
78
+ return "No metric values found."
79
+
80
+ output = [f"Found {len(values)} value(s):\n"]
81
+ output.append("Step | Timestamp | Value")
82
+ output.append("-" * 50)
83
+
84
+ for value in values:
85
+ step = value.get("step", "N/A")
86
+ timestamp = value.get("timestamp", "N/A")
87
+ val = value.get("value", "N/A")
88
+ output.append(f"{step} | {timestamp} | {val}")
89
+
90
+ return "\n".join(output)
91
+
92
+
93
+ def format_system_metrics(metrics: list[dict]) -> str:
94
+ """Format system metrics in human-readable format."""
95
+ if not metrics:
96
+ return "No system metrics found."
97
+
98
+ output = [f"Found {len(metrics)} system metric entry/entries:\n"]
99
+
100
+ for i, entry in enumerate(metrics):
101
+ timestamp = entry.get("timestamp", "N/A")
102
+ output.append(f"\nEntry {i + 1} (Timestamp: {timestamp}):")
103
+ for key, value in entry.items():
104
+ if key != "timestamp":
105
+ output.append(f" {key}: {value}")
106
+
107
+ return "\n".join(output)
108
+
109
+
110
+ def format_system_metric_names(names: list[str]) -> str:
111
+ """Format system metric names in human-readable format."""
112
+ return format_list(names, "System Metrics")
113
+
114
+
115
+ def format_snapshot(snapshot: dict[str, list[dict]]) -> str:
116
+ """Format a metrics snapshot in human-readable format."""
117
+ if not snapshot:
118
+ return "No metrics found in the specified range."
119
+
120
+ output = []
121
+ for metric_name, values in sorted(snapshot.items()):
122
+ output.append(f"\n{metric_name}:")
123
+ output.append(" Step | Timestamp | Value")
124
+ output.append(" " + "-" * 48)
125
+ for v in values:
126
+ step = v.get("step", "N/A")
127
+ ts = v.get("timestamp", "N/A")
128
+ val = v.get("value", "N/A")
129
+ output.append(f" {step} | {ts} | {val}")
130
+
131
+ return "\n".join(output)
132
+
133
+
134
+ def format_alerts(alerts: list[dict]) -> str:
135
+ """Format alerts in human-readable format."""
136
+ if not alerts:
137
+ return "No alerts found."
138
+
139
+ output = [f"Found {len(alerts)} alert(s):\n"]
140
+ output.append("Timestamp | Run | Level | Title | Text | Step")
141
+ output.append("-" * 80)
142
+
143
+ for a in alerts:
144
+ ts = a.get("timestamp", "N/A")
145
+ run = a.get("run", "N/A")
146
+ level = a.get("level", "N/A").upper()
147
+ title = a.get("title", "")
148
+ text = a.get("text", "") or ""
149
+ step = a.get("step", "N/A")
150
+ output.append(f"{ts} | {run} | {level} | {title} | {text} | {step}")
151
+
152
+ return "\n".join(output)
153
+
154
+
155
+ def format_query_result(result: dict[str, Any]) -> str:
156
+ """Format SQL query results in human-readable format."""
157
+ columns = result.get("columns", [])
158
+ rows = result.get("rows", [])
159
+ row_count = result.get("row_count", 0)
160
+
161
+ if not columns:
162
+ return f"Query returned {row_count} row(s)."
163
+
164
+ rendered_rows = []
165
+ for row in rows:
166
+ rendered_rows.append(
167
+ [
168
+ "" if row.get(column) is None else str(row.get(column))
169
+ for column in columns
170
+ ]
171
+ )
172
+
173
+ widths = []
174
+ for idx, column in enumerate(columns):
175
+ cell_width = max(
176
+ (len(rendered_row[idx]) for rendered_row in rendered_rows), default=0
177
+ )
178
+ widths.append(max(len(column), cell_width))
179
+
180
+ header = " | ".join(
181
+ column.ljust(width) for column, width in zip(columns, widths, strict=False)
182
+ )
183
+ separator = "-+-".join("-" * width for width in widths)
184
+ output = [f"Query returned {row_count} row(s).", header, separator]
185
+
186
+ if not rendered_rows:
187
+ output.append("(no rows)")
188
+ return "\n".join(output)
189
+
190
+ for rendered_row in rendered_rows:
191
+ output.append(
192
+ " | ".join(
193
+ value.ljust(width)
194
+ for value, width in zip(rendered_row, widths, strict=False)
195
+ )
196
+ )
197
+
198
+ return "\n".join(output)
199
+
200
+
201
+ def error_exit(message: str, code: int = 1) -> None:
202
+ """Print error message and exit."""
203
+ print(f"Error: {message}", file=sys.stderr)
204
+ sys.exit(code)
trackio/commit_scheduler.py ADDED
@@ -0,0 +1,310 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Originally copied from https://github.com/huggingface/huggingface_hub/blob/d0a948fc2a32ed6e557042a95ef3e4af97ec4a7c/src/huggingface_hub/_commit_scheduler.py
2
+
3
+ import atexit
4
+ import logging
5
+ import time
6
+ from concurrent.futures import Future
7
+ from dataclasses import dataclass
8
+ from pathlib import Path
9
+ from threading import Lock, Thread
10
+ from typing import Callable, Dict, List, Union
11
+
12
+ from huggingface_hub.hf_api import (
13
+ DEFAULT_IGNORE_PATTERNS,
14
+ CommitInfo,
15
+ CommitOperationAdd,
16
+ HfApi,
17
+ )
18
+ from huggingface_hub.utils import filter_repo_objects
19
+
20
+ logger = logging.getLogger(__name__)
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class _FileToUpload:
25
+ """Temporary dataclass to store info about files to upload. Not meant to be used directly."""
26
+
27
+ local_path: Path
28
+ path_in_repo: str
29
+ size_limit: int
30
+ last_modified: float
31
+
32
+
33
+ class CommitScheduler:
34
+ """
35
+ Scheduler to upload a local folder to the Hub at regular intervals (e.g. push to hub every 5 minutes).
36
+
37
+ The recommended way to use the scheduler is to use it as a context manager. This ensures that the scheduler is
38
+ properly stopped and the last commit is triggered when the script ends. The scheduler can also be stopped manually
39
+ with the `stop` method. Checkout the [upload guide](https://huggingface.co/docs/huggingface_hub/guides/upload#scheduled-uploads)
40
+ to learn more about how to use it.
41
+
42
+ Args:
43
+ repo_id (`str`):
44
+ The id of the repo to commit to.
45
+ folder_path (`str` or `Path`):
46
+ Path to the local folder to upload regularly.
47
+ every (`int` or `float`, *optional*):
48
+ The number of minutes between each commit. Defaults to 5 minutes.
49
+ path_in_repo (`str`, *optional*):
50
+ Relative path of the directory in the repo, for example: `"checkpoints/"`. Defaults to the root folder
51
+ of the repository.
52
+ repo_type (`str`, *optional*):
53
+ The type of the repo to commit to. Defaults to `model`.
54
+ revision (`str`, *optional*):
55
+ The revision of the repo to commit to. Defaults to `main`.
56
+ private (`bool`, *optional*):
57
+ Whether to make the repo private. If `None` (default), the repo will be public unless the organization's default is private. This value is ignored if the repo already exists.
58
+ token (`str`, *optional*):
59
+ The token to use to commit to the repo. Defaults to the token saved on the machine.
60
+ allow_patterns (`List[str]` or `str`, *optional*):
61
+ If provided, only files matching at least one pattern are uploaded.
62
+ ignore_patterns (`List[str]` or `str`, *optional*):
63
+ If provided, files matching any of the patterns are not uploaded.
64
+ squash_history (`bool`, *optional*):
65
+ Whether to squash the history of the repo after each commit. Defaults to `False`. Squashing commits is
66
+ useful to avoid degraded performances on the repo when it grows too large.
67
+ hf_api (`HfApi`, *optional*):
68
+ The [`HfApi`] client to use to commit to the Hub. Can be set with custom settings (user agent, token,...).
69
+ on_before_commit (`Callable[[], None]`, *optional*):
70
+ If specified, a function that will be called before the CommitScheduler lists files to create a commit.
71
+
72
+ Example:
73
+ ```py
74
+ >>> from pathlib import Path
75
+ >>> from huggingface_hub import CommitScheduler
76
+
77
+ # Scheduler uploads every 10 minutes
78
+ >>> csv_path = Path("watched_folder/data.csv")
79
+ >>> CommitScheduler(repo_id="test_scheduler", repo_type="dataset", folder_path=csv_path.parent, every=10)
80
+
81
+ >>> with csv_path.open("a") as f:
82
+ ... f.write("first line")
83
+
84
+ # Some time later (...)
85
+ >>> with csv_path.open("a") as f:
86
+ ... f.write("second line")
87
+ ```
88
+
89
+ Example using a context manager:
90
+ ```py
91
+ >>> from pathlib import Path
92
+ >>> from huggingface_hub import CommitScheduler
93
+
94
+ >>> with CommitScheduler(repo_id="test_scheduler", repo_type="dataset", folder_path="watched_folder", every=10) as scheduler:
95
+ ... csv_path = Path("watched_folder/data.csv")
96
+ ... with csv_path.open("a") as f:
97
+ ... f.write("first line")
98
+ ... (...)
99
+ ... with csv_path.open("a") as f:
100
+ ... f.write("second line")
101
+
102
+ # Scheduler is now stopped and last commit have been triggered
103
+ ```
104
+ """
105
+
106
+ def __init__(
107
+ self,
108
+ *,
109
+ repo_id: str,
110
+ folder_path: Union[str, Path],
111
+ every: Union[int, float] = 5,
112
+ path_in_repo: str | None = None,
113
+ repo_type: str | None = None,
114
+ revision: str | None = None,
115
+ private: bool | None = None,
116
+ token: str | None = None,
117
+ allow_patterns: list[str] | str | None = None,
118
+ ignore_patterns: list[str] | str | None = None,
119
+ squash_history: bool = False,
120
+ hf_api: HfApi | None = None,
121
+ on_before_commit: Callable[[], None] | None = None,
122
+ ) -> None:
123
+ self.api = hf_api or HfApi(token=token)
124
+ self.on_before_commit = on_before_commit
125
+
126
+ # Folder
127
+ self.folder_path = Path(folder_path).expanduser().resolve()
128
+ self.path_in_repo = path_in_repo or ""
129
+ self.allow_patterns = allow_patterns
130
+
131
+ if ignore_patterns is None:
132
+ ignore_patterns = []
133
+ elif isinstance(ignore_patterns, str):
134
+ ignore_patterns = [ignore_patterns]
135
+ self.ignore_patterns = ignore_patterns + DEFAULT_IGNORE_PATTERNS
136
+
137
+ if self.folder_path.is_file():
138
+ raise ValueError(
139
+ f"'folder_path' must be a directory, not a file: '{self.folder_path}'."
140
+ )
141
+ self.folder_path.mkdir(parents=True, exist_ok=True)
142
+
143
+ # Repository
144
+ repo_url = self.api.create_repo(
145
+ repo_id=repo_id, private=private, repo_type=repo_type, exist_ok=True
146
+ )
147
+ self.repo_id = repo_url.repo_id
148
+ self.repo_type = repo_type
149
+ self.revision = revision
150
+ self.token = token
151
+
152
+ self.last_uploaded: Dict[Path, float] = {}
153
+ self.last_push_time: float | None = None
154
+
155
+ if not every > 0:
156
+ raise ValueError(f"'every' must be a positive integer, not '{every}'.")
157
+ self.lock = Lock()
158
+ self.every = every
159
+ self.squash_history = squash_history
160
+
161
+ logger.info(
162
+ f"Scheduled job to push '{self.folder_path}' to '{self.repo_id}' every {self.every} minutes."
163
+ )
164
+ self._scheduler_thread = Thread(target=self._run_scheduler, daemon=True)
165
+ self._scheduler_thread.start()
166
+ atexit.register(self._push_to_hub)
167
+
168
+ self.__stopped = False
169
+
170
+ def stop(self) -> None:
171
+ """Stop the scheduler.
172
+
173
+ A stopped scheduler cannot be restarted. Mostly for tests purposes.
174
+ """
175
+ self.__stopped = True
176
+
177
+ def __enter__(self) -> "CommitScheduler":
178
+ return self
179
+
180
+ def __exit__(self, exc_type, exc_value, traceback) -> None:
181
+ # Upload last changes before exiting
182
+ self.trigger().result()
183
+ self.stop()
184
+ return
185
+
186
+ def _run_scheduler(self) -> None:
187
+ """Dumb thread waiting between each scheduled push to Hub."""
188
+ while True:
189
+ self.last_future = self.trigger()
190
+ time.sleep(self.every * 60)
191
+ if self.__stopped:
192
+ break
193
+
194
+ def trigger(self) -> Future:
195
+ """Trigger a `push_to_hub` and return a future.
196
+
197
+ This method is automatically called every `every` minutes. You can also call it manually to trigger a commit
198
+ immediately, without waiting for the next scheduled commit.
199
+ """
200
+ return self.api.run_as_future(self._push_to_hub)
201
+
202
+ def _push_to_hub(self) -> CommitInfo | None:
203
+ if self.__stopped: # If stopped, already scheduled commits are ignored
204
+ return None
205
+
206
+ logger.info("(Background) scheduled commit triggered.")
207
+ try:
208
+ value = self.push_to_hub()
209
+ if self.squash_history:
210
+ logger.info("(Background) squashing repo history.")
211
+ self.api.super_squash_history(
212
+ repo_id=self.repo_id, repo_type=self.repo_type, branch=self.revision
213
+ )
214
+ return value
215
+ except Exception as e:
216
+ logger.error(
217
+ f"Error while pushing to Hub: {e}"
218
+ ) # Depending on the setup, error might be silenced
219
+ raise
220
+
221
+ def push_to_hub(self) -> CommitInfo | None:
222
+ """
223
+ Push folder to the Hub and return the commit info.
224
+
225
+ <Tip warning={true}>
226
+
227
+ This method is not meant to be called directly. It is run in the background by the scheduler, respecting a
228
+ queue mechanism to avoid concurrent commits. Making a direct call to the method might lead to concurrency
229
+ issues.
230
+
231
+ </Tip>
232
+
233
+ The default behavior of `push_to_hub` is to assume an append-only folder. It lists all files in the folder and
234
+ uploads only changed files. If no changes are found, the method returns without committing anything. If you want
235
+ to change this behavior, you can inherit from [`CommitScheduler`] and override this method. This can be useful
236
+ for example to compress data together in a single file before committing. For more details and examples, check
237
+ out our [integration guide](https://huggingface.co/docs/huggingface_hub/main/en/guides/upload#scheduled-uploads).
238
+ """
239
+ # Check files to upload (with lock)
240
+ with self.lock:
241
+ if self.on_before_commit is not None:
242
+ self.on_before_commit()
243
+
244
+ logger.debug("Listing files to upload for scheduled commit.")
245
+
246
+ # List files from folder (taken from `_prepare_upload_folder_additions`)
247
+ relpath_to_abspath = {
248
+ path.relative_to(self.folder_path).as_posix(): path
249
+ for path in sorted(
250
+ self.folder_path.glob("**/*")
251
+ ) # sorted to be deterministic
252
+ if path.is_file()
253
+ }
254
+ prefix = f"{self.path_in_repo.strip('/')}/" if self.path_in_repo else ""
255
+
256
+ # Filter with pattern + filter out unchanged files + retrieve current file size
257
+ files_to_upload: List[_FileToUpload] = []
258
+ for relpath in filter_repo_objects(
259
+ relpath_to_abspath.keys(),
260
+ allow_patterns=self.allow_patterns,
261
+ ignore_patterns=self.ignore_patterns,
262
+ ):
263
+ local_path = relpath_to_abspath[relpath]
264
+ stat = local_path.stat()
265
+ if (
266
+ self.last_uploaded.get(local_path) is None
267
+ or self.last_uploaded[local_path] != stat.st_mtime
268
+ ):
269
+ files_to_upload.append(
270
+ _FileToUpload(
271
+ local_path=local_path,
272
+ path_in_repo=prefix + relpath,
273
+ size_limit=stat.st_size,
274
+ last_modified=stat.st_mtime,
275
+ )
276
+ )
277
+
278
+ # Return if nothing to upload
279
+ if len(files_to_upload) == 0:
280
+ logger.debug("Dropping schedule commit: no changed file to upload.")
281
+ return None
282
+
283
+ # Convert `_FileToUpload` as `CommitOperationAdd` (=> compute file shas + limit to file size)
284
+ logger.debug("Removing unchanged files since previous scheduled commit.")
285
+ add_operations = [
286
+ CommitOperationAdd(
287
+ # TODO: Cap the file to its current size, even if the user append data to it while a scheduled commit is happening
288
+ # (requires an upstream fix for XET-535: `hf_xet` should support `BinaryIO` for upload)
289
+ path_or_fileobj=file_to_upload.local_path,
290
+ path_in_repo=file_to_upload.path_in_repo,
291
+ )
292
+ for file_to_upload in files_to_upload
293
+ ]
294
+
295
+ # Upload files (append mode expected - no need for lock)
296
+ logger.debug("Uploading files for scheduled commit.")
297
+ commit_info = self.api.create_commit(
298
+ repo_id=self.repo_id,
299
+ repo_type=self.repo_type,
300
+ operations=add_operations,
301
+ commit_message="Scheduled Commit",
302
+ revision=self.revision,
303
+ )
304
+
305
+ for file in files_to_upload:
306
+ self.last_uploaded[file.local_path] = file.last_modified
307
+
308
+ self.last_push_time = time.time()
309
+
310
+ return commit_info
trackio/context_vars.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import contextvars
2
+ from typing import TYPE_CHECKING
3
+
4
+ if TYPE_CHECKING:
5
+ from trackio.run import Run
6
+
7
+ current_run: contextvars.ContextVar["Run | None"] = contextvars.ContextVar(
8
+ "current_run", default=None
9
+ )
10
+ current_project: contextvars.ContextVar[str | None] = contextvars.ContextVar(
11
+ "current_project", default=None
12
+ )
13
+ current_server: contextvars.ContextVar[str | None] = contextvars.ContextVar(
14
+ "current_server", default=None
15
+ )
16
+ current_space_id: contextvars.ContextVar[str | None] = contextvars.ContextVar(
17
+ "current_space_id", default=None
18
+ )
19
+ current_server_write_token: contextvars.ContextVar[str | None] = contextvars.ContextVar(
20
+ "current_server_write_token", default=None
21
+ )
trackio/deploy.py ADDED
@@ -0,0 +1,1107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import importlib.metadata
2
+ import io
3
+ import json as json_mod
4
+ import os
5
+ import shutil
6
+ import sys
7
+ import tempfile
8
+ import threading
9
+ import time
10
+ from collections import Counter
11
+ from importlib.resources import files
12
+ from pathlib import Path
13
+
14
+ if sys.version_info >= (3, 11):
15
+ import tomllib
16
+ else:
17
+ import tomli as tomllib
18
+
19
+ import httpx
20
+ import huggingface_hub
21
+ from gradio_client import handle_file
22
+ from httpx import ReadTimeout
23
+ from huggingface_hub import Volume
24
+ from huggingface_hub.errors import (
25
+ BucketNotFoundError,
26
+ HfHubHTTPError,
27
+ RepositoryNotFoundError,
28
+ )
29
+
30
+ import trackio
31
+ from trackio.bucket_storage import (
32
+ create_bucket_if_not_exists,
33
+ export_from_bucket_for_static,
34
+ upload_project_to_bucket,
35
+ upload_project_to_bucket_for_static,
36
+ )
37
+ from trackio.remote_client import RemoteClient
38
+ from trackio.sqlite_storage import SQLiteStorage
39
+ from trackio.utils import (
40
+ MEDIA_DIR,
41
+ get_or_create_project_hash,
42
+ on_spaces,
43
+ preprocess_space_and_dataset_ids,
44
+ )
45
+
46
+ SPACE_HOST_URL = "https://{user_name}-{space_name}.hf.space/"
47
+ SPACE_URL = "https://huggingface.co/spaces/{space_id}"
48
+ _BOLD_ORANGE = "\033[1m\033[38;5;208m"
49
+ _RESET = "\033[0m"
50
+
51
+
52
+ def raise_if_space_is_frozen_for_logging(space_id: str) -> None:
53
+ try:
54
+ info = huggingface_hub.HfApi().space_info(space_id)
55
+ except RepositoryNotFoundError:
56
+ return
57
+ if getattr(info, "sdk", None) == "static":
58
+ raise RuntimeError(
59
+ f"Cannot log to Hugging Face Space '{space_id}' because it has been frozen "
60
+ f"(it uses the static SDK: a read-only dashboard with no live Trackio server).\n\n"
61
+ f"Use a different space_id for training, or create a new Gradio Trackio Space. "
62
+ f"Freezing converts a live Gradio Space to static after a run; a frozen Space "
63
+ f'cannot accept new logs. See trackio.sync(..., sdk="static") in the Trackio docs.'
64
+ )
65
+
66
+
67
+ def _readme_linked_hub_yaml(dataset_id: str | None) -> str:
68
+ if dataset_id is not None:
69
+ return f"datasets:\n - {dataset_id}\n"
70
+ return ""
71
+
72
+
73
+ _SPACE_APP_PY = "import trackio\ntrackio.show()\n"
74
+
75
+
76
+ def _retry_hf_write(op_name: str, fn, retries: int = 4, initial_delay: float = 1.5):
77
+ delay = initial_delay
78
+ for attempt in range(1, retries + 1):
79
+ try:
80
+ return fn()
81
+ except ReadTimeout:
82
+ if attempt == retries:
83
+ raise
84
+ print(
85
+ f"* {op_name} timed out (attempt {attempt}/{retries}). Retrying in {delay:.1f}s..."
86
+ )
87
+ time.sleep(delay)
88
+ delay = min(delay * 2, 12)
89
+ except HfHubHTTPError as e:
90
+ status = e.response.status_code if e.response is not None else None
91
+ if status is None or status < 500 or attempt == retries:
92
+ raise
93
+ print(
94
+ f"* {op_name} failed with HTTP {status} (attempt {attempt}/{retries}). Retrying in {delay:.1f}s..."
95
+ )
96
+ time.sleep(delay)
97
+ delay = min(delay * 2, 12)
98
+
99
+
100
+ def _get_space_volumes(space_id: str) -> list[Volume]:
101
+ """
102
+ Return mounted volumes for a Space.
103
+
104
+ `HfApi.get_space_runtime()` does not always populate `volumes`, even when the
105
+ mount exists. Fall back to `space_info().runtime.volumes`, which currently
106
+ carries the volume metadata for running Spaces.
107
+ """
108
+ hf_api = huggingface_hub.HfApi()
109
+ runtime = hf_api.get_space_runtime(space_id)
110
+ if runtime.volumes:
111
+ return list(runtime.volumes)
112
+
113
+ info = hf_api.space_info(space_id)
114
+ if info.runtime and info.runtime.volumes:
115
+ return list(info.runtime.volumes)
116
+
117
+ return []
118
+
119
+
120
+ def _get_space_bucket_at_data_mount(space_id: str) -> str | None:
121
+ for volume in _get_space_volumes(space_id):
122
+ if volume.type == "bucket" and volume.mount_path == "/data":
123
+ return volume.source
124
+ return None
125
+
126
+
127
+ def _get_existing_space_bucket(space_id: str) -> str | None:
128
+ """Return the Trackio bucket for a Space, preferring the canonical /data mount."""
129
+ bucket_at_data = _get_space_bucket_at_data_mount(space_id)
130
+ if bucket_at_data is not None:
131
+ return bucket_at_data
132
+
133
+ for volume in _get_space_volumes(space_id):
134
+ if volume.type == "bucket":
135
+ return volume.source
136
+ return None
137
+
138
+
139
+ def _ensure_bucket_mounted_at_data(
140
+ space_id: str,
141
+ bucket_id: str,
142
+ hf_api: huggingface_hub.HfApi | None = None,
143
+ ) -> None:
144
+ hf_api = hf_api or huggingface_hub.HfApi()
145
+ existing = _get_space_volumes(space_id)
146
+ already_mounted = any(
147
+ v.type == "bucket" and v.source == bucket_id and v.mount_path == "/data"
148
+ for v in existing
149
+ )
150
+ if not already_mounted:
151
+ preserved = [
152
+ v
153
+ for v in existing
154
+ if not (
155
+ v.type == "bucket"
156
+ and (v.source == bucket_id or v.mount_path == "/data")
157
+ )
158
+ ]
159
+ hf_api.set_space_volumes(
160
+ space_id,
161
+ preserved + [Volume(type="bucket", source=bucket_id, mount_path="/data")],
162
+ )
163
+ print(f"* Attached bucket {bucket_id} at '/data'")
164
+
165
+ huggingface_hub.add_space_variable(space_id, "TRACKIO_DIR", "/data/trackio")
166
+
167
+
168
+ def _bucket_exists(bucket_id: str, hf_api: huggingface_hub.HfApi | None = None) -> bool:
169
+ hf_api = hf_api or huggingface_hub.HfApi()
170
+ try:
171
+ hf_api.bucket_info(bucket_id)
172
+ return True
173
+ except BucketNotFoundError:
174
+ return False
175
+
176
+
177
+ def _find_available_bucket_id(
178
+ preferred_bucket_id: str, hf_api: huggingface_hub.HfApi | None = None
179
+ ) -> str:
180
+ hf_api = hf_api or huggingface_hub.HfApi()
181
+ if not _bucket_exists(preferred_bucket_id, hf_api):
182
+ return preferred_bucket_id
183
+
184
+ suffix = 2
185
+ while True:
186
+ candidate = f"{preferred_bucket_id}-{suffix}"
187
+ if not _bucket_exists(candidate, hf_api):
188
+ return candidate
189
+ suffix += 1
190
+
191
+
192
+ def resolve_auto_bucket_id(
193
+ space_id: str,
194
+ preferred_bucket_id: str,
195
+ hf_api: huggingface_hub.HfApi | None = None,
196
+ ) -> str:
197
+ """
198
+ Resolve the bucket to use for an auto-generated bucket ID.
199
+
200
+ Rules:
201
+ - Existing Space with a bucket mounted at /data -> reuse that bucket.
202
+ - Otherwise -> use the preferred auto bucket ID if free, or a suffixed variant.
203
+ """
204
+ hf_api = hf_api or huggingface_hub.HfApi()
205
+
206
+ try:
207
+ hf_api.space_info(space_id)
208
+ except RepositoryNotFoundError:
209
+ existing_bucket_id = None
210
+ else:
211
+ existing_bucket_id = _get_existing_space_bucket(space_id)
212
+ if existing_bucket_id is not None:
213
+ return existing_bucket_id
214
+
215
+ bucket_id = _find_available_bucket_id(preferred_bucket_id, hf_api)
216
+ if bucket_id != preferred_bucket_id:
217
+ print(
218
+ f"* Auto-generated bucket {preferred_bucket_id} already exists; "
219
+ f"using {bucket_id} instead"
220
+ )
221
+ return bucket_id
222
+
223
+
224
+ def _get_source_install_dependencies() -> str:
225
+ """Get trackio dependencies from pyproject.toml for source installs."""
226
+ trackio_path = files("trackio")
227
+ pyproject_path = Path(trackio_path).parent / "pyproject.toml"
228
+ with open(pyproject_path, "rb") as f:
229
+ pyproject = tomllib.load(f)
230
+ deps = pyproject["project"]["dependencies"]
231
+ spaces_deps = (
232
+ pyproject["project"].get("optional-dependencies", {}).get("spaces", [])
233
+ )
234
+ mcp_deps = pyproject["project"].get("optional-dependencies", {}).get("mcp", [])
235
+ return "\n".join(deps + spaces_deps + mcp_deps)
236
+
237
+
238
+ def _get_space_install_requirement() -> str:
239
+ return f"trackio[spaces,mcp]=={trackio.__version__}"
240
+
241
+
242
+ def _is_trackio_installed_from_source() -> bool:
243
+ """Check if trackio is installed from source/editable install vs PyPI."""
244
+ try:
245
+ trackio_file = trackio.__file__
246
+ if "site-packages" not in trackio_file and "dist-packages" not in trackio_file:
247
+ return True
248
+
249
+ dist = importlib.metadata.distribution("trackio")
250
+ if dist.files:
251
+ files = list(dist.files)
252
+ has_pth = any(".pth" in str(f) for f in files)
253
+ if has_pth:
254
+ return True
255
+
256
+ return False
257
+ except (
258
+ AttributeError,
259
+ importlib.metadata.PackageNotFoundError,
260
+ importlib.metadata.MetadataError,
261
+ ValueError,
262
+ TypeError,
263
+ ):
264
+ return True
265
+
266
+
267
+ def deploy_as_space(
268
+ space_id: str,
269
+ space_storage: huggingface_hub.SpaceStorage | None = None,
270
+ dataset_id: str | None = None,
271
+ bucket_id: str | None = None,
272
+ private: bool | None = None,
273
+ ):
274
+ if on_spaces(): # in case a repo with this function is uploaded to spaces
275
+ return
276
+
277
+ if dataset_id is not None and bucket_id is not None:
278
+ raise ValueError(
279
+ "Cannot use bucket volume options together with dataset_id; use one persistence mode."
280
+ )
281
+
282
+ trackio_path = files("trackio")
283
+
284
+ hf_api = huggingface_hub.HfApi()
285
+
286
+ try:
287
+ huggingface_hub.create_repo(
288
+ space_id,
289
+ private=private,
290
+ space_sdk="gradio",
291
+ space_storage=space_storage,
292
+ repo_type="space",
293
+ exist_ok=True,
294
+ )
295
+ except HfHubHTTPError as e:
296
+ if e.response.status_code in [401, 403]: # unauthorized or forbidden
297
+ print("Need 'write' access token to create a Spaces repo.")
298
+ huggingface_hub.login(add_to_git_credential=False)
299
+ huggingface_hub.create_repo(
300
+ space_id,
301
+ private=private,
302
+ space_sdk="gradio",
303
+ space_storage=space_storage,
304
+ repo_type="space",
305
+ exist_ok=True,
306
+ )
307
+ else:
308
+ raise ValueError(f"Failed to create Space: {e}")
309
+
310
+ # We can assume huggingface-hub is available; requirements.txt pins trackio.
311
+ # Make sure necessary dependencies are installed by creating a requirements.txt.
312
+ is_source_install = _is_trackio_installed_from_source()
313
+
314
+ if bucket_id is not None:
315
+ create_bucket_if_not_exists(bucket_id, private=private)
316
+
317
+ with open(Path(trackio_path, "README.md"), "r") as f:
318
+ readme_content = f.read()
319
+ readme_content = readme_content.replace("sdk_version: {GRADIO_VERSION}\n", "")
320
+ readme_content = readme_content.replace("{APP_FILE}", "app.py")
321
+ readme_content = readme_content.replace(
322
+ "{LINKED_HUB_METADATA}", _readme_linked_hub_yaml(dataset_id)
323
+ )
324
+ readme_buffer = io.BytesIO(readme_content.encode("utf-8"))
325
+ hf_api.upload_file(
326
+ path_or_fileobj=readme_buffer,
327
+ path_in_repo="README.md",
328
+ repo_id=space_id,
329
+ repo_type="space",
330
+ )
331
+
332
+ if is_source_install:
333
+ requirements_content = _get_source_install_dependencies()
334
+ else:
335
+ requirements_content = _get_space_install_requirement()
336
+
337
+ requirements_buffer = io.BytesIO(requirements_content.encode("utf-8"))
338
+ hf_api.upload_file(
339
+ path_or_fileobj=requirements_buffer,
340
+ path_in_repo="requirements.txt",
341
+ repo_id=space_id,
342
+ repo_type="space",
343
+ )
344
+
345
+ huggingface_hub.utils.disable_progress_bars()
346
+
347
+ if is_source_install:
348
+ dist_index = (
349
+ Path(trackio.__file__).resolve().parent / "frontend" / "dist" / "index.html"
350
+ )
351
+ if not dist_index.is_file():
352
+ raise ValueError(
353
+ "The Trackio frontend build is missing. From the repository root run "
354
+ "`cd trackio/frontend && npm ci && npm run build`, then deploy again."
355
+ )
356
+ hf_api.upload_folder(
357
+ repo_id=space_id,
358
+ repo_type="space",
359
+ folder_path=trackio_path,
360
+ path_in_repo="trackio",
361
+ ignore_patterns=[
362
+ "README.md",
363
+ "frontend/node_modules/**",
364
+ "frontend/src/**",
365
+ "frontend/.gitignore",
366
+ "frontend/package.json",
367
+ "frontend/package-lock.json",
368
+ "frontend/vite.config.js",
369
+ "frontend/svelte.config.js",
370
+ "**/__pycache__/**",
371
+ "*.pyc",
372
+ ],
373
+ )
374
+
375
+ app_file_content = _SPACE_APP_PY
376
+ app_file_buffer = io.BytesIO(app_file_content.encode("utf-8"))
377
+ hf_api.upload_file(
378
+ path_or_fileobj=app_file_buffer,
379
+ path_in_repo="app.py",
380
+ repo_id=space_id,
381
+ repo_type="space",
382
+ )
383
+
384
+ if hf_token := huggingface_hub.utils.get_token():
385
+ huggingface_hub.add_space_secret(space_id, "HF_TOKEN", hf_token)
386
+ if bucket_id is not None:
387
+ _ensure_bucket_mounted_at_data(space_id, bucket_id, hf_api)
388
+ elif dataset_id is not None:
389
+ huggingface_hub.add_space_variable(space_id, "TRACKIO_DATASET_ID", dataset_id)
390
+ if logo_light_url := os.environ.get("TRACKIO_LOGO_LIGHT_URL"):
391
+ huggingface_hub.add_space_variable(
392
+ space_id, "TRACKIO_LOGO_LIGHT_URL", logo_light_url
393
+ )
394
+ if logo_dark_url := os.environ.get("TRACKIO_LOGO_DARK_URL"):
395
+ huggingface_hub.add_space_variable(
396
+ space_id, "TRACKIO_LOGO_DARK_URL", logo_dark_url
397
+ )
398
+ if plot_order := os.environ.get("TRACKIO_PLOT_ORDER"):
399
+ huggingface_hub.add_space_variable(space_id, "TRACKIO_PLOT_ORDER", plot_order)
400
+ if theme := os.environ.get("TRACKIO_THEME"):
401
+ huggingface_hub.add_space_variable(space_id, "TRACKIO_THEME", theme)
402
+ huggingface_hub.add_space_variable(space_id, "GRADIO_MCP_SERVER", "True")
403
+
404
+
405
+ def create_space_if_not_exists(
406
+ space_id: str,
407
+ space_storage: huggingface_hub.SpaceStorage | None = None,
408
+ dataset_id: str | None = None,
409
+ bucket_id: str | None = None,
410
+ private: bool | None = None,
411
+ ) -> None:
412
+ """
413
+ Creates a new Hugging Face Space if it does not exist.
414
+
415
+ Args:
416
+ space_id (`str`):
417
+ The ID of the Space to create.
418
+ space_storage ([`~huggingface_hub.SpaceStorage`], *optional*):
419
+ Choice of persistent storage tier for the Space.
420
+ dataset_id (`str`, *optional*):
421
+ Deprecated. Use `bucket_id` instead.
422
+ bucket_id (`str`, *optional*):
423
+ Full Hub bucket id (`namespace/name`) to attach via the Hub volumes API (platform mount).
424
+ Sets `TRACKIO_DIR` to the mount path.
425
+ private (`bool`, *optional*):
426
+ Whether to make the Space private. If `None` (default), the repo will be
427
+ public unless the organization's default is private. This value is ignored
428
+ if the repo already exists.
429
+ """
430
+ if "/" not in space_id:
431
+ raise ValueError(
432
+ f"Invalid space ID: {space_id}. Must be in the format: username/reponame or orgname/reponame."
433
+ )
434
+ if dataset_id is not None and "/" not in dataset_id:
435
+ raise ValueError(
436
+ f"Invalid dataset ID: {dataset_id}. Must be in the format: username/datasetname or orgname/datasetname."
437
+ )
438
+ if bucket_id is not None and "/" not in bucket_id:
439
+ raise ValueError(
440
+ f"Invalid bucket ID: {bucket_id}. Must be in the format: username/bucketname or orgname/bucketname."
441
+ )
442
+ try:
443
+ huggingface_hub.repo_info(space_id, repo_type="space")
444
+ print(
445
+ f"* Found existing space: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
446
+ )
447
+ if bucket_id is not None:
448
+ create_bucket_if_not_exists(bucket_id, private=private)
449
+ _ensure_bucket_mounted_at_data(space_id, bucket_id)
450
+ elif dataset_id is not None:
451
+ huggingface_hub.add_space_variable(space_id, "TRACKIO_DATASET_ID", dataset_id)
452
+ return
453
+ except RepositoryNotFoundError:
454
+ pass
455
+ except HfHubHTTPError as e:
456
+ if e.response.status_code in [401, 403]: # unauthorized or forbidden
457
+ print("Need 'write' access token to create a Spaces repo.")
458
+ huggingface_hub.login(add_to_git_credential=False)
459
+ else:
460
+ raise ValueError(f"Failed to create Space: {e}")
461
+
462
+ print(
463
+ f"* Creating new space: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
464
+ )
465
+ deploy_as_space(
466
+ space_id,
467
+ space_storage,
468
+ dataset_id,
469
+ bucket_id,
470
+ private,
471
+ )
472
+ print("* Waiting for Space to be ready...")
473
+ _wait_until_space_running(space_id)
474
+
475
+
476
+ def _wait_until_space_running(space_id: str, timeout: int = 300) -> None:
477
+ hf_api = huggingface_hub.HfApi()
478
+ start = time.time()
479
+ delay = 2
480
+ request_timeout = 45.0
481
+ failure_stages = frozenset(
482
+ ("NO_APP_FILE", "CONFIG_ERROR", "BUILD_ERROR", "RUNTIME_ERROR")
483
+ )
484
+ while time.time() - start < timeout:
485
+ try:
486
+ info = hf_api.space_info(space_id, timeout=request_timeout)
487
+ if info.runtime:
488
+ stage = str(info.runtime.stage)
489
+ if stage in failure_stages:
490
+ raise RuntimeError(
491
+ f"Space {space_id} entered terminal stage {stage}. "
492
+ "Fix README.md or app files; see build logs on the Hub."
493
+ )
494
+ if stage == "RUNNING":
495
+ return
496
+ except RuntimeError:
497
+ raise
498
+ except (huggingface_hub.utils.HfHubHTTPError, httpx.RequestError):
499
+ pass
500
+ time.sleep(delay)
501
+ delay = min(delay * 1.5, 15)
502
+ raise TimeoutError(
503
+ f"Space {space_id} did not reach RUNNING within {timeout}s. "
504
+ "Check status and build logs on the Hub."
505
+ )
506
+
507
+
508
+ def wait_until_space_exists(
509
+ space_id: str,
510
+ ) -> None:
511
+ """
512
+ Blocks the current thread until the Space exists.
513
+
514
+ Args:
515
+ space_id (`str`):
516
+ The ID of the Space to wait for.
517
+
518
+ Raises:
519
+ `TimeoutError`: If waiting for the Space takes longer than expected.
520
+ """
521
+ hf_api = huggingface_hub.HfApi()
522
+ delay = 1
523
+ for _ in range(30):
524
+ try:
525
+ hf_api.space_info(space_id)
526
+ return
527
+ except (huggingface_hub.utils.HfHubHTTPError, httpx.RequestError):
528
+ time.sleep(delay)
529
+ delay = min(delay * 2, 60)
530
+ raise TimeoutError("Waiting for space to exist took longer than expected")
531
+
532
+
533
+ def upload_db_to_space(project: str, space_id: str, force: bool = False) -> None:
534
+ """
535
+ Uploads the database of a local Trackio project to a Hugging Face Space.
536
+
537
+ This uses the Trackio remote client so newer Trackio Spaces can speak the direct
538
+ HTTP API while older Gradio-based Spaces still work through `gradio_client`.
539
+
540
+ Args:
541
+ project (`str`):
542
+ The name of the project to upload.
543
+ space_id (`str`):
544
+ The ID of the Space to upload to.
545
+ force (`bool`, *optional*, defaults to `False`):
546
+ If `True`, overwrites the existing database without prompting. If `False`,
547
+ prompts for confirmation.
548
+ """
549
+ db_path = SQLiteStorage.get_project_db_path(project)
550
+ client = RemoteClient(
551
+ space_id,
552
+ hf_token=huggingface_hub.utils.get_token(),
553
+ httpx_kwargs={"timeout": 90},
554
+ )
555
+
556
+ if not force:
557
+ try:
558
+ existing_projects = client.predict(api_name="/get_all_projects")
559
+ if project in existing_projects:
560
+ response = input(
561
+ f"Database for project '{project}' already exists on Space '{space_id}'. "
562
+ f"Overwrite it? (y/N): "
563
+ )
564
+ if response.lower() not in ["y", "yes"]:
565
+ print("* Upload cancelled.")
566
+ return
567
+ except Exception as e:
568
+ print(f"* Warning: Could not check if project exists on Space: {e}")
569
+ print("* Proceeding with upload...")
570
+
571
+ client.predict(
572
+ api_name="/upload_db_to_space",
573
+ project=project,
574
+ uploaded_db=handle_file(db_path),
575
+ hf_token=huggingface_hub.utils.get_token(),
576
+ )
577
+
578
+
579
+ SYNC_BATCH_SIZE = 500
580
+
581
+
582
+ def sync_incremental(
583
+ project: str,
584
+ space_id: str,
585
+ private: bool | None = None,
586
+ pending_only: bool = False,
587
+ ) -> None:
588
+ """
589
+ Syncs a local Trackio project to a Space via the bulk_log API endpoints
590
+ instead of uploading the entire DB file. Supports incremental sync.
591
+
592
+ Args:
593
+ project: The name of the project to sync.
594
+ space_id: The HF Space ID to sync to.
595
+ private: Whether to make the Space private if creating.
596
+ pending_only: If True, only sync rows tagged with space_id (pending data).
597
+ """
598
+ print(
599
+ f"* Syncing project '{project}' to: {SPACE_URL.format(space_id=space_id)} (please wait...)"
600
+ )
601
+ create_space_if_not_exists(space_id, private=private)
602
+ wait_until_space_exists(space_id)
603
+ hf_token = huggingface_hub.utils.get_token()
604
+ expected_run_counts: Counter[str] = Counter()
605
+
606
+ client = RemoteClient(
607
+ space_id,
608
+ hf_token=hf_token,
609
+ httpx_kwargs={"timeout": 90},
610
+ )
611
+
612
+ if pending_only:
613
+ pending_logs = SQLiteStorage.get_pending_logs(project)
614
+ if pending_logs:
615
+ logs = pending_logs["logs"]
616
+ expected_run_counts.update(log["run"] for log in logs)
617
+ for i in range(0, len(logs), SYNC_BATCH_SIZE):
618
+ batch = logs[i : i + SYNC_BATCH_SIZE]
619
+ print(
620
+ f" Syncing metrics: {min(i + SYNC_BATCH_SIZE, len(logs))}/{len(logs)}..."
621
+ )
622
+ client.predict(api_name="/bulk_log", logs=batch, hf_token=hf_token)
623
+ SQLiteStorage.clear_pending_logs(project, pending_logs["ids"])
624
+
625
+ pending_sys = SQLiteStorage.get_pending_system_logs(project)
626
+ if pending_sys:
627
+ logs = pending_sys["logs"]
628
+ for i in range(0, len(logs), SYNC_BATCH_SIZE):
629
+ batch = logs[i : i + SYNC_BATCH_SIZE]
630
+ print(
631
+ f" Syncing system metrics: {min(i + SYNC_BATCH_SIZE, len(logs))}/{len(logs)}..."
632
+ )
633
+ client.predict(
634
+ api_name="/bulk_log_system", logs=batch, hf_token=hf_token
635
+ )
636
+ SQLiteStorage.clear_pending_system_logs(project, pending_sys["ids"])
637
+
638
+ pending_uploads = SQLiteStorage.get_pending_uploads(project)
639
+ if pending_uploads:
640
+ upload_entries = []
641
+ for u in pending_uploads["uploads"]:
642
+ fp = u["file_path"]
643
+ if os.path.exists(fp):
644
+ upload_entries.append(
645
+ {
646
+ "project": u["project"],
647
+ "run": u["run"],
648
+ "step": u["step"],
649
+ "relative_path": u["relative_path"],
650
+ "uploaded_file": handle_file(fp),
651
+ }
652
+ )
653
+ if upload_entries:
654
+ print(f" Syncing {len(upload_entries)} media files...")
655
+ client.predict(
656
+ api_name="/bulk_upload_media",
657
+ uploads=upload_entries,
658
+ hf_token=hf_token,
659
+ )
660
+ SQLiteStorage.clear_pending_uploads(project, pending_uploads["ids"])
661
+ else:
662
+ all_logs = SQLiteStorage.get_all_logs_for_sync(project)
663
+ if all_logs:
664
+ expected_run_counts.update(log["run"] for log in all_logs)
665
+ for i in range(0, len(all_logs), SYNC_BATCH_SIZE):
666
+ batch = all_logs[i : i + SYNC_BATCH_SIZE]
667
+ print(
668
+ f" Syncing metrics: {min(i + SYNC_BATCH_SIZE, len(all_logs))}/{len(all_logs)}..."
669
+ )
670
+ client.predict(api_name="/bulk_log", logs=batch, hf_token=hf_token)
671
+
672
+ all_sys_logs = SQLiteStorage.get_all_system_logs_for_sync(project)
673
+ if all_sys_logs:
674
+ for i in range(0, len(all_sys_logs), SYNC_BATCH_SIZE):
675
+ batch = all_sys_logs[i : i + SYNC_BATCH_SIZE]
676
+ print(
677
+ f" Syncing system metrics: {min(i + SYNC_BATCH_SIZE, len(all_sys_logs))}/{len(all_sys_logs)}..."
678
+ )
679
+ client.predict(
680
+ api_name="/bulk_log_system", logs=batch, hf_token=hf_token
681
+ )
682
+
683
+ _wait_for_remote_sync(client, project, expected_run_counts)
684
+ SQLiteStorage.set_project_metadata(project, "space_id", space_id)
685
+ print(
686
+ f"* Synced successfully to space: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
687
+ )
688
+
689
+
690
+ def _wait_for_remote_sync(
691
+ client: RemoteClient,
692
+ project: str,
693
+ expected_run_counts: Counter[str],
694
+ timeout: int = 180,
695
+ ) -> None:
696
+ if not expected_run_counts:
697
+ return
698
+
699
+ deadline = time.time() + timeout
700
+ delay = 2
701
+ last_error: Exception | None = None
702
+ pending = dict(expected_run_counts)
703
+
704
+ while time.time() < deadline and pending:
705
+ completed = []
706
+ for run_name, expected_num_logs in pending.items():
707
+ try:
708
+ summary = client.predict(
709
+ project=project, run=run_name, api_name="/get_run_summary"
710
+ )
711
+ if summary.get("num_logs") == expected_num_logs:
712
+ completed.append(run_name)
713
+ except Exception as e:
714
+ last_error = e
715
+ for run_name in completed:
716
+ pending.pop(run_name, None)
717
+ if pending:
718
+ time.sleep(delay)
719
+ delay = min(delay * 1.5, 15)
720
+
721
+ if pending:
722
+ raise TimeoutError(
723
+ f"Remote sync for project '{project}' did not become visible for runs "
724
+ f"{sorted(pending.items())} within {timeout}s. "
725
+ f"Last error: {last_error!r}"
726
+ )
727
+
728
+
729
+ def upload_dataset_for_static(
730
+ project: str,
731
+ dataset_id: str,
732
+ private: bool | None = None,
733
+ ) -> None:
734
+ hf_api = huggingface_hub.HfApi()
735
+
736
+ try:
737
+ huggingface_hub.create_repo(
738
+ dataset_id,
739
+ private=private,
740
+ repo_type="dataset",
741
+ exist_ok=True,
742
+ )
743
+ except HfHubHTTPError as e:
744
+ if e.response.status_code in [401, 403]:
745
+ print("Need 'write' access token to create a Dataset repo.")
746
+ huggingface_hub.login(add_to_git_credential=False)
747
+ huggingface_hub.create_repo(
748
+ dataset_id,
749
+ private=private,
750
+ repo_type="dataset",
751
+ exist_ok=True,
752
+ )
753
+ else:
754
+ raise ValueError(f"Failed to create Dataset: {e}")
755
+
756
+ with tempfile.TemporaryDirectory() as tmp_dir:
757
+ output_dir = Path(tmp_dir)
758
+ SQLiteStorage.export_for_static_space(project, output_dir)
759
+
760
+ media_dir = MEDIA_DIR / project
761
+ if media_dir.exists():
762
+ dest = output_dir / "media"
763
+ shutil.copytree(media_dir, dest)
764
+
765
+ _retry_hf_write(
766
+ "Dataset upload",
767
+ lambda: hf_api.upload_folder(
768
+ repo_id=dataset_id,
769
+ repo_type="dataset",
770
+ folder_path=str(output_dir),
771
+ ),
772
+ )
773
+
774
+ print(f"* Dataset uploaded: https://huggingface.co/datasets/{dataset_id}")
775
+
776
+
777
+ def deploy_as_static_space(
778
+ space_id: str,
779
+ dataset_id: str | None,
780
+ project: str,
781
+ bucket_id: str | None = None,
782
+ private: bool | None = None,
783
+ hf_token: str | None = None,
784
+ ) -> None:
785
+ if on_spaces():
786
+ return
787
+
788
+ hf_api = huggingface_hub.HfApi()
789
+
790
+ try:
791
+ huggingface_hub.create_repo(
792
+ space_id,
793
+ private=private,
794
+ space_sdk="static",
795
+ repo_type="space",
796
+ exist_ok=True,
797
+ )
798
+ except HfHubHTTPError as e:
799
+ if e.response.status_code in [401, 403]:
800
+ print("Need 'write' access token to create a Spaces repo.")
801
+ huggingface_hub.login(add_to_git_credential=False)
802
+ huggingface_hub.create_repo(
803
+ space_id,
804
+ private=private,
805
+ space_sdk="static",
806
+ repo_type="space",
807
+ exist_ok=True,
808
+ )
809
+ else:
810
+ raise ValueError(f"Failed to create Space: {e}")
811
+
812
+ linked = _readme_linked_hub_yaml(dataset_id)
813
+ readme_content = (
814
+ f"---\nsdk: static\npinned: false\ntags:\n - trackio\n{linked}---\n"
815
+ )
816
+ _retry_hf_write(
817
+ "Static Space README upload",
818
+ lambda: hf_api.upload_file(
819
+ path_or_fileobj=io.BytesIO(readme_content.encode("utf-8")),
820
+ path_in_repo="README.md",
821
+ repo_id=space_id,
822
+ repo_type="space",
823
+ ),
824
+ )
825
+
826
+ trackio_path = files("trackio")
827
+ dist_dir = Path(trackio_path).parent / "trackio" / "frontend" / "dist"
828
+ if not dist_dir.is_dir():
829
+ dist_dir = Path(trackio.__file__).resolve().parent / "frontend" / "dist"
830
+ if not dist_dir.is_dir():
831
+ raise ValueError(
832
+ "The Trackio frontend build is missing. From the repository root run "
833
+ "`cd trackio/frontend && npm ci && npm run build`, then deploy again."
834
+ )
835
+
836
+ _retry_hf_write(
837
+ "Static Space frontend upload",
838
+ lambda: hf_api.upload_folder(
839
+ repo_id=space_id,
840
+ repo_type="space",
841
+ folder_path=str(dist_dir),
842
+ ),
843
+ )
844
+
845
+ config = {
846
+ "mode": "static",
847
+ "project": project,
848
+ "private": bool(private),
849
+ }
850
+ if bucket_id is not None:
851
+ config["bucket_id"] = bucket_id
852
+ if dataset_id is not None:
853
+ config["dataset_id"] = dataset_id
854
+ if hf_token and private:
855
+ config["hf_token"] = hf_token
856
+
857
+ _retry_hf_write(
858
+ "Static Space config upload",
859
+ lambda: hf_api.upload_file(
860
+ path_or_fileobj=io.BytesIO(json_mod.dumps(config).encode("utf-8")),
861
+ path_in_repo="config.json",
862
+ repo_id=space_id,
863
+ repo_type="space",
864
+ ),
865
+ )
866
+
867
+ assets_dir = Path(trackio.__file__).resolve().parent / "assets"
868
+ if assets_dir.is_dir():
869
+ _retry_hf_write(
870
+ "Static Space assets upload",
871
+ lambda: hf_api.upload_folder(
872
+ repo_id=space_id,
873
+ repo_type="space",
874
+ folder_path=str(assets_dir),
875
+ path_in_repo="assets",
876
+ ),
877
+ )
878
+
879
+ print(
880
+ f"* Static Space deployed: {_BOLD_ORANGE}{SPACE_URL.format(space_id=space_id)}{_RESET}"
881
+ )
882
+
883
+
884
+ def sync(
885
+ project: str,
886
+ space_id: str | None = None,
887
+ private: bool | None = None,
888
+ force: bool = False,
889
+ run_in_background: bool = False,
890
+ sdk: str = "gradio",
891
+ dataset_id: str | None = None,
892
+ bucket_id: str | None = None,
893
+ ) -> str:
894
+ """
895
+ Syncs a local Trackio project's database to a Hugging Face Space.
896
+ If the Space does not exist, it will be created. Local data is never deleted.
897
+
898
+ **Freezing:** Passing ``sdk="static"`` deploys a static Space backed by an HF Bucket
899
+ (read-only dashboard, no Gradio server). You can sync the same project again later to
900
+ refresh that static Space. If you want a one-time snapshot of an existing Gradio Space,
901
+ use ``freeze()`` instead.
902
+
903
+ Args:
904
+ project (`str`): The name of the project to upload.
905
+ space_id (`str`, *optional*): The ID of the Space to upload to (e.g., `"username/space_id"`).
906
+ If not provided, checks project metadata first, then generates a random space_id.
907
+ private (`bool`, *optional*):
908
+ Whether to make the Space private. If None (default), the repo will be
909
+ public unless the organization's default is private. This value is ignored
910
+ if the repo already exists.
911
+ force (`bool`, *optional*, defaults to `False`):
912
+ If `True`, overwrite the existing database without prompting for confirmation.
913
+ If `False`, prompt the user before overwriting an existing database.
914
+ run_in_background (`bool`, *optional*, defaults to `False`):
915
+ If `True`, the Space creation and database upload will be run in a background thread.
916
+ If `False`, all the steps will be run synchronously.
917
+ sdk (`str`, *optional*, defaults to `"gradio"`):
918
+ The type of Space to deploy. `"gradio"` deploys a Gradio Space with a live
919
+ server. `"static"` freezes the Space: deploys a static Space that reads from an HF Bucket
920
+ (no server needed).
921
+ dataset_id (`str`, *optional*):
922
+ Deprecated. Use `bucket_id` instead.
923
+ bucket_id (`str`, *optional*):
924
+ The ID of the HF Bucket to sync to. By default, a bucket is auto-generated
925
+ from the space_id.
926
+ Returns:
927
+ `str`: The Space ID of the synced project.
928
+ """
929
+ if sdk not in ("gradio", "static"):
930
+ raise ValueError(f"sdk must be 'gradio' or 'static', got '{sdk}'")
931
+ bucket_id_was_explicit = bucket_id is not None
932
+
933
+ if space_id is None:
934
+ space_id = SQLiteStorage.get_space_id(project)
935
+ if space_id is None:
936
+ space_id = f"{project}-{get_or_create_project_hash(project)}"
937
+ space_id, dataset_id, bucket_id = preprocess_space_and_dataset_ids(
938
+ space_id, dataset_id, bucket_id
939
+ )
940
+ if dataset_id is None and bucket_id is not None and not bucket_id_was_explicit:
941
+ bucket_id = resolve_auto_bucket_id(space_id, bucket_id)
942
+
943
+ def _do_sync():
944
+ try:
945
+ info = huggingface_hub.HfApi().space_info(space_id)
946
+ existing_sdk = info.sdk
947
+ if existing_sdk and existing_sdk != sdk:
948
+ raise ValueError(
949
+ f"Space '{space_id}' is a '{existing_sdk}' Space but sdk='{sdk}' was requested. "
950
+ f"The sdk must match the existing Space type."
951
+ )
952
+ except RepositoryNotFoundError:
953
+ pass
954
+
955
+ if sdk == "static":
956
+ if dataset_id is not None:
957
+ upload_dataset_for_static(project, dataset_id, private=private)
958
+ hf_token = huggingface_hub.utils.get_token() if private else None
959
+ deploy_as_static_space(
960
+ space_id,
961
+ dataset_id,
962
+ project,
963
+ private=private,
964
+ hf_token=hf_token,
965
+ )
966
+ elif bucket_id is not None:
967
+ create_bucket_if_not_exists(bucket_id, private=private)
968
+ upload_project_to_bucket_for_static(project, bucket_id)
969
+ print(
970
+ f"* Project data uploaded to bucket: https://huggingface.co/buckets/{bucket_id}"
971
+ )
972
+ deploy_as_static_space(
973
+ space_id,
974
+ None,
975
+ project,
976
+ bucket_id=bucket_id,
977
+ private=private,
978
+ hf_token=huggingface_hub.utils.get_token() if private else None,
979
+ )
980
+ else:
981
+ if bucket_id is not None:
982
+ create_bucket_if_not_exists(bucket_id, private=private)
983
+ upload_project_to_bucket(project, bucket_id)
984
+ print(
985
+ f"* Project data uploaded to bucket: https://huggingface.co/buckets/{bucket_id}"
986
+ )
987
+ create_space_if_not_exists(
988
+ space_id, bucket_id=bucket_id, private=private
989
+ )
990
+ _wait_for_remote_sync(
991
+ RemoteClient(space_id, verbose=False, httpx_kwargs={"timeout": 90}),
992
+ project,
993
+ Counter(
994
+ log["run"]
995
+ for log in SQLiteStorage.get_all_logs_for_sync(project)
996
+ ),
997
+ )
998
+ else:
999
+ sync_incremental(project, space_id, private=private, pending_only=False)
1000
+ SQLiteStorage.set_project_metadata(project, "space_id", space_id)
1001
+
1002
+ if run_in_background:
1003
+ threading.Thread(target=_do_sync).start()
1004
+ else:
1005
+ _do_sync()
1006
+ return space_id
1007
+
1008
+
1009
+ def _get_source_bucket(space_id: str) -> str:
1010
+ bucket_id = _get_existing_space_bucket(space_id)
1011
+ if bucket_id is not None:
1012
+ _ensure_bucket_mounted_at_data(space_id, bucket_id)
1013
+ return bucket_id
1014
+ raise ValueError(
1015
+ f"Space '{space_id}' has no bucket mounted at '/data'. "
1016
+ f"freeze() requires the source Space to use bucket storage."
1017
+ )
1018
+
1019
+
1020
+ def freeze(
1021
+ space_id: str,
1022
+ project: str,
1023
+ new_space_id: str | None = None,
1024
+ private: bool | None = None,
1025
+ bucket_id: str | None = None,
1026
+ ) -> str:
1027
+ """
1028
+ Creates a new static Hugging Face Space containing a read-only snapshot of
1029
+ the data for the specified project from the source Gradio Space. The data is
1030
+ read from the bucket attached to the source Space at freeze time. The original
1031
+ Space is not modified, and the new static Space does not automatically reflect
1032
+ metrics uploaded to the original Gradio Space after the freeze completes.
1033
+
1034
+ Args:
1035
+ space_id (`str`):
1036
+ The ID of the source Gradio Space (e.g., `"username/my-space"` or a
1037
+ short repo name with the logged-in namespace inferred, like `init()`).
1038
+ Must be a Gradio Space with a bucket mounted at `/data`.
1039
+ project (`str`):
1040
+ The name of the project whose data to include in the frozen Space.
1041
+ new_space_id (`str`, *optional*):
1042
+ The ID for the new static Space. If not provided, defaults to
1043
+ `"{space_id}_static"`.
1044
+ private (`bool`, *optional*):
1045
+ Whether to make the new Space private. If None (default), the repo
1046
+ will be public unless the organization's default is private.
1047
+ bucket_id (`str`, *optional*):
1048
+ The ID of the HF Bucket for the new static Space's data storage.
1049
+ If not provided, one is auto-generated from the new Space ID.
1050
+
1051
+ Returns:
1052
+ `str`: The Space ID of the newly created static Space.
1053
+ """
1054
+ space_id, _, _ = preprocess_space_and_dataset_ids(space_id, None, None)
1055
+
1056
+ try:
1057
+ info = huggingface_hub.HfApi().space_info(space_id)
1058
+ if info.sdk != "gradio":
1059
+ raise ValueError(
1060
+ f"Space '{space_id}' is not a Gradio Space (sdk='{info.sdk}'). "
1061
+ f"freeze() requires a Gradio Space as the source."
1062
+ )
1063
+ except RepositoryNotFoundError:
1064
+ raise ValueError(
1065
+ f"Space '{space_id}' not found. Provide an existing Gradio Space ID."
1066
+ )
1067
+
1068
+ source_bucket_id = _get_source_bucket(space_id)
1069
+ print(f"* Reading project data from bucket: {source_bucket_id}")
1070
+
1071
+ bucket_id_was_explicit = bucket_id is not None
1072
+
1073
+ if new_space_id is None:
1074
+ new_space_id = f"{space_id}_static"
1075
+ new_space_id, _dataset_id, bucket_id = preprocess_space_and_dataset_ids(
1076
+ new_space_id, None, bucket_id
1077
+ )
1078
+ if bucket_id is not None and not bucket_id_was_explicit:
1079
+ bucket_id = resolve_auto_bucket_id(new_space_id, bucket_id)
1080
+
1081
+ hf_api = huggingface_hub.HfApi()
1082
+ try:
1083
+ dest_info = hf_api.space_info(new_space_id)
1084
+ tags = dest_info.tags or []
1085
+ if dest_info.sdk != "static" or "trackio" not in tags:
1086
+ raise ValueError(
1087
+ f"Space '{new_space_id}' already exists and is not a Trackio static Space "
1088
+ f"(sdk='{dest_info.sdk}', tags={tags}). Choose a different new_space_id "
1089
+ f"or delete the existing Space first."
1090
+ )
1091
+ except RepositoryNotFoundError:
1092
+ pass
1093
+
1094
+ create_bucket_if_not_exists(bucket_id, private=private)
1095
+ export_from_bucket_for_static(source_bucket_id, bucket_id, project)
1096
+ print(
1097
+ f"* Project data uploaded to bucket: https://huggingface.co/buckets/{bucket_id}"
1098
+ )
1099
+ deploy_as_static_space(
1100
+ new_space_id,
1101
+ None,
1102
+ project,
1103
+ bucket_id=bucket_id,
1104
+ private=private,
1105
+ hf_token=huggingface_hub.utils.get_token() if private else None,
1106
+ )
1107
+ return new_space_id
trackio/dummy_commit_scheduler.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from concurrent.futures import Future
2
+
3
+
4
+ class DummyCommitSchedulerLock:
5
+ def __enter__(self):
6
+ return None
7
+
8
+ def __exit__(self, exception_type, exception_value, exception_traceback):
9
+ pass
10
+
11
+
12
+ class DummyCommitScheduler:
13
+ def __init__(self):
14
+ self.lock = DummyCommitSchedulerLock()
15
+
16
+ def trigger(self) -> Future:
17
+ fut: Future = Future()
18
+ fut.set_result(None)
19
+ return fut
trackio/exceptions.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ class TrackioAPIError(Exception):
2
+ pass
trackio/frontend/dist/assets/index-BgSTPqpC.css ADDED
@@ -0,0 +1 @@
 
 
1
+ :root{--primary-50: #fff7ed;--primary-100: #ffedd5;--primary-200: #fed7aa;--primary-300: #fdba74;--primary-400: #fb923c;--primary-500: #f97316;--primary-600: #ea580c;--primary-700: #c2410c;--primary-800: #9a3412;--primary-900: #7c2d12;--primary-950: #6c2e12;--secondary-50: #eff6ff;--secondary-100: #dbeafe;--secondary-200: #bfdbfe;--secondary-300: #93c5fd;--secondary-400: #60a5fa;--secondary-500: #3b82f6;--secondary-600: #2563eb;--secondary-700: #1d4ed8;--secondary-800: #1e40af;--secondary-900: #1e3a8a;--secondary-950: #1d3660;--neutral-50: #f9fafb;--neutral-100: #f3f4f6;--neutral-200: #e5e7eb;--neutral-300: #d1d5db;--neutral-400: #9ca3af;--neutral-500: #6b7280;--neutral-600: #4b5563;--neutral-700: #374151;--neutral-800: #1f2937;--neutral-900: #111827;--neutral-950: #0b0f19;--size-0-5: 2px;--size-1: 4px;--size-2: 8px;--size-3: 12px;--size-4: 16px;--size-5: 20px;--size-6: 24px;--size-8: 32px;--size-14: 56px;--size-16: 64px;--size-28: 112px;--size-full: 100%;--spacing-xxs: 1px;--spacing-xs: 2px;--spacing-sm: 4px;--spacing-md: 6px;--spacing-lg: 8px;--spacing-xl: 10px;--spacing-xxl: 16px;--radius-xxs: 1px;--radius-xs: 2px;--radius-sm: 3px;--radius-md: 4px;--radius-lg: 5px;--radius-xl: 8px;--radius-xxl: 12px;--text-xxs: 9px;--text-xs: 10px;--text-sm: 12px;--text-md: 14px;--text-lg: 16px;--text-xl: 22px;--text-xxl: 26px;--line-sm: 1.4;--background-fill-primary: white;--background-fill-secondary: var(--neutral-50);--body-text-color: var(--neutral-900);--body-text-color-subdued: var(--neutral-600);--border-color-primary: var(--neutral-200);--color-accent: var(--primary-500);--color-accent-soft: var(--primary-50);--shadow-drop: rgba(0, 0, 0, .05) 0px 1px 2px 0px;--shadow-drop-lg: 0 1px 3px 0 rgb(0 0 0 / .1), 0 1px 2px -1px rgb(0 0 0 / .1);--shadow-inset: rgba(0, 0, 0, .05) 0px 2px 4px 0px inset;--shadow-spread: 3px;--block-title-text-color: var(--neutral-500);--block-title-text-size: var(--text-md);--block-title-text-weight: 400;--block-info-text-color: var(--body-text-color-subdued);--block-info-text-size: var(--text-sm);--input-background-fill: white;--input-background-fill-focus: var(--primary-500);--input-border-color: var(--border-color-primary);--input-border-color-focus: var(--primary-300);--input-border-width: 1px;--input-padding: var(--spacing-xl);--input-placeholder-color: var(--neutral-400);--input-radius: var(--radius-lg);--input-shadow: 0 0 0 var(--shadow-spread) transparent, var(--shadow-inset);--input-shadow-focus: 0 0 0 var(--shadow-spread) var(--primary-50), var(--shadow-inset);--input-text-size: var(--text-md);--checkbox-background-color: var(--background-fill-primary);--checkbox-background-color-focus: var(--checkbox-background-color);--checkbox-background-color-hover: var(--checkbox-background-color);--checkbox-background-color-selected: var(--primary-600);--checkbox-border-color: var(--neutral-300);--checkbox-border-color-focus: var(--primary-500);--checkbox-border-color-hover: var(--neutral-300);--checkbox-border-color-selected: var(--primary-600);--checkbox-border-radius: var(--radius-sm);--checkbox-border-width: var(--input-border-width);--checkbox-label-gap: var(--spacing-lg);--checkbox-label-padding: var(--spacing-md) calc(2 * var(--spacing-md));--checkbox-label-text-size: var(--text-md);--checkbox-shadow: var(--input-shadow);--checkbox-check: url("data:image/svg+xml,%3csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3e%3cpath d='M12.207 4.793a1 1 0 010 1.414l-5 5a1 1 0 01-1.414 0l-2-2a1 1 0 011.414-1.414L6.5 9.086l4.293-4.293a1 1 0 011.414 0z'/%3e%3c/svg%3e");--slider-color: var(--primary-500);--container-radius: var(--radius-lg);--layer-top: 9999}.navbar.svelte-d8j1hi{display:flex;align-items:stretch;border-bottom:1px solid var(--border-color-primary, #e5e7eb);background:var(--background-fill-primary, white);padding:0;flex-shrink:0;min-height:44px}.nav-spacer.svelte-d8j1hi{flex:1 1 0;min-width:0}.nav-tabs.svelte-d8j1hi{display:flex;gap:0;flex-shrink:0;padding-right:8px}.nav-link.svelte-d8j1hi{padding:10px 16px;border:none;background:none;color:var(--body-text-color-subdued, #6b7280);font-size:var(--text-md, 14px);cursor:pointer;white-space:nowrap;border-bottom:2px solid transparent;transition:color .15s;font-weight:400}.nav-link.svelte-d8j1hi:hover{color:var(--body-text-color, #1f2937)}.nav-link.active.svelte-d8j1hi{color:var(--body-text-color, #1f2937);border-bottom-color:var(--body-text-color, #1f2937);font-weight:500}.settings-btn.svelte-d8j1hi{display:flex;align-items:center;gap:6px}.settings-btn.svelte-d8j1hi svg:where(.svelte-d8j1hi){flex-shrink:0}.checkbox-group.svelte-17gmtkf{display:flex;flex-direction:column}.checkbox-item.svelte-17gmtkf{display:flex;align-items:center;gap:8px;padding:3px 0;cursor:pointer;font-size:13px}.checkbox-item.svelte-17gmtkf input[type=checkbox]:where(.svelte-17gmtkf){-moz-appearance:none;appearance:none;-webkit-appearance:none;width:16px;height:16px;margin:0;border:1px solid var(--checkbox-border-color, #d1d5db);border-radius:var(--checkbox-border-radius, 4px);background-color:var(--checkbox-background-color, white);box-shadow:var(--checkbox-shadow);cursor:pointer;flex-shrink:0;transition:background-color .15s,border-color .15s}.checkbox-item.svelte-17gmtkf input[type=checkbox]:where(.svelte-17gmtkf):checked{background-image:var(--checkbox-check);background-color:var(--checkbox-background-color-selected, #f97316);border-color:var(--checkbox-border-color-selected, #f97316)}.checkbox-item.svelte-17gmtkf input[type=checkbox]:where(.svelte-17gmtkf):hover{border-color:var(--checkbox-border-color-hover, #d1d5db)}.color-dot.svelte-17gmtkf{width:10px;height:10px;border-radius:50%;flex-shrink:0}.run-name.svelte-17gmtkf{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--body-text-color, #1f2937)}.dropdown-container.svelte-kgylqb{width:100%}.label.svelte-kgylqb{display:block;font-size:13px;font-weight:500;color:var(--body-text-color-subdued, #6b7280);margin-bottom:6px}.info.svelte-kgylqb{display:block;font-size:12px;color:var(--body-text-color-subdued, #9ca3af);margin-bottom:4px}.wrap.svelte-kgylqb{position:relative;border-radius:var(--input-radius, 8px);background:var(--input-background-fill, white);border:1px solid var(--border-color-primary, #e5e7eb);transition:border-color .15s,box-shadow .15s}.wrap.focused.svelte-kgylqb{border-color:var(--input-border-color-focus, #fdba74);box-shadow:0 0 0 2px var(--primary-50, #fff7ed)}.wrap-inner.svelte-kgylqb{display:flex;position:relative;align-items:center;padding:0 10px}.secondary-wrap.svelte-kgylqb{display:flex;flex:1;align-items:center}input.svelte-kgylqb{margin:0;outline:none;border:none;background:inherit;width:100%;color:var(--body-text-color, #1f2937);font-size:13px;font-family:inherit;padding:7px 0}input.svelte-kgylqb::placeholder{color:var(--input-placeholder-color, #9ca3af)}input[readonly].svelte-kgylqb{cursor:pointer}.icon-wrap.svelte-kgylqb{color:var(--body-text-color-subdued, #9ca3af);width:16px;flex-shrink:0;pointer-events:none}.options.svelte-kgylqb{position:fixed;z-index:var(--layer-top, 9999);margin:0;padding:4px 0;box-shadow:0 4px 12px #0000001f;border-radius:var(--input-radius, 8px);border:1px solid var(--border-color-primary, #e5e7eb);background:var(--background-fill-primary, white);min-width:fit-content;overflow:auto;color:var(--body-text-color, #1f2937);list-style:none}.item.svelte-kgylqb{display:flex;cursor:pointer;padding:6px 10px;font-size:13px;word-break:break-word}.item.svelte-kgylqb:hover,.item.active.svelte-kgylqb{background:var(--background-fill-secondary, #f9fafb)}.item.selected.svelte-kgylqb{font-weight:500}.check-mark.svelte-kgylqb{padding-right:6px;min-width:16px;font-size:12px}.check-mark.hide.svelte-kgylqb{visibility:hidden}.checkbox-container.svelte-oj84db{display:flex;align-items:center;gap:8px;cursor:pointer;margin:8px 0}.label-text.svelte-oj84db{color:var(--body-text-color, #1f2937);font-size:13px;line-height:1.4}input[type=checkbox].svelte-oj84db{--ring-color: transparent;position:relative;-moz-appearance:none;appearance:none;-webkit-appearance:none;width:16px;height:16px;box-shadow:var(--checkbox-shadow);border:1px solid var(--checkbox-border-color, #d1d5db);border-radius:var(--checkbox-border-radius, 4px);background-color:var(--checkbox-background-color, white);flex-shrink:0;cursor:pointer;transition:background-color .15s,border-color .15s}input[type=checkbox].svelte-oj84db:checked,input[type=checkbox].svelte-oj84db:checked:hover,input[type=checkbox].svelte-oj84db:checked:focus{background-image:var(--checkbox-check);background-color:var(--checkbox-background-color-selected, #f97316);border-color:var(--checkbox-border-color-selected, #f97316)}input[type=checkbox].svelte-oj84db:hover{border-color:var(--checkbox-border-color-hover, #d1d5db);background-color:var(--checkbox-background-color-hover, white)}input[type=checkbox].svelte-oj84db:focus{border-color:var(--checkbox-border-color-focus, #f97316);background-color:var(--checkbox-background-color-focus, white);outline:none}.slider-wrap.svelte-wei6ev{display:flex;flex-direction:column;width:100%}.head.svelte-wei6ev{margin-bottom:4px;display:flex;justify-content:space-between;align-items:center;width:100%}.label.svelte-wei6ev{flex:1;font-size:13px;font-weight:500;color:var(--body-text-color-subdued, #6b7280)}.info.svelte-wei6ev{display:block;font-size:12px;color:var(--body-text-color-subdued, #9ca3af);margin-bottom:4px}.slider-input-container.svelte-wei6ev{display:flex;align-items:center;gap:6px}input[type=range].svelte-wei6ev{-webkit-appearance:none;-moz-appearance:none;appearance:none;width:100%;cursor:pointer;outline:none;border-radius:var(--radius-xl, 12px);min-width:var(--size-28, 112px);background:transparent}input[type=range].svelte-wei6ev::-webkit-slider-runnable-track{height:6px;border-radius:var(--radius-xl, 12px);background:linear-gradient(to right,var(--slider-color, #f97316) var(--range_progress, 50%),var(--neutral-200, #e5e7eb) var(--range_progress, 50%))}input[type=range].svelte-wei6ev::-webkit-slider-thumb{-webkit-appearance:none;-moz-appearance:none;appearance:none;height:16px;width:16px;background-color:var(--slider-color, #f97316);border:2px solid var(--background-fill-primary, white);border-radius:50%;margin-top:-5px;box-shadow:0 0 0 1px var(--border-color-primary, rgba(0, 0, 0, .08)),0 1px 3px #0003}input[type=range].svelte-wei6ev::-moz-range-track{height:6px;background:var(--neutral-200, #e5e7eb);border-radius:var(--radius-xl, 12px)}input[type=range].svelte-wei6ev::-moz-range-thumb{-webkit-appearance:none;-moz-appearance:none;appearance:none;height:16px;width:16px;background-color:var(--slider-color, #f97316);border:2px solid var(--background-fill-primary, white);border-radius:50%;box-shadow:0 0 0 1px var(--border-color-primary, rgba(0, 0, 0, .08)),0 1px 3px #0003}input[type=range].svelte-wei6ev::-moz-range-progress{height:6px;background-color:var(--slider-color, #f97316);border-radius:var(--radius-xl, 12px)}.bound.svelte-wei6ev{font-size:11px;color:var(--body-text-color-subdued, #9ca3af);min-width:12px;text-align:center}.textbox-container.svelte-6yncpg{width:100%}.label.svelte-6yncpg{display:block;font-size:13px;font-weight:500;color:var(--body-text-color-subdued, #6b7280);margin-bottom:6px}.info.svelte-6yncpg{display:block;font-size:12px;color:var(--body-text-color-subdued, #9ca3af);margin-bottom:4px}.input-wrap.svelte-6yncpg{border-radius:var(--input-radius, 8px);background:var(--input-background-fill, white);border:1px solid var(--border-color-primary, #e5e7eb);transition:border-color .15s,box-shadow .15s}.input-wrap.svelte-6yncpg:focus-within{border-color:var(--input-border-color-focus, #fdba74);box-shadow:0 0 0 2px var(--primary-50, #fff7ed)}input.svelte-6yncpg{width:100%;padding:7px 10px;outline:none;border:none;background:transparent;color:var(--body-text-color, #1f2937);font-size:13px;font-family:inherit;border-radius:var(--input-radius, 8px)}input.svelte-6yncpg::placeholder{color:var(--input-placeholder-color, #9ca3af)}.sidebar.svelte-181dlmc{width:290px;min-width:290px;background:var(--background-fill-primary, white);border-right:1px solid var(--border-color-primary, #e5e7eb);display:flex;flex-direction:column;position:relative;overflow:hidden;transition:width .2s,min-width .2s}.sidebar.collapsed.svelte-181dlmc{width:40px;min-width:40px}.toggle-btn.svelte-181dlmc{position:absolute;top:12px;right:8px;z-index:10;border:none;background:none;color:var(--body-text-color-subdued, #9ca3af);cursor:pointer;padding:4px;display:flex;align-items:center;justify-content:center;border-radius:var(--radius-sm, 4px);transition:color .15s,background-color .15s}.toggle-btn.svelte-181dlmc:hover{color:var(--body-text-color, #1f2937);background-color:var(--background-fill-secondary, #f9fafb)}.sidebar-content.svelte-181dlmc{padding:16px;flex:1;min-height:0;display:flex;flex-direction:column}.sidebar-scroll.svelte-181dlmc{overflow-y:auto;flex:1;min-height:0}.oauth-footer.svelte-181dlmc{flex-shrink:0;margin-top:12px;padding-top:12px;border-top:1px solid var(--border-color-primary, #e5e7eb)}.readonly-footer.svelte-181dlmc{flex-shrink:0;margin-top:12px;padding-top:12px;border-top:1px solid var(--border-color-primary, #e5e7eb);display:flex;align-items:center;gap:8px;flex-wrap:wrap}.readonly-badge.svelte-181dlmc{display:inline-flex;align-items:center;border:1px solid var(--border-color-primary, #e5e7eb);border-radius:999px;padding:2px 8px;font-size:10px;letter-spacing:.06em;font-weight:600;color:var(--body-text-color-subdued, #6b7280);background:var(--background-fill-secondary, #f9fafb)}.readonly-link.svelte-181dlmc{font-size:12px;color:var(--body-text-color-subdued, #6b7280);text-decoration:none;max-width:100%;overflow-wrap:anywhere}.readonly-link.svelte-181dlmc:hover{color:var(--body-text-color, #1f2937);text-decoration:underline}.oauth-line.svelte-181dlmc{margin:0;font-size:12px;line-height:1.4;color:var(--body-text-color-subdued, #6b7280)}.oauth-warn.svelte-181dlmc{color:var(--body-text-color, #92400e)}.hf-login-btn.svelte-181dlmc{display:inline-flex;align-items:center;justify-content:center;gap:8px;width:100%;padding:8px 12px;font-size:13px;font-weight:600;color:#fff;background:#141c2e;border-radius:var(--radius-lg, 8px);text-decoration:none;border:none;cursor:pointer;box-sizing:border-box}.hf-login-btn.svelte-181dlmc:hover{background:#283042}.hf-logo.svelte-181dlmc{width:20px;height:20px;flex-shrink:0}.oauth-hint.svelte-181dlmc{margin:8px 0 0;font-size:11px;line-height:1.35;color:var(--body-text-color-subdued, #9ca3af)}.oauth-signed-in.svelte-181dlmc{margin:0;font-size:12px;color:var(--body-text-color-subdued, #6b7280)}.oauth-logout.svelte-181dlmc{font-size:12px;color:var(--body-text-color-subdued, #9ca3af);text-decoration:none;cursor:pointer}.oauth-logout.svelte-181dlmc:hover{text-decoration:underline;color:var(--body-text-color, #1f2937)}.logo-section.svelte-181dlmc{margin-bottom:20px}.logo.svelte-181dlmc{width:80%;max-width:200px}.section.svelte-181dlmc{margin-bottom:18px}.section-label.svelte-181dlmc{font-size:13px;font-weight:500;color:var(--body-text-color-subdued, #6b7280)}.locked-project.svelte-181dlmc{margin-top:4px;font-size:13px;font-weight:500;color:var(--body-text-color, #1f2937);padding:8px 10px;border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-md, 6px);background:var(--background-fill-secondary, #f9fafb)}.runs-header.svelte-181dlmc{display:flex;align-items:center;justify-content:space-between;margin-bottom:6px}.select-all-label.svelte-181dlmc{display:flex;align-items:center;gap:6px;cursor:pointer}.select-all-cb.svelte-181dlmc{-moz-appearance:none;appearance:none;-webkit-appearance:none;width:16px;height:16px;border:1px solid var(--checkbox-border-color, #d1d5db);border-radius:var(--checkbox-border-radius, 4px);background-color:var(--checkbox-background-color, white);cursor:pointer;flex-shrink:0;position:relative;transition:background-color .15s,border-color .15s}.select-all-cb.svelte-181dlmc:checked{background-color:var(--checkbox-background-color-selected, var(--color-accent, #f97316));border-color:var(--checkbox-background-color-selected, var(--color-accent, #f97316));background-image:var(--checkbox-check)}.select-all-cb.svelte-181dlmc:indeterminate{background-color:var(--checkbox-background-color-selected, var(--color-accent, #f97316));border-color:var(--checkbox-background-color-selected, var(--color-accent, #f97316));background-image:url("data:image/svg+xml,%3Csvg viewBox='0 0 16 16' fill='white' xmlns='http://www.w3.org/2000/svg'%3E%3Crect x='3' y='7' width='10' height='2' rx='1'/%3E%3C/svg%3E");background-size:12px;background-position:center;background-repeat:no-repeat}.latest-toggle.svelte-181dlmc{display:flex;align-items:center;gap:6px;font-size:12px;color:var(--body-text-color-subdued, #6b7280);cursor:pointer}.latest-toggle.svelte-181dlmc input[type=checkbox]:where(.svelte-181dlmc){-moz-appearance:none;appearance:none;-webkit-appearance:none;width:16px;height:16px;margin:0;border:1px solid var(--checkbox-border-color, #d1d5db);border-radius:var(--checkbox-border-radius, 4px);background-color:var(--checkbox-background-color, white);box-shadow:var(--checkbox-shadow);cursor:pointer;flex-shrink:0;transition:background-color .15s,border-color .15s}.latest-toggle.svelte-181dlmc input[type=checkbox]:where(.svelte-181dlmc):checked{background-image:var(--checkbox-check);background-color:var(--checkbox-background-color-selected, #f97316);border-color:var(--checkbox-border-color-selected, #f97316)}.checkbox-list.svelte-181dlmc{max-height:300px;overflow-y:auto;margin-top:8px}.device-group.svelte-181dlmc{margin-top:14px;padding-top:12px;border-top:1px solid var(--border-color-primary, #e5e7eb)}.section-sublabel.svelte-181dlmc{font-size:12px;font-weight:600;color:var(--body-text-color-subdued, #6b7280)}.checkbox-group.svelte-181dlmc{display:flex;flex-direction:column;margin-top:8px}.checkbox-item.svelte-181dlmc{display:flex;align-items:center;gap:8px;padding:3px 0;cursor:pointer;font-size:13px}.checkbox-item.svelte-181dlmc input[type=checkbox]:where(.svelte-181dlmc){-moz-appearance:none;appearance:none;-webkit-appearance:none;width:16px;height:16px;margin:0;border:1px solid var(--checkbox-border-color, #d1d5db);border-radius:var(--checkbox-border-radius, 4px);background-color:var(--checkbox-background-color, white);box-shadow:var(--checkbox-shadow);cursor:pointer;flex-shrink:0;transition:background-color .15s,border-color .15s}.checkbox-item.svelte-181dlmc input[type=checkbox]:where(.svelte-181dlmc):checked{background-image:var(--checkbox-check);background-color:var(--checkbox-background-color-selected, #f97316);border-color:var(--checkbox-border-color-selected, #f97316)}.checkbox-item.svelte-181dlmc input[type=checkbox]:where(.svelte-181dlmc):hover{border-color:var(--checkbox-border-color-hover, #d1d5db)}.run-name.svelte-181dlmc{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;color:var(--body-text-color, #1f2937)}.alert-panel.svelte-x5aqew{position:fixed;bottom:16px;right:16px;width:380px;max-height:400px;background:var(--background-fill-primary, white);border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);box-shadow:var(--shadow-drop-lg);z-index:1000;overflow:hidden;display:flex;flex-direction:column}.alert-panel.collapsed.svelte-x5aqew{max-height:none}.alert-header.svelte-x5aqew{padding:10px 12px;border:none;border-bottom:1px solid var(--border-color-primary, #e5e7eb);background:none;width:100%;display:flex;align-items:center;justify-content:space-between;cursor:pointer;gap:8px}.alert-panel.collapsed.svelte-x5aqew .alert-header:where(.svelte-x5aqew){border-bottom:none}.collapse-icon.svelte-x5aqew{color:var(--body-text-color-subdued, #9ca3af);flex-shrink:0;transition:transform .15s}.collapse-icon.rotated.svelte-x5aqew{transform:rotate(-90deg)}.alert-title.svelte-x5aqew{font-size:13px;font-weight:600;color:var(--body-text-color, #1f2937)}.filter-pills.svelte-x5aqew{display:flex;gap:4px}.pill.svelte-x5aqew{border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-xxl, 22px);padding:2px 8px;font-size:11px;background:var(--background-fill-secondary, #f9fafb);color:var(--body-text-color-subdued, #6b7280);cursor:pointer}.pill.active.svelte-x5aqew{background:var(--color-accent, #f97316);color:#fff;border-color:var(--color-accent, #f97316)}.alert-list.svelte-x5aqew{overflow-y:auto;flex:1}.alert-item.svelte-x5aqew{border-bottom:1px solid var(--neutral-100, #f3f4f6)}.alert-row.svelte-x5aqew{display:flex;align-items:center;gap:8px;width:100%;padding:8px 12px;border:none;background:none;text-align:left;cursor:pointer;font-size:var(--text-sm, 12px)}.alert-row.svelte-x5aqew:hover{background:var(--background-fill-secondary, #f9fafb)}.alert-text.svelte-x5aqew{flex:1;color:var(--body-text-color, #1f2937)}.alert-meta.svelte-x5aqew{font-size:var(--text-xs, 10px);color:var(--body-text-color-subdued, #9ca3af);white-space:nowrap}.alert-detail.svelte-x5aqew{padding:4px 12px 8px 32px;font-size:var(--text-sm, 12px);color:var(--body-text-color-subdued, #6b7280)}.plot-container.svelte-9thu1j{min-width:350px;flex:1;background:var(--background-fill-primary, white);border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);padding:12px;overflow:hidden;position:relative}.plot-container[draggable=true].svelte-9thu1j{cursor:grab}.plot-container[draggable=true].svelte-9thu1j:active{cursor:grabbing}.hidden-plot.svelte-9thu1j{visibility:hidden;height:0;padding:0;margin:0;border:none;overflow:hidden;pointer-events:none}.drag-handle.svelte-9thu1j{position:absolute;top:8px;left:8px;color:var(--body-text-color-subdued, #9ca3af);opacity:0;transition:opacity .15s;z-index:5}.plot-container.svelte-9thu1j:hover .drag-handle:where(.svelte-9thu1j){opacity:.5}.drag-handle.svelte-9thu1j:hover{opacity:1!important}.plot-toolbar.svelte-9thu1j{position:absolute;top:8px;right:8px;display:flex;gap:4px;z-index:5;opacity:0;transition:opacity .15s}.plot-container.svelte-9thu1j:hover .plot-toolbar:where(.svelte-9thu1j){opacity:1}.toolbar-btn.svelte-9thu1j{border:1px solid var(--border-color-primary, #e5e7eb);background:var(--background-fill-primary, white);color:var(--body-text-color-subdued, #6b7280);cursor:pointer;padding:4px 6px;border-radius:var(--radius-sm, 4px);display:flex;align-items:center;justify-content:center}.toolbar-btn.svelte-9thu1j:hover{background:var(--neutral-100, #f3f4f6);color:var(--body-text-color, #1f2937)}.plot-chart-wrap.svelte-9thu1j{position:relative;width:100%}.plot-chart-wrap--fs.svelte-9thu1j{flex:1;min-height:0;display:flex;flex-direction:column}.reset-zoom-btn.svelte-9thu1j{position:absolute;bottom:1px;right:1px;z-index:6;display:inline-flex;align-items:center;justify-content:center;margin:0;min-width:52px;padding:5px 12px 5px 10px;border:none;border-radius:4px;background:transparent;color:var(--body-text-color-subdued, #334155);cursor:pointer;opacity:.92;transform:translateY(6px);transition:opacity .15s ease,color .15s ease,background .15s ease;box-shadow:none}.reset-zoom-btn.svelte-9thu1j:hover{opacity:1;color:var(--body-text-color, #0f172a);background:var(--background-fill-secondary, rgba(226, 232, 240, .85));transform:translateY(6px)}.reset-zoom-btn.svelte-9thu1j svg:where(.svelte-9thu1j){display:block;flex-shrink:0;filter:drop-shadow(0 0 .5px rgba(255,255,255,.95))}.plot.svelte-9thu1j{width:100%}.plot.svelte-9thu1j .vega-embed{width:100%!important}.plot.svelte-9thu1j .vega-embed summary{display:none}.fullscreen-host.svelte-9thu1j{position:fixed;top:0;right:0;bottom:0;left:0;z-index:10000;box-sizing:border-box;display:flex;flex-direction:column;background:var(--background-fill-primary, white);padding:12px;gap:8px;pointer-events:auto}.fullscreen-host.svelte-9thu1j:fullscreen{width:100%;height:100%}.fullscreen-host.svelte-9thu1j:-webkit-full-screen{width:100%;height:100%}.fullscreen-toolbar.svelte-9thu1j{flex-shrink:0;display:flex;justify-content:flex-end;gap:4px;z-index:5}.fullscreen-chart-wrap.svelte-9thu1j{flex:1;min-height:0;display:flex;flex-direction:column}.fullscreen-legend.svelte-9thu1j{flex-shrink:0}.fullscreen-plot.svelte-9thu1j{flex:1;min-height:0;width:100%;overflow:hidden}.fullscreen-plot.svelte-9thu1j .vega-embed{width:100%!important;height:100%!important;min-height:0;display:flex;flex-direction:column}.fullscreen-plot.svelte-9thu1j .vega-embed .vega-view{flex:1;min-height:0}.fullscreen-plot.svelte-9thu1j .vega-embed summary{display:none}.custom-legend.svelte-9thu1j{display:flex;align-items:center;justify-content:center;gap:12px;padding:6px 0 0;flex-wrap:wrap}.legend-title.svelte-9thu1j{font-size:11px;color:var(--body-text-color-subdued, #6b7280);font-weight:600}.legend-item.svelte-9thu1j{display:flex;align-items:center;gap:4px}.legend-dot.svelte-9thu1j{width:10px;height:10px;border-radius:50%;flex-shrink:0}.legend-line-swatch.svelte-9thu1j{width:24px;height:10px;flex-shrink:0;color:var(--body-text-color, #1f2937)}.legend-label.svelte-9thu1j{font-size:11px;color:var(--body-text-color-subdued, #6b7280)}.legend-toggle.svelte-9thu1j{font-size:11px;color:var(--body-text-color-subdued, #6b7280);background:none;border:none;padding:0 4px;cursor:pointer;text-decoration:underline}.legend-toggle.svelte-9thu1j:hover{color:var(--body-text-color, #1f2937)}.plot-container.svelte-1swghqy{min-width:350px;flex:1;background:var(--background-fill-primary, white);border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);padding:12px;overflow:hidden;position:relative}.plot-container[draggable=true].svelte-1swghqy{cursor:grab}.plot-container[draggable=true].svelte-1swghqy:active{cursor:grabbing}.hidden-plot.svelte-1swghqy{visibility:hidden;height:0;padding:0;margin:0;border:none;overflow:hidden;pointer-events:none}.drag-handle.svelte-1swghqy{position:absolute;top:8px;left:8px;color:var(--body-text-color-subdued, #9ca3af);opacity:0;transition:opacity .15s;z-index:5}.plot-container.svelte-1swghqy:hover .drag-handle:where(.svelte-1swghqy){opacity:.5}.drag-handle.svelte-1swghqy:hover{opacity:1!important}.plot-toolbar.svelte-1swghqy{position:absolute;top:8px;right:8px;display:flex;gap:4px;z-index:5;opacity:0;transition:opacity .15s}.plot-container.svelte-1swghqy:hover .plot-toolbar:where(.svelte-1swghqy){opacity:1}.toolbar-btn.svelte-1swghqy{border:1px solid var(--border-color-primary, #e5e7eb);background:var(--background-fill-primary, white);color:var(--body-text-color-subdued, #6b7280);cursor:pointer;padding:4px 6px;border-radius:var(--radius-sm, 4px);display:flex;align-items:center;justify-content:center}.toolbar-btn.svelte-1swghqy:hover{background:var(--neutral-100, #f3f4f6);color:var(--body-text-color, #1f2937)}.plot-chart-wrap.svelte-1swghqy{position:relative;width:100%}.plot-chart-wrap--fs.svelte-1swghqy{flex:1;min-height:0;display:flex;flex-direction:column}.plot.svelte-1swghqy{width:100%}.plot.svelte-1swghqy .vega-embed{width:100%!important}.plot.svelte-1swghqy .vega-embed summary{display:none}.fullscreen-host.svelte-1swghqy{position:fixed;top:0;right:0;bottom:0;left:0;z-index:10000;box-sizing:border-box;display:flex;flex-direction:column;background:var(--background-fill-primary, white);padding:12px;gap:8px;pointer-events:auto}.fullscreen-host.svelte-1swghqy:fullscreen{width:100%;height:100%}.fullscreen-host.svelte-1swghqy:-webkit-full-screen{width:100%;height:100%}.fullscreen-toolbar.svelte-1swghqy{flex-shrink:0;display:flex;justify-content:flex-end;gap:4px;z-index:5}.fullscreen-chart-wrap.svelte-1swghqy{flex:1;min-height:0;display:flex;flex-direction:column}.fullscreen-legend.svelte-1swghqy{flex-shrink:0}.fullscreen-plot.svelte-1swghqy{flex:1;min-height:0;width:100%;overflow:hidden}.fullscreen-plot.svelte-1swghqy .vega-embed{width:100%!important;height:100%!important;min-height:0;display:flex;flex-direction:column}.fullscreen-plot.svelte-1swghqy .vega-embed .vega-view{flex:1;min-height:0}.fullscreen-plot.svelte-1swghqy .vega-embed summary{display:none}.custom-legend.svelte-1swghqy{display:flex;align-items:center;justify-content:center;gap:12px;padding:6px 0 0;flex-wrap:wrap}.legend-title.svelte-1swghqy{font-size:11px;color:var(--body-text-color-subdued, #6b7280);font-weight:600}.legend-item.svelte-1swghqy{display:flex;align-items:center;gap:4px}.legend-dot.svelte-1swghqy{width:10px;height:10px;border-radius:50%;flex-shrink:0}.legend-label.svelte-1swghqy{font-size:11px;color:var(--body-text-color-subdued, #6b7280)}.legend-toggle.svelte-1swghqy{font-size:11px;color:var(--body-text-color-subdued, #6b7280);background:none;border:none;padding:0 4px;cursor:pointer;text-decoration:underline}.legend-toggle.svelte-1swghqy:hover{color:var(--body-text-color, #1f2937)}.accordion.svelte-1jep0a{margin-bottom:12px;border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);background:var(--background-fill-primary, white);overflow:hidden}.accordion-hidden.svelte-1jep0a{margin-bottom:8px}.accordion-header.svelte-1jep0a{display:flex;align-items:center;gap:8px;width:100%;padding:10px 14px;border:none;background:var(--background-fill-primary, white);color:var(--body-text-color, #1f2937);font-size:var(--text-md, 14px);font-weight:600;cursor:pointer;text-align:left}.accordion-header.svelte-1jep0a:hover{background:var(--background-fill-secondary, #f9fafb)}.arrow.svelte-1jep0a{font-size:14px;transition:transform .15s;color:var(--body-text-color, #1f2937);display:inline-block}.arrow.svelte-1jep0a:not(.rotated){transform:rotate(-90deg)}.accordion-body.svelte-1jep0a{padding:0 14px 14px}.trackio-loading.svelte-1kc6b2l{display:flex;align-items:center;justify-content:center;width:100%;min-height:min(70vh,640px);padding:32px 24px;box-sizing:border-box;background:transparent}.logo-stack.svelte-1kc6b2l{position:relative;width:min(100%,200px);max-width:min(92vw,200px);line-height:0;background:transparent;isolation:isolate}.logo-base.svelte-1kc6b2l{display:block;background:transparent}.logo-img.svelte-1kc6b2l{width:100%;height:auto;display:block;background:transparent}.logo-overlay.svelte-1kc6b2l{position:absolute;left:0;top:0;width:100%;animation:svelte-1kc6b2l-trackio-logo-sweep 4s linear infinite;pointer-events:none;background:transparent}.logo-overlay.svelte-1kc6b2l .logo-img:where(.svelte-1kc6b2l){width:100%;height:auto;object-position:left center}.logo-img--gray.svelte-1kc6b2l{filter:grayscale(1)}@keyframes svelte-1kc6b2l-trackio-logo-sweep{0%{clip-path:inset(0 0 0 0)}50%{clip-path:inset(0 0 0 100%)}to{clip-path:inset(0 0 0 0)}}@media(prefers-reduced-motion:reduce){.logo-overlay.svelte-1kc6b2l{display:none}}.sr-only.svelte-1kc6b2l{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}.metrics-page.svelte-2bul55{padding:20px 24px;overflow-y:auto;flex:1;min-height:0}.plot-grid.svelte-2bul55{display:flex;flex-wrap:wrap;gap:16px}.subgroup-list.svelte-2bul55{margin-top:16px}.empty-state.svelte-2bul55{max-width:640px;padding:40px 24px;color:var(--body-text-color, #1f2937)}.empty-state.svelte-2bul55 h2:where(.svelte-2bul55){margin:0 0 8px;font-size:20px;font-weight:700}.empty-state.svelte-2bul55 p:where(.svelte-2bul55){margin:12px 0 8px;color:var(--body-text-color-subdued, #6b7280)}.empty-state.svelte-2bul55 pre:where(.svelte-2bul55){background:var(--background-fill-secondary, #f9fafb);padding:16px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:13px;overflow-x:auto}.empty-state.svelte-2bul55 code:where(.svelte-2bul55){background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.empty-state.svelte-2bul55 pre:where(.svelte-2bul55) code:where(.svelte-2bul55){background:none;padding:0}.system-page.svelte-nv5os4{padding:20px 24px;overflow-y:auto;flex:1;min-height:0}.plot-grid.svelte-nv5os4{display:flex;flex-wrap:wrap;gap:12px}.subgroup-list.svelte-nv5os4{margin-top:16px}.empty-state.svelte-nv5os4{max-width:640px;padding:40px 24px;color:var(--body-text-color, #1f2937)}.empty-state.svelte-nv5os4 h2:where(.svelte-nv5os4){margin:0 0 8px;font-size:20px;font-weight:700}.empty-state.svelte-nv5os4 p:where(.svelte-nv5os4){margin:12px 0 8px;color:var(--body-text-color-subdued, #6b7280)}.empty-state.svelte-nv5os4 pre:where(.svelte-nv5os4){background:var(--background-fill-secondary, #f9fafb);padding:16px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:13px;overflow-x:auto}.empty-state.svelte-nv5os4 ul:where(.svelte-nv5os4){list-style:disc;padding-left:20px;margin:4px 0 0}.empty-state.svelte-nv5os4 li:where(.svelte-nv5os4){margin:4px 0;color:var(--body-text-color, #1f2937)}.empty-state.svelte-nv5os4 code:where(.svelte-nv5os4){background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.empty-state.svelte-nv5os4 pre:where(.svelte-nv5os4) code:where(.svelte-nv5os4){background:none;padding:0}.table-container.svelte-1cp60rw{display:flex;flex-direction:column;gap:var(--size-2, 8px);position:relative}.header-row.svelte-1cp60rw{display:flex;justify-content:flex-end;align-items:center;min-height:var(--size-6, 24px);width:100%}.header-row.svelte-1cp60rw .label:where(.svelte-1cp60rw){flex:1;margin:0;color:var(--block-label-text-color, var(--neutral-500, #6b7280));font-size:var(--block-label-text-size, 12px);line-height:var(--line-sm, 1.4)}.table-wrap.svelte-1cp60rw{position:relative;overflow:auto;border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--table-radius, var(--radius-lg, 8px))}table.svelte-1cp60rw{width:100%;table-layout:auto;color:var(--body-text-color, #1f2937);font-size:var(--input-text-size, 14px);line-height:var(--line-sm, 1.4);border-spacing:0;border-collapse:separate}thead.svelte-1cp60rw{position:sticky;top:0;z-index:5;box-shadow:var(--shadow-drop, rgba(0,0,0,.05) 0px 1px 2px 0px)}th.svelte-1cp60rw{padding:0;background:var(--table-even-background-fill, white);border-right-width:0px;border-left-width:1px;border-bottom-width:1px;border-style:solid;border-color:var(--border-color-primary, #e5e7eb);text-align:left;cursor:pointer;-webkit-user-select:none;user-select:none}th.first.svelte-1cp60rw{border-left-width:0;border-top-left-radius:var(--table-radius, var(--radius-lg, 8px))}th.last.svelte-1cp60rw{border-top-right-radius:var(--table-radius, var(--radius-lg, 8px))}.th-inner.svelte-1cp60rw{padding:var(--size-2, 8px);display:flex;align-items:center;gap:4px;font-weight:600;font-size:var(--text-sm, 12px);white-space:nowrap}.sort-arrow.svelte-1cp60rw{font-size:10px;color:var(--body-text-color-subdued, #9ca3af);visibility:hidden}.sort-arrow.visible.svelte-1cp60rw{visibility:visible}td.svelte-1cp60rw{padding:var(--size-2, 8px);border-right-width:0px;border-left-width:1px;border-bottom-width:1px;border-style:solid;border-color:var(--border-color-primary, #e5e7eb);font-size:var(--text-sm, 12px)}td.first.svelte-1cp60rw{border-left-width:0}tr.svelte-1cp60rw{background:var(--table-even-background-fill, white);border-bottom:1px solid var(--border-color-primary, #e5e7eb);text-align:left}tr.row-odd.svelte-1cp60rw{background:var(--table-odd-background-fill, var(--neutral-50, #f9fafb))}tr.selected.svelte-1cp60rw{background:var(--color-accent-soft, var(--primary-50, #fff7ed))}tr.svelte-1cp60rw:last-child td.first:where(.svelte-1cp60rw){border-bottom-left-radius:var(--table-radius, var(--radius-lg, 8px))}tr.svelte-1cp60rw:last-child td.last:where(.svelte-1cp60rw){border-bottom-right-radius:var(--table-radius, var(--radius-lg, 8px))}.check-col.svelte-1cp60rw{width:40px;text-align:center;padding:var(--size-2, 8px);border-left-width:0}.check-col.svelte-1cp60rw input[type=checkbox]:where(.svelte-1cp60rw){-moz-appearance:none;appearance:none;-webkit-appearance:none;width:16px;height:16px;border:1px solid var(--checkbox-border-color, #d1d5db);border-radius:var(--checkbox-border-radius, 4px);background-color:var(--checkbox-background-color, white);cursor:pointer;flex-shrink:0;transition:background-color .15s,border-color .15s}.check-col.svelte-1cp60rw input[type=checkbox]:where(.svelte-1cp60rw):checked{background-image:var(--checkbox-check);background-color:var(--checkbox-background-color-selected, #2563eb);border-color:var(--checkbox-border-color-selected, #2563eb)}.media-page.svelte-outb32{padding:20px 24px;overflow-y:auto;flex:1}.section-title.svelte-outb32{font-size:var(--text-lg, 16px);font-weight:600;color:var(--body-text-color, #1f2937);margin:16px 0 8px}.gallery.svelte-outb32{display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:12px}.gallery-item.svelte-outb32{border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);overflow:hidden;background:var(--background-fill-secondary, #f9fafb)}.gallery-item.svelte-outb32 img:where(.svelte-outb32),.gallery-item.svelte-outb32 video:where(.svelte-outb32){width:100%;display:block}.caption.svelte-outb32{padding:4px 8px;font-size:var(--text-sm, 12px);color:var(--body-text-color-subdued, #9ca3af)}.step-label.svelte-outb32{padding:4px 8px;font-size:var(--text-xs, 10px);color:var(--body-text-color-subdued, #9ca3af)}.audio-list.svelte-outb32{display:flex;flex-direction:column;gap:8px}.audio-item.svelte-outb32{display:flex;align-items:center;gap:12px;padding:8px;border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px)}.audio-label.svelte-outb32{font-size:var(--text-sm, 12px);color:var(--body-text-color-subdued, #9ca3af);min-width:120px}.table-section.svelte-outb32{margin-bottom:16px}.empty-state.svelte-outb32{max-width:640px;padding:40px 24px;color:var(--body-text-color, #1f2937)}.empty-state.svelte-outb32 h2:where(.svelte-outb32){margin:0 0 8px;font-size:20px;font-weight:700}.empty-state.svelte-outb32 p:where(.svelte-outb32){margin:12px 0 8px;color:var(--body-text-color-subdued, #6b7280)}.empty-state.svelte-outb32 pre:where(.svelte-outb32){background:var(--background-fill-secondary, #f9fafb);padding:16px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:13px;overflow-x:auto}.empty-state.svelte-outb32 code:where(.svelte-outb32){background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.empty-state.svelte-outb32 pre:where(.svelte-outb32) code:where(.svelte-outb32){background:none;padding:0}.reports-page.svelte-iufsej{padding:20px 24px;overflow-y:auto;flex:1}.controls.svelte-iufsej{display:flex;gap:16px;margin-bottom:16px;flex-wrap:wrap;align-items:flex-end}.control.svelte-iufsej{min-width:200px}.filter-pills.svelte-iufsej{display:flex;gap:4px}.pill.svelte-iufsej{border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-xxl, 22px);padding:4px 12px;font-size:var(--text-sm, 12px);background:var(--background-fill-secondary, #f9fafb);color:var(--body-text-color-subdued, #6b7280);cursor:pointer;transition:background-color .15s,color .15s}.pill.svelte-iufsej:hover{background:var(--neutral-100, #f3f4f6)}.pill.active.svelte-iufsej{background:var(--color-accent, #f97316);color:#fff;border-color:var(--color-accent, #f97316)}.empty-state.svelte-iufsej{max-width:640px;padding:40px 24px;color:var(--body-text-color, #1f2937)}.empty-state.svelte-iufsej h2:where(.svelte-iufsej){margin:0 0 8px;font-size:20px;font-weight:700}.empty-state.svelte-iufsej p:where(.svelte-iufsej){margin:12px 0 8px;color:var(--body-text-color-subdued, #6b7280)}.empty-state.svelte-iufsej pre:where(.svelte-iufsej){background:var(--background-fill-secondary, #f9fafb);padding:16px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:13px;overflow-x:auto}.empty-state.svelte-iufsej code:where(.svelte-iufsej){background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.empty-state.svelte-iufsej pre:where(.svelte-iufsej) code:where(.svelte-iufsej){background:none;padding:0}.alerts-table.svelte-iufsej{width:100%;border-collapse:collapse;font-size:var(--text-md, 14px)}.alerts-table.svelte-iufsej th:where(.svelte-iufsej){text-align:left;padding:8px 12px;border-bottom:2px solid var(--border-color-primary, #e5e7eb);color:var(--body-text-color-subdued, #6b7280);font-weight:600;font-size:var(--text-sm, 12px);text-transform:uppercase;letter-spacing:.05em}.alerts-table.svelte-iufsej td:where(.svelte-iufsej){padding:8px 12px;border-bottom:1px solid var(--border-color-primary, #e5e7eb);color:var(--body-text-color, #1f2937)}.alerts-table.svelte-iufsej tbody:where(.svelte-iufsej) tr:where(.svelte-iufsej):nth-child(odd){background:var(--table-odd-background-fill, var(--background-fill-primary, white))}.alerts-table.svelte-iufsej tbody:where(.svelte-iufsej) tr:where(.svelte-iufsej):nth-child(2n){background:var(--table-even-background-fill, var(--background-fill-secondary, #f9fafb))}.alerts-table.svelte-iufsej tr:where(.svelte-iufsej):hover{background:var(--background-fill-secondary, #f3f4f6)}.section-title.svelte-iufsej{font-size:16px;font-weight:700;margin:0 0 12px;color:var(--body-text-color, #1f2937)}.reports-section.svelte-iufsej,.alerts-section.svelte-iufsej{margin-bottom:32px}.report-card.svelte-iufsej{border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);padding:16px 20px;margin-bottom:12px;background:var(--background-fill-primary, white)}.report-meta.svelte-iufsej{font-size:var(--text-sm, 12px);color:var(--body-text-color-subdued, #6b7280);margin-bottom:8px}.report-content.svelte-iufsej{font-size:var(--text-md, 14px);color:var(--body-text-color, #1f2937);line-height:1.6}.report-content.svelte-iufsej h2{font-size:18px;font-weight:700;margin:0 0 8px}.report-content.svelte-iufsej h3{font-size:16px;font-weight:600;margin:12px 0 6px}.report-content.svelte-iufsej h4{font-size:14px;font-weight:600;margin:10px 0 4px}.report-content.svelte-iufsej code{background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.report-content.svelte-iufsej ul{margin:4px 0;padding-left:20px}.report-content.svelte-iufsej li{margin:2px 0}.report-content.svelte-iufsej p{margin:6px 0}.filter-empty.svelte-iufsej{color:var(--body-text-color-subdued, #6b7280);font-size:var(--text-md, 14px)}.runs-page.svelte-1yb6d54{padding:20px 24px;overflow-y:auto;flex:1}.empty-state.svelte-1yb6d54{max-width:640px;padding:40px 24px;color:var(--body-text-color, #1f2937)}.empty-state.svelte-1yb6d54 h2:where(.svelte-1yb6d54){margin:0 0 8px;font-size:20px;font-weight:700}.empty-state.svelte-1yb6d54 p:where(.svelte-1yb6d54){margin:12px 0 8px;color:var(--body-text-color-subdued, #6b7280)}.empty-state.svelte-1yb6d54 pre:where(.svelte-1yb6d54){background:var(--background-fill-secondary, #f9fafb);padding:16px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:13px;overflow-x:auto}.empty-state.svelte-1yb6d54 code:where(.svelte-1yb6d54){background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.empty-state.svelte-1yb6d54 pre:where(.svelte-1yb6d54) code:where(.svelte-1yb6d54){background:none;padding:0}.runs-table.svelte-1yb6d54{width:100%;border-collapse:collapse;font-size:var(--text-md, 14px)}.runs-table.svelte-1yb6d54 th:where(.svelte-1yb6d54){text-align:left;padding:8px 12px;border-bottom:2px solid var(--border-color-primary, #e5e7eb);color:var(--body-text-color-subdued, #6b7280);font-weight:600;font-size:var(--text-sm, 12px);text-transform:uppercase;letter-spacing:.05em}.runs-table.svelte-1yb6d54 td:where(.svelte-1yb6d54){padding:8px 12px;border-bottom:1px solid var(--border-color-primary, #e5e7eb);color:var(--body-text-color, #1f2937)}.runs-table.svelte-1yb6d54 tbody:where(.svelte-1yb6d54) tr:where(.svelte-1yb6d54):nth-child(odd){background:var(--table-odd-background-fill, var(--background-fill-primary, white))}.runs-table.svelte-1yb6d54 tbody:where(.svelte-1yb6d54) tr:where(.svelte-1yb6d54):nth-child(2n){background:var(--table-even-background-fill, var(--background-fill-secondary, #f9fafb))}.runs-table.svelte-1yb6d54 tr:where(.svelte-1yb6d54):hover{background:var(--background-fill-secondary, #f3f4f6)}.run-name-cell.svelte-1yb6d54{font-weight:500}.run-name-with-dot.svelte-1yb6d54{display:inline-flex;align-items:center;gap:8px;max-width:100%}.run-dot.svelte-1yb6d54{width:10px;height:10px;border-radius:50%;flex-shrink:0}.link-btn.svelte-1yb6d54{background:none;border:none;color:var(--color-accent, #f97316);cursor:pointer;font:inherit;font-weight:500;padding:0;text-align:left}.link-btn.svelte-1yb6d54:hover{text-decoration:underline}.rename-input.svelte-1yb6d54{font:inherit;padding:2px 6px;border:1px solid var(--color-accent, #f97316);border-radius:var(--radius-sm, 4px);outline:none;width:100%}.actions-cell.svelte-1yb6d54{display:flex;gap:4px}.action-btn.svelte-1yb6d54{background:none;border:1px solid transparent;color:var(--body-text-color-subdued, #6b7280);cursor:pointer;padding:4px;border-radius:var(--radius-sm, 4px);display:flex;align-items:center}.action-btn.svelte-1yb6d54:hover{background:var(--background-fill-secondary, #f9fafb);border-color:var(--border-color-primary, #e5e7eb);color:var(--body-text-color, #1f2937)}.delete-btn.svelte-1yb6d54:hover{color:#dc2626;border-color:#fecaca;background:#fef2f2}.action-btn.svelte-1yb6d54:disabled{opacity:.45;cursor:not-allowed;pointer-events:none}.run-detail-page.svelte-1bpgsx2{padding:20px 24px;overflow-y:auto;flex:1}.detail-card.svelte-1bpgsx2{background:var(--background-fill-primary, white);border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);padding:24px;max-width:800px}.detail-card.svelte-1bpgsx2 h2:where(.svelte-1bpgsx2){color:var(--body-text-color, #1f2937);margin:0 0 16px;font-size:var(--text-xl, 22px)}.detail-card.svelte-1bpgsx2 h3:where(.svelte-1bpgsx2){color:var(--body-text-color, #1f2937);margin:20px 0 8px;font-size:var(--text-lg, 16px)}.detail-grid.svelte-1bpgsx2{display:grid;grid-template-columns:repeat(auto-fill,minmax(200px,1fr));gap:12px}.detail-item.svelte-1bpgsx2{display:flex;flex-direction:column;gap:2px}.detail-label.svelte-1bpgsx2{font-size:var(--text-xs, 10px);font-weight:600;color:var(--body-text-color-subdued, #9ca3af);text-transform:uppercase}.detail-value.svelte-1bpgsx2{font-size:var(--text-md, 14px);color:var(--body-text-color, #1f2937)}.config-block.svelte-1bpgsx2{background:var(--background-fill-secondary, #f9fafb);padding:12px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:var(--text-sm, 12px);color:var(--body-text-color, #1f2937);overflow-x:auto}.empty-state.svelte-1bpgsx2{max-width:640px;padding:40px 24px;color:var(--body-text-color, #1f2937)}.empty-state.svelte-1bpgsx2 h2:where(.svelte-1bpgsx2){margin:0 0 8px;font-size:20px;font-weight:700}.empty-state.svelte-1bpgsx2 p:where(.svelte-1bpgsx2){margin:12px 0 8px;color:var(--body-text-color-subdued, #6b7280)}.empty-state.svelte-1bpgsx2 pre:where(.svelte-1bpgsx2){background:var(--background-fill-secondary, #f9fafb);padding:16px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:13px;overflow-x:auto}.empty-state.svelte-1bpgsx2 code:where(.svelte-1bpgsx2){background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.empty-state.svelte-1bpgsx2 pre:where(.svelte-1bpgsx2) code:where(.svelte-1bpgsx2){background:none;padding:0}.files-page.svelte-1xvfk9n{padding:20px 24px;overflow-y:auto;flex:1}.page-title.svelte-1xvfk9n{color:var(--body-text-color, #1f2937);font-size:16px;font-weight:700;margin:0 0 4px}.page-subtitle.svelte-1xvfk9n{color:var(--body-text-color-subdued, #6b7280);font-size:var(--text-sm, 12px);margin:0 0 16px}.file-list.svelte-1xvfk9n{display:flex;flex-direction:column;gap:4px}.file-item.svelte-1xvfk9n{border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);background:var(--background-fill-primary, white);overflow:hidden}.file-item.expanded.svelte-1xvfk9n{border-color:var(--color-accent, #f97316)}.file-row.svelte-1xvfk9n{display:flex;align-items:center;justify-content:space-between;padding:10px 14px;gap:12px}.file-name.svelte-1xvfk9n{display:flex;align-items:center;gap:8px;background:none;border:none;padding:0;font-size:var(--text-md, 14px);color:var(--body-text-color, #1f2937);cursor:pointer;text-align:left}.file-name.svelte-1xvfk9n:hover{color:var(--color-accent, #f97316)}.file-icon.svelte-1xvfk9n{font-size:14px;flex-shrink:0}.file-actions.svelte-1xvfk9n{display:flex;align-items:center;gap:12px;flex-shrink:0}.file-size.svelte-1xvfk9n{font-size:var(--text-sm, 12px);color:var(--body-text-color-subdued, #6b7280);white-space:nowrap}.download-btn.svelte-1xvfk9n{display:flex;align-items:center;justify-content:center;width:28px;height:28px;border-radius:var(--radius-md, 6px);color:var(--body-text-color-subdued, #6b7280);transition:background-color .15s,color .15s}.download-btn.svelte-1xvfk9n:hover{background:var(--background-fill-secondary, #f3f4f6);color:var(--body-text-color, #1f2937)}.file-preview.svelte-1xvfk9n{border-top:1px solid var(--border-color-primary, #e5e7eb);padding:12px 14px;background:var(--background-fill-secondary, #f9fafb)}.preview-code.svelte-1xvfk9n{margin:0;font-size:12px;line-height:1.5;max-height:400px;overflow:auto;white-space:pre-wrap;word-break:break-all;color:var(--body-text-color, #1f2937)}.preview-loading.svelte-1xvfk9n,.preview-unavailable.svelte-1xvfk9n{color:var(--body-text-color-subdued, #6b7280);font-size:var(--text-sm, 12px);padding:8px 0}.preview-unavailable.svelte-1xvfk9n a:where(.svelte-1xvfk9n){color:var(--color-accent, #f97316);text-decoration:none}.preview-unavailable.svelte-1xvfk9n a:where(.svelte-1xvfk9n):hover{text-decoration:underline}.empty-state.svelte-1xvfk9n{max-width:640px;padding:40px 24px;color:var(--body-text-color, #1f2937)}.empty-state.svelte-1xvfk9n h2:where(.svelte-1xvfk9n){margin:0 0 8px;font-size:20px;font-weight:700}.empty-state.svelte-1xvfk9n p:where(.svelte-1xvfk9n){margin:12px 0 8px;color:var(--body-text-color-subdued, #6b7280)}.empty-state.svelte-1xvfk9n pre:where(.svelte-1xvfk9n){background:var(--background-fill-secondary, #f9fafb);padding:16px;border-radius:var(--radius-lg, 8px);border:1px solid var(--border-color-primary, #e5e7eb);font-size:13px;overflow-x:auto}.empty-state.svelte-1xvfk9n code:where(.svelte-1xvfk9n){background:var(--background-fill-secondary, #f0f0f0);padding:1px 5px;border-radius:var(--radius-sm, 4px);font-size:13px}.empty-state.svelte-1xvfk9n pre:where(.svelte-1xvfk9n) code:where(.svelte-1xvfk9n){background:none;padding:0}.settings-page.svelte-1ozf5k3{padding:24px 32px;overflow-y:auto;flex:1}.page-title.svelte-1ozf5k3{color:var(--body-text-color, #1f2937);font-size:18px;font-weight:700;margin:0 0 24px}.two-col.svelte-1ozf5k3{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr);gap:32px;align-items:start}@media(max-width:900px){.two-col.svelte-1ozf5k3{grid-template-columns:1fr}}.settings-section.svelte-1ozf5k3{margin-bottom:32px}.section-title.svelte-1ozf5k3{color:var(--body-text-color, #1f2937);font-size:15px;font-weight:600;margin:0 0 4px}.section-desc.svelte-1ozf5k3{color:var(--body-text-color-subdued, #6b7280);font-size:var(--text-sm, 12px);margin:0 0 12px;line-height:1.5}.section-desc.svelte-1ozf5k3 code:where(.svelte-1ozf5k3){background:var(--background-fill-secondary, #f3f4f6);padding:1px 5px;border-radius:var(--radius-sm, 3px);font-size:11px}.section-desc.svelte-1ozf5k3 strong:where(.svelte-1ozf5k3){color:var(--color-accent, #f97316)}.theme-switcher.svelte-1ozf5k3{display:inline-flex;border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);overflow:hidden}.theme-option.svelte-1ozf5k3{display:inline-flex;align-items:center;gap:6px;padding:8px 20px;border:none;background:var(--background-fill-primary, white);color:var(--body-text-color-subdued, #6b7280);font-size:var(--text-md, 14px);cursor:pointer;transition:all .15s;border-right:1px solid var(--border-color-primary, #e5e7eb)}.theme-option.svelte-1ozf5k3:last-child{border-right:none}.theme-option.svelte-1ozf5k3:hover{color:var(--body-text-color, #1f2937);background:var(--background-fill-secondary, #f9fafb)}.theme-option.selected.svelte-1ozf5k3{background:var(--color-accent, #f97316);color:#fff;font-weight:500}.project-selector.svelte-1ozf5k3{display:flex;align-items:center;gap:10px;margin-bottom:12px}.selector-label.svelte-1ozf5k3{font-size:var(--text-sm, 12px);color:var(--body-text-color-subdued, #6b7280);flex-shrink:0}.selector-select.svelte-1ozf5k3{padding:6px 10px;border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-md, 4px);background:var(--background-fill-primary, white);color:var(--body-text-color, #1f2937);font-size:var(--text-sm, 12px);min-width:160px;cursor:pointer}.selector-select.svelte-1ozf5k3:focus{outline:none;border-color:var(--color-accent, #f97316)}.commands-table.svelte-1ozf5k3{border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-lg, 8px);overflow:hidden}.command-row.svelte-1ozf5k3{display:flex;align-items:center;gap:16px;padding:10px 14px;border-bottom:1px solid var(--border-color-primary, #e5e7eb)}.command-row.svelte-1ozf5k3:last-child{border-bottom:none}.command-label.svelte-1ozf5k3{width:180px;flex-shrink:0;font-size:var(--text-sm, 12px);color:var(--body-text-color-subdued, #6b7280)}.command-value.svelte-1ozf5k3{flex:1;display:flex;align-items:center;gap:8px;min-width:0}.command-value.svelte-1ozf5k3 code:where(.svelte-1ozf5k3){flex:1;font-family:SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace;font-size:12px;color:var(--body-text-color, #1f2937);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.copy-btn.svelte-1ozf5k3{display:flex;align-items:center;justify-content:center;width:26px;height:26px;flex-shrink:0;border:none;background:none;border-radius:var(--radius-md, 4px);color:var(--body-text-color-subdued, #6b7280);cursor:pointer;transition:background-color .15s,color .15s}.copy-btn.svelte-1ozf5k3:hover{background:var(--background-fill-secondary, #f3f4f6);color:var(--body-text-color, #1f2937)}.copy-btn.copied.svelte-1ozf5k3{color:var(--color-accent, #f97316)}.agent-tabs.svelte-1ozf5k3{display:flex;border-bottom:1px solid var(--border-color-primary, #e5e7eb);gap:0;margin-bottom:0}.agent-tab.svelte-1ozf5k3{padding:8px 16px;border:none;background:none;color:var(--body-text-color-subdued, #6b7280);font-size:var(--text-sm, 12px);cursor:pointer;border-bottom:2px solid transparent;transition:all .15s;white-space:nowrap}.agent-tab.svelte-1ozf5k3:hover{color:var(--body-text-color, #1f2937)}.agent-tab.active.svelte-1ozf5k3{color:var(--color-accent, #f97316);border-bottom-color:var(--color-accent, #f97316);font-weight:500}.agent-panel.svelte-1ozf5k3{border:1px solid var(--border-color-primary, #e5e7eb);border-top:none;border-radius:0 0 var(--radius-lg, 8px) var(--radius-lg, 8px);padding:16px}.install-block.svelte-1ozf5k3{margin-bottom:16px}.install-label.svelte-1ozf5k3{display:block;font-size:11px;font-weight:500;color:var(--body-text-color-subdued, #6b7280);text-transform:uppercase;letter-spacing:.04em;margin-bottom:6px}.install-cmd.svelte-1ozf5k3{display:flex;align-items:center;gap:8px;background:var(--background-fill-secondary, #f3f4f6);border-radius:var(--radius-md, 4px);padding:8px 10px}.install-cmd.svelte-1ozf5k3 code:where(.svelte-1ozf5k3){flex:1;font-family:SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace;font-size:12px;color:var(--body-text-color, #1f2937);white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.example-block.svelte-1ozf5k3{background:var(--background-fill-secondary, #f9fafb);border:1px solid var(--border-color-primary, #e5e7eb);border-radius:var(--radius-md, 4px);padding:12px}.example-header.svelte-1ozf5k3{display:flex;align-items:center;justify-content:space-between;margin-bottom:8px}.example-label.svelte-1ozf5k3{font-size:11px;font-weight:500;color:var(--body-text-color-subdued, #6b7280);text-transform:uppercase;letter-spacing:.04em}.example-text.svelte-1ozf5k3{margin:0;font-size:var(--text-sm, 12px);color:var(--body-text-color, #1f2937);line-height:1.6;font-style:italic}*{margin:0;padding:0;box-sizing:border-box}body{font-family:-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,sans-serif;background:var(--background-fill-primary, #fff);color:var(--body-text-color, #1f2937);font-size:var(--text-md, 14px);-webkit-font-smoothing:antialiased}.app.svelte-1n46o8q{display:flex;height:100vh;overflow:hidden}.main.svelte-1n46o8q{flex:1;display:flex;flex-direction:column;overflow:hidden;min-width:0}.page-content.svelte-1n46o8q{flex:1;overflow:hidden;display:flex;background:var(--bg-primary)}
trackio/frontend/dist/assets/index-Djj_n7T6.js ADDED
The diff for this file is too large to render. See raw diff
 
trackio/frontend/dist/index.html ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Trackio Dashboard</title>
7
+ <link rel="icon" type="image/png" href="/static/trackio/trackio_logo_light.png" />
8
+ <script type="module" crossorigin src="/assets/index-Djj_n7T6.js"></script>
9
+ <link rel="stylesheet" crossorigin href="/assets/index-BgSTPqpC.css">
10
+ </head>
11
+ <body>
12
+ <div id="app"></div>
13
+ </body>
14
+ </html>
trackio/frontend/eslint.config.js ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import js from "@eslint/js";
2
+ import svelte from "eslint-plugin-svelte";
3
+ import svelteParser from "svelte-eslint-parser";
4
+ import globals from "globals";
5
+
6
+ export default [
7
+ { ignores: ["dist/**", "node_modules/**"] },
8
+ {
9
+ files: ["**/*.js"],
10
+ languageOptions: {
11
+ globals: {
12
+ ...globals.browser,
13
+ ...globals.es2021,
14
+ $state: "readonly",
15
+ $derived: "readonly",
16
+ $effect: "readonly",
17
+ $props: "readonly",
18
+ $bindable: "readonly",
19
+ $inspect: "readonly",
20
+ },
21
+ },
22
+ rules: {
23
+ ...js.configs.recommended.rules,
24
+ "no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
25
+ "no-empty": "off",
26
+ },
27
+ },
28
+ {
29
+ files: ["**/*.svelte"],
30
+ languageOptions: {
31
+ parser: svelteParser,
32
+ globals: { ...globals.browser, ...globals.es2021 },
33
+ },
34
+ plugins: { svelte },
35
+ rules: {
36
+ ...js.configs.recommended.rules,
37
+ ...svelte.configs.recommended.rules,
38
+ "no-unused-vars": ["error", { argsIgnorePattern: "^_", varsIgnorePattern: "^\\$" }],
39
+ "no-empty": "off",
40
+ },
41
+ },
42
+ ];
trackio/frontend/index.html ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Trackio Dashboard</title>
7
+ <link rel="icon" type="image/png" href="/static/trackio/trackio_logo_light.png" />
8
+ </head>
9
+ <body>
10
+ <div id="app"></div>
11
+ <script type="module" src="/src/main.js"></script>
12
+ </body>
13
+ </html>
trackio/frontend_server.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Serves the built Svelte frontend alongside the Trackio HTTP API."""
2
+
3
+ import logging
4
+ import re
5
+ from pathlib import Path
6
+
7
+ from starlette.responses import HTMLResponse
8
+ from starlette.routing import Mount, Route
9
+ from starlette.staticfiles import StaticFiles
10
+
11
+ FRONTEND_DIR = Path(__file__).parent / "frontend" / "dist"
12
+ ASSETS_DIR = Path(__file__).parent / "assets"
13
+
14
+ _logger = logging.getLogger(__name__)
15
+
16
+ _SPA_SEGMENTS = (
17
+ "metrics",
18
+ "system",
19
+ "media",
20
+ "reports",
21
+ "runs",
22
+ "run",
23
+ "files",
24
+ "settings",
25
+ )
26
+
27
+
28
+ def mount_frontend(app):
29
+ if not FRONTEND_DIR.exists():
30
+ _logger.warning(
31
+ "Trackio dashboard UI was not mounted: %s is missing. "
32
+ "Build the frontend with `npm ci && npm run build` in trackio/frontend.",
33
+ FRONTEND_DIR,
34
+ )
35
+ return
36
+
37
+ index_html_path = FRONTEND_DIR / "index.html"
38
+ if not index_html_path.exists():
39
+ _logger.warning(
40
+ "Trackio dashboard UI was not mounted: %s is missing.",
41
+ index_html_path,
42
+ )
43
+ return
44
+
45
+ index_html_content = index_html_path.read_text()
46
+ patched_html = re.sub(
47
+ r'/assets/(index-[^"]+)',
48
+ r"/assets/app/\1",
49
+ index_html_content,
50
+ )
51
+
52
+ async def serve_frontend(request):
53
+ return HTMLResponse(patched_html)
54
+
55
+ vite_assets = StaticFiles(directory=str(FRONTEND_DIR / "assets"))
56
+ static_assets = StaticFiles(directory=str(ASSETS_DIR))
57
+
58
+ app.routes.insert(0, Mount("/static/trackio", app=static_assets))
59
+ app.routes.insert(0, Mount("/assets/app", app=vite_assets))
60
+
61
+ for seg in reversed(_SPA_SEGMENTS):
62
+ app.routes.insert(0, Route(f"/{seg}/", serve_frontend, methods=["GET"]))
63
+ app.routes.insert(0, Route(f"/{seg}", serve_frontend, methods=["GET"]))
64
+ app.routes.insert(0, Route("/", serve_frontend, methods=["GET"]))
trackio/gpu.py ADDED
@@ -0,0 +1,381 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import threading
3
+ import warnings
4
+ from typing import TYPE_CHECKING, Any
5
+
6
+ if TYPE_CHECKING:
7
+ from trackio.run import Run
8
+
9
+ pynvml: Any = None
10
+ PYNVML_AVAILABLE = False
11
+ _nvml_initialized = False
12
+ _nvml_lock = threading.Lock()
13
+ _energy_baseline: dict[int, float] = {}
14
+
15
+
16
+ def _ensure_pynvml():
17
+ global PYNVML_AVAILABLE, pynvml
18
+ if PYNVML_AVAILABLE:
19
+ return pynvml
20
+ try:
21
+ import pynvml as _pynvml
22
+
23
+ pynvml = _pynvml
24
+ PYNVML_AVAILABLE = True
25
+ return pynvml
26
+ except ImportError:
27
+ raise ImportError(
28
+ "nvidia-ml-py is required for GPU monitoring. "
29
+ "Install it with: pip install nvidia-ml-py"
30
+ )
31
+
32
+
33
+ def _init_nvml() -> bool:
34
+ global _nvml_initialized
35
+ with _nvml_lock:
36
+ if _nvml_initialized:
37
+ return True
38
+ try:
39
+ nvml = _ensure_pynvml()
40
+ nvml.nvmlInit()
41
+ _nvml_initialized = True
42
+ return True
43
+ except Exception:
44
+ return False
45
+
46
+
47
+ def get_gpu_count() -> tuple[int, list[int]]:
48
+ """
49
+ Get the number of GPUs visible to this process and their physical indices.
50
+ Respects CUDA_VISIBLE_DEVICES environment variable.
51
+
52
+ Returns:
53
+ Tuple of (count, physical_indices) where:
54
+ - count: Number of visible GPUs
55
+ - physical_indices: List mapping logical index to physical GPU index.
56
+ e.g., if CUDA_VISIBLE_DEVICES=2,3 returns (2, [2, 3])
57
+ meaning logical GPU 0 = physical GPU 2, logical GPU 1 = physical GPU 3
58
+ """
59
+ if not _init_nvml():
60
+ return 0, []
61
+
62
+ cuda_visible = os.environ.get("CUDA_VISIBLE_DEVICES")
63
+ if cuda_visible is not None and cuda_visible.strip():
64
+ try:
65
+ indices = [int(x.strip()) for x in cuda_visible.split(",") if x.strip()]
66
+ return len(indices), indices
67
+ except ValueError:
68
+ pass
69
+
70
+ try:
71
+ total = pynvml.nvmlDeviceGetCount()
72
+ return total, list(range(total))
73
+ except Exception:
74
+ return 0, []
75
+
76
+
77
+ def get_all_gpu_count() -> tuple[int, list[int]]:
78
+ """
79
+ Get the total number of physical GPUs on the machine, ignoring CUDA_VISIBLE_DEVICES.
80
+
81
+ Returns:
82
+ Tuple of (count, physical_indices) for ALL GPUs on the machine.
83
+ e.g., on a 4-GPU machine returns (4, [0, 1, 2, 3]) regardless of
84
+ CUDA_VISIBLE_DEVICES setting.
85
+ """
86
+ if not _init_nvml():
87
+ return 0, []
88
+
89
+ try:
90
+ total = pynvml.nvmlDeviceGetCount()
91
+ return total, list(range(total))
92
+ except Exception:
93
+ return 0, []
94
+
95
+
96
+ def gpu_available() -> bool:
97
+ """
98
+ Check if GPU monitoring is available.
99
+
100
+ Returns True if nvidia-ml-py is installed and at least one NVIDIA GPU is detected.
101
+ This is used for auto-detection of GPU logging.
102
+ """
103
+ try:
104
+ _ensure_pynvml()
105
+ count, _ = get_gpu_count()
106
+ return count > 0
107
+ except ImportError:
108
+ return False
109
+ except Exception:
110
+ return False
111
+
112
+
113
+ def reset_energy_baseline():
114
+ """Reset the energy baseline for all GPUs. Called when a new run starts."""
115
+ global _energy_baseline
116
+ _energy_baseline = {}
117
+
118
+
119
+ def collect_gpu_metrics(device: int | None = None, all_gpus: bool = False) -> dict:
120
+ """
121
+ Collect GPU metrics for visible GPUs.
122
+
123
+ Args:
124
+ device: CUDA device index to collect metrics from. If None, collects
125
+ from all GPUs visible to this process (respects CUDA_VISIBLE_DEVICES).
126
+ The device index is the logical CUDA index (0, 1, 2...), not the
127
+ physical GPU index.
128
+ all_gpus: If True and device is None, collect metrics for ALL physical GPUs
129
+ on the machine, ignoring CUDA_VISIBLE_DEVICES. Used by GpuMonitor
130
+ to report system-wide GPU metrics in distributed training.
131
+
132
+ Returns:
133
+ Dictionary of GPU metrics. Keys use device indices (gpu/0/, gpu/1/, etc.).
134
+ """
135
+ if not _init_nvml():
136
+ return {}
137
+
138
+ if all_gpus and device is None:
139
+ gpu_count, visible_gpus = get_all_gpu_count()
140
+ else:
141
+ gpu_count, visible_gpus = get_gpu_count()
142
+ if gpu_count == 0:
143
+ return {}
144
+
145
+ if device is not None:
146
+ if device < 0 or device >= gpu_count:
147
+ return {}
148
+ gpu_indices = [(device, visible_gpus[device])]
149
+ else:
150
+ gpu_indices = list(enumerate(visible_gpus))
151
+
152
+ metrics = {}
153
+ total_util = 0.0
154
+ total_mem_used_gib = 0.0
155
+ total_power = 0.0
156
+ max_temp = 0.0
157
+ valid_util_count = 0
158
+
159
+ for logical_idx, physical_idx in gpu_indices:
160
+ prefix = f"gpu/{logical_idx}"
161
+ try:
162
+ handle = pynvml.nvmlDeviceGetHandleByIndex(physical_idx)
163
+
164
+ try:
165
+ util = pynvml.nvmlDeviceGetUtilizationRates(handle)
166
+ metrics[f"{prefix}/utilization"] = util.gpu
167
+ metrics[f"{prefix}/memory_utilization"] = util.memory
168
+ total_util += util.gpu
169
+ valid_util_count += 1
170
+ except Exception:
171
+ pass
172
+
173
+ try:
174
+ mem = pynvml.nvmlDeviceGetMemoryInfo(handle)
175
+ mem_used_gib = mem.used / (1024**3)
176
+ mem_total_gib = mem.total / (1024**3)
177
+ metrics[f"{prefix}/allocated_memory"] = mem_used_gib
178
+ metrics[f"{prefix}/total_memory"] = mem_total_gib
179
+ if mem.total > 0:
180
+ metrics[f"{prefix}/memory_usage"] = mem.used / mem.total
181
+ total_mem_used_gib += mem_used_gib
182
+ except Exception:
183
+ pass
184
+
185
+ try:
186
+ power_mw = pynvml.nvmlDeviceGetPowerUsage(handle)
187
+ power_w = power_mw / 1000.0
188
+ metrics[f"{prefix}/power"] = power_w
189
+ total_power += power_w
190
+ except Exception:
191
+ pass
192
+
193
+ try:
194
+ power_limit_mw = pynvml.nvmlDeviceGetPowerManagementLimit(handle)
195
+ power_limit_w = power_limit_mw / 1000.0
196
+ metrics[f"{prefix}/power_limit"] = power_limit_w
197
+ if power_limit_w > 0 and f"{prefix}/power" in metrics:
198
+ metrics[f"{prefix}/power_percent"] = (
199
+ metrics[f"{prefix}/power"] / power_limit_w
200
+ ) * 100
201
+ except Exception:
202
+ pass
203
+
204
+ try:
205
+ temp = pynvml.nvmlDeviceGetTemperature(
206
+ handle, pynvml.NVML_TEMPERATURE_GPU
207
+ )
208
+ metrics[f"{prefix}/temp"] = temp
209
+ max_temp = max(max_temp, temp)
210
+ except Exception:
211
+ pass
212
+
213
+ try:
214
+ sm_clock = pynvml.nvmlDeviceGetClockInfo(handle, pynvml.NVML_CLOCK_SM)
215
+ metrics[f"{prefix}/sm_clock"] = sm_clock
216
+ except Exception:
217
+ pass
218
+
219
+ try:
220
+ mem_clock = pynvml.nvmlDeviceGetClockInfo(handle, pynvml.NVML_CLOCK_MEM)
221
+ metrics[f"{prefix}/memory_clock"] = mem_clock
222
+ except Exception:
223
+ pass
224
+
225
+ try:
226
+ fan_speed = pynvml.nvmlDeviceGetFanSpeed(handle)
227
+ metrics[f"{prefix}/fan_speed"] = fan_speed
228
+ except Exception:
229
+ pass
230
+
231
+ try:
232
+ pstate = pynvml.nvmlDeviceGetPerformanceState(handle)
233
+ metrics[f"{prefix}/performance_state"] = pstate
234
+ except Exception:
235
+ pass
236
+
237
+ try:
238
+ energy_mj = pynvml.nvmlDeviceGetTotalEnergyConsumption(handle)
239
+ if physical_idx not in _energy_baseline:
240
+ _energy_baseline[physical_idx] = energy_mj
241
+ energy_consumed_mj = energy_mj - _energy_baseline[physical_idx]
242
+ metrics[f"{prefix}/energy_consumed"] = energy_consumed_mj / 1000.0
243
+ except Exception:
244
+ pass
245
+
246
+ try:
247
+ pcie_tx = pynvml.nvmlDeviceGetPcieThroughput(
248
+ handle, pynvml.NVML_PCIE_UTIL_TX_BYTES
249
+ )
250
+ pcie_rx = pynvml.nvmlDeviceGetPcieThroughput(
251
+ handle, pynvml.NVML_PCIE_UTIL_RX_BYTES
252
+ )
253
+ metrics[f"{prefix}/pcie_tx"] = pcie_tx / 1024.0
254
+ metrics[f"{prefix}/pcie_rx"] = pcie_rx / 1024.0
255
+ except Exception:
256
+ pass
257
+
258
+ try:
259
+ throttle = pynvml.nvmlDeviceGetCurrentClocksThrottleReasons(handle)
260
+ metrics[f"{prefix}/throttle_thermal"] = int(
261
+ bool(throttle & pynvml.nvmlClocksThrottleReasonSwThermalSlowdown)
262
+ )
263
+ metrics[f"{prefix}/throttle_power"] = int(
264
+ bool(throttle & pynvml.nvmlClocksThrottleReasonSwPowerCap)
265
+ )
266
+ metrics[f"{prefix}/throttle_hw_slowdown"] = int(
267
+ bool(throttle & pynvml.nvmlClocksThrottleReasonHwSlowdown)
268
+ )
269
+ metrics[f"{prefix}/throttle_apps"] = int(
270
+ bool(
271
+ throttle
272
+ & pynvml.nvmlClocksThrottleReasonApplicationsClocksSetting
273
+ )
274
+ )
275
+ except Exception:
276
+ pass
277
+
278
+ try:
279
+ ecc_corrected = pynvml.nvmlDeviceGetTotalEccErrors(
280
+ handle,
281
+ pynvml.NVML_MEMORY_ERROR_TYPE_CORRECTED,
282
+ pynvml.NVML_VOLATILE_ECC,
283
+ )
284
+ metrics[f"{prefix}/corrected_memory_errors"] = ecc_corrected
285
+ except Exception:
286
+ pass
287
+
288
+ try:
289
+ ecc_uncorrected = pynvml.nvmlDeviceGetTotalEccErrors(
290
+ handle,
291
+ pynvml.NVML_MEMORY_ERROR_TYPE_UNCORRECTED,
292
+ pynvml.NVML_VOLATILE_ECC,
293
+ )
294
+ metrics[f"{prefix}/uncorrected_memory_errors"] = ecc_uncorrected
295
+ except Exception:
296
+ pass
297
+
298
+ except Exception:
299
+ continue
300
+
301
+ if valid_util_count > 0:
302
+ metrics["gpu/mean_utilization"] = total_util / valid_util_count
303
+ if total_mem_used_gib > 0:
304
+ metrics["gpu/total_allocated_memory"] = total_mem_used_gib
305
+ if total_power > 0:
306
+ metrics["gpu/total_power"] = total_power
307
+ if max_temp > 0:
308
+ metrics["gpu/max_temp"] = max_temp
309
+
310
+ return metrics
311
+
312
+
313
+ class GpuMonitor:
314
+ def __init__(self, run: "Run", interval: float = 10.0):
315
+ self._run = run
316
+ self._interval = interval
317
+ self._stop_flag = threading.Event()
318
+ self._thread: "threading.Thread | None" = None
319
+
320
+ def start(self):
321
+ count, _ = get_all_gpu_count()
322
+ if count == 0:
323
+ warnings.warn(
324
+ "auto_log_gpu=True but no NVIDIA GPUs detected. GPU logging disabled."
325
+ )
326
+ return
327
+
328
+ reset_energy_baseline()
329
+ self._thread = threading.Thread(target=self._monitor_loop, daemon=True)
330
+ self._thread.start()
331
+
332
+ def stop(self):
333
+ self._stop_flag.set()
334
+ if self._thread is not None:
335
+ self._thread.join(timeout=2.0)
336
+
337
+ def _monitor_loop(self):
338
+ while not self._stop_flag.is_set():
339
+ try:
340
+ metrics = collect_gpu_metrics(all_gpus=True)
341
+ if metrics:
342
+ self._run.log_system(metrics)
343
+ except Exception:
344
+ pass
345
+
346
+ self._stop_flag.wait(timeout=self._interval)
347
+
348
+
349
+ def log_gpu(run: "Run | None" = None, device: int | None = None) -> dict:
350
+ """
351
+ Log GPU metrics to the current or specified run as system metrics.
352
+
353
+ Args:
354
+ run: Optional Run instance. If None, uses current run from context.
355
+ device: CUDA device index to collect metrics from. If None, collects
356
+ from all GPUs visible to this process (respects CUDA_VISIBLE_DEVICES).
357
+
358
+ Returns:
359
+ dict: The GPU metrics that were logged.
360
+
361
+ Example:
362
+ ```python
363
+ import trackio
364
+
365
+ run = trackio.init(project="my-project")
366
+ trackio.log({"loss": 0.5})
367
+ trackio.log_gpu() # logs all visible GPUs
368
+ trackio.log_gpu(device=0) # logs only CUDA device 0
369
+ ```
370
+ """
371
+ from trackio import context_vars
372
+
373
+ if run is None:
374
+ run = context_vars.current_run.get()
375
+ if run is None:
376
+ raise RuntimeError("Call trackio.init() before trackio.log_gpu().")
377
+
378
+ metrics = collect_gpu_metrics(device=device)
379
+ if metrics:
380
+ run.log_system(metrics)
381
+ return metrics
trackio/histogram.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Sequence
2
+
3
+ import numpy as np
4
+
5
+
6
+ class Histogram:
7
+ """
8
+ Histogram data type for Trackio, compatible with wandb.Histogram.
9
+
10
+ Args:
11
+ sequence (`np.ndarray` or `Sequence[float]` or `Sequence[int]`, *optional*):
12
+ Sequence of values to create the histogram from.
13
+ np_histogram (`tuple`, *optional*):
14
+ Pre-computed NumPy histogram as a `(hist, bins)` tuple.
15
+ num_bins (`int`, *optional*, defaults to `64`):
16
+ Number of bins for the histogram (maximum `512`).
17
+
18
+ Example:
19
+ ```python
20
+ import trackio
21
+ import numpy as np
22
+
23
+ # Create histogram from sequence
24
+ data = np.random.randn(1000)
25
+ trackio.log({"distribution": trackio.Histogram(data)})
26
+
27
+ # Create histogram from numpy histogram
28
+ hist, bins = np.histogram(data, bins=30)
29
+ trackio.log({"distribution": trackio.Histogram(np_histogram=(hist, bins))})
30
+
31
+ # Specify custom number of bins
32
+ trackio.log({"distribution": trackio.Histogram(data, num_bins=50)})
33
+ ```
34
+ """
35
+
36
+ TYPE = "trackio.histogram"
37
+
38
+ def __init__(
39
+ self,
40
+ sequence: np.ndarray | Sequence[float] | Sequence[int] | None = None,
41
+ np_histogram: tuple | None = None,
42
+ num_bins: int = 64,
43
+ ):
44
+ if sequence is None and np_histogram is None:
45
+ raise ValueError("Must provide either sequence or np_histogram")
46
+
47
+ if sequence is not None and np_histogram is not None:
48
+ raise ValueError("Cannot provide both sequence and np_histogram")
49
+
50
+ num_bins = min(num_bins, 512)
51
+
52
+ if np_histogram is not None:
53
+ self.histogram, self.bins = np_histogram
54
+ self.histogram = np.asarray(self.histogram)
55
+ self.bins = np.asarray(self.bins)
56
+ else:
57
+ data = np.asarray(sequence).flatten()
58
+ data = data[np.isfinite(data)]
59
+ if len(data) == 0:
60
+ self.histogram = np.array([])
61
+ self.bins = np.array([])
62
+ else:
63
+ self.histogram, self.bins = np.histogram(data, bins=num_bins)
64
+
65
+ def _to_dict(self) -> dict:
66
+ """Convert histogram to dictionary for storage."""
67
+ return {
68
+ "_type": self.TYPE,
69
+ "bins": self.bins.tolist(),
70
+ "values": self.histogram.tolist(),
71
+ }
trackio/imports.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import csv
2
+ import os
3
+ from pathlib import Path
4
+
5
+ from trackio import deploy, utils
6
+ from trackio.sqlite_storage import SQLiteStorage
7
+
8
+
9
+ def import_csv(
10
+ csv_path: str | Path,
11
+ project: str,
12
+ name: str | None = None,
13
+ space_id: str | None = None,
14
+ dataset_id: str | None = None,
15
+ private: bool | None = None,
16
+ force: bool = False,
17
+ ) -> None:
18
+ """
19
+ Imports a CSV file into a Trackio project. The CSV file must contain a `"step"`
20
+ column, may optionally contain a `"timestamp"` column, and any other columns will be
21
+ treated as metrics. It should also include a header row with the column names.
22
+
23
+ TODO: call init() and return a Run object so that the user can continue to log metrics to it.
24
+
25
+ Args:
26
+ csv_path (`str` or `Path`):
27
+ The str or Path to the CSV file to import.
28
+ project (`str`):
29
+ The name of the project to import the CSV file into. Must not be an existing
30
+ project.
31
+ name (`str`, *optional*):
32
+ The name of the Run to import the CSV file into. If not provided, a default
33
+ name will be generated.
34
+ name (`str`, *optional*):
35
+ The name of the run (if not provided, a default name will be generated).
36
+ space_id (`str`, *optional*):
37
+ If provided, the project will be logged to a Hugging Face Space instead of a
38
+ local directory. Should be a complete Space name like `"username/reponame"`
39
+ or `"orgname/reponame"`, or just `"reponame"` in which case the Space will
40
+ be created in the currently-logged-in Hugging Face user's namespace. If the
41
+ Space does not exist, it will be created. If the Space already exists, the
42
+ project will be logged to it.
43
+ dataset_id (`str`, *optional*):
44
+ Deprecated. Use `bucket_id` instead.
45
+ private (`bool`, *optional*):
46
+ Whether to make the Space private. If None (default), the repo will be
47
+ public unless the organization's default is private. This value is ignored
48
+ if the repo already exists.
49
+ """
50
+ if SQLiteStorage.get_runs(project):
51
+ raise ValueError(
52
+ f"Project '{project}' already exists. Cannot import CSV into existing project."
53
+ )
54
+
55
+ csv_path = Path(csv_path)
56
+ if not csv_path.exists():
57
+ raise FileNotFoundError(f"CSV file not found: {csv_path}")
58
+
59
+ with csv_path.open(newline="", encoding="utf-8") as csv_file:
60
+ reader = csv.DictReader(csv_file)
61
+ source_columns = reader.fieldnames or []
62
+ rows = list(reader)
63
+
64
+ if not rows:
65
+ raise ValueError("CSV file is empty")
66
+
67
+ column_mapping = utils.simplify_column_names(source_columns)
68
+ normalized_rows = [
69
+ {column_mapping[key]: value for key, value in row.items()} for row in rows
70
+ ]
71
+ columns = list(normalized_rows[0].keys())
72
+
73
+ step_column = None
74
+ for col in columns:
75
+ if col.lower() == "step":
76
+ step_column = col
77
+ break
78
+
79
+ if step_column is None:
80
+ raise ValueError("CSV file must contain a 'step' or 'Step' column")
81
+
82
+ if name is None:
83
+ name = csv_path.stem
84
+
85
+ metrics_list = []
86
+ steps = []
87
+ timestamps = []
88
+
89
+ numeric_columns = []
90
+ for column in columns:
91
+ if column == step_column:
92
+ continue
93
+ if column == "timestamp":
94
+ continue
95
+
96
+ try:
97
+ for row in normalized_rows:
98
+ value = row[column]
99
+ if value in ("", None):
100
+ continue
101
+ float(value)
102
+ except (ValueError, TypeError):
103
+ continue
104
+ numeric_columns.append(column)
105
+
106
+ for row in normalized_rows:
107
+ metrics = {}
108
+ for column in numeric_columns:
109
+ value = row[column]
110
+ if value not in ("", None):
111
+ metrics[column] = float(value)
112
+
113
+ if metrics:
114
+ metrics_list.append(metrics)
115
+ steps.append(int(float(row[step_column])))
116
+
117
+ if "timestamp" in row and row["timestamp"] not in ("", None):
118
+ timestamps.append(str(row["timestamp"]))
119
+ else:
120
+ timestamps.append("")
121
+
122
+ if metrics_list:
123
+ SQLiteStorage.bulk_log(
124
+ project=project,
125
+ run=name,
126
+ metrics_list=metrics_list,
127
+ steps=steps,
128
+ timestamps=timestamps,
129
+ )
130
+
131
+ print(
132
+ f"* Imported {len(metrics_list)} rows from {csv_path} into project '{project}' as run '{name}'"
133
+ )
134
+ print(f"* Metrics found: {', '.join(metrics_list[0].keys())}")
135
+
136
+ space_id, dataset_id, _ = utils.preprocess_space_and_dataset_ids(
137
+ space_id, dataset_id
138
+ )
139
+ if dataset_id is not None:
140
+ os.environ["TRACKIO_DATASET_ID"] = dataset_id
141
+ print(f"* Trackio metrics will be synced to Hugging Face Dataset: {dataset_id}")
142
+
143
+ if space_id is None:
144
+ utils.print_dashboard_instructions(project)
145
+ else:
146
+ deploy.create_space_if_not_exists(
147
+ space_id=space_id, dataset_id=dataset_id, private=private
148
+ )
149
+ deploy.wait_until_space_exists(space_id=space_id)
150
+ deploy.upload_db_to_space(project=project, space_id=space_id, force=force)
151
+ print(
152
+ f"* View dashboard by going to: {deploy.SPACE_URL.format(space_id=space_id)}"
153
+ )
154
+
155
+
156
+ def import_tf_events(
157
+ log_dir: str | Path,
158
+ project: str,
159
+ name: str | None = None,
160
+ space_id: str | None = None,
161
+ dataset_id: str | None = None,
162
+ private: bool | None = None,
163
+ force: bool = False,
164
+ ) -> None:
165
+ """
166
+ Imports TensorFlow Events files from a directory into a Trackio project. Each
167
+ subdirectory in the log directory will be imported as a separate run.
168
+
169
+ Args:
170
+ log_dir (`str` or `Path`):
171
+ The str or Path to the directory containing TensorFlow Events files.
172
+ project (`str`):
173
+ The name of the project to import the TensorFlow Events files into. Must not
174
+ be an existing project.
175
+ name (`str`, *optional*):
176
+ The name prefix for runs (if not provided, will use directory names). Each
177
+ subdirectory will create a separate run.
178
+ space_id (`str`, *optional*):
179
+ If provided, the project will be logged to a Hugging Face Space instead of a
180
+ local directory. Should be a complete Space name like `"username/reponame"`
181
+ or `"orgname/reponame"`, or just `"reponame"` in which case the Space will
182
+ be created in the currently-logged-in Hugging Face user's namespace. If the
183
+ Space does not exist, it will be created. If the Space already exists, the
184
+ project will be logged to it.
185
+ dataset_id (`str`, *optional*):
186
+ Deprecated. Use `bucket_id` instead.
187
+ private (`bool`, *optional*):
188
+ Whether to make the Space private. If None (default), the repo will be
189
+ public unless the organization's default is private. This value is ignored
190
+ if the repo already exists.
191
+ """
192
+ try:
193
+ from tbparse import SummaryReader
194
+ except ImportError:
195
+ raise ImportError(
196
+ "The `tbparse` package is not installed but is required for `import_tf_events`. Please install trackio with the `tensorboard` extra: `pip install trackio[tensorboard]`."
197
+ )
198
+
199
+ if SQLiteStorage.get_runs(project):
200
+ raise ValueError(
201
+ f"Project '{project}' already exists. Cannot import TF events into existing project."
202
+ )
203
+
204
+ path = Path(log_dir)
205
+ if not path.exists():
206
+ raise FileNotFoundError(f"TF events directory not found: {path}")
207
+
208
+ # Use tbparse to read all tfevents files in the directory structure
209
+ reader = SummaryReader(str(path), extra_columns={"dir_name"})
210
+ df = reader.scalars
211
+
212
+ if df.empty:
213
+ raise ValueError(f"No TensorFlow events data found in {path}")
214
+
215
+ total_imported = 0
216
+ imported_runs = []
217
+
218
+ # Group by dir_name to create separate runs
219
+ for dir_name, group_df in df.groupby("dir_name"):
220
+ try:
221
+ # Determine run name based on directory name
222
+ if dir_name == "":
223
+ run_name = "main" # For files in the root directory
224
+ else:
225
+ run_name = dir_name # Use directory name
226
+
227
+ if name:
228
+ run_name = f"{name}_{run_name}"
229
+
230
+ if group_df.empty:
231
+ print(f"* Skipping directory {dir_name}: no scalar data found")
232
+ continue
233
+
234
+ metrics_list = []
235
+ steps = []
236
+ timestamps = []
237
+
238
+ for _, row in group_df.iterrows():
239
+ # Convert row values to appropriate types
240
+ tag = str(row["tag"])
241
+ value = float(row["value"])
242
+ step = int(row["step"])
243
+
244
+ metrics = {tag: value}
245
+ metrics_list.append(metrics)
246
+ steps.append(step)
247
+
248
+ # Use wall_time if present, else fallback
249
+ if "wall_time" in group_df.columns and not utils.is_missing_value(
250
+ row["wall_time"]
251
+ ):
252
+ timestamps.append(str(row["wall_time"]))
253
+ else:
254
+ timestamps.append("")
255
+
256
+ if metrics_list:
257
+ SQLiteStorage.bulk_log(
258
+ project=project,
259
+ run=str(run_name),
260
+ metrics_list=metrics_list,
261
+ steps=steps,
262
+ timestamps=timestamps,
263
+ )
264
+
265
+ total_imported += len(metrics_list)
266
+ imported_runs.append(run_name)
267
+
268
+ print(
269
+ f"* Imported {len(metrics_list)} scalar events from directory '{dir_name}' as run '{run_name}'"
270
+ )
271
+ print(f"* Metrics in this run: {', '.join(set(group_df['tag']))}")
272
+
273
+ except Exception as e:
274
+ print(f"* Error processing directory {dir_name}: {e}")
275
+ continue
276
+
277
+ if not imported_runs:
278
+ raise ValueError("No valid TensorFlow events data could be imported")
279
+
280
+ print(f"* Total imported events: {total_imported}")
281
+ print(f"* Created runs: {', '.join(imported_runs)}")
282
+
283
+ space_id, dataset_id, _ = utils.preprocess_space_and_dataset_ids(
284
+ space_id, dataset_id
285
+ )
286
+ if dataset_id is not None:
287
+ os.environ["TRACKIO_DATASET_ID"] = dataset_id
288
+ print(f"* Trackio metrics will be synced to Hugging Face Dataset: {dataset_id}")
289
+
290
+ if space_id is None:
291
+ utils.print_dashboard_instructions(project)
292
+ else:
293
+ deploy.create_space_if_not_exists(
294
+ space_id, dataset_id=dataset_id, private=private
295
+ )
296
+ deploy.wait_until_space_exists(space_id)
297
+ deploy.upload_db_to_space(project, space_id, force=force)
298
+ print(
299
+ f"* View dashboard by going to: {deploy.SPACE_URL.format(space_id=space_id)}"
300
+ )
trackio/launch.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import secrets
5
+ import socket
6
+ import threading
7
+ import time
8
+ import warnings
9
+ from pathlib import Path
10
+ from typing import Any
11
+
12
+ import httpx
13
+ import uvicorn
14
+ from uvicorn.config import Config
15
+
16
+ from trackio._vendor.gradio_exceptions import ChecksumMismatchError
17
+ from trackio._vendor.networking import normalize_share_url, setup_tunnel, url_ok
18
+ from trackio._vendor.tunneling import BINARY_PATH
19
+ from trackio.launch_utils import colab_check, is_hosted_notebook
20
+
21
+ INITIAL_PORT_VALUE = int(os.getenv("GRADIO_SERVER_PORT", "7860"))
22
+ TRY_NUM_PORTS = int(os.getenv("GRADIO_NUM_PORTS", "100"))
23
+ LOCALHOST_NAME = os.getenv("GRADIO_SERVER_NAME", "127.0.0.1")
24
+
25
+
26
+ class _UvicornServer(uvicorn.Server):
27
+ def install_signal_handlers(self) -> None:
28
+ pass
29
+
30
+ def run_in_thread(self) -> None:
31
+ self.thread = threading.Thread(target=self.run, daemon=True)
32
+ self.thread.start()
33
+ start = time.time()
34
+ while not self.started:
35
+ time.sleep(1e-3)
36
+ if time.time() - start > 60:
37
+ raise RuntimeError(
38
+ "Server failed to start. Please check that the port is available."
39
+ )
40
+
41
+
42
+ def _bind_host(server_name: str) -> str:
43
+ if server_name.startswith("[") and server_name.endswith("]"):
44
+ return server_name[1:-1]
45
+ return server_name
46
+
47
+
48
+ def start_server(
49
+ app: Any,
50
+ server_name: str | None = None,
51
+ server_port: int | None = None,
52
+ ssl_keyfile: str | None = None,
53
+ ssl_certfile: str | None = None,
54
+ ssl_keyfile_password: str | None = None,
55
+ ) -> tuple[str, int, str, _UvicornServer]:
56
+ server_name = server_name or LOCALHOST_NAME
57
+ url_host_name = "localhost" if server_name == "0.0.0.0" else server_name
58
+
59
+ host = _bind_host(server_name)
60
+
61
+ server_ports = (
62
+ [server_port]
63
+ if server_port is not None
64
+ else range(INITIAL_PORT_VALUE, INITIAL_PORT_VALUE + TRY_NUM_PORTS)
65
+ )
66
+
67
+ port_used = None
68
+ server = None
69
+ for port in server_ports:
70
+ try:
71
+ socket_family = socket.AF_INET6 if ":" in host else socket.AF_INET
72
+ with socket.socket(socket_family, socket.SOCK_STREAM) as s:
73
+ s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
74
+ s.bind((host, port))
75
+ config = Config(
76
+ app=app,
77
+ port=port,
78
+ host=host,
79
+ log_level="warning",
80
+ ssl_keyfile=ssl_keyfile,
81
+ ssl_certfile=ssl_certfile,
82
+ ssl_keyfile_password=ssl_keyfile_password,
83
+ )
84
+ server = _UvicornServer(config=config)
85
+ server.run_in_thread()
86
+ port_used = port
87
+ break
88
+ except (OSError, RuntimeError):
89
+ continue
90
+ else:
91
+ raise OSError(
92
+ f"Cannot find empty port in range: {min(server_ports)}-{max(server_ports)}. "
93
+ "Set GRADIO_SERVER_PORT or pass server_port to trackio.show()."
94
+ )
95
+
96
+ assert port_used is not None and server is not None
97
+
98
+ if ssl_keyfile is not None:
99
+ path_to_local_server = f"https://{url_host_name}:{port_used}/"
100
+ else:
101
+ path_to_local_server = f"http://{url_host_name}:{port_used}/"
102
+
103
+ return server_name, port_used, path_to_local_server, server
104
+
105
+
106
+ def launch_trackio_dashboard(
107
+ starlette_app: Any,
108
+ *,
109
+ server_name: str | None = None,
110
+ server_port: int | None = None,
111
+ share: bool | None = None,
112
+ share_server_address: str | None = None,
113
+ share_server_protocol: str | None = None,
114
+ share_server_tls_certificate: str | None = None,
115
+ mcp_server: bool = False,
116
+ ssl_verify: bool = True,
117
+ quiet: bool = False,
118
+ ) -> tuple[str | None, str | None, str | None, Any]:
119
+ is_colab = colab_check()
120
+ is_hosted_nb = is_hosted_notebook()
121
+ space_id = os.getenv("SPACE_ID")
122
+
123
+ if share is None:
124
+ if is_colab or is_hosted_nb:
125
+ if not quiet:
126
+ print(
127
+ "It looks like you are running Trackio on a hosted Jupyter notebook, which requires "
128
+ "`share=True`. Automatically setting `share=True` "
129
+ "(set `share=False` in `show()` to disable).\n"
130
+ )
131
+ share = True
132
+ else:
133
+ share = os.getenv("GRADIO_SHARE", "").lower() == "true"
134
+
135
+ sn = server_name
136
+ if sn is None and os.getenv("SYSTEM") == "spaces":
137
+ sn = "0.0.0.0"
138
+ elif sn is None:
139
+ sn = LOCALHOST_NAME
140
+
141
+ server_name_r, server_port_r, local_url, uv_server = start_server(
142
+ starlette_app,
143
+ server_name=sn,
144
+ server_port=server_port,
145
+ )
146
+
147
+ local_api_url = f"{local_url.rstrip('/')}/api/"
148
+ try:
149
+ httpx.get(f"{local_url.rstrip('/')}/version", verify=ssl_verify, timeout=10)
150
+ except Exception as e:
151
+ raise RuntimeError(
152
+ f"Could not reach Trackio server at {local_url.rstrip('/')}/version: {e}"
153
+ ) from e
154
+
155
+ if share and space_id:
156
+ warnings.warn("Setting share=True is not supported on Hugging Face Spaces")
157
+ share = False
158
+
159
+ share_url: str | None = None
160
+ if share:
161
+ try:
162
+ share_tok = secrets.token_urlsafe(32)
163
+ proto = share_server_protocol or (
164
+ "http" if share_server_address is not None else "https"
165
+ )
166
+ raw = setup_tunnel(
167
+ local_host=server_name_r,
168
+ local_port=server_port_r,
169
+ share_token=share_tok,
170
+ share_server_address=share_server_address,
171
+ share_server_tls_certificate=share_server_tls_certificate,
172
+ )
173
+ share_url = normalize_share_url(raw, proto)
174
+ if not quiet:
175
+ print(f"* Running on public URL: {share_url}")
176
+ print(
177
+ "\nThis share link expires in 1 week. For permanent hosting, deploy to Hugging Face Spaces."
178
+ )
179
+ except Exception as e:
180
+ share_url = None
181
+ if not quiet:
182
+ if isinstance(e, ChecksumMismatchError):
183
+ print(
184
+ "\nCould not create share link. Checksum mismatch for frpc binary."
185
+ )
186
+ elif Path(BINARY_PATH).exists():
187
+ print(
188
+ "\nCould not create share link. Check your internet connection or https://status.gradio.app."
189
+ )
190
+ else:
191
+ print(
192
+ f"\nCould not create share link. Missing frpc at {BINARY_PATH}. {e}"
193
+ )
194
+
195
+ if not share_url and not quiet:
196
+ print("* To create a public link, set `share=True` in `trackio.show()`.")
197
+
198
+ return local_url, share_url, local_api_url, uv_server
199
+
200
+
201
+ def url_ok_local(local_url: str) -> bool:
202
+ return url_ok(local_url)
trackio/launch_utils.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+
4
+ def colab_check() -> bool:
5
+ is_colab = False
6
+ try:
7
+ from IPython.core.getipython import get_ipython # noqa: PLC0415
8
+
9
+ from_ipynb = get_ipython()
10
+ if "google.colab" in str(from_ipynb):
11
+ is_colab = True
12
+ except (ImportError, NameError):
13
+ pass
14
+ return is_colab
15
+
16
+
17
+ def is_hosted_notebook() -> bool:
18
+ return bool(
19
+ os.environ.get("KAGGLE_KERNEL_RUN_TYPE")
20
+ or os.path.exists("/home/ec2-user/SageMaker")
21
+ )
22
+
23
+
24
+ def ipython_check() -> bool:
25
+ is_ipython = False
26
+ try:
27
+ from IPython.core.getipython import get_ipython # noqa: PLC0415
28
+
29
+ if get_ipython() is not None:
30
+ is_ipython = True
31
+ except (ImportError, NameError):
32
+ pass
33
+ return is_ipython
trackio/markdown.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Markdown:
2
+ """
3
+ Markdown report data type for Trackio.
4
+
5
+ Args:
6
+ text (`str`):
7
+ Markdown content to log.
8
+ """
9
+
10
+ TYPE = "trackio.markdown"
11
+
12
+ def __init__(self, text: str = ""):
13
+ if not isinstance(text, str):
14
+ raise ValueError("Markdown text must be a string")
15
+ self.text = text
16
+
17
+ def _to_dict(self) -> dict:
18
+ return {
19
+ "_type": self.TYPE,
20
+ "_value": self.text,
21
+ }
trackio/mcp_setup.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ import secrets
5
+ from contextlib import asynccontextmanager
6
+ from typing import Any
7
+
8
+ from starlette.routing import Mount
9
+
10
+ from trackio.sqlite_storage import SQLiteStorage
11
+
12
+
13
+ def _assert_mcp_mutation_access(
14
+ *,
15
+ hf_token: str | None = None,
16
+ write_token: str | None = None,
17
+ ) -> None:
18
+ import trackio.server as trackio_server # noqa: PLC0415
19
+
20
+ if os.getenv("SYSTEM") == "spaces":
21
+ try:
22
+ trackio_server.check_hf_token_has_write_access(hf_token)
23
+ except PermissionError as e:
24
+ raise ValueError(str(e)) from e
25
+ return
26
+
27
+ if not secrets.compare_digest(write_token or "", trackio_server.write_token or ""):
28
+ raise ValueError(
29
+ "A write_token is required for Trackio MCP mutations. "
30
+ "Use the write token from the dashboard URL."
31
+ )
32
+
33
+
34
+ def create_mcp_integration() -> tuple[list[Any], Any]:
35
+ from mcp.server.fastmcp import FastMCP # noqa: PLC0415
36
+
37
+ import trackio.server as trackio_server # noqa: PLC0415
38
+
39
+ mcp = FastMCP(
40
+ "Trackio",
41
+ instructions="Inspect and manage Trackio experiment data.",
42
+ streamable_http_path="/",
43
+ log_level="WARNING",
44
+ )
45
+
46
+ mcp.add_tool(
47
+ trackio_server.get_all_projects,
48
+ description="List all Trackio projects available on this server.",
49
+ structured_output=True,
50
+ )
51
+ mcp.add_tool(
52
+ trackio_server.get_runs_for_project,
53
+ description="List runs for a given Trackio project.",
54
+ structured_output=True,
55
+ )
56
+ mcp.add_tool(
57
+ trackio_server.get_metrics_for_run,
58
+ description="List metric names recorded for a given Trackio run.",
59
+ structured_output=True,
60
+ )
61
+ mcp.add_tool(
62
+ trackio_server.get_project_summary,
63
+ description="Return summary metadata for a Trackio project.",
64
+ structured_output=True,
65
+ )
66
+ mcp.add_tool(
67
+ trackio_server.get_run_summary,
68
+ description="Return summary metadata for a Trackio run.",
69
+ structured_output=True,
70
+ )
71
+ mcp.add_tool(
72
+ trackio_server.get_metric_values,
73
+ description="Fetch metric values for a run, optionally around a step or time.",
74
+ structured_output=True,
75
+ )
76
+ mcp.add_tool(
77
+ trackio_server.get_system_metrics_for_run,
78
+ description="List system metric names recorded for a run.",
79
+ structured_output=True,
80
+ )
81
+ mcp.add_tool(
82
+ trackio_server.get_system_logs,
83
+ description="Fetch system metric logs for a run.",
84
+ structured_output=True,
85
+ )
86
+ mcp.add_tool(
87
+ trackio_server.get_snapshot,
88
+ description="Fetch a single Trackio snapshot around a step or timestamp.",
89
+ structured_output=True,
90
+ )
91
+ mcp.add_tool(
92
+ trackio_server.get_logs,
93
+ description="Fetch Trackio metric logs for a run.",
94
+ structured_output=True,
95
+ )
96
+ mcp.add_tool(
97
+ trackio_server.get_alerts,
98
+ description="Fetch alerts for a project, optionally filtered by run or level.",
99
+ structured_output=True,
100
+ )
101
+ mcp.add_tool(
102
+ trackio_server.get_settings,
103
+ description="Return Trackio dashboard settings and asset configuration.",
104
+ structured_output=True,
105
+ )
106
+
107
+ @mcp.tool(
108
+ description="Delete a run. On Spaces, pass an hf_token with write access.",
109
+ structured_output=True,
110
+ )
111
+ def delete_run(
112
+ project: str,
113
+ run: str,
114
+ hf_token: str | None = None,
115
+ write_token: str | None = None,
116
+ ) -> bool:
117
+ _assert_mcp_mutation_access(hf_token=hf_token, write_token=write_token)
118
+ return SQLiteStorage.delete_run(project, run)
119
+
120
+ @mcp.tool(
121
+ description="Rename a run. On Spaces, pass an hf_token with write access.",
122
+ structured_output=True,
123
+ )
124
+ def rename_run(
125
+ project: str,
126
+ old_name: str,
127
+ new_name: str,
128
+ hf_token: str | None = None,
129
+ write_token: str | None = None,
130
+ ) -> bool:
131
+ _assert_mcp_mutation_access(hf_token=hf_token, write_token=write_token)
132
+ SQLiteStorage.rename_run(project, old_name, new_name)
133
+ return True
134
+
135
+ @mcp.tool(
136
+ description=(
137
+ "Trigger a Trackio export/sync pass. On Spaces, pass an hf_token with "
138
+ "write access."
139
+ ),
140
+ structured_output=True,
141
+ )
142
+ def trigger_sync(
143
+ hf_token: str | None = None,
144
+ write_token: str | None = None,
145
+ ) -> bool:
146
+ _assert_mcp_mutation_access(hf_token=hf_token, write_token=write_token)
147
+ return trackio_server.force_sync()
148
+
149
+ mcp_app = mcp.streamable_http_app()
150
+
151
+ @asynccontextmanager
152
+ async def mcp_lifespan_context(app):
153
+ async with mcp.session_manager.run():
154
+ yield
155
+
156
+ return [Mount("/mcp", app=mcp_app)], mcp_lifespan_context
trackio/media/__init__.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Media module for Trackio.
3
+
4
+ This module contains all media-related functionality including:
5
+ - TrackioImage, TrackioVideo, TrackioAudio classes
6
+ - Video writing utilities
7
+ - Audio conversion utilities
8
+ """
9
+
10
+ from trackio.media.audio import TrackioAudio
11
+ from trackio.media.image import TrackioImage
12
+ from trackio.media.media import TrackioMedia
13
+ from trackio.media.utils import get_project_media_path
14
+ from trackio.media.video import TrackioVideo
15
+
16
+ write_audio = TrackioAudio.write_audio
17
+ write_video = TrackioVideo.write_video
18
+
19
+ __all__ = [
20
+ "TrackioMedia",
21
+ "TrackioImage",
22
+ "TrackioVideo",
23
+ "TrackioAudio",
24
+ "get_project_media_path",
25
+ "write_video",
26
+ "write_audio",
27
+ ]
trackio/media/audio.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import warnings
4
+ from pathlib import Path
5
+ from typing import Literal
6
+
7
+ import numpy as np
8
+ from pydub import AudioSegment
9
+
10
+ from trackio.media.media import TrackioMedia
11
+ from trackio.media.utils import check_ffmpeg_installed, check_path
12
+
13
+ SUPPORTED_FORMATS = ["wav", "mp3"]
14
+ AudioFormatType = Literal["wav", "mp3"]
15
+ TrackioAudioSourceType = str | Path | np.ndarray
16
+
17
+
18
+ class TrackioAudio(TrackioMedia):
19
+ """
20
+ Initializes an Audio object.
21
+
22
+ Example:
23
+ ```python
24
+ import trackio
25
+ import numpy as np
26
+
27
+ # Generate a 1-second 440 Hz sine wave (mono)
28
+ sr = 16000
29
+ t = np.linspace(0, 1, sr, endpoint=False)
30
+ wave = 0.2 * np.sin(2 * np.pi * 440 * t)
31
+ audio = trackio.Audio(wave, caption="A4 sine", sample_rate=sr, format="wav")
32
+ trackio.log({"tone": audio})
33
+
34
+ # Stereo from numpy array (shape: samples, 2)
35
+ stereo = np.stack([wave, wave], axis=1)
36
+ audio = trackio.Audio(stereo, caption="Stereo", sample_rate=sr, format="mp3")
37
+ trackio.log({"stereo": audio})
38
+
39
+ # From an existing file
40
+ audio = trackio.Audio("path/to/audio.wav", caption="From file")
41
+ trackio.log({"file_audio": audio})
42
+ ```
43
+
44
+ Args:
45
+ value (`str`, `Path`, or `numpy.ndarray`, *optional*):
46
+ A path to an audio file, or a numpy array.
47
+ The array should be shaped `(samples,)` for mono or `(samples, 2)` for stereo.
48
+ Float arrays will be peak-normalized and converted to 16-bit PCM; integer arrays will be converted to 16-bit PCM as needed.
49
+ caption (`str`, *optional*):
50
+ A string caption for the audio.
51
+ sample_rate (`int`, *optional*):
52
+ Sample rate in Hz. Required when `value` is a numpy array.
53
+ format (`Literal["wav", "mp3"]`, *optional*):
54
+ Audio format used when `value` is a numpy array. Default is "wav".
55
+ """
56
+
57
+ TYPE = "trackio.audio"
58
+
59
+ def __init__(
60
+ self,
61
+ value: TrackioAudioSourceType,
62
+ caption: str | None = None,
63
+ sample_rate: int | None = None,
64
+ format: AudioFormatType | None = None,
65
+ ):
66
+ super().__init__(value, caption)
67
+ if isinstance(value, np.ndarray):
68
+ if sample_rate is None:
69
+ raise ValueError("Sample rate is required when value is an ndarray")
70
+ if format is None:
71
+ format = "wav"
72
+ self._format = format
73
+ self._sample_rate = sample_rate
74
+
75
+ def _save_media(self, file_path: Path):
76
+ if isinstance(self._value, np.ndarray):
77
+ TrackioAudio.write_audio(
78
+ data=self._value,
79
+ sample_rate=self._sample_rate,
80
+ filename=file_path,
81
+ format=self._format,
82
+ )
83
+ elif isinstance(self._value, str | Path):
84
+ if os.path.isfile(self._value):
85
+ shutil.copy(self._value, file_path)
86
+ else:
87
+ raise ValueError(f"File not found: {self._value}")
88
+
89
+ @staticmethod
90
+ def ensure_int16_pcm(data: np.ndarray) -> np.ndarray:
91
+ """
92
+ Convert input audio array to contiguous int16 PCM.
93
+ Peak normalization is applied to floating inputs.
94
+ """
95
+ arr = np.asarray(data)
96
+ if arr.ndim not in (1, 2):
97
+ raise ValueError("Audio data must be 1D (mono) or 2D ([samples, channels])")
98
+
99
+ if arr.dtype != np.int16:
100
+ warnings.warn(
101
+ f"Converting {arr.dtype} audio to int16 PCM; pass int16 to avoid conversion.",
102
+ stacklevel=2,
103
+ )
104
+
105
+ arr = np.nan_to_num(arr, copy=False)
106
+
107
+ # Floating types: normalize to peak 1.0, then scale to int16
108
+ if np.issubdtype(arr.dtype, np.floating):
109
+ max_abs = float(np.max(np.abs(arr))) if arr.size else 0.0
110
+ if max_abs > 0.0:
111
+ arr = arr / max_abs
112
+ out = (arr * 32767.0).clip(-32768, 32767).astype(np.int16, copy=False)
113
+ return np.ascontiguousarray(out)
114
+
115
+ converters: dict[np.dtype, callable] = {
116
+ np.dtype(np.int16): lambda a: a,
117
+ np.dtype(np.int32): lambda a: (a.astype(np.int32) // 65536).astype(
118
+ np.int16, copy=False
119
+ ),
120
+ np.dtype(np.uint16): lambda a: (a.astype(np.int32) - 32768).astype(
121
+ np.int16, copy=False
122
+ ),
123
+ np.dtype(np.uint8): lambda a: (a.astype(np.int32) * 257 - 32768).astype(
124
+ np.int16, copy=False
125
+ ),
126
+ np.dtype(np.int8): lambda a: (a.astype(np.int32) * 256).astype(
127
+ np.int16, copy=False
128
+ ),
129
+ }
130
+
131
+ conv = converters.get(arr.dtype)
132
+ if conv is not None:
133
+ out = conv(arr)
134
+ return np.ascontiguousarray(out)
135
+ raise TypeError(f"Unsupported audio dtype: {arr.dtype}")
136
+
137
+ @staticmethod
138
+ def write_audio(
139
+ data: np.ndarray,
140
+ sample_rate: int,
141
+ filename: str | Path,
142
+ format: AudioFormatType = "wav",
143
+ ) -> None:
144
+ if not isinstance(sample_rate, int) or sample_rate <= 0:
145
+ raise ValueError(f"Invalid sample_rate: {sample_rate}")
146
+ if format not in SUPPORTED_FORMATS:
147
+ raise ValueError(
148
+ f"Unsupported format: {format}. Supported: {SUPPORTED_FORMATS}"
149
+ )
150
+
151
+ check_path(filename)
152
+
153
+ pcm = TrackioAudio.ensure_int16_pcm(data)
154
+
155
+ if format != "wav":
156
+ check_ffmpeg_installed()
157
+
158
+ channels = 1 if pcm.ndim == 1 else pcm.shape[1]
159
+ audio = AudioSegment(
160
+ pcm.tobytes(),
161
+ frame_rate=sample_rate,
162
+ sample_width=2, # int16
163
+ channels=channels,
164
+ )
165
+
166
+ file = audio.export(str(filename), format=format)
167
+ file.close()
trackio/media/image.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ from pathlib import Path
4
+
5
+ import numpy as np
6
+ from PIL import Image as PILImage
7
+
8
+ from trackio.media.media import TrackioMedia
9
+
10
+ TrackioImageSourceType = str | Path | np.ndarray | PILImage.Image
11
+
12
+
13
+ class TrackioImage(TrackioMedia):
14
+ """
15
+ Initializes an Image object.
16
+
17
+ Example:
18
+ ```python
19
+ import trackio
20
+ import numpy as np
21
+ from PIL import Image
22
+
23
+ # Create an image from numpy array
24
+ image_data = np.random.randint(0, 255, (64, 64, 3), dtype=np.uint8)
25
+ image = trackio.Image(image_data, caption="Random image")
26
+ trackio.log({"my_image": image})
27
+
28
+ # Create an image from PIL Image
29
+ pil_image = Image.new('RGB', (100, 100), color='red')
30
+ image = trackio.Image(pil_image, caption="Red square")
31
+ trackio.log({"red_image": image})
32
+
33
+ # Create an image from file path
34
+ image = trackio.Image("path/to/image.jpg", caption="Photo from file")
35
+ trackio.log({"file_image": image})
36
+ ```
37
+
38
+ Args:
39
+ value (`str`, `Path`, `numpy.ndarray`, or `PIL.Image`, *optional*):
40
+ A path to an image, a PIL Image, or a numpy array of shape (height, width, channels).
41
+ If numpy array, should be of type `np.uint8` with RGB values in the range `[0, 255]`.
42
+ caption (`str`, *optional*):
43
+ A string caption for the image.
44
+ """
45
+
46
+ TYPE = "trackio.image"
47
+
48
+ def __init__(self, value: TrackioImageSourceType, caption: str | None = None):
49
+ super().__init__(value, caption)
50
+ self._format: str | None = None
51
+
52
+ if not isinstance(self._value, TrackioImageSourceType):
53
+ raise ValueError(
54
+ f"Invalid value type, expected {TrackioImageSourceType}, got {type(self._value)}"
55
+ )
56
+ if isinstance(self._value, np.ndarray) and self._value.dtype != np.uint8:
57
+ raise ValueError(
58
+ f"Invalid value dtype, expected np.uint8, got {self._value.dtype}"
59
+ )
60
+ if (
61
+ isinstance(self._value, np.ndarray | PILImage.Image)
62
+ and self._format is None
63
+ ):
64
+ self._format = "png"
65
+
66
+ def _as_pil(self) -> PILImage.Image | None:
67
+ try:
68
+ if isinstance(self._value, np.ndarray):
69
+ arr = np.asarray(self._value).astype("uint8")
70
+ return PILImage.fromarray(arr).convert("RGBA")
71
+ if isinstance(self._value, PILImage.Image):
72
+ return self._value.convert("RGBA")
73
+ except Exception as e:
74
+ raise ValueError(f"Failed to process image data: {self._value}") from e
75
+ return None
76
+
77
+ def _save_media(self, file_path: Path):
78
+ if pil := self._as_pil():
79
+ pil.save(file_path, format=self._format)
80
+ elif isinstance(self._value, str | Path):
81
+ if os.path.isfile(self._value):
82
+ shutil.copy(self._value, file_path)
83
+ else:
84
+ raise ValueError(f"File not found: {self._value}")
trackio/media/media.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uuid
3
+ from abc import ABC, abstractmethod
4
+ from pathlib import Path
5
+
6
+ from trackio.media.utils import get_project_media_path
7
+ from trackio.utils import MEDIA_DIR
8
+
9
+
10
+ class TrackioMedia(ABC):
11
+ """
12
+ Abstract base class for Trackio media objects
13
+ Provides shared functionality for file handling and serialization.
14
+ """
15
+
16
+ TYPE: str
17
+
18
+ def __init_subclass__(cls, **kwargs):
19
+ """Ensure subclasses define the TYPE attribute."""
20
+ super().__init_subclass__(**kwargs)
21
+ if not hasattr(cls, "TYPE") or cls.TYPE is None:
22
+ raise TypeError(f"Class {cls.__name__} must define TYPE attribute")
23
+
24
+ def __init__(self, value, caption: str | None = None):
25
+ """
26
+ Saves the value and caption, and if the value is a file path, checks if the file exists.
27
+ """
28
+ self.caption = caption
29
+ self._value = value
30
+ self._file_path: Path | None = None
31
+
32
+ if isinstance(self._value, str | Path):
33
+ if not os.path.isfile(self._value):
34
+ raise ValueError(f"File not found: {self._value}")
35
+
36
+ def _file_extension(self) -> str:
37
+ if self._file_path:
38
+ return self._file_path.suffix[1:].lower()
39
+ if isinstance(self._value, str | Path):
40
+ path = Path(self._value)
41
+ return path.suffix[1:].lower()
42
+ if hasattr(self, "_format") and self._format:
43
+ return self._format
44
+ return "unknown"
45
+
46
+ def _get_relative_file_path(self) -> Path | None:
47
+ return self._file_path
48
+
49
+ def _get_absolute_file_path(self) -> Path | None:
50
+ if self._file_path:
51
+ return MEDIA_DIR / self._file_path
52
+ return None
53
+
54
+ def _save(self, project: str, run: str, step: int = 0):
55
+ if self._file_path:
56
+ return
57
+
58
+ media_dir = get_project_media_path(project=project, run=run, step=step)
59
+ filename = f"{uuid.uuid4()}.{self._file_extension()}"
60
+ file_path = media_dir / filename
61
+
62
+ self._save_media(file_path)
63
+ self._file_path = file_path.relative_to(MEDIA_DIR)
64
+
65
+ @abstractmethod
66
+ def _save_media(self, file_path: Path):
67
+ """
68
+ Performs the actual media saving logic.
69
+ """
70
+ pass
71
+
72
+ def _to_dict(self) -> dict:
73
+ if not self._file_path:
74
+ raise ValueError("Media must be saved to file before serialization")
75
+ return {
76
+ "_type": self.TYPE,
77
+ "file_path": str(self._get_relative_file_path()),
78
+ "caption": self.caption,
79
+ }
trackio/media/utils.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+ from pathlib import Path
3
+
4
+ from trackio.utils import MEDIA_DIR
5
+
6
+
7
+ def check_path(file_path: str | Path) -> None:
8
+ """Raise an error if the parent directory does not exist."""
9
+ file_path = Path(file_path)
10
+ if not file_path.parent.exists():
11
+ try:
12
+ file_path.parent.mkdir(parents=True, exist_ok=True)
13
+ except OSError as e:
14
+ raise ValueError(
15
+ f"Failed to create parent directory {file_path.parent}: {e}"
16
+ )
17
+
18
+
19
+ def check_ffmpeg_installed() -> None:
20
+ """Raise an error if ffmpeg is not available on the system PATH."""
21
+ if shutil.which("ffmpeg") is None:
22
+ raise RuntimeError(
23
+ "ffmpeg is required to write video but was not found on your system. "
24
+ "Please install ffmpeg and ensure it is available on your PATH."
25
+ )
26
+
27
+
28
+ def get_project_media_path(
29
+ project: str,
30
+ run: str | None = None,
31
+ step: int | None = None,
32
+ relative_path: str | Path | None = None,
33
+ ) -> Path:
34
+ """
35
+ Get the full path where uploaded files are stored for a Trackio project (and create the directory if it doesn't exist).
36
+ If a run is not provided, the files are stored in a project-level directory with the given relative path.
37
+
38
+ Args:
39
+ project: The project name
40
+ run: The run name
41
+ step: The step number
42
+ relative_path: The relative path within the directory (only used if run is not provided)
43
+
44
+ Returns:
45
+ The full path to the media file
46
+ """
47
+ if step is not None and run is None:
48
+ raise ValueError("Uploading files at a specific step requires a run")
49
+
50
+ path = MEDIA_DIR / project
51
+ if run:
52
+ path /= run
53
+ if step is not None:
54
+ path /= str(step)
55
+ else:
56
+ path /= "files"
57
+ if relative_path:
58
+ path /= relative_path
59
+ path.mkdir(parents=True, exist_ok=True)
60
+ return path
trackio/media/video.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import subprocess
4
+ from pathlib import Path
5
+ from typing import Literal
6
+
7
+ import numpy as np
8
+
9
+ from trackio.media.media import TrackioMedia
10
+ from trackio.media.utils import check_ffmpeg_installed, check_path
11
+
12
+ TrackioVideoSourceType = str | Path | np.ndarray
13
+ TrackioVideoFormatType = Literal["gif", "mp4", "webm"]
14
+ VideoCodec = Literal["h264", "vp9", "gif"]
15
+
16
+
17
+ class TrackioVideo(TrackioMedia):
18
+ """
19
+ Initializes a Video object.
20
+
21
+ Example:
22
+ ```python
23
+ import trackio
24
+ import numpy as np
25
+
26
+ # Create a simple video from numpy array
27
+ frames = np.random.randint(0, 255, (10, 3, 64, 64), dtype=np.uint8)
28
+ video = trackio.Video(frames, caption="Random video", fps=30)
29
+
30
+ # Create a batch of videos
31
+ batch_frames = np.random.randint(0, 255, (3, 10, 3, 64, 64), dtype=np.uint8)
32
+ batch_video = trackio.Video(batch_frames, caption="Batch of videos", fps=15)
33
+
34
+ # Create video from file path
35
+ video = trackio.Video("path/to/video.mp4", caption="Video from file")
36
+ ```
37
+
38
+ Args:
39
+ value (`str`, `Path`, or `numpy.ndarray`, *optional*):
40
+ A path to a video file, or a numpy array.
41
+ If numpy array, should be of type `np.uint8` with RGB values in the range `[0, 255]`.
42
+ It is expected to have shape of either (frames, channels, height, width) or (batch, frames, channels, height, width).
43
+ For the latter, the videos will be tiled into a grid.
44
+ caption (`str`, *optional*):
45
+ A string caption for the video.
46
+ fps (`int`, *optional*):
47
+ Frames per second for the video. Only used when value is an ndarray. Default is `24`.
48
+ format (`Literal["gif", "mp4", "webm"]`, *optional*):
49
+ Video format ("gif", "mp4", or "webm"). Only used when value is an ndarray. Default is "gif".
50
+ """
51
+
52
+ TYPE = "trackio.video"
53
+
54
+ def __init__(
55
+ self,
56
+ value: TrackioVideoSourceType,
57
+ caption: str | None = None,
58
+ fps: int | None = None,
59
+ format: TrackioVideoFormatType | None = None,
60
+ ):
61
+ super().__init__(value, caption)
62
+
63
+ if not isinstance(self._value, TrackioVideoSourceType):
64
+ raise ValueError(
65
+ f"Invalid value type, expected {TrackioVideoSourceType}, got {type(self._value)}"
66
+ )
67
+ if isinstance(self._value, np.ndarray):
68
+ if self._value.dtype != np.uint8:
69
+ raise ValueError(
70
+ f"Invalid value dtype, expected np.uint8, got {self._value.dtype}"
71
+ )
72
+ if format is None:
73
+ format = "gif"
74
+ if fps is None:
75
+ fps = 24
76
+ self._fps = fps
77
+ self._format = format
78
+
79
+ @staticmethod
80
+ def _check_array_format(video: np.ndarray) -> None:
81
+ """Raise an error if the array is not in the expected format."""
82
+ if not (video.ndim == 4 and video.shape[-1] == 3):
83
+ raise ValueError(
84
+ f"Expected RGB input shaped (F, H, W, 3), got {video.shape}. "
85
+ f"Input has {video.ndim} dimensions, expected 4."
86
+ )
87
+ if video.dtype != np.uint8:
88
+ raise TypeError(
89
+ f"Expected dtype=uint8, got {video.dtype}. "
90
+ "Please convert your video data to uint8 format."
91
+ )
92
+
93
+ @staticmethod
94
+ def write_video(
95
+ file_path: str | Path, video: np.ndarray, fps: float, codec: VideoCodec
96
+ ) -> None:
97
+ """RGB uint8 only, shape (F, H, W, 3)."""
98
+ check_ffmpeg_installed()
99
+ check_path(file_path)
100
+
101
+ if codec not in {"h264", "vp9", "gif"}:
102
+ raise ValueError("Unsupported codec. Use h264, vp9, or gif.")
103
+
104
+ arr = np.asarray(video)
105
+ TrackioVideo._check_array_format(arr)
106
+
107
+ frames = np.ascontiguousarray(arr)
108
+ _, height, width, _ = frames.shape
109
+ out_path = str(file_path)
110
+
111
+ cmd = [
112
+ "ffmpeg",
113
+ "-y",
114
+ "-f",
115
+ "rawvideo",
116
+ "-s",
117
+ f"{width}x{height}",
118
+ "-pix_fmt",
119
+ "rgb24",
120
+ "-r",
121
+ str(fps),
122
+ "-i",
123
+ "-",
124
+ "-an",
125
+ ]
126
+
127
+ if codec == "gif":
128
+ video_filter = "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse"
129
+ cmd += [
130
+ "-vf",
131
+ video_filter,
132
+ "-loop",
133
+ "0",
134
+ ]
135
+ elif codec == "h264":
136
+ cmd += [
137
+ "-vcodec",
138
+ "libx264",
139
+ "-pix_fmt",
140
+ "yuv420p",
141
+ "-movflags",
142
+ "+faststart",
143
+ ]
144
+ elif codec == "vp9":
145
+ bpp = 0.08
146
+ bps = int(width * height * fps * bpp)
147
+ if bps >= 1_000_000:
148
+ bitrate = f"{round(bps / 1_000_000)}M"
149
+ elif bps >= 1_000:
150
+ bitrate = f"{round(bps / 1_000)}k"
151
+ else:
152
+ bitrate = str(max(bps, 1))
153
+ cmd += [
154
+ "-vcodec",
155
+ "libvpx-vp9",
156
+ "-b:v",
157
+ bitrate,
158
+ "-pix_fmt",
159
+ "yuv420p",
160
+ ]
161
+ cmd += [out_path]
162
+ proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
163
+ try:
164
+ for frame in frames:
165
+ proc.stdin.write(frame.tobytes())
166
+ finally:
167
+ if proc.stdin:
168
+ proc.stdin.close()
169
+ stderr = (
170
+ proc.stderr.read().decode("utf-8", errors="ignore")
171
+ if proc.stderr
172
+ else ""
173
+ )
174
+ ret = proc.wait()
175
+ if ret != 0:
176
+ raise RuntimeError(f"ffmpeg failed with code {ret}\n{stderr}")
177
+
178
+ @property
179
+ def _codec(self) -> str:
180
+ match self._format:
181
+ case "gif":
182
+ return "gif"
183
+ case "mp4":
184
+ return "h264"
185
+ case "webm":
186
+ return "vp9"
187
+ case _:
188
+ raise ValueError(f"Unsupported format: {self._format}")
189
+
190
+ def _save_media(self, file_path: Path):
191
+ if isinstance(self._value, np.ndarray):
192
+ video = TrackioVideo._process_ndarray(self._value)
193
+ TrackioVideo.write_video(file_path, video, fps=self._fps, codec=self._codec)
194
+ elif isinstance(self._value, str | Path):
195
+ if os.path.isfile(self._value):
196
+ shutil.copy(self._value, file_path)
197
+ else:
198
+ raise ValueError(f"File not found: {self._value}")
199
+
200
+ @staticmethod
201
+ def _process_ndarray(value: np.ndarray) -> np.ndarray:
202
+ # Verify value is either 4D (single video) or 5D array (batched videos).
203
+ # Expected format: (frames, channels, height, width) or (batch, frames, channels, height, width)
204
+ if value.ndim < 4:
205
+ raise ValueError(
206
+ "Video requires at least 4 dimensions (frames, channels, height, width)"
207
+ )
208
+ if value.ndim > 5:
209
+ raise ValueError(
210
+ "Videos can have at most 5 dimensions (batch, frames, channels, height, width)"
211
+ )
212
+ if value.ndim == 4:
213
+ # Reshape to 5D with single batch: (1, frames, channels, height, width)
214
+ value = value[np.newaxis, ...]
215
+
216
+ value = TrackioVideo._tile_batched_videos(value)
217
+ return value
218
+
219
+ @staticmethod
220
+ def _tile_batched_videos(video: np.ndarray) -> np.ndarray:
221
+ """
222
+ Tiles a batch of videos into a grid of videos.
223
+
224
+ Input format: (batch, frames, channels, height, width) - original FCHW format
225
+ Output format: (frames, total_height, total_width, channels)
226
+ """
227
+ batch_size, frames, channels, height, width = video.shape
228
+
229
+ next_pow2 = 1 << (batch_size - 1).bit_length()
230
+ if batch_size != next_pow2:
231
+ pad_len = next_pow2 - batch_size
232
+ pad_shape = (pad_len, frames, channels, height, width)
233
+ padding = np.zeros(pad_shape, dtype=video.dtype)
234
+ video = np.concatenate((video, padding), axis=0)
235
+ batch_size = next_pow2
236
+
237
+ n_rows = 1 << ((batch_size.bit_length() - 1) // 2)
238
+ n_cols = batch_size // n_rows
239
+
240
+ # Reshape to grid layout: (n_rows, n_cols, frames, channels, height, width)
241
+ video = video.reshape(n_rows, n_cols, frames, channels, height, width)
242
+
243
+ # Rearrange dimensions to (frames, total_height, total_width, channels)
244
+ video = video.transpose(2, 0, 4, 1, 5, 3)
245
+ video = video.reshape(frames, n_rows * height, n_cols * width, channels)
246
+ return video
trackio/package.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "name": "trackio",
3
+ "version": "0.23.0",
4
+ "description": "",
5
+ "python": "true"
6
+ }
trackio/py.typed ADDED
File without changes
trackio/remote_client.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import Any
5
+ from urllib.parse import urljoin, urlparse
6
+
7
+ import httpx
8
+ from gradio_client import Client as GradioClient
9
+ from huggingface_hub.utils import build_hf_headers
10
+
11
+ from trackio.utils import parse_trackio_server_url
12
+
13
+ HTTP_API_VERSION = 1
14
+
15
+ WRITE_TOKEN_HEADER = "x-trackio-write-token"
16
+
17
+
18
+ def _normalize_src(src: str) -> str:
19
+ return src if src.endswith("/") else src + "/"
20
+
21
+
22
+ def _space_id_to_url(space_id: str) -> str:
23
+ namespace, name = space_id.split("/", 1)
24
+ subdomain = f"{namespace}-{name}".lower().replace("_", "-").replace(".", "-")
25
+ return f"https://{subdomain}.hf.space/"
26
+
27
+
28
+ def _host_is_hf_space(url: str) -> bool:
29
+ p = urlparse(url)
30
+ h = (p.hostname or "").lower()
31
+ return h.endswith(".hf.space")
32
+
33
+
34
+ def _resolve_src_url(src: str) -> str:
35
+ if src.startswith(("http://", "https://")):
36
+ base, _ = parse_trackio_server_url(src)
37
+ return _normalize_src(base)
38
+ if "/" in src:
39
+ return _space_id_to_url(src)
40
+ raise ValueError(
41
+ f"Could not resolve Trackio remote source '{src}'. "
42
+ "Pass a full Space id like 'user/space' or a URL."
43
+ )
44
+
45
+
46
+ def _is_local_file_data(value: Any) -> bool:
47
+ return (
48
+ isinstance(value, dict)
49
+ and "path" in value
50
+ and isinstance(value["path"], str)
51
+ and value.get("meta", {}).get("_type") == "gradio.FileData"
52
+ and Path(value["path"]).exists()
53
+ )
54
+
55
+
56
+ def _merge_client_headers(
57
+ hf_token: str | None, write_token: str | None
58
+ ) -> dict[str, str]:
59
+ headers: dict[str, str] = {}
60
+ if hf_token:
61
+ headers.update(build_hf_headers(token=hf_token))
62
+ if write_token:
63
+ headers[WRITE_TOKEN_HEADER] = write_token
64
+ return headers
65
+
66
+
67
+ class _TrackioHTTPClient:
68
+ def __init__(
69
+ self,
70
+ src: str,
71
+ hf_token: str | None = None,
72
+ write_token: str | None = None,
73
+ httpx_kwargs: dict[str, Any] | None = None,
74
+ ) -> None:
75
+ self.src = _resolve_src_url(src)
76
+ self.httpx_kwargs = dict(httpx_kwargs or {})
77
+ self.httpx_kwargs.setdefault("timeout", 60)
78
+ extra = self.httpx_kwargs.pop("headers", None)
79
+ h = _merge_client_headers(hf_token, write_token)
80
+ if isinstance(extra, dict):
81
+ h.update({str(k): str(v) for k, v in extra.items()})
82
+ self.headers = h
83
+
84
+ def _upload_file(self, file_data: dict[str, Any]) -> dict[str, Any]:
85
+ path = Path(file_data["path"])
86
+ with path.open("rb") as f:
87
+ resp = httpx.post(
88
+ urljoin(self.src, "api/upload"),
89
+ headers=self.headers,
90
+ files={"files": (path.name, f)},
91
+ **self.httpx_kwargs,
92
+ )
93
+ resp.raise_for_status()
94
+ uploaded_path = resp.json()["paths"][0]
95
+ return {
96
+ **file_data,
97
+ "path": uploaded_path,
98
+ "orig_name": file_data.get("orig_name", path.name),
99
+ }
100
+
101
+ def _prepare_value(self, value: Any) -> Any:
102
+ if _is_local_file_data(value):
103
+ return self._upload_file(value)
104
+ if isinstance(value, list):
105
+ return [self._prepare_value(item) for item in value]
106
+ if isinstance(value, tuple):
107
+ return [self._prepare_value(item) for item in value]
108
+ if isinstance(value, dict):
109
+ return {k: self._prepare_value(v) for k, v in value.items()}
110
+ return value
111
+
112
+ def predict(self, *args, api_name: str, **kwargs) -> Any:
113
+ api_name = api_name.lstrip("/")
114
+ payload = {
115
+ "args": self._prepare_value(list(args)),
116
+ "kwargs": self._prepare_value(kwargs),
117
+ }
118
+ resp = httpx.post(
119
+ urljoin(self.src, f"api/{api_name}"),
120
+ headers=self.headers,
121
+ json=payload,
122
+ **self.httpx_kwargs,
123
+ )
124
+ if resp.status_code == 404:
125
+ raise RuntimeError(
126
+ f"Space '{self.src}' does not support '/{api_name}'. Redeploy with `trackio sync`."
127
+ )
128
+ resp.raise_for_status()
129
+ body = resp.json()
130
+ if body.get("error") is not None:
131
+ raise RuntimeError(body["error"])
132
+ return body.get("data")
133
+
134
+
135
+ class _TrackioGradioCompatClient:
136
+ def __init__(
137
+ self,
138
+ src: str,
139
+ hf_token: str | None = None,
140
+ write_token: str | None = None,
141
+ httpx_kwargs: dict[str, Any] | None = None,
142
+ verbose: bool = False,
143
+ ) -> None:
144
+ kwargs: dict[str, Any] = {"verbose": verbose}
145
+ if hf_token:
146
+ kwargs["hf_token"] = hf_token
147
+ merged = dict(httpx_kwargs or {})
148
+ h = _merge_client_headers(
149
+ hf_token if hf_token else None,
150
+ write_token,
151
+ )
152
+ extra = merged.pop("headers", None)
153
+ if isinstance(extra, dict):
154
+ h.update({str(k): str(v) for k, v in extra.items()})
155
+ if h:
156
+ merged["headers"] = h
157
+ if merged:
158
+ kwargs["httpx_kwargs"] = merged
159
+ self._client = GradioClient(src, **kwargs)
160
+
161
+ def predict(self, *args, api_name: str, **kwargs) -> Any:
162
+ try:
163
+ return self._client.predict(*args, api_name=api_name, **kwargs)
164
+ except Exception as e:
165
+ if "API Not Found" in str(e) or "api_name" in str(e):
166
+ raise RuntimeError(
167
+ f"Space '{self._client.src}' does not support '{api_name}'. "
168
+ "Redeploy with `trackio sync`."
169
+ ) from e
170
+ raise
171
+
172
+
173
+ def _supports_http_api(
174
+ src: str,
175
+ hf_token: str | None = None,
176
+ write_token: str | None = None,
177
+ httpx_kwargs: dict[str, Any] | None = None,
178
+ ) -> bool:
179
+ url = _resolve_src_url(src)
180
+ headers = _merge_client_headers(hf_token, write_token)
181
+ kwargs = dict(httpx_kwargs or {})
182
+ kwargs.setdefault("timeout", 10)
183
+ try:
184
+ resp = httpx.get(urljoin(url, "version"), headers=headers, **kwargs)
185
+ if not resp.is_success:
186
+ return False
187
+ data = resp.json()
188
+ return data.get("api_version") == HTTP_API_VERSION
189
+ except Exception:
190
+ return False
191
+
192
+
193
+ class RemoteClient:
194
+ def __init__(
195
+ self,
196
+ space: str,
197
+ hf_token: str | None = None,
198
+ write_token: str | None = None,
199
+ httpx_kwargs: dict[str, Any] | None = None,
200
+ verbose: bool = False,
201
+ ) -> None:
202
+ self._space = space
203
+ src_for_resolve = space
204
+ hf_effective = hf_token
205
+ wt_effective = write_token
206
+ if space.startswith(("http://", "https://")):
207
+ base, url_tok = parse_trackio_server_url(space)
208
+ src_for_resolve = base
209
+ if wt_effective is None:
210
+ wt_effective = url_tok
211
+ if not _host_is_hf_space(_normalize_src(base)):
212
+ hf_effective = None
213
+ try:
214
+ if _supports_http_api(
215
+ src_for_resolve,
216
+ hf_token=hf_effective,
217
+ write_token=wt_effective,
218
+ httpx_kwargs=httpx_kwargs,
219
+ ):
220
+ self._client = _TrackioHTTPClient(
221
+ src_for_resolve,
222
+ hf_token=hf_effective,
223
+ write_token=wt_effective,
224
+ httpx_kwargs=httpx_kwargs,
225
+ )
226
+ else:
227
+ self._client = _TrackioGradioCompatClient(
228
+ src_for_resolve,
229
+ hf_token=hf_effective,
230
+ write_token=wt_effective,
231
+ httpx_kwargs=httpx_kwargs,
232
+ verbose=verbose,
233
+ )
234
+ except ValueError:
235
+ raise
236
+ except Exception as e:
237
+ raise ConnectionError(
238
+ f"Could not connect to Space '{space}'. Is it running?\n{e}"
239
+ ) from e
240
+
241
+ def predict(self, *args, api_name: str, **kwargs) -> Any:
242
+ return self._client.predict(*args, api_name=api_name, **kwargs)