File size: 767 Bytes
3378662
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Optional FastAPI microservice for Imager-1-Pro."""
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
from io import BytesIO
from PIL import Image

from .config import load_model, predict

app = FastAPI(title="Imager-1-Pro", version="1.0.0")

_model = None


def get_model():
    global _model
    if _model is None:
        _model = load_model()
    return _model


@app.get("/")
def root():
    return {"name": "imager-1-pro", "status": "ok"}


@app.get("/health")
def health():
    return {"status": "healthy"}


@app.post("/predict")
async def predict_endpoint(file: UploadFile = File(...)):
    image = Image.open(BytesIO(await file.read()))
    result = predict(image, model=get_model())
    return JSONResponse(result)