juned001 commited on
Commit
83b6204
·
verified ·
1 Parent(s): 18eff20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -6
app.py CHANGED
@@ -1,11 +1,9 @@
1
  import gradio as gr
2
  from transformers import AutoModelForImageClassification, AutoFeatureExtractor
3
- from PIL import Image
4
- import requests
5
  import torch
6
 
7
- # Hugging Face se model ka repo name
8
- model_name = "google/vit-base-patch16-224"
9
 
10
  # Model aur feature extractor load karo
11
  model = AutoModelForImageClassification.from_pretrained(model_name)
@@ -19,6 +17,11 @@ def classify_image(image):
19
  pred_idx = torch.argmax(probs)
20
  pred_label = model.config.id2label[pred_idx.item()]
21
  confidence = probs[0][pred_idx].item()
 
 
 
 
 
22
  return f"{pred_label} ({confidence*100:.2f}%)"
23
 
24
  # Gradio interface
@@ -26,8 +29,8 @@ demo = gr.Interface(
26
  fn=classify_image,
27
  inputs=gr.Image(type="pil"),
28
  outputs="text",
29
- title="Food Classifier",
30
- description="Upload any food image and get the prediction!"
31
  )
32
 
33
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoModelForImageClassification, AutoFeatureExtractor
 
 
3
  import torch
4
 
5
+ # Food-specific model
6
+ model_name = "nateraw/food101"
7
 
8
  # Model aur feature extractor load karo
9
  model = AutoModelForImageClassification.from_pretrained(model_name)
 
17
  pred_idx = torch.argmax(probs)
18
  pred_label = model.config.id2label[pred_idx.item()]
19
  confidence = probs[0][pred_idx].item()
20
+
21
+ # Agar confidence bahut low hai to reject kar do
22
+ if confidence < 0.30:
23
+ return "Not a food/vegetable"
24
+
25
  return f"{pred_label} ({confidence*100:.2f}%)"
26
 
27
  # Gradio interface
 
29
  fn=classify_image,
30
  inputs=gr.Image(type="pil"),
31
  outputs="text",
32
+ title="Food/Vegetable Classifier",
33
+ description="Upload any food or vegetable image. If it's not food, you'll get a 'Not a food/vegetable' message."
34
  )
35
 
36
  demo.launch()