"""Preflight for the fal beautify engine — readiness check, NO network/paid call. Verifies everything needed BEFORE spending a real fal call: Python deps, the face detector, the local image steps (mask + skin grain on a synthetic image), and whether FAL_KEY is present. It never contacts fal and never prints the key. Exit 0 = ready for a real run; exit 1 = something is missing (see report). """ from __future__ import annotations import importlib.util import sys from io import BytesIO from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) def _check(label, ok, detail=""): mark = "PASS" if ok else "FAIL" print(f"[{mark}] {label}{(' - ' + detail) if detail else ''}") return ok def main() -> int: print("fal beautify preflight (no network, no paid call)\n") ok = True # 1) Python deps for mod in ("httpx", "PIL", "numpy"): ok &= _check(f"dep: {mod}", importlib.util.find_spec(mod) is not None) for mod in ("insightface", "onnxruntime", "cv2"): present = importlib.util.find_spec(mod) is not None _check(f"dep (face detect): {mod}", present, "" if present else "needed for face/gender detection") ok &= present # 2) Engine imports try: from app.services.falinpaint_beautify import beautify_with_fal # noqa: F401 from app.services.falinpaint_client import FalInpaintBeautifyClient # noqa: F401 from app.services.gemini_client import add_skin_grain, build_beautify_inpaint_mask _check("engine imports", True) except Exception as exc: # noqa: BLE001 return 1 if not _check("engine imports", False, str(exc)) else 1 # 3) Local image steps work on a synthetic image (no network) try: from PIL import Image buf = BytesIO(); Image.new("RGB", (200, 260), (120, 110, 100)).save(buf, "PNG") png = buf.getvalue() m = build_beautify_inpaint_mask((200, 260), (60, 60, 80, 110)) mbuf = BytesIO(); m.save(mbuf, "PNG") grained = add_skin_grain(png, mbuf.getvalue(), amount=10) steps_ok = (m.size == (200, 260)) and (len(grained) > 0) ok &= _check("local steps (mask + skin grain)", steps_ok) except Exception as exc: # noqa: BLE001 ok &= _check("local steps (mask + skin grain)", False, str(exc)) # 4) Provider selection + FAL_KEY presence (value never printed) try: from app.services.fal_client import fal_real_enabled from app.services.falinpaint_beautify import fal_inpaint_model key_present = fal_real_enabled() print(f"[{'PASS' if key_present else 'WARN'}] FAL_KEY present" + ("" if key_present else " - set FAL_KEY before a real run (this shell only)")) print(f"[INFO] fal model = {fal_inpaint_model()}") print(f"[INFO] real run possible now = {key_present}") except Exception as exc: # noqa: BLE001 ok &= _check("provider check", False, str(exc)) print("\n" + ("READY: deps/engine/local steps OK." if ok else "NOT READY: fix FAIL items above.")) print("(A real beautify still needs FAL_KEY + the operator to run it.) " "Pilot Ready: NOT CONFIRMED.") return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())