File size: 850 Bytes
5fbe75c
 
 
 
 
 
 
 
b8124d1
5fbe75c
 
 
 
 
 
 
 
 
 
 
b8124d1
5fbe75c
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import os

from PIL import Image
from transformers import pipeline

def predict_image(image):
    pipe = pipeline("image-classification", model="rizvandwiki/gender-classification")

    if isinstance(image, np.ndarray):
        image = Image.fromarray(image.astype('uint8'))
    elif isinstance(image, str): 
        image = Image.open(image)

    result = pipe(image)

    label = result[0]['label']
    confidence = result[0]['score']

    return label, confidence

example_images = [
    os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
]

interface = gr.Interface(
    fn=predict_image,
    inputs=gr.Image(type="numpy", label="Upload an Image"),
    outputs=[gr.Textbox(label="Prediction"), gr.Textbox(label="Confidence")],
    examples=example_images
)

interface.launch()