#!/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 ") sys.exit(1) path = sys.argv[1] success = validate_space(path) sys.exit(0 if success else 1)