Spaces:
Sleeping
Sleeping
File size: 13,072 Bytes
99f938a | 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | #!/usr/bin/env python
"""
Pre-submission validation script for HF Spaces deployment.
Checks:
β Environment variables are set
β Required files exist
β inference.py format compliance
β Docker build (optional)
β Inference script execution
β stdout format compliance
Run:
python validate.py
python validate.py --skip-inference # Skip inference execution
python validate.py --docker # Also validate Docker build
"""
import os
import sys
import subprocess
import json
import re
from pathlib import Path
from typing import List, Tuple
class Colors:
GREEN = "\033[92m"
RED = "\033[91m"
YELLOW = "\033[93m"
BLUE = "\033[94m"
RESET = "\033[0m"
def print_check(name: str, passed: bool, msg: str = ""):
status = f"{Colors.GREEN}β{Colors.RESET}" if passed else f"{Colors.RED}β{Colors.RESET}"
print(f" {status} {name}")
if msg:
print(f" {msg}")
def check_environment_variables() -> bool:
"""Check mandatory environment variables"""
print(f"\n{Colors.BLUE}1. Environment Variables{Colors.RESET}")
required_vars = {
"API_BASE_URL": "LLM API endpoint",
"MODEL_NAME": "Model identifier",
"HF_TOKEN": "Hugging Face token",
}
all_ok = True
for var, desc in required_vars.items():
value = os.getenv(var)
passed = bool(value)
print_check(f"{var}", passed, desc if not passed else f"Set: {value[:50]}...")
all_ok = all_ok and passed
return all_ok
def check_required_files() -> bool:
"""Check required project files"""
print(f"\n{Colors.BLUE}2. Required Files{Colors.RESET}")
required_files = [
("inference.py", "Inference script"),
("requirements.txt", "Python dependencies"),
("openenv.yaml", "OpenEnv specification"),
("Dockerfile", "Container definition"),
(".env.example", "Environment template"),
]
all_ok = True
for filename, desc in required_files:
passed = Path(filename).exists()
print_check(filename, passed, desc)
all_ok = all_ok and passed
return all_ok
def check_inference_compliance() -> bool:
"""Check inference.py OpenEnv compliance"""
print(f"\n{Colors.BLUE}3. Inference Script Compliance{Colors.RESET}")
checks = []
# Read inference.py
try:
with open("inference.py") as f:
content = f.read()
# Check 1: Imports
has_openai = "from openai import OpenAI" in content or "import openai" in content
checks.append(("OpenAI imports", has_openai))
# Check 2: env variables
has_api_base = "API_BASE_URL" in content
checks.append(("API_BASE_URL config", has_api_base))
has_model = "MODEL_NAME" in content
checks.append(("MODEL_NAME config", has_model))
has_hf_token = "HF_TOKEN" in content
checks.append(("HF_TOKEN config", has_hf_token))
# Check 3: Logging format
has_start_log = '[START]' in content and 'task=' in content
checks.append(("[START] logging", has_start_log))
has_step_log = '[STEP]' in content and 'step=' in content
checks.append(("[STEP] logging", has_step_log))
has_end_log = '[END]' in content and 'success=' in content
checks.append(("[END] logging", has_end_log))
# Check 4: Main function
has_main = "if __name__" in content and "main()" in content
checks.append(("Main entry point", has_main))
# Check 5: Async support
has_async = "asyncio" in content or "async def" in content
checks.append(("Async support", has_async))
except Exception as e:
print_check("Read inference.py", False, str(e))
return False
all_ok = True
for check_name, passed in checks:
print_check(check_name, passed)
all_ok = all_ok and passed
return all_ok
def check_requirements() -> bool:
"""Check requirements.txt"""
print(f"\n{Colors.BLUE}4. Python Dependencies{Colors.RESET}")
required_packages = [
"openai",
"numpy",
"opencv-python",
]
try:
with open("requirements.txt") as f:
content = f.read().lower()
all_ok = True
for pkg in required_packages:
found = pkg.lower() in content
print_check(pkg, found)
all_ok = all_ok and found
return all_ok
except Exception as e:
print_check("Read requirements.txt", False, str(e))
return False
def check_openenv_yaml() -> bool:
"""Check openenv.yaml structure"""
print(f"\n{Colors.BLUE}5. OpenEnv YAML Specification{Colors.RESET}")
checks = []
try:
with open("openenv.yaml") as f:
content = f.read()
# Check basic structure
has_name = "name:" in content
checks.append(("Name field", has_name))
has_endpoints = "endpoints:" in content
checks.append(("Endpoints section", has_endpoints))
has_reset = "reset:" in content
checks.append(("Reset endpoint", has_reset))
has_step = "step:" in content
checks.append(("Step endpoint", has_step))
has_tasks = "tasks:" in content
checks.append(("Tasks section", has_tasks))
has_env_vars = "environment_variables:" in content
checks.append(("Environment variables", has_env_vars))
has_validation = "validation:" in content
checks.append(("Validation rules", has_validation))
except Exception as e:
print_check("Read openenv.yaml", False, str(e))
return False
all_ok = True
for check_name, passed in checks:
print_check(check_name, passed)
all_ok = all_ok and passed
return all_ok
def validate_stdout_format(output: str) -> Tuple[bool, List[str]]:
"""Validate stdout format compliance"""
errors = []
lines = output.strip().split("\n")
# Must have START, STEP(s), and END
has_start = any(line.startswith("[START]") for line in lines)
has_step = any(line.startswith("[STEP]") for line in lines)
has_end = any(line.startswith("[END]") for line in lines)
if not has_start:
errors.append("Missing [START] line")
if not has_step:
errors.append("Missing [STEP] lines")
if not has_end:
errors.append("Missing [END] line")
# Validate line formats
for line in lines:
if line.startswith("[START]"):
if not re.search(r"task=\w+", line):
errors.append(f"[START] missing task field: {line}")
if not re.search(r"env=\w+", line):
errors.append(f"[START] missing env field: {line}")
if not re.search(r"model=", line):
errors.append(f"[START] missing model field: {line}")
elif line.startswith("[STEP]"):
if not re.search(r"step=\d+", line):
errors.append(f"[STEP] missing step field: {line}")
if not re.search(r"reward=[\d.]+", line):
errors.append(f"[STEP] missing reward field: {line}")
if not re.search(r"done=(true|false)", line):
errors.append(f"[STEP] missing done field: {line}")
elif line.startswith("[END]"):
if not re.search(r"success=(true|false)", line):
errors.append(f"[END] missing success field: {line}")
if not re.search(r"steps=\d+", line):
errors.append(f"[END] missing steps field: {line}")
if not re.search(r"score=[\d.]+", line):
errors.append(f"[END] missing score field: {line}")
return len(errors) == 0, errors
def check_docker_build() -> bool:
"""Check if Dockerfile builds"""
print(f"\n{Colors.BLUE}6. Docker Build{Colors.RESET}")
if not Path("Dockerfile").exists():
print_check("Dockerfile exists", False)
return False
try:
print(" Building Docker image (this may take a few minutes)...")
result = subprocess.run(
["docker", "build", "-t", "autonomous-traffic-control:test", "."],
capture_output=True,
text=True,
timeout=600,
)
passed = result.returncode == 0
print_check("Docker build", passed)
if not passed:
print(f" Error: {result.stderr[:200]}")
return passed
except FileNotFoundError:
print(f" {Colors.YELLOW}β Docker not found - skipping{Colors.RESET}")
return True
except Exception as e:
print_check("Docker build", False, str(e))
return False
def check_inference_execution() -> bool:
"""Run inference and check output format"""
print(f"\n{Colors.BLUE}7. Inference Execution{Colors.RESET}")
if not Path("inference.py").exists():
print_check("inference.py", False)
return False
try:
print(" Running inference script (timeout: 30s)...")
env = os.environ.copy()
if not env.get("API_BASE_URL"):
print(f" {Colors.YELLOW}β API_BASE_URL not set - using default{Colors.RESET}")
if not env.get("HF_TOKEN"):
print(f" {Colors.YELLOW}β HF_TOKEN not set - validation will fail{Colors.RESET}")
result = subprocess.run(
["python", "inference.py"],
capture_output=True,
text=True,
timeout=30,
env=env,
)
output = result.stdout + result.stderr
# Check execution
execution_ok = result.returncode == 0
print_check("Script execution", execution_ok)
# Check stdout format
format_ok, errors = validate_stdout_format(output)
print_check("Stdout format", format_ok)
if errors:
for error in errors[:3]:
print(f" {Colors.RED}Error: {error}{Colors.RESET}")
# Show sample output
if "[START]" in output:
start_line = [l for l in output.split("\n") if "[START]" in l][0]
print(f" Sample: {start_line[:80]}...")
return execution_ok and format_ok
except subprocess.TimeoutExpired:
print_check("Script execution", False, "Timeout (>30s)")
return False
except Exception as e:
print_check("Script execution", False, str(e))
return False
def main():
"""Run all validation checks"""
import argparse
parser = argparse.ArgumentParser(
description="Validate autonomous-traffic-control for HF Spaces deployment"
)
parser.add_argument(
"--skip-inference",
action="store_true",
help="Skip inference execution check",
)
parser.add_argument(
"--docker",
action="store_true",
help="Also validate Docker build",
)
args = parser.parse_args()
print(f"\n{Colors.BLUE}{'='*60}")
print("Autonomous Traffic Control - Pre-submission Validation")
print(f"{'='*60}{Colors.RESET}\n")
results = {
"Environment Variables": check_environment_variables(),
"Required Files": check_required_files(),
"Inference Compliance": check_inference_compliance(),
"Python Dependencies": check_requirements(),
"OpenEnv YAML": check_openenv_yaml(),
}
if args.docker:
results["Docker Build"] = check_docker_build()
if not args.skip_inference:
results["Inference Execution"] = check_inference_execution()
# Summary
print(f"\n{Colors.BLUE}{'='*60}")
print("Validation Summary")
print(f"{'='*60}{Colors.RESET}\n")
for check_name, passed in results.items():
status = f"{Colors.GREEN}PASS{Colors.RESET}" if passed else f"{Colors.RED}FAIL{Colors.RESET}"
print(f" {check_name:<30} {status}")
all_passed = all(results.values())
print(f"\n{Colors.BLUE}{'='*60}{Colors.RESET}\n")
if all_passed:
print(f"{Colors.GREEN}β All checks passed!{Colors.RESET}")
print(" Your project is ready for HF Spaces submission.")
return 0
else:
print(f"{Colors.RED}β Some checks failed.{Colors.RESET}")
print(" Please fix the issues above before submitting.")
return 1
if __name__ == "__main__":
sys.exit(main())
|