Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| AI Safety Lab - System Validation Script | |
| Validates the complete AI Safety Lab system for deployment readiness. | |
| This script checks imports, basic functionality, and system integrity. | |
| """ | |
| import sys | |
| import os | |
| import importlib.util | |
| from pathlib import Path | |
| def check_file_structure(): | |
| """Verify all required files are present""" | |
| print("π Checking file structure...") | |
| required_files = { | |
| 'app.py': 'Main Gradio application', | |
| 'requirements.txt': 'Python dependencies', | |
| 'README.md': 'Documentation', | |
| 'roadmap.md': 'Development roadmap', | |
| 'agents/red_team.py': 'Red teaming agent', | |
| 'agents/safety_judge.py': 'Safety judge agent', | |
| 'models/hf_interface.py': 'HuggingFace model interface', | |
| 'orchestration/loop.py': 'Evaluation orchestration', | |
| 'evals/metrics.py': 'Safety metrics calculator' | |
| } | |
| missing_files = [] | |
| for file_path, description in required_files.items(): | |
| if Path(file_path).exists(): | |
| print(f" β {file_path} - {description}") | |
| else: | |
| print(f" β {file_path} - {description} - MISSING") | |
| missing_files.append(file_path) | |
| return len(missing_files) == 0 | |
| def check_python_syntax(): | |
| """Check Python syntax for all Python files""" | |
| print("\nπ Checking Python syntax...") | |
| python_files = list(Path(".").rglob("*.py")) | |
| syntax_errors = [] | |
| for py_file in python_files: | |
| try: | |
| with open(py_file, 'r', encoding='utf-8') as f: | |
| compile(f.read(), str(py_file), 'exec') | |
| print(f" β {py_file}") | |
| except SyntaxError as e: | |
| print(f" β {py_file} - Syntax error: {e}") | |
| syntax_errors.append(str(py_file)) | |
| except Exception as e: | |
| print(f" β οΈ {py_file} - Error: {e}") | |
| return len(syntax_errors) == 0 | |
| def check_data_models(): | |
| """Test basic data model instantiation""" | |
| print("\nποΈ Testing data models...") | |
| try: | |
| from agents.red_team import AdversarialPrompt | |
| from agents.safety_judge import SafetyJudgment, HarmSeverity | |
| # Test AdversarialPrompt | |
| prompt = AdversarialPrompt( | |
| prompt='test prompt', | |
| attack_vector='test_vector', | |
| expected_violation='test_violation', | |
| complexity_score=0.5 | |
| ) | |
| print(" β AdversarialPrompt instantiation") | |
| # Test SafetyJudgment | |
| judgment = SafetyJudgment( | |
| overall_risk_score=0.3, | |
| policy_violation_likelihood=0.2, | |
| harm_severity=HarmSeverity.LOW, | |
| ambiguity_risk=0.1, | |
| exploitability=0.15, | |
| dimensions=[], | |
| flag_reasons=[], | |
| recommendation='APPROVE: Output appears safe' | |
| ) | |
| print(" β SafetyJudgment instantiation") | |
| return True | |
| except Exception as e: | |
| print(f" β Data model error: {e}") | |
| return False | |
| def check_deployment_readiness(): | |
| """Check deployment-specific requirements""" | |
| print("\nπ Checking deployment readiness...") | |
| # Check Hugging Face token | |
| hf_token = os.environ.get('HUGGINGFACEHUB_API_TOKEN') | |
| if hf_token: | |
| print(" β HUGGINGFACEHUB_API_TOKEN found") | |
| else: | |
| print(" β οΈ HUGGINGFACEHUB_API_TOKEN not set (required for deployment)") | |
| # Check Gradio compatibility | |
| try: | |
| import gradio as gr | |
| print(" β Gradio available") | |
| except ImportError: | |
| print(" β Gradio not available") | |
| return False | |
| # Check DSPy compatibility | |
| try: | |
| import dspy | |
| print(" β DSPy available") | |
| except ImportError: | |
| print(" β DSPy not available") | |
| return False | |
| return True | |
| def main(): | |
| """Run complete system validation""" | |
| print("π‘οΈ AI Safety Lab - System Validation") | |
| print("=" * 50) | |
| # Run all checks | |
| structure_ok = check_file_structure() | |
| syntax_ok = check_python_syntax() | |
| models_ok = check_data_models() | |
| deployment_ok = check_deployment_readiness() | |
| # Summary | |
| print("\n" + "=" * 50) | |
| print("π VALIDATION SUMMARY") | |
| print("=" * 50) | |
| checks = [ | |
| ("File Structure", structure_ok), | |
| ("Python Syntax", syntax_ok), | |
| ("Data Models", models_ok), | |
| ("Deployment Ready", deployment_ok) | |
| ] | |
| all_passed = True | |
| for check_name, passed in checks: | |
| status = "β PASS" if passed else "β FAIL" | |
| print(f" {check_name:20} {status}") | |
| if not passed: | |
| all_passed = False | |
| print("\n" + "=" * 50) | |
| if all_passed: | |
| print("π ALL CHECKS PASSED - System ready for deployment!") | |
| print("\nNext steps:") | |
| print("1. Set HUGGINGFACEHUB_API_TOKEN environment variable") | |
| print("2. Deploy to Hugging Face Space") | |
| print("3. Run safety evaluations") | |
| return 0 | |
| else: | |
| print("β SOME CHECKS FAILED - Fix issues before deployment") | |
| return 1 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |