File size: 1,558 Bytes
e1d23e4
 
 
 
 
 
94cce00
e1d23e4
 
 
 
 
 
 
 
 
 
9bde03d
e1d23e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import math
import numpy as np
import pandas as pd

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


def get_x(x):
    return pascal_source/"train"/f'{x[0]}'

def get_y(x):
    return x[1].split(' ')

pascal_source = '.'
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)
    # gradio doesn't support tensors, so converting to float
    labels_probs = {labels[i]: float(probs[i]) for i, _ in enumerate(labels)}
    return labels_probs
    # return f"This grapevine leave is {_pred} with {100*probs[torch.argmax(probs)].item():.2f}% probability"

# get the inputs
inputs = gr.inputs.Image(shape=(192, 192))

# the app outputs two segmented images
output = gr.outputs.Label(num_top_classes=3)
# it's good practice to pass examples, description and a title to guide users
title = 'Multilabel Image classification'
description = 'Detect which type of object appearing in the image'
article = "Author: <a href=\"https://huggingface.co/geninhu\">Nhu Hoang</a>. "
examples = [f'{EXAMPLES_PATH}/{f.name}' for f in EXAMPLES_PATH.iterdir()]

gr.Interface(infer, inputs, output, examples= examples, allow_flagging='never',
             title=title, description=description, article=article, live=False).launch(enable_queue=True, debug=False, inbrowser=False)