from fastapi import FastAPI, UploadFile, HTTPException | |
from fastapi.responses import FileResponse | |
from fastapi.staticfiles import StaticFiles | |
from PIL import Image | |
from inference import make_inferentor | |
app = FastAPI() | |
inferentor = make_inferentor('resnet18-fruits.pt', 'classnames.json') | |
def helloworld(file: UploadFile): | |
print(f"filesize: {file.size}") | |
print(type(file)) | |
try: | |
im = Image.open(file.file) | |
if im.mode in ("RGBA", "P"): | |
im = im.convert("RGB") | |
return inferentor(im) | |
except Exception: | |
raise HTTPException(status_code=500, detail='Something went wrong') | |
finally: | |
file.file.close() | |
im.close() | |
# return 'hello world' | |
app.mount("/", StaticFiles(directory="static", html=True), name="static") | |
def index(): | |
return FileResponse(path="/static/index.html", media_type="text/html") | |