|
| 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}") |
0 commit comments