File size: 701 Bytes
5ef6130
0b16e31
5ef6130
0b16e31
 
 
 
 
 
 
 
5ef6130
0b16e31
5ef6130
 
0b16e31
 
 
 
5ef6130
 
0b16e31
5ef6130
 
 
0b16e31
 
5ef6130
 
 
 
0b16e31
5ef6130
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
from fastapi import FastAPI, File
from fastai.vision.all import *
from typing_extensions import Annotated
from huggingface_hub import hf_hub_download
import __main__

app = FastAPI()

REPO_ID = "jsagl/test-fastai"
FILENAME = "model.pkl"


def is_cat(x): return x[0].isupper()


__main__.is_cat = is_cat

learn = load_learner(hf_hub_download(repo_id=REPO_ID, filename=FILENAME))

categories = ('Dog', 'Cat')


def classify_images(img):
    pred, idx, probs = learn.predict(img)
    return dict(zip(categories, map(float, probs)))


@app.post("/classify")
def classify_image(file: Annotated[bytes, File()]):
    img = PILImage.create(file)
    img.thumbnail((192, 192))

    return classify_images(img)