Spaces:
Sleeping
Sleeping
Commit ·
ceaacb0
1
Parent(s): 564e7fd
added confidencesDDD
Browse files
app.py
CHANGED
|
@@ -2,8 +2,7 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
-
# Load your model
|
| 6 |
-
# You can add top_k=10 to get all class probabilities
|
| 7 |
model = pipeline("image-classification", model="wellCh4n/tomato-leaf-disease-classification-vit", top_k=10)
|
| 8 |
|
| 9 |
def classify(image):
|
|
@@ -13,23 +12,18 @@ def classify(image):
|
|
| 13 |
# The model returns a list of dictionaries, e.g., [{'label': '...', 'score': ...}]
|
| 14 |
predictions = model(image)
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
#
|
| 18 |
-
#
|
| 19 |
-
confidences =
|
| 20 |
|
| 21 |
-
|
| 22 |
-
# This is the format our Next.js API is expecting.
|
| 23 |
-
return {
|
| 24 |
-
'label': predictions[0]['label'],
|
| 25 |
-
'confidences': confidences
|
| 26 |
-
}
|
| 27 |
|
| 28 |
# Define the interface
|
| 29 |
app = gr.Interface(
|
| 30 |
fn=classify,
|
| 31 |
inputs=gr.Image(type="pil"),
|
| 32 |
-
#
|
| 33 |
outputs=gr.Label(),
|
| 34 |
title="Tomato Leaf Disease Analyzer",
|
| 35 |
description="Upload a tomato leaf image to detect the disease."
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
|
| 5 |
+
# Load your model and get all class results
|
|
|
|
| 6 |
model = pipeline("image-classification", model="wellCh4n/tomato-leaf-disease-classification-vit", top_k=10)
|
| 7 |
|
| 8 |
def classify(image):
|
|
|
|
| 12 |
# The model returns a list of dictionaries, e.g., [{'label': '...', 'score': ...}]
|
| 13 |
predictions = model(image)
|
| 14 |
|
| 15 |
+
# --- THE FIX IS HERE ---
|
| 16 |
+
# Convert the list of dicts into a single dict that gr.Label expects:
|
| 17 |
+
# {'label_string': score_float, ...}
|
| 18 |
+
confidences = {p['label']: p['score'] for p in predictions}
|
| 19 |
|
| 20 |
+
return confidences
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Define the interface
|
| 23 |
app = gr.Interface(
|
| 24 |
fn=classify,
|
| 25 |
inputs=gr.Image(type="pil"),
|
| 26 |
+
# gr.Label() can now correctly process the new output format
|
| 27 |
outputs=gr.Label(),
|
| 28 |
title="Tomato Leaf Disease Analyzer",
|
| 29 |
description="Upload a tomato leaf image to detect the disease."
|