chimithecat commited on
Commit
564e7fd
·
1 Parent(s): ca265db

added confidences

Browse files
Files changed (1) hide show
  1. app.py +21 -6
app.py CHANGED
@@ -3,22 +3,37 @@ from transformers import pipeline
3
  from PIL import Image
4
 
5
  # Load your model
6
- model = pipeline("image-classification", model="wellCh4n/tomato-leaf-disease-classification-vit")
 
7
 
8
  def classify(image):
9
  if not isinstance(image, Image.Image):
10
  image = Image.fromarray(image)
11
- result = model(image)
12
- return result[0]['label']
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Define the interface
15
  app = gr.Interface(
16
  fn=classify,
17
  inputs=gr.Image(type="pil"),
18
- outputs="label",
 
19
  title="Tomato Leaf Disease Analyzer",
20
  description="Upload a tomato leaf image to detect the disease."
21
  )
22
 
23
- # Disable SSR to enable API correctly
24
- app.launch(show_api=True)
 
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):
10
  if not isinstance(image, Image.Image):
11
  image = Image.fromarray(image)
12
+
13
+ # The model returns a list of dictionaries, e.g., [{'label': '...', 'score': ...}]
14
+ predictions = model(image)
15
+
16
+ # The gr.Label component and our Next.js client expect a 'confidences' key
17
+ # with a list of dicts that have a 'confidence' key (not 'score').
18
+ # We will reformat the output to match this.
19
+ confidences = [{'label': p['label'], 'confidence': p['score']} for p in predictions]
20
+
21
+ # Return a dictionary containing the top label and the full list of confidences.
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
+ # Use gr.Label() for rich output that can handle confidence scores
33
+ outputs=gr.Label(),
34
  title="Tomato Leaf Disease Analyzer",
35
  description="Upload a tomato leaf image to detect the disease."
36
  )
37
 
38
+ # Launch the app
39
+ app.launch(show_api=True)