File size: 882 Bytes
7e35296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
from PIL import Image

# Load image classification pipeline
classifier = pipeline(
    task="image-classification",
    model="google/vit-base-patch16-224"
)

def classify_image(image):
    if image is None:
        return "No image provided."

    # Convert to PIL Image if needed
    if not isinstance(image, Image.Image):
        image = Image.fromarray(image)

    results = classifier(image)
    return {r["label"]: r["score"] for r in results}


# Gradio interface
app = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil", label="Upload an Animal Image"),
    outputs=gr.Label(label="Prediction"),
    title="Animal Image Classification",
    description="Upload an image of an animal and the model will predict what it is."
)

if __name__ == "__main__":
    app.launch()