SmolFactory / docs /LATEST_DEPLOYMENT_APPROACH.md
Tonic's picture
adds sft , quantization, better readmes
40fd629 verified
|
raw
history blame
6.84 kB

Latest Trackio Space Deployment Approach

Overview

Based on the Hugging Face Hub repository code, I've updated the Trackio Space deployment to use the latest Hugging Face Hub Python API instead of CLI commands.

Key Improvements

1. Latest HF Hub API Integration

Before: Using CLI commands

cmd = ["hf", "repo", "create", f"{username}/{space_name}", "--type", "space"]

After: Using Python API

from huggingface_hub import create_repo

create_repo(
    repo_id=f"{username}/{space_name}",
    token=token,
    repo_type="space",
    exist_ok=True,
    private=False,
    space_sdk="gradio",
    space_hardware="cpu-basic"
)

2. Robust Fallback Mechanism

The deployment script now includes both API and CLI approaches:

def create_space(self) -> bool:
    """Create a new Hugging Face Space using the latest API"""
    try:
        if not HF_HUB_AVAILABLE:
            return self._create_space_cli()
        
        # Use latest API
        create_repo(...)
        
    except Exception as api_error:
        # Fallback to CLI
        return self._create_space_cli()

3. Enhanced Dependencies

Updated requirements/requirements_core.txt:

# Hugging Face Hub for model and space management
huggingface_hub>=0.19.0

API Parameters

Required Parameters

  • repo_id: Repository identifier (username/space-name)
  • token: Hugging Face token with write permissions

Optional Parameters

  • repo_type: Set to "space" for Spaces
  • exist_ok: Allow existing repositories (default: True)
  • private: Make repository private (default: False)
  • space_sdk: SDK type (default: "gradio")
  • space_hardware: Hardware specification (default: "cpu-basic")

Deployment Process

Step 1: API Creation

# Create space using latest API
create_repo(
    repo_id=f"{username}/{space_name}",
    token=token,
    repo_type="space",
    exist_ok=True,
    private=False,
    space_sdk="gradio",
    space_hardware="cpu-basic"
)

Step 2: File Preparation

# Prepare files in temporary directory
temp_dir = tempfile.mkdtemp()
# Copy template files
shutil.copy2(source_path, dest_path)
# Update README with actual space URL
readme_content.replace("{SPACE_URL}", self.space_url)

Step 3: Git Upload

# Initialize git in temp directory
os.chdir(temp_dir)
subprocess.run(["git", "init"], check=True)
subprocess.run(["git", "remote", "add", "origin", space_url], check=True)
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", "Initial Trackio Space setup"], check=True)
subprocess.run(["git", "push", "origin", "main"], check=True)

Testing the Latest Deployment

Run Latest Deployment Tests

python tests/test_latest_deployment.py

Expected output: ``` πŸš€ Testing Latest Trackio Space Deployment

πŸ” Testing huggingface_hub import... βœ… huggingface_hub imported successfully

πŸ” Testing deployment script import... βœ… TrackioSpaceDeployer class imported successfully βœ… HF API initialized

πŸ” Testing API methods... βœ… Method exists: create_space βœ… Method exists: _create_space_cli βœ… Method exists: prepare_space_files βœ… Method exists: upload_files_to_space βœ… Method exists: test_space βœ… Method exists: deploy

πŸ” Testing create_repo API... βœ… Required parameter: repo_id βœ… Required parameter: token βœ… Optional parameter: repo_type βœ… Optional parameter: space_sdk βœ… Optional parameter: space_hardware βœ… create_repo API signature looks correct

πŸ” Testing space creation logic... βœ… Space URL formatted correctly βœ… Repo ID formatted correctly

πŸ” Testing template files... βœ… app.py exists βœ… requirements.txt exists βœ… README.md exists

πŸ” Testing temporary directory handling... βœ… Created temp directory: /tmp/tmp_xxxxx βœ… File copying works βœ… Cleanup successful

πŸ“Š Test Results: 7/7 tests passed βœ… All deployment tests passed! The latest deployment should work correctly.


## Files Updated

### **Core Deployment Files**
1. **`scripts/trackio_tonic/deploy_trackio_space.py`**
   - Added HF Hub API integration
   - Implemented fallback mechanism
   - Enhanced error handling
   - Better logging and debugging

### **Dependencies**
2. **`requirements/requirements_core.txt`**
   - Updated huggingface_hub to >=0.19.0
   - Organized dependencies by category
   - Added missing dependencies

### **Testing**
3. **`tests/test_latest_deployment.py`**
   - Comprehensive API testing
   - Import validation
   - Method verification
   - Template file checking

## Benefits of Latest Approach

### **1. Better Error Handling**
- API-first approach with CLI fallback
- Detailed error messages
- Graceful degradation

### **2. More Reliable**
- Uses official HF Hub API
- Better parameter validation
- Consistent behavior

### **3. Future-Proof**
- Follows latest HF Hub patterns
- Easy to update with new API features
- Maintains backward compatibility

### **4. Enhanced Logging**
- Detailed progress reporting
- Better debugging information
- Clear success/failure indicators

## Usage Instructions

### **1. Install Latest Dependencies**
```bash
pip install huggingface_hub>=0.19.0

2. Test the Deployment

python tests/test_latest_deployment.py

3. Deploy Trackio Space

python scripts/trackio_tonic/deploy_trackio_space.py

4. Verify Deployment

  • Check the Space URL
  • Test the interface
  • Verify API endpoints

Troubleshooting

Common Issues

1. Import Errors

❌ Failed to import huggingface_hub

Solution: Install latest version

pip install huggingface_hub>=0.19.0

2. API Errors

API creation failed: 401 Client Error

Solution: Check token permissions and validity

3. Git Push Errors

❌ Error uploading files: git push failed

Solution: Verify git configuration and token access

Fallback Behavior

The deployment script automatically falls back to CLI if:

  • huggingface_hub is not available
  • API creation fails
  • Network issues occur

Reference Implementation

Based on the Hugging Face Hub repository, this implementation:

  1. Uses the latest API patterns
  2. Follows HF Hub best practices
  3. Maintains backward compatibility
  4. Provides robust error handling

The Trackio Space deployment should now work reliably with the latest Hugging Face Hub infrastructure! πŸš€