CoolFace
Apppublic

vinayaknandi05/sql-optimization-openenv

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
validate.py123 linesDownload Raw Back to root
1"""2Pre-Submission Validation Script3Checks all items from the Pre-Submission Checklist locally4before pushing to HF Spaces.5"""6import subprocess7import sys8import os9import json10 11sys.path.insert(0, os.path.dirname(__file__))12 13PASS = "✅"14FAIL = "❌"15results = []16 17def check(name, fn):18    try:19        ok, detail = fn()20        results.append((name, ok, detail))21        symbol = PASS if ok else FAIL22        print(f"{symbol} {name}: {detail}")23    except Exception as e:24        results.append((name, False, str(e)))25        print(f"{FAIL} {name}: EXCEPTION — {e}")26 27 28# ── 1. openenv.yaml exists and has required fields ───────────────────────────29def check_yaml():30    import yaml31    with open("openenv.yaml") as f:32        data = yaml.safe_load(f)33    required = ["name", "observation_space", "action_space", "tasks", "endpoints"]34    missing = [k for k in required if k not in data]35    if missing:36        return False, f"Missing fields: {missing}"37    return True, f"Valid — {len(data['tasks'])} tasks defined"38 39check("openenv.yaml valid", check_yaml)40 41 42# ── 2. Dockerfile exists ─────────────────────────────────────────────────────43def check_dockerfile():44    ok = os.path.exists("Dockerfile")45    return ok, "Dockerfile found" if ok else "Dockerfile missing"46 47check("Dockerfile exists", check_dockerfile)48 49 50# ── 3. inference.py exists at root ───────────────────────────────────────────51def check_inference():52    ok = os.path.exists("inference.py")53    return ok, "inference.py found at root" if ok else "inference.py missing from root"54 55check("inference.py at root", check_inference)56 57 58# ── 4. Environment can reset ─────────────────────────────────────────────────59def check_reset():60    from env.environment import SQLOptimizationEnv61    env = SQLOptimizationEnv()62    obs = env.reset()63    ok = obs.echoed_message is not None and obs.original_query64    return ok, f"reset() returned observation with echoed_message='{obs.echoed_message[:50]}...'"65 66check("reset() works", check_reset)67 68 69# ── 5. All 3 tasks produce scores in 0.0–1.0 ─────────────────────────────────70def check_graders():71    from env.environment import SQLOptimizationEnv, SQLAction72    env = SQLOptimizationEnv()73    for task_id in ["task_easy", "task_medium", "task_hard"]:74        env.reset(task_id=task_id)75        _, _, _, info = env.step(SQLAction(query="SELECT 1;"))76        s = info["score"]77        if not (0.0 <= s <= 1.0):78            return False, f"Task {task_id} score={s} out of range"79    return True, "All 3 tasks produce scores in [0.0, 1.0]"80 81check("3+ tasks with graders (0.0–1.0)", check_graders)82 83 84# ── 6. step() and state() return typed Pydantic models ───────────────────────85def check_typed_models():86    from env.environment import SQLOptimizationEnv, SQLAction, SQLObservation87    env = SQLOptimizationEnv()88    env.reset()89    obs, reward, done, info = env.step(SQLAction(query="SELECT id FROM employees LIMIT 1;"))90    assert isinstance(obs, SQLObservation), "step() must return SQLObservation"91    assert isinstance(reward, float), "reward must be float"92    assert isinstance(done, bool), "done must be bool"93    state = env.state()94    assert isinstance(state, dict), "state() must return dict"95    return True, "step() → SQLObservation, reward: float, done: bool; state() → dict"96 97check("Typed models (Pydantic)", check_typed_models)98 99 100# ── 7. Pytest suite passes ────────────────────────────────────────────────────101def check_tests():102    result = subprocess.run(103        [sys.executable, "-m", "pytest", "tests/", "-q", "--tb=short"],104        capture_output=True, text=True105    )106    passed = result.returncode == 0107    last_line = result.stdout.strip().split("\n")[-1] if result.stdout else result.stderr[:200]108    return passed, last_line109 110check("pytest suite passes", check_tests)111 112 113# ── Summary ───────────────────────────────────────────────────────────────────114print("\n" + "="*60)115passed = sum(1 for _, ok, _ in results if ok)116total = len(results)117print(f"Pre-Submission Validation: {passed}/{total} checks passed")118if passed == total:119    print("🎉 All checks passed — ready to submit!")120else:121    print("⚠️  Fix the failing checks above before submitting.")122    sys.exit(1)123