Monyrak's picture
Update app.py
f35bda7 verified
raw
history blame
2.08 kB
import gradio as gr
from transformers import pipeline
import os
# Modelle laden
vit_classifier = pipeline("image-classification", model="kuhs/vit-base-oxford-iiit-pets")
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification")
# Label-Liste für das CLIP-Modell
labels_oxford_pets = [
'Siamese', 'Birman', 'shiba inu', 'staffordshire bull terrier', 'basset hound', 'Bombay', 'japanese chin',
'chihuahua', 'german shorthaired', 'pomeranian', 'beagle', 'english cocker spaniel', 'american pit bull terrier',
'Ragdoll', 'Persian', 'Egyptian Mau', 'miniature pinscher', 'Sphynx', 'Maine Coon', 'keeshond', 'yorkshire terrier',
'havanese', 'leonberger', 'wheaten terrier', 'american bulldog', 'english setter', 'boxer', 'newfoundland', 'Bengal',
'samoyed', 'British Shorthair', 'great pyrenees', 'Abyssinian', 'pug', 'saint bernard', 'Russian Blue', 'scottish terrier'
]
# Beispielbilder vorbereiten
example_images = []
example_dir = "example_images"
if os.path.exists(example_dir):
for img in os.listdir(example_dir):
if img.endswith(('.jpg', '.jpeg', '.png')):
example_images.append([os.path.join(example_dir, img)])
# Klassifikationsfunktion
def classify_pet(image):
vit_results = vit_classifier(image)
vit_output = {result['label']: result['score'] for result in vit_results}
clip_results = clip_detector(image, candidate_labels=labels_oxford_pets)
clip_output = {result['label']: result['score'] for result in clip_results}
return {
"ViT Classification": vit_output,
"CLIP Zero-Shot Classification": clip_output
}
# Gradio-Interface mit Beispielbildern
iface = gr.Interface(
fn=classify_pet,
inputs=gr.Image(type="filepath"),
outputs=gr.JSON(),
title="Pet Classification Comparison",
description="Upload an image of a pet, and compare results from a trained ViT model and a zero-shot CLIP model.",
examples=example_images if example_images else None,
cache_examples=False
)
# App starten
iface.launch()