File size: 2,556 Bytes
43ea24d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Pre-deploy validation for HF Space experiments."""

import os
import sys
import re

def validate_space(path):
    """Validate a Space before deployment."""
    errors = []
    warnings = []
    
    # Check README.md with YAML frontmatter
    readme_path = os.path.join(path, "README.md")
    if os.path.exists(readme_path):
        with open(readme_path) as f:
            content = f.read()
        if not content.startswith("---"):
            errors.append("README.md missing YAML frontmatter")
    else:
        errors.append("README.md not found")
    
    # Check requirements.txt
    req_path = os.path.join(path, "requirements.txt")
    if os.path.exists(req_path):
        with open(req_path) as f:
            reqs = f.read()
        if "gradio==4.36.0" not in reqs:
            errors.append("requirements.txt must pin gradio==4.36.0")
        if "huggingface_hub==0.25.2" not in reqs:
            warnings.append("Consider pinning huggingface_hub==0.25.2")
    else:
        errors.append("requirements.txt not found")
    
    # Check app.py for lazy loading
    app_path = os.path.join(path, "app.py")
    if os.path.exists(app_path):
        with open(app_path) as f:
            app_content = f.read()
        
        # Check for lazy loading pattern
        if "_inference_client = None" not in app_content and "lazy" not in app_content.lower():
            warnings.append("Consider implementing lazy loading for models")
        
        # Check for hardcoded tokens
        if re.search(r'hf_[a-zA-Z0-9]{34}', app_content):
            errors.append("Hardcoded HF token detected in app.py")
        
        # Check for torch imports at module level
        if re.search(r'^import torch', app_content, re.MULTILINE):
            warnings.append("torch imported at module level - may slow startup")
    else:
        errors.append("app.py not found")
    
    # Summary
    print(f"Validation for {path}:")
    if errors:
        print(f"  ❌ ERRORS ({len(errors)}):")
        for e in errors:
            print(f"    - {e}")
    if warnings:
        print(f"  ⚠️  WARNINGS ({len(warnings)}):")
        for w in warnings:
            print(f"    - {w}")
    if not errors and not warnings:
        print("  ✅ All checks passed")
    
    return len(errors) == 0

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python experiment_deploy_validator.py <path>")
        sys.exit(1)
    
    path = sys.argv[1]
    success = validate_space(path)
    sys.exit(0 if success else 1)