Upload src/server.py with huggingface_hub
Browse files- src/server.py +38 -0
src/server.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Optional FastAPI microservice for Imager-1-Tiny.
|
| 3 |
+
Run: uvicorn src.server:app --host 0.0.0.0 --port 8000
|
| 4 |
+
"""
|
| 5 |
+
from fastapi import FastAPI, File, UploadFile
|
| 6 |
+
from fastapi.responses import JSONResponse
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
from PIL import Image
|
| 9 |
+
|
| 10 |
+
from .config import load_model, predict
|
| 11 |
+
|
| 12 |
+
app = FastAPI(title="Imager-1-Tiny", version="1.0.0")
|
| 13 |
+
|
| 14 |
+
_model = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_model():
|
| 18 |
+
global _model
|
| 19 |
+
if _model is None:
|
| 20 |
+
_model = load_model()
|
| 21 |
+
return _model
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@app.get("/")
|
| 25 |
+
def root():
|
| 26 |
+
return {"name": "imager-1-tiny", "status": "ok"}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@app.get("/health")
|
| 30 |
+
def health():
|
| 31 |
+
return {"status": "healthy"}
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@app.post("/predict")
|
| 35 |
+
async def predict_endpoint(file: UploadFile = File(...)):
|
| 36 |
+
image = Image.open(BytesIO(await file.read()))
|
| 37 |
+
result = predict(image, model=get_model())
|
| 38 |
+
return JSONResponse(result)
|