projectxddi's picture
AI server: backend, start-up, status and admin pages
093748c verified
Raw History Blame Contribute Delete
2.96 kB
"""BindSite AI server on a ZeroGPU Space: the FastAPI backend of this repository (backend/app), unchanged, with
* / public status page (Gradio)
* /<ADMIN_PATH> admin page: visits, sessions, predictions (Gradio, user "admin" + ADMIN_PASSWORD)
* /predict, /compare … the REST API the BindSite Explorer page calls (CORS: the static site)
* /worker/next|result jobs for the backup machine (WORKER_KEY)
Models run on the Space's CPUs (16 vCPU): visitors without a Hugging Face login get only a few ZeroGPU runs, so the
GPU is not on the prediction path. ZeroGPU still needs one @spaces.GPU function (the admin page's GPU check).
Space secrets: HF_TOKEN (fine-grained, read/write of the bucket projectxddi/bindsite-logs), ADMIN_PATH,
ADMIN_PASSWORD, WORKER_KEY. Built from this repository by scripts/push_ai_space.sh.
"""
import spaces # noqa: F401 (first: ZeroGPU patches CUDA before torch is imported)
import logging
import os
import sys
from pathlib import Path
APP = Path(__file__).resolve().parent
sys.path[:0] = [str(APP / "backend"), str(APP / "adabind")] # the backend's `app` package and pbind
_console = logging.StreamHandler(sys.stdout) # start-up steps only: the backend sets up its own "bindsite" logging
_console.setFormatter(logging.Formatter("%(asctime)s %(levelname)-7s %(name)s %(message)s"))
logging.getLogger("bindsite.bootstrap").addHandler(_console)
logging.getLogger("bindsite.bootstrap").setLevel(logging.INFO)
logging.getLogger("bindsite.bootstrap").propagate = False
import bootstrap # noqa: E402
bootstrap.prepare()
import gradio as gr # noqa: E402
import torch # noqa: E402
import uvicorn # noqa: E402
from app.main import create_app # noqa: E402
from pages import admin_page, status_page # noqa: E402
@spaces.GPU(duration=10)
def gpu_check() -> str:
"""Which GPU ZeroGPU attaches (admin page). Not used by predictions."""
props = torch.cuda.get_device_properties(0)
return f"{props.name}, {props.total_memory / 1e9:.0f} GB"
api = create_app()
admin_path = os.environ.get("ADMIN_PATH", "").strip("/ ")
admin_password = os.environ.get("ADMIN_PASSWORD", "")
if admin_path and admin_password:
api = gr.mount_gradio_app(api, admin_page(api, gpu_check), path=f"/{admin_path}",
auth=("admin", admin_password), ssr_mode=False)
else:
logging.getLogger("bindsite").warning("No admin page: set the secrets ADMIN_PATH and ADMIN_PASSWORD")
api = gr.mount_gradio_app(api, status_page(api), path="/", ssr_mode=False) # SSR would start Node on port 7860
if __name__ == "__main__":
import spaces.zero
if hasattr(spaces.zero, "startup"):
spaces.zero.startup() # registers @spaces.GPU functions; gr.Blocks.launch() would do it, uvicorn does not
uvicorn.run(api, host=os.environ.get("BINDSITE_HOST", "0.0.0.0"), port=int(os.environ.get("PORT", "7860")),
log_level="warning", timeout_keep_alive=75)