File size: 3,243 Bytes
e10b69d
 
 
93f5d72
e10b69d
4a6a535
 
 
 
e10b69d
3abda85
e10b69d
 
66ff153
 
 
e10b69d
 
 
 
a97f9e1
e10b69d
 
 
 
 
 
 
66ff153
 
 
e10b69d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebaf5f7
 
 
e10b69d
0a3d8f3
 
 
 
e10b69d
0a3d8f3
 
 
 
 
e10b69d
 
 
 
 
 
 
 
 
 
 
 
 
 
ebaf5f7
e10b69d
 
d2e925c
e10b69d
 
 
 
 
ebaf5f7
 
 
 
e10b69d
 
d2e925c
e10b69d
 
 
 
ebaf5f7
 
 
 
 
e10b69d
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# %% Imports
from gradio_client import Client, file
import gradio as gr
import os

from pillow_heif import register_heif_opener

register_heif_opener()

# %% Connect to private MaskClassifier and set up API calls to get predictions
client = Client("MA-maba/MaskClassifierPrivate", hf_token=os.environ.get("HF_TOKEN"))

def predict_single_out(input):  
    if input is None:
        return ''
        
    api_result = client.predict(
        img = file(input),
        api_name="/predict"
    )
    
    api_result = api_result['confidences']

    return ({api_result[i]['label']: 
        api_result[i]['confidence'] for i in range(len(api_result))})
    
    
def predict_multi_out(input):
    if input is None:
        return '','','',''
    
    api_result = client.predict(
        img = file(input),
        api_name="/predict_1"
    )

    mask = api_result[0]['confidences']
    manufacturer = api_result[1]['confidences']
    maskType = api_result[2]['confidences']
    ventedType = api_result[3]['confidences']

    return ({mask[i]['label']: 
        mask[i]['confidence'] for i in range(len(mask))},
        {manufacturer[i]['label']: 
        manufacturer[i]['confidence'] for i in range(len(manufacturer))}, 
        {maskType[i]['label']: 
        maskType[i]['confidence'] for i in range(len(maskType))}, 
        {ventedType[i]['label']: 
        ventedType[i]['confidence'] for i in range(len(ventedType))})

def identity_fn(img):
    return img

# %% Create the interface
image_single_out = gr.Image(type="filepath",
                            height=400,
                            sources=['upload', 'clipboard'],
                            label='Image')
label_singleOut = gr.components.Label(label='Mask')

image_multi_out = gr.Image(type="filepath",
                            height=400,
                            sources=['upload', 'clipboard'],
                            label='Image')
label_manufacturer = gr.components.Label(label='Manufacturer')
label_maskType = gr.components.Label(label='Mask Type')
label_ventedType = gr.components.Label(label='Ventilation Type')
label_mask = gr.components.Label(label='Mask')

examples = ["NoMask.jpg",
            "LenaVented.jpg",
            "LenaNonVented.jpg",
            "JuliaNasal.jpg",
            "Eson.jpg",
            "CaraNasal.jpg"]

title = "Mask Classifier"

# Single Out Interface
single_out = gr.Interface(
    fn=predict_single_out, 
    inputs=image_single_out, 
    outputs=label_singleOut, 
    examples=examples,
    title="Single Output Mask Classifier",
    live=True)

with single_out:
    image_single_out.upload(fn=identity_fn, inputs=image_single_out, outputs=image_single_out)

# Multi Out Interface
multi_out = gr.Interface(
    fn=predict_multi_out, 
    inputs=image_multi_out, 
    outputs=[label_mask, label_manufacturer, label_maskType, label_ventedType], 
    examples=examples,
    title="Multi Output Mask Classifier",
    live=True)  

with multi_out:
    image_multi_out.upload(fn=identity_fn, inputs=image_multi_out, outputs=image_multi_out)   

# Combined interface
intf = gr.TabbedInterface([single_out, multi_out], ["Single Output", "Multi Output"], title)

if __name__ == "__main__":
    intf.launch(inline=False)
# %%