File size: 799 Bytes
851fba8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Initialize the image classification pipeline
classifier = pipeline("image-classification")
# Alternatively you can define what model should the pipeline use, sometimes it requires that you login with your token
#classifier = pipeline("image-classification", model="microsoft/resnet-50")

#print(classifier.model)

def classify_image(image):
    results = classifier(image)
    # Get the top prediction
    top_result = results[0]
    label = top_result['label']
    score = top_result['score']
    return f"Label: {label}, Confidence: {score:.2f}"

iface = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil", label="Upload an Image"),
    outputs=gr.Textbox(label="Prediction"),
    title="Image Classifier"
)

iface.launch()