Spaces:
Paused
Paused
| #!/usr/bin/env python3 | |
| """ | |
| Pre-deploy validation for HF Spaces | |
| """ | |
| import os | |
| import re | |
| import sys | |
| def validate_space(exp_dir): | |
| """Validate Space before deployment""" | |
| errors = [] | |
| warnings = [] | |
| # Check README.md has YAML frontmatter | |
| readme_path = os.path.join(exp_dir, "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(exp_dir, "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 missing pinned gradio==4.36.0") | |
| else: | |
| errors.append("requirements.txt not found") | |
| # Check app.py | |
| app_path = os.path.join(exp_dir, "app.py") | |
| if os.path.exists(app_path): | |
| with open(app_path) as f: | |
| app_code = f.read() | |
| # Check for lazy loading | |
| if "import torch" in app_code and "def " not in app_code.split("import torch")[0]: | |
| warnings.append("torch imported at module level - consider lazy loading") | |
| # Check for hardcoded tokens | |
| if re.search(r'hf_[a-zA-Z0-9]{20,}', app_code): | |
| errors.append("Hardcoded HF token detected in app.py") | |
| if re.search(r'sk-[a-zA-Z0-9]{20,}', app_code): | |
| errors.append("Hardcoded API key detected in app.py") | |
| else: | |
| errors.append("app.py not found") | |
| return errors, warnings | |
| if __name__ == "__main__": | |
| exp_dir = sys.argv[1] if len(sys.argv) > 1 else "." | |
| errors, warnings = validate_space(exp_dir) | |
| if errors: | |
| print("ERRORS:") | |
| for e in errors: | |
| print(f" - {e}") | |
| sys.exit(1) | |
| if warnings: | |
| print("WARNINGS:") | |
| for w in warnings: | |
| print(f" - {w}") | |
| print("Validation passed") | |
| sys.exit(0) | |