app.py 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import gradio as gr
from predictor import Predictor

model_path = "paddlecv://models/vit/v2.4/imagenet2012-ViT-B_16-224_infer.pdmodel"
params_path = "paddlecv://models/vit/v2.4/imagenet2012-ViT-B_16-224_infer.pdiparams"
label_path = "paddlecv://dataset/imagenet2012_labels.txt"

predictor = None


def model_inference(image):
    global predictor
    if predictor is None:
        predictor = Predictor(
            model_path=model_path,
            params_path=params_path,
            label_path=label_path)
G
Guoxia Wang 已提交
18 19 20 21 22 23
    class_ids, scores, labels = predictor.predict(image)
    json_out = {
        "class_ids": class_ids.tolist(),
        "scores": scores.tolist(),
        "labels": labels.tolist()
    }
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    return image, json_out


def clear_all():
    return None, None, None


with gr.Blocks() as demo:
    gr.Markdown("Classification based on ViT")

    with gr.Column(scale=1, min_width=100):

        img_in = gr.Image(
            value="https://plsc.bj.bcebos.com/dataset/test_images/cat.jpg",
            label="Input")

        with gr.Row():
            btn1 = gr.Button("Clear")
            btn2 = gr.Button("Submit")

        img_out = gr.Image(label="Output")
        json_out = gr.JSON(label="jsonOutput")

    btn2.click(fn=model_inference, inputs=img_in, outputs=[img_out, json_out])
    btn1.click(fn=clear_all, inputs=None, outputs=[img_in, img_out, json_out])
    gr.Button.style(1)

demo.launch()