Spaces:
Running
Running
File size: 9,458 Bytes
5fe83da 769bb84 5fe83da 769bb84 5fe83da 769bb84 5fe83da 93ed7a1 5fe83da 93ed7a1 5fe83da 93ed7a1 5fe83da 93ed7a1 5fe83da 93ed7a1 ebe598e 5fe83da 93ed7a1 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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
#!/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"
]
# Try to create the space first
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
# Try alternative approach without space-specific flags
print("Retrying with basic space creation...")
cmd = [
"huggingface-cli", "repo", "create",
f"{self.username}/{self.space_name}"
]
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...")
# Get the project root directory (3 levels up from this script)
project_root = Path(__file__).parent.parent.parent
templates_dir = project_root / "templates" / "spaces"
# Files to upload from templates/spaces
files_to_upload = [
"app.py",
"requirements.txt"
]
# README.md will be created by configure_space method
# Copy files from templates/spaces to current directory
copied_files = []
for file_name in files_to_upload:
source_path = templates_dir / file_name
if source_path.exists():
import shutil
shutil.copy2(source_path, file_name)
copied_files.append(file_name)
print(f"β
Copied {file_name} from templates")
else:
print(f"β οΈ File not found: {source_path}")
# Check if we're in a git repository
try:
subprocess.run(["git", "status"], capture_output=True, check=True)
except subprocess.CalledProcessError:
print("β οΈ Not in a git repository, initializing...")
subprocess.run(["git", "init"], check=True)
subprocess.run(["git", "remote", "add", "origin", f"https://huggingface.co/spaces/{self.username}/{self.space_name}"], check=True)
# Add all files at once
existing_files = [f for f in files_to_upload if os.path.exists(f)]
if existing_files:
subprocess.run(["git", "add"] + existing_files, check=True)
subprocess.run(["git", "add", "README.md"], check=True) # Add README.md that was created in configure_space
subprocess.run(["git", "commit", "-m", "Initial Space setup"], check=True)
# Push to the space
try:
subprocess.run(["git", "push", "origin", "main"], check=True)
print(f"β
Uploaded {len(existing_files)} files")
except subprocess.CalledProcessError:
# Try pushing to master branch if main doesn't exist
subprocess.run(["git", "push", "origin", "master"], check=True)
print(f"β
Uploaded {len(existing_files)} files")
else:
print("β οΈ No files found to upload")
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...")
# Get the project root directory (3 levels up from this script)
project_root = Path(__file__).parent.parent.parent
templates_dir = project_root / "templates" / "spaces"
readme_template_path = templates_dir / "README.md"
# Read README template if it exists
if readme_template_path.exists():
with open(readme_template_path, 'r', encoding='utf-8') as f:
readme_template = f.read()
# Replace placeholder with actual space URL
readme_content = readme_template.replace("{SPACE_URL}", self.space_url)
# Write README.md for the space
with open("README.md", "w", encoding='utf-8') as f:
f.write(readme_content)
print(f"β
Created README.md from template")
else:
print(f"β οΈ README template not found: {readme_template_path}")
# Fallback to basic README
basic_readme = f"""---
title: Trackio Tonic
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.
Visit: {self.space_url}
"""
with open("README.md", "w", encoding='utf-8') as f:
f.write(basic_readme)
print(f"β
Created basic README.md")
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() |