2023-05-17 17:07:17 +00:00
|
|
|
import os
|
2023-06-18 03:49:19 +00:00
|
|
|
import io
|
2023-06-05 14:40:48 +00:00
|
|
|
from typing import Any
|
2023-06-07 01:48:51 +00:00
|
|
|
|
|
|
|
from cache import ModelCache
|
2023-06-05 14:40:48 +00:00
|
|
|
from schemas import (
|
|
|
|
EmbeddingResponse,
|
|
|
|
FaceResponse,
|
|
|
|
TagResponse,
|
|
|
|
MessageResponse,
|
|
|
|
TextModelRequest,
|
|
|
|
TextResponse,
|
|
|
|
)
|
2023-05-17 17:07:17 +00:00
|
|
|
import uvicorn
|
2023-03-18 13:44:42 +00:00
|
|
|
from PIL import Image
|
2023-06-18 03:49:19 +00:00
|
|
|
from fastapi import FastAPI, HTTPException, Depends, Body
|
2023-06-07 01:48:51 +00:00
|
|
|
from models import get_model, run_classification, run_facial_recognition
|
2023-06-18 03:49:19 +00:00
|
|
|
from config import settings
|
2023-06-03 02:42:47 +00:00
|
|
|
|
2023-06-07 01:48:51 +00:00
|
|
|
_model_cache = None
|
2023-06-18 03:49:19 +00:00
|
|
|
|
2023-05-17 17:07:17 +00:00
|
|
|
app = FastAPI()
|
|
|
|
|
2023-04-26 10:39:24 +00:00
|
|
|
|
2023-05-20 03:37:01 +00:00
|
|
|
@app.on_event("startup")
|
2023-06-05 14:40:48 +00:00
|
|
|
async def startup_event() -> None:
|
2023-06-07 01:48:51 +00:00
|
|
|
global _model_cache
|
2023-06-18 03:49:19 +00:00
|
|
|
_model_cache = ModelCache(ttl=settings.model_ttl, revalidate=True)
|
2023-06-03 02:42:47 +00:00
|
|
|
models = [
|
2023-06-18 03:49:19 +00:00
|
|
|
(settings.classification_model, "image-classification"),
|
|
|
|
(settings.clip_image_model, "clip"),
|
|
|
|
(settings.clip_text_model, "clip"),
|
|
|
|
(settings.facial_recognition_model, "facial-recognition"),
|
2023-06-03 02:42:47 +00:00
|
|
|
]
|
|
|
|
|
2023-05-20 03:37:01 +00:00
|
|
|
# Get all models
|
2023-06-03 02:42:47 +00:00
|
|
|
for model_name, model_type in models:
|
2023-06-18 03:49:19 +00:00
|
|
|
if settings.eager_startup:
|
2023-06-07 01:48:51 +00:00
|
|
|
await _model_cache.get_cached_model(model_name, model_type)
|
2023-06-03 02:42:47 +00:00
|
|
|
else:
|
2023-06-07 01:48:51 +00:00
|
|
|
get_model(model_name, model_type)
|
2023-05-20 03:37:01 +00:00
|
|
|
|
|
|
|
|
2023-06-18 03:49:19 +00:00
|
|
|
def dep_model_cache():
|
|
|
|
if _model_cache is None:
|
|
|
|
raise HTTPException(status_code=500, detail="Unable to load model.")
|
|
|
|
|
|
|
|
def dep_input_image(image: bytes = Body(...)) -> Image:
|
|
|
|
return Image.open(io.BytesIO(image))
|
|
|
|
|
2023-06-05 14:40:48 +00:00
|
|
|
@app.get("/", response_model=MessageResponse)
|
|
|
|
async def root() -> dict[str, str]:
|
2023-04-26 10:39:24 +00:00
|
|
|
return {"message": "Immich ML"}
|
|
|
|
|
|
|
|
|
2023-06-05 14:40:48 +00:00
|
|
|
@app.get("/ping", response_model=TextResponse)
|
|
|
|
def ping() -> str:
|
2023-02-18 15:13:37 +00:00
|
|
|
return "pong"
|
|
|
|
|
2023-06-03 02:42:47 +00:00
|
|
|
|
2023-06-18 03:49:19 +00:00
|
|
|
@app.post(
|
|
|
|
"/image-classifier/tag-image",
|
|
|
|
response_model=TagResponse,
|
|
|
|
status_code=200,
|
|
|
|
dependencies=[Depends(dep_model_cache)],
|
|
|
|
)
|
|
|
|
async def image_classification(
|
|
|
|
image: Image = Depends(dep_input_image)
|
|
|
|
) -> list[str]:
|
|
|
|
try:
|
|
|
|
model = await _model_cache.get_cached_model(
|
|
|
|
settings.classification_model, "image-classification"
|
|
|
|
)
|
|
|
|
labels = run_classification(model, image, settings.min_tag_score)
|
|
|
|
except Exception as ex:
|
|
|
|
raise HTTPException(status_code=500, detail=str(ex))
|
|
|
|
else:
|
|
|
|
return labels
|
2023-04-26 10:39:24 +00:00
|
|
|
|
2023-03-18 13:44:42 +00:00
|
|
|
|
2023-06-05 14:40:48 +00:00
|
|
|
@app.post(
|
|
|
|
"/sentence-transformer/encode-image",
|
|
|
|
response_model=EmbeddingResponse,
|
|
|
|
status_code=200,
|
2023-06-18 03:49:19 +00:00
|
|
|
dependencies=[Depends(dep_model_cache)],
|
2023-06-05 14:40:48 +00:00
|
|
|
)
|
2023-06-18 03:49:19 +00:00
|
|
|
async def clip_encode_image(
|
|
|
|
image: Image = Depends(dep_input_image)
|
|
|
|
) -> list[float]:
|
|
|
|
model = await _model_cache.get_cached_model(settings.clip_image_model, "clip")
|
2023-06-07 01:48:51 +00:00
|
|
|
embedding = model.encode(image).tolist()
|
|
|
|
return embedding
|
2023-02-18 15:13:37 +00:00
|
|
|
|
2023-04-26 10:39:24 +00:00
|
|
|
|
2023-06-05 14:40:48 +00:00
|
|
|
@app.post(
|
|
|
|
"/sentence-transformer/encode-text",
|
|
|
|
response_model=EmbeddingResponse,
|
|
|
|
status_code=200,
|
2023-06-18 03:49:19 +00:00
|
|
|
dependencies=[Depends(dep_model_cache)],
|
2023-06-05 14:40:48 +00:00
|
|
|
)
|
2023-06-18 03:49:19 +00:00
|
|
|
async def clip_encode_text(
|
|
|
|
payload: TextModelRequest
|
|
|
|
) -> list[float]:
|
|
|
|
model = await _model_cache.get_cached_model(settings.clip_text_model, "clip")
|
2023-06-07 01:48:51 +00:00
|
|
|
embedding = model.encode(payload.text).tolist()
|
|
|
|
return embedding
|
2023-04-26 10:39:24 +00:00
|
|
|
|
2023-02-18 15:13:37 +00:00
|
|
|
|
2023-06-05 14:40:48 +00:00
|
|
|
@app.post(
|
2023-06-18 03:49:19 +00:00
|
|
|
"/facial-recognition/detect-faces",
|
|
|
|
response_model=FaceResponse,
|
|
|
|
status_code=200,
|
|
|
|
dependencies=[Depends(dep_model_cache)],
|
2023-06-05 14:40:48 +00:00
|
|
|
)
|
2023-06-18 03:49:19 +00:00
|
|
|
async def facial_recognition(
|
|
|
|
image: bytes = Body(...),
|
|
|
|
) -> list[dict[str, Any]]:
|
2023-06-07 01:48:51 +00:00
|
|
|
model = await _model_cache.get_cached_model(
|
2023-06-18 03:49:19 +00:00
|
|
|
settings.facial_recognition_model, "facial-recognition"
|
2023-06-07 01:48:51 +00:00
|
|
|
)
|
2023-06-18 03:49:19 +00:00
|
|
|
faces = run_facial_recognition(model, image)
|
2023-06-07 01:48:51 +00:00
|
|
|
return faces
|
2023-06-03 02:42:47 +00:00
|
|
|
|
|
|
|
|
2023-02-18 15:13:37 +00:00
|
|
|
if __name__ == "__main__":
|
2023-05-20 03:37:01 +00:00
|
|
|
is_dev = os.getenv("NODE_ENV") == "development"
|
2023-06-18 03:49:19 +00:00
|
|
|
uvicorn.run(
|
|
|
|
"main:app",
|
|
|
|
host=settings.host,
|
|
|
|
port=settings.port,
|
|
|
|
reload=is_dev,
|
|
|
|
workers=settings.workers,
|
|
|
|
)
|