Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import tempfile
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Lade des trainiertes ViT-Modell für die Food101-Klassifikation
|
| 7 |
+
vit_classifier = pipeline("image-classification", model="alimoh02/vit-base-food101")
|
| 8 |
+
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification")
|
| 9 |
+
|
| 10 |
+
# Labels aus Food101
|
| 11 |
+
labels_food101 = [
|
| 12 |
+
"apple_pie", "baby_back_ribs", "baklava", "beef_carpaccio", "beef_tartare", "beet_salad", "beignets",
|
| 13 |
+
"bibimbap", "bread_pudding", "breakfast_burrito", "bruschetta", "caesar_salad", "cannoli", "caprese_salad",
|
| 14 |
+
"carrot_cake", "ceviche", "cheesecake", "cheese_plate", "chicken_curry", "chicken_quesadilla",
|
| 15 |
+
"chicken_wings", "chocolate_cake", "chocolate_mousse", "churros", "clam_chowder", "club_sandwich",
|
| 16 |
+
"crab_cakes", "creme_brulee", "croque_madame", "cup_cakes", "deviled_eggs", "donuts", "dumplings",
|
| 17 |
+
"edamame", "eggs_benedict", "escargots", "falafel", "filet_mignon", "fish_and_chips", "foie_gras",
|
| 18 |
+
"french_fries", "french_onion_soup", "french_toast", "fried_calamari", "fried_rice", "frozen_yogurt",
|
| 19 |
+
"garlic_bread", "gnocchi", "greek_salad", "grilled_cheese_sandwich", "grilled_salmon", "guacamole",
|
| 20 |
+
"gyoza", "hamburger", "hot_and_sour_soup", "hot_dog", "huevos_rancheros", "hummus", "ice_cream",
|
| 21 |
+
"lasagna", "lobster_bisque", "lobster_roll_sandwich", "macaroni_and_cheese", "macarons", "miso_soup",
|
| 22 |
+
"mussels", "nachos", "omelette", "onion_rings", "oysters", "pad_thai", "paella", "pancakes",
|
| 23 |
+
"panna_cotta", "peking_duck", "pho", "pizza", "pork_chop", "poutine", "prime_rib", "pulled_pork_sandwich",
|
| 24 |
+
"ramen", "ravioli", "red_velvet_cake", "risotto", "samosa", "sashimi", "scallops", "seaweed_salad",
|
| 25 |
+
"shrimp_and_grits", "spaghetti_bolognese", "spaghetti_carbonara", "spring_rolls", "steak",
|
| 26 |
+
"strawberry_shortcake", "sushi", "tacos", "takoyaki", "tiramisu", "tuna_tartare", "waffles"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# Klassifikationsfunktion
|
| 30 |
+
def classify_food(image: Image.Image):
|
| 31 |
+
vit_results = vit_classifier(image)
|
| 32 |
+
|
| 33 |
+
vit_output = {}
|
| 34 |
+
for result in vit_results:
|
| 35 |
+
try:
|
| 36 |
+
label_index = int(result['label'])
|
| 37 |
+
label_name = labels_food101[label_index]
|
| 38 |
+
except:
|
| 39 |
+
label_name = str(result['label']) # fallback
|
| 40 |
+
vit_output[label_name] = round(result['score'], 4)
|
| 41 |
+
|
| 42 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 43 |
+
image.save(tmp.name)
|
| 44 |
+
clip_results = clip_detector(tmp.name, candidate_labels=labels_food101)
|
| 45 |
+
clip_output = {str(result['label']): round(result['score'], 4) for result in clip_results}
|
| 46 |
+
|
| 47 |
+
return {
|
| 48 |
+
"ViT Classification": vit_output,
|
| 49 |
+
"CLIP Zero-Shot Classification": clip_output
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# Beispielbilder
|
| 53 |
+
example_images = [
|
| 54 |
+
["Cheeseburger.jpg"],
|
| 55 |
+
["Sushi.jpg"],
|
| 56 |
+
["Brownie.jpg"],
|
| 57 |
+
["Tiramisu.jpg"],
|
| 58 |
+
["Guacamole.jpg"],
|
| 59 |
+
["Samosa.jpg"],
|
| 60 |
+
["Oysters.jpg"]
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
# UI mit Gradio
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=classify_food,
|
| 66 |
+
inputs=gr.Image(type="pil", label="Upload a food image"),
|
| 67 |
+
outputs=gr.JSON(),
|
| 68 |
+
title="Food Image Classification Comparison",
|
| 69 |
+
description="Vergleiche ein trainiertes ViT-Modell (Food101) mit einem CLIP Zero-Shot-Modell.",
|
| 70 |
+
cache_examples=False,
|
| 71 |
+
examples=example_images
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
iface.launch()
|