File size: 1,836 Bytes
2746b21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import random

class_names = ['cat', 'dog']

def UpdateDropdown(className):
    class_names.append(className)
    return gr.Dropdown.update(choices=class_names)

def showPickedClass(className):
    return className

def image_classifier(inp):
    if inp is None:
        return {'cat': 0.3, 'dog': 0.7}

    num_class = len(class_names)

    # Generate random percentages between 0 and 1
    percentages = [random.random() for _ in range(num_class)]
    total = sum(percentages)

    # Normalize the percentages to ensure they sum up to 1
    normalized_percentages = [p / total for p in percentages]

    labeled_result = {name:score for name, score in zip(class_names, normalized_percentages)}

    return labeled_result, gr.Dropdown.update(choices=class_names)

demo = gr.Blocks()

with demo as app:
    with gr.Row():
        with gr.Column():
            inp_img = gr.Image()
            with gr.Row():
                clear_btn = gr.Button(value="Clear")
                process_btn = gr.Button(value="Process", variant="primary")
        with gr.Column():
            out_txt = gr.Label(label="Probabilities", num_top_classes=3)

    text_input = gr.Textbox(label="Input the new class here")
    b1 = gr.Button("Add new class")

    text_options = gr.Dropdown(class_names, label="Class Label", multiselect=False)
    b2 = gr.Button("Show me the picked class")
    picked_class = gr.Textbox()

    b1.click(UpdateDropdown, inputs=text_input, outputs=text_options)
    b2.click(showPickedClass, inputs=text_options, outputs=picked_class)

    process_btn.click(image_classifier, inputs=inp_img, outputs=[out_txt, text_options])
    clear_btn.click(lambda:(
        gr.update(value=None),
        gr.update(value=None)
        ),
        inputs=None,
        outputs=[inp_img, out_txt])


demo.launch(debug=True)