-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
63 lines (47 loc) · 1.59 KB
/
server.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import FastAPI, UploadFile, Request
from pydantic_settings import BaseSettings
from typing import List, Optional
import uvicorn
import logging
from PIL import Image
import rawpy
import magic
from load_models import load_model
from contextlib import asynccontextmanager
class Settings(BaseSettings):
model_name: str = "ram_plus"
image_size: int = 384
threshold: float = 0.68
delete_tag_index: Optional[List[int]] = None
settings = Settings()
@asynccontextmanager
async def lifespan(app: FastAPI):
yield load_model(
settings.model_name,
settings.image_size,
settings.threshold,
settings.delete_tag_index
)
app = FastAPI(lifespan=lifespan)
@app.on_event("startup")
async def startup_event():
logger = logging.getLogger("uvicorn.access")
console_formatter = uvicorn.logging.ColourizedFormatter(
"{asctime} {levelprefix} : {message}",
style="{", use_colors=True)
logger.handlers[0].setFormatter(console_formatter)
@app.post("/")
async def handle(request: Request, file: UploadFile):
inference = request.state.inference
if is_raw_image(file):
# TODO: may not work for all TIFF files, but tested with Nikon (.nef) and Sony (.arw) files
rawImage = rawpy.imread(file.file)
rgb = rawImage.postprocess(use_camera_wb=True)
image = Image.fromarray(rgb)
else:
image = Image.open(file.file)
return inference(image)
def is_raw_image(file: UploadFile):
result = magic.from_buffer(file.file.read(2048), mime=True)
file.file.seek(0)
return result == "image/tiff"