File size: 984 Bytes
d06c1ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

import gradio as gr
from fastai.vision.all import *
from huggingface_hub import from_pretrained_fastai

EXAMPLES_PATH = Path("./examples")
repo_id = "hugginglearners/multi-object-classification"
learner = from_pretrained_fastai(repo_id)
labels = learner.dls.vocab


def infer(img):
    img = PILImage.create(img)
    _pred, _pred_w_idx, probs = learner.predict(img)
    return {labels[i]: float(probs[i]) for i, _ in enumerate(labels)}


examples = [str(path) for path in EXAMPLES_PATH.iterdir()] if EXAMPLES_PATH.exists() else None

demo = gr.Interface(
    infer,
    gr.Image(type="pil", height=192, width=192),
    gr.Label(num_top_classes=3),
    examples=examples,
    flagging_mode="never",
    title="Multilabel Image classification",
    description="Detect which type of object appears in the image.",
    article='Author: <a href="https://huggingface.co/geninhu">Nhu Hoang</a>.',
    live=False,
)
demo.queue().launch(debug=False, inbrowser=False)