Spaces:
Running
Running
File size: 7,227 Bytes
5fe83da |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
#!/usr/bin/env python3
"""
Deployment script for Trackio on Hugging Face Spaces
Automates the process of creating and configuring a Trackio Space
"""
import os
import json
import requests
import subprocess
import sys
from pathlib import Path
from typing import Dict, Any, Optional
class TrackioSpaceDeployer:
"""Deployer for Trackio on Hugging Face Spaces"""
def __init__(self, space_name: str, username: str, token: str):
self.space_name = space_name
self.username = username
self.token = token
self.space_url = f"https://huggingface.co/spaces/{username}/{space_name}"
def create_space(self) -> bool:
"""Create a new Hugging Face Space"""
try:
print(f"Creating Space: {self.space_name}")
# Create space using Hugging Face CLI
cmd = [
"huggingface-cli", "repo", "create",
f"{self.username}/{self.space_name}",
"--type", "space",
"--space-sdk", "gradio",
"--space-hardware", "cpu-basic"
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
print(f"β
Space created successfully: {self.space_url}")
return True
else:
print(f"β Failed to create space: {result.stderr}")
return False
except Exception as e:
print(f"β Error creating space: {e}")
return False
def upload_files(self) -> bool:
"""Upload necessary files to the Space"""
try:
print("Uploading files to Space...")
# Files to upload
files_to_upload = [
"app.py",
"requirements_space.txt",
"README.md"
]
for file_path in files_to_upload:
if os.path.exists(file_path):
# Use git to add and push files
subprocess.run(["git", "add", file_path], check=True)
subprocess.run(["git", "commit", "-m", f"Add {file_path}"], check=True)
subprocess.run(["git", "push"], check=True)
print(f"β
Uploaded {file_path}")
else:
print(f"β οΈ File not found: {file_path}")
return True
except Exception as e:
print(f"β Error uploading files: {e}")
return False
def configure_space(self) -> bool:
"""Configure the Space settings"""
try:
print("Configuring Space settings...")
# Create space configuration
space_config = {
"title": "Trackio - Experiment Tracking",
"emoji": "π",
"colorFrom": "blue",
"colorTo": "purple",
"sdk": "gradio",
"sdk_version": "4.0.0",
"app_file": "app.py",
"pinned": False
}
# Write README.md for the space
space_readme = f"""---
title: Trackio for Petite Elle L'Aime
emoji: π
colorFrom: indigo
colorTo: yellow
sdk: gradio
sdk_version: 5.38.0
app_file: app.py
pinned: true
license: mit
short_description: trackio for training monitoring
---
# Trackio Experiment Tracking
A Gradio interface for experiment tracking and monitoring.
## Features
- Create and manage experiments
- Log training metrics and parameters
- View experiment details and results
- Update experiment status
## Usage
1. Create a new experiment using the "Create Experiment" tab
2. Log metrics during training using the "Log Metrics" tab
3. View experiment details using the "View Experiments" tab
4. Update experiment status using the "Update Status" tab
## Integration
To connect your training script to this Trackio Space:
```python
from monitoring import SmolLM3Monitor
monitor = SmolLM3Monitor(
experiment_name="my_experiment",
trackio_url="{self.space_url}",
enable_tracking=True
)
```
Visit: {self.space_url}
"""
with open("README.md", "w") as f:
f.write(space_readme)
return True
except Exception as e:
print(f"β Error configuring space: {e}")
return False
def test_space(self) -> bool:
"""Test if the Space is working correctly"""
try:
print("Testing Space...")
# Wait a bit for the space to build
import time
time.sleep(30)
# Try to access the space
response = requests.get(self.space_url, timeout=10)
if response.status_code == 200:
print(f"β
Space is accessible: {self.space_url}")
return True
else:
print(f"β οΈ Space returned status code: {response.status_code}")
return False
except Exception as e:
print(f"β Error testing space: {e}")
return False
def deploy(self) -> bool:
"""Complete deployment process"""
print("π Starting Trackio Space deployment...")
# Step 1: Create space
if not self.create_space():
return False
# Step 2: Configure space
if not self.configure_space():
return False
# Step 3: Upload files
if not self.upload_files():
return False
# Step 4: Test space
if not self.test_space():
print("β οΈ Space created but may need time to build")
print(f"π Deployment completed!")
print(f"π Trackio Space URL: {self.space_url}")
print(f"π§ Space configuration: {self.space_url}/settings")
return True
def main():
"""Main deployment function"""
print("Trackio Space Deployment Script")
print("=" * 40)
# Get user input
username = input("Enter your Hugging Face username: ").strip()
space_name = input("Enter Space name (e.g., trackio-monitoring): ").strip()
token = input("Enter your Hugging Face token (optional): ").strip()
if not username or not space_name:
print("β Username and Space name are required")
sys.exit(1)
# Create deployer
deployer = TrackioSpaceDeployer(space_name, username, token)
# Run deployment
success = deployer.deploy()
if success:
print("\nβ
Deployment successful!")
print(f"π Your Trackio Space: {deployer.space_url}")
print("\nNext steps:")
print("1. Wait for the Space to build (usually 2-5 minutes)")
print("2. Test the interface by visiting the Space URL")
print("3. Use the Space URL in your training scripts")
else:
print("\nβ Deployment failed!")
print("Check the error messages above and try again.")
if __name__ == "__main__":
main() |