Skip to content

Commit e2d53e4

Browse files
committed
changes
1 parent 2c71da8 commit e2d53e4

3 files changed

Lines changed: 82 additions & 3 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
Example: deploy a Gradio Space during training, then convert it to a static Space once done.
3+
4+
This demonstrates the Gradio -> Static conversion flow:
5+
1. Start training with a live Gradio dashboard (real-time updates)
6+
2. After training finishes, convert the Space to static (no server needed, cheaper)
7+
8+
Usage:
9+
python examples/convert-gradio-to-static.py
10+
"""
11+
12+
import math
13+
import random
14+
import time
15+
16+
import trackio
17+
18+
PROJECT = f"gradio-to-static-{random.randint(100000, 999999)}"
19+
SPACE_ID = f"convert-demo-{random.randint(100000, 999999)}"
20+
EPOCHS = 10
21+
22+
for run in range(2):
23+
trackio.init(
24+
project=PROJECT,
25+
name=f"run-{run}",
26+
config={"epochs": EPOCHS, "lr": 0.001 * (run + 1), "batch_size": 32},
27+
space_id=SPACE_ID,
28+
auto_log_gpu=False,
29+
)
30+
31+
for epoch in range(EPOCHS):
32+
progress = epoch / EPOCHS
33+
loss = 2.0 * math.exp(-3 * progress) + 0.1 + random.gauss(0, 0.05)
34+
acc = 0.95 / (1 + math.exp(-6 * (progress - 0.5))) + random.gauss(0, 0.02)
35+
36+
trackio.log(
37+
{
38+
"train/loss": round(max(0.01, loss), 4),
39+
"train/accuracy": round(min(0.99, max(0, acc)), 4),
40+
},
41+
step=epoch,
42+
)
43+
44+
trackio.log_system(
45+
{
46+
"cpu_percent": round(40 + epoch * 3 + random.uniform(-2, 2), 1),
47+
"memory_gb": round(4.0 + epoch * 0.1 + random.uniform(-0.05, 0.05), 2),
48+
}
49+
)
50+
51+
time.sleep(0.3)
52+
53+
trackio.finish()
54+
55+
print("\nTraining complete. Converting Gradio Space to static...")
56+
space_id = trackio.sync(project=PROJECT, space_id=SPACE_ID, sdk="static")
57+
print(f"Static dashboard: https://huggingface.co/spaces/{space_id}")

trackio/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,6 @@ def init(
259259
space_url = deploy.SPACE_HOST_URL.format(
260260
user_name=user_name, space_name=space_name
261261
)
262-
print(
263-
f"* View dashboard by going to: {deploy._BOLD_ORANGE}{space_url}{deploy._RESET}"
264-
)
265262
if utils.is_in_notebook() and embed:
266263
utils.embed_url_in_notebook(space_url)
267264
context_vars.current_project.set(project)

trackio/bucket_storage.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,32 @@ def upload_project_to_bucket(project: str, bucket_id: str) -> None:
4343
huggingface_hub.batch_bucket_files(bucket_id, add=files_to_add)
4444

4545

46+
def _download_db_from_bucket(project: str, bucket_id: str) -> bool:
47+
db_filename = SQLiteStorage.get_project_db_filename(project)
48+
remote_path = f"trackio/{db_filename}"
49+
local_path = SQLiteStorage.get_project_db_path(project)
50+
local_path.parent.mkdir(parents=True, exist_ok=True)
51+
try:
52+
huggingface_hub.download_bucket_files(
53+
bucket_id,
54+
files=[(remote_path, str(local_path))],
55+
)
56+
return local_path.exists()
57+
except Exception:
58+
return False
59+
60+
4661
def upload_project_to_bucket_for_static(project: str, bucket_id: str) -> None:
62+
db_path = SQLiteStorage.get_project_db_path(project)
63+
local_db_empty = not db_path.exists() or db_path.stat().st_size == 0
64+
if not local_db_empty:
65+
with sqlite3.connect(str(db_path)) as conn:
66+
count = conn.execute("SELECT COUNT(*) FROM metrics").fetchone()[0]
67+
local_db_empty = count == 0
68+
69+
if local_db_empty:
70+
_download_db_from_bucket(project, bucket_id)
71+
4772
with tempfile.TemporaryDirectory() as tmp_dir:
4873
output_dir = Path(tmp_dir)
4974
SQLiteStorage.export_for_static_space(project, output_dir)

0 commit comments

Comments
 (0)