AI_Safety_Lab / validate_system.py
soupstick's picture
Initial DSPy-based AI Safety Lab implementation
4fef010
#!/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())