Download serve.py from inclusionAI/LemonDecider-4B-preview: direct link, hf CLI and curl.
- Browser
- Download file 3.19 kB
-
https://huggingface.co/inclusionAI/LemonDecider-4B-preview/resolve/main/serve.py
- Command line
-
hf download hf://inclusionAI/LemonDecider-4B-preview/serve.py
-
curl -L -o serve.py https://huggingface.co/inclusionAI/LemonDecider-4B-preview/resolve/main/serve.py
3.19 kB
| #!/usr/bin/env python3 | |
| """Serve this self-contained LemonDecider package over the typed-decision HTTP API. | |
| Endpoints: GET /health, GET /v1/models, POST /v1/systemone. | |
| The service is text-only, unauthenticated, and serializes requests on one GPU. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| import threading | |
| import time | |
| from pathlib import Path | |
| import uvicorn | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel, ConfigDict, Field | |
| SERVICE = {"model": None, "lock": threading.Lock(), "checkpoint": "", "started_at": time.time()} | |
| class Question(BaseModel): | |
| model_config = ConfigDict(extra="forbid") | |
| type: str | |
| instructions: str | dict | list | None = None | |
| criteria: dict | list | None = None | |
| class DecisionRequest(BaseModel): | |
| model_config = ConfigDict(extra="forbid") | |
| model: str = Field(min_length=1) | |
| state: str | dict | list | |
| questions: dict[str, Question] = Field(min_length=1) | |
| images: list[str] = Field(default_factory=list, max_length=4) | |
| app = FastAPI(title="LemonDecider HTTP service", version="1.0.0") | |
| def health(): | |
| model = SERVICE["model"] | |
| if model is None: | |
| return {"status": "loading", "checkpoint": SERVICE["checkpoint"], "authentication": False} | |
| return { | |
| "status": "ready", | |
| "model": model.config.model_id, | |
| "checkpoint": SERVICE["checkpoint"], | |
| "device": model.device, | |
| "max_input_length": model.config.max_input_length, | |
| "max_position_embeddings": model.config.text_config.max_position_embeddings, | |
| "modalities": ["text"], | |
| "authentication": False, | |
| "uptime_seconds": round(time.time() - SERVICE["started_at"], 1), | |
| } | |
| def models(): | |
| model = SERVICE["model"] | |
| return {"data": [{"id": model.config.model_id if model is not None else "loading"}]} | |
| def systemone(req: DecisionRequest): | |
| model = SERVICE["model"] | |
| if model is None: | |
| raise HTTPException(503, "model still loading") | |
| if req.images: | |
| raise HTTPException(400, "This release supports text and structured JSON only") | |
| request = { | |
| "state": req.state, | |
| "questions": {name: q.model_dump(exclude_none=True) for name, q in req.questions.items()}, | |
| } | |
| with SERVICE["lock"]: | |
| try: | |
| return model.predict(request) | |
| except ValueError as exc: | |
| raise HTTPException(400, str(exc)) from exc | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--checkpoint", default=str(Path(__file__).resolve().parent)) | |
| parser.add_argument("--host", default="0.0.0.0") | |
| parser.add_argument("--port", type=int, default=8100) | |
| parser.add_argument("--device", default="cuda:0") | |
| args = parser.parse_args() | |
| checkpoint = Path(args.checkpoint).resolve() | |
| sys.path.insert(0, str(checkpoint)) | |
| from model import DecisionModel | |
| SERVICE["checkpoint"] = str(checkpoint) | |
| SERVICE["model"] = DecisionModel(checkpoint, device=args.device) | |
| uvicorn.run(app, host=args.host, port=args.port, log_level="info") | |
| if __name__ == "__main__": | |
| main() | |