Spaces:
Sleeping
Sleeping
Download app.py from delpierito/node-test-predict: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/delpierito/node-test-predict/resolve/main/app.py
- Command line
-
hf download hf://spaces/delpierito/node-test-predict/app.py
-
curl -L -o app.py https://huggingface.co/spaces/delpierito/node-test-predict/resolve/main/app.py
1.06 kB
| from fastapi import FastAPI, Header, HTTPException | |
| from pydantic import BaseModel | |
| import os | |
| from sklearn.linear_model import LogisticRegression | |
| import numpy as np | |
| app = FastAPI() | |
| TOKEN = os.environ.get("API_TOKEN", "demo-token-change-me") | |
| # Toy model — XOR-ish dataset, entrenado al arrancar el container | |
| X_train = [[0, 0], [1, 1], [0, 1], [1, 0], [0.2, 0.1], [0.9, 0.8]] | |
| y_train = [0, 1, 1, 0, 0, 1] | |
| model = LogisticRegression().fit(X_train, y_train) | |
| class Body(BaseModel): | |
| instances: list[dict] | |
| def root(): | |
| return {"status": "ok", "endpoint": "/predict"} | |
| def predict(body: Body, authorization: str = Header(None)): | |
| if authorization != f"Bearer {TOKEN}": | |
| raise HTTPException(status_code=401, detail="invalid token") | |
| X = np.array([[r.get("x", 0), r.get("y", 0)] for r in body.instances]) | |
| probs = model.predict_proba(X)[:, 1] | |
| return { | |
| "predictions": [ | |
| {"score": float(p), "label": "positive" if p > 0.5 else "negative"} | |
| for p in probs | |
| ] | |
| } | |