File size: 2,638 Bytes
234009d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import gradio as gr
from utils import load_specific_model, inference
import markdown

current_model = None  # Initialize the current model as None

# Define a set of example images
example_images = [
    ("Beispielbild Glas", "src/examples/Glas.jpg"),
    ("Beispielbild Organic", "src/examples/Organic.jpg"),
    ("Beispielbild Papier", "src/examples/Papier.jpg"),
    ("Beispielbild Restmüll", "src/examples/Restmuell.jpg"),
    ("Beispielbild Wertstoff", "src/examples/Wertstoff.jpg")
]

def load_model(model_name):
    global current_model
    if model_name is None:
        raise gr.Error("No model selected!")
    if current_model is not None:
        current_model = None

    current_model = load_specific_model(model_name)
    current_model.eval()

def predict(inp):
    global current_model
    if current_model is None:
        raise gr.Error("No model loaded!")

    confidences = inference(current_model, inp)
    return confidences

with gr.Blocks() as demo:
    with open('src/app_template.md', 'r') as f:
        markdown_string = f.read()
    header = gr.Markdown(markdown_string)
    
    with gr.Row(variant="panel", equal_height=True):

        user_image = gr.Image(
            type="pil",
            label="Upload Your Own Image",
            info="You can also upload your own image for prediction.",
            scale=2,
            height=350,
        )
        
        with gr.Column():
            output = gr.Label(
                num_top_classes=3,
                label="Output",
                info="Top three predicted classes and their confidences.",
                scale=2,
            )

            model_dropdown = gr.Dropdown(
                ["EfficientNet-B3", "EfficientNet-B4", "vgg19", "resnet50", "dinov2_vits14"],
                label="Model",
                info="Select a model to use.",
                scale=1,
            )
            model_dropdown.change(load_model, model_dropdown, show_progress=True, queue=True)
            predict_button = gr.Button(label="Predict", info="Click to make a prediction.", scale=1)
            predict_button.click(fn=predict, inputs=user_image, outputs=output, queue=True)

    gr.Markdown("## Example Images")
    gr.Markdown("You can just drag and drop these images into the image uploader above!")
    
    with gr.Row():
        for name, image_path in example_images:
            example_image = gr.Image(
                value=image_path,
                label=name,
                type="pil",
                height=220,
                interactive=False,
            )

if __name__ == "__main__":
    demo.queue()
    demo.launch()