Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files- .gitattributes +1 -0
- app.py +67 -0
- example_images/.DS_Store +0 -0
- example_images/burger.jpg +3 -0
- example_images/donut.jpg +0 -0
- example_images/fries.jpg +0 -0
- example_images/pizza.jpg +0 -0
- example_images/sushi.jpg +0 -0
- requirements.txt +2 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
example_images/burger.jpg filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#! pip install gradio
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Lade beide Modelle
|
| 6 |
+
vit_classifier = pipeline("image-classification", model="chrisis2/vit-food-classification-chrisis2") # ViT: speziell traineirtes Modell für Lebensmittelklassifikation
|
| 7 |
+
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification") #CLIP: Allgmeines Zero-Shot-Modell das Bilde rmit Textbeschreibungen vergleicht
|
| 8 |
+
|
| 9 |
+
# Liste aller Food-Labes für Zero-Shot Modell
|
| 10 |
+
food_labels = [
|
| 11 |
+
"Baked Potato", "Crispy Chicken", "Donut", "Fries", "Hot Dog", "Sandwich", "Taco", "Taquito",
|
| 12 |
+
"apple_pie", "burger", "butter_naan", "chai", "chapati", "cheesecake", "chicken_curry",
|
| 13 |
+
"chole_bhature", "dal_makhani", "dhokla", "fried_rice", "ice_cream", "idli", "jalebi",
|
| 14 |
+
"kaathi_rolls", "kadai_paneer", "kulfi", "masala_dosa", "momos", "omelette", "paani_puri",
|
| 15 |
+
"pakode", "pav_bhaji", "pizza", "samosa", "sushi"
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
# Klassifikationsfunktion
|
| 19 |
+
def classify_food(image):
|
| 20 |
+
# Klassifikation mit ViT
|
| 21 |
+
vit_results = vit_classifier(image)
|
| 22 |
+
vit_output = {r['label']: r['score'] for r in vit_results} # Ergebnisse als Dictionary
|
| 23 |
+
vit_top_label = vit_results[0]['label'] #beste Vrohersage
|
| 24 |
+
vit_top_score = vit_results[0]['score'] * 100 # zu % umwandeln
|
| 25 |
+
|
| 26 |
+
# Klassifikation mit CLIP
|
| 27 |
+
clip_results = clip_detector(image, candidate_labels=food_labels)
|
| 28 |
+
clip_output = {r['label']: r['score'] for r in clip_results} # Ergebnisse als Dictionary
|
| 29 |
+
clip_top_label = clip_results[0]['label'] #beste Vrohersage
|
| 30 |
+
clip_top_score = clip_results[0]['score'] * 100 # zu % umwandeln
|
| 31 |
+
|
| 32 |
+
# Formatierte Zusammenfassung für Anzeige
|
| 33 |
+
summary = (
|
| 34 |
+
"### Höchste Vorhersagewahrscheinlichkeit pro Modell:\n\n"
|
| 35 |
+
f"**Trainiertes ViT-Modell**: erkennt **{vit_top_label}** mit **{vit_top_score:.3f}%** Wahrscheinlichkeit.\n\n"
|
| 36 |
+
f"**CLIP Zero-Shot-Modell**: erkennt **{clip_top_label}** mit **{clip_top_score:.3f}%** Wahrscheinlichkeit."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
return {"Trainiertes ViT-Modell": vit_output, "CLIP Zero-Shot": clip_output}, summary
|
| 40 |
+
|
| 41 |
+
# Beispielbilder für Anzeige
|
| 42 |
+
example_images = [
|
| 43 |
+
["example_images/burger.jpg"],
|
| 44 |
+
["example_images/donut.jpg"],
|
| 45 |
+
["example_images/fries.jpg"],
|
| 46 |
+
["example_images/pizza.jpg"],
|
| 47 |
+
["example_images/sushi.jpg"],
|
| 48 |
+
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
# Erstellung der Gradio-Oberfläche
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=classify_food,
|
| 54 |
+
inputs=gr.Image(type="filepath"),
|
| 55 |
+
outputs=[gr.JSON(label="Detaillierte Modell-Ausgaben"), gr.Markdown(label="Fazit der besten Vorhersagen")],
|
| 56 |
+
title="🍔 KI-Modellvergleich zur Klassifikation von Gerichten 🍟",
|
| 57 |
+
description="""Diese Anwendung vergleicht zwei KI-Modelle zur Klassifikation von Gerichten einschliesslich der westlichen und vor allem indischen Küche:
|
| 58 |
+
|
| 59 |
+
Modell 1: Ein trainiertes ViT-Modell: Wurde speziell mit einem Datensatz aus Food-Bildern trainiert und erkennt häufige Speisen besonders zuverlässig.
|
| 60 |
+
|
| 61 |
+
Modell 2: Ein CLIP Zero-Shot-Modell: Wurde nicht speziell für Food trainiert. Es nutzt Sprachverständnis, um das Bild mit Text-Labels zu vergleichen.
|
| 62 |
+
|
| 63 |
+
📸 Lade ein Bild eines Gerichts hoch (z. B. Pizza, Sushi oder Donut) und sieh dir an, wie beide Modelle es erkennen und ob sie sich einig sind!""",
|
| 64 |
+
examples=example_images
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
iface.launch(share=True)
|
example_images/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
example_images/burger.jpg
ADDED
|
Git LFS Details
|
example_images/donut.jpg
ADDED
|
example_images/fries.jpg
ADDED
|
example_images/pizza.jpg
ADDED
|
example_images/sushi.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|