File size: 1,944 Bytes
62106e2
 
84adb96
 
62106e2
 
 
 
 
 
8ebcdc8
 
62106e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ebcdc8
 
62106e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
import gradio as gr
from transformers import pipeline
import tensorflow
import torch

global pipeline
global default_model_name
default_model_name = "google/vit-base-patch16-224"

def predict(image, model_name):
    if model_name == "":
        model_name = default_model_name
    pipe = pipeline(task="image-classification", model=model_name)
    predictions = pipe(image)
    return {p["label"]: p["score"] for p in predictions}
    
    
with gr.Blocks() as demo:
    
    with gr.Row():
        gr.Markdown(
        """
        # Settings
        [Here](https://huggingface.co/models?pipeline_tag=image-classification&sort=downloads) are some popular image classification models.  
        Or use default model **"google/vit-base-patch16-224"**
        """)
        gr.Markdown(
        """
        # Image Classifier Result
        """)
    with gr.Row():
        with gr.Column(scale=1):
            #input_model = gr.Textbox(label="Enter a custom model name:", value=default_model_name, scale=1)
            input_model = gr.Textbox(label="Enter a custom model name:", scale=1)
                
            gr.Markdown("Upload image")
            #images_input = gr.File(file_count="multiple", file_types=["image"], label="Input images", scale=1)
            #images_input = gr.Files(file_count="multiple", file_types=["image"], label="Input images", scale=1)
            input_image = gr.Image(label="Input Image", type="filepath")

        #output = gr.Label(label="Output", num_top_classes=3, scale=2)
        output = gr.Label(num_top_classes=10, scale=2)
    with gr.Row(equal_height=True):
        clear_button = gr.ClearButton(value="Clear", scale=0)
        submit_button = gr.Button(value="Submit", variant="primary", scale=0)
    submit_button.click(fn=predict, inputs=[input_image, input_model], outputs=output)
    clear_button.click(lambda: [None, None, None], outputs=[input_model, input_image, output])
    
demo.launch()