# AUTOGENERATED! DO NOT EDIT! File to edit: 02_spider_deploy.ipynb.

# %% auto 0
__all__ = ['repo_id', 'learn', 'path', 'categories', 'image', 'output', 'examples', 'intf', 'second', 'classify_image']

# %% 02_spider_deploy.ipynb 1
from fastai.vision.all import *
from huggingface_hub import from_pretrained_fastai
import gradio as gr

# %% 02_spider_deploy.ipynb 2
repo_id = "xt0r3/spider-classifier"
learn = from_pretrained_fastai(repo_id)

# %% 02_spider_deploy.ipynb 3
path = Path("data/spiders")
categories = learn.dls.vocab

# %% 02_spider_deploy.ipynb 4
def second(x):
    a, b = x
    return b

def classify_image(img):
    image = Image.fromarray(img)
    image.thumbnail(size=(224,224))
    img = np.asarray(img)
    ans,idx,preds = learn.predict(img)
    results = zip(categories, map(float, preds))
    results = sorted(results, key=second, reverse=True)
    return dict(results[:10])

# %% 02_spider_deploy.ipynb 6
image = gr.inputs.Image()
output = gr.outputs.Label()
examples = ["data/spider1.jpg", "data/spider2.jpg"]

intf = gr.Interface(fn=classify_image, inputs=image, outputs=output, examples=examples)
intf.launch(inline=False)