maba11's picture
Update app.py
66ff153 verified
Raw History Blame Contribute Delete
3.24 kB
# %% 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)
# %%