Update app.py
Browse files
app.py
CHANGED
|
@@ -3,10 +3,10 @@ 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")
|
| 7 |
-
clip_detector = pipeline(model="openai/clip-vit-large-patch14", task="zero-shot-image-classification")
|
| 8 |
|
| 9 |
-
# Food-
|
| 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",
|
|
@@ -19,17 +19,17 @@ food_labels = [
|
|
| 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}
|
| 23 |
-
vit_top_label = vit_results[0]['label']
|
| 24 |
-
vit_top_score = vit_results[0]['score'] * 100
|
| 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}
|
| 29 |
-
clip_top_label = clip_results[0]['label']
|
| 30 |
-
clip_top_score = clip_results[0]['score'] * 100
|
| 31 |
|
| 32 |
-
# Formatierte Zusammenfassung
|
| 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"
|
|
@@ -38,29 +38,30 @@ def classify_food(image):
|
|
| 38 |
|
| 39 |
return {"Trainiertes ViT-Modell": vit_output, "CLIP Zero-Shot": clip_output}, summary
|
| 40 |
|
| 41 |
-
# Beispielbilder
|
| 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 |
iface = gr.Interface(
|
| 52 |
fn=classify_food,
|
| 53 |
inputs=gr.Image(type="filepath"),
|
| 54 |
-
outputs=[
|
| 55 |
-
gr.JSON(label="🔬 Detaillierte Modell-Ausgaben"),
|
| 56 |
-
gr.Markdown(label="📝 Zusammenfassung")
|
| 57 |
-
],
|
| 58 |
title="🍔 KI-Modellvergleich zur Klassifikation von Gerichten 🍟",
|
| 59 |
-
description="Diese Anwendung vergleicht zwei KI-Modelle zur Klassifikation von Gerichten
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
examples=example_images
|
| 64 |
)
|
| 65 |
|
| 66 |
-
iface.launch(share=True)
|
|
|
|
| 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",
|
|
|
|
| 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"
|
|
|
|
| 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 globalen und 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)
|