Prajwall11 commited on
Commit
a26f13d
·
verified ·
1 Parent(s): d0f0dbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -13
app.py CHANGED
@@ -1,29 +1,20 @@
1
  from PIL import Image
2
- from transformers import ViTFeatureExtractor, ViTForImageClassification
3
  import warnings
4
  import requests
5
  import gradio as gr
6
 
7
  warnings.filterwarnings('ignore')
8
 
9
- # Load the pre-trained Vision Transformer model and feature extractor
10
- model_name = "google/vit-base-patch16-224"
11
- feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
12
- model = ViTForImageClassification.from_pretrained(model_name)
13
-
14
  # API key for the nutrition information
15
  api_key = 'XshljGSwf/pq3GcgBdHtOg==G9X2wnPqW5c6vp0F'
16
 
17
  def identify_image(image_path):
18
  """Identify the food item in the image."""
 
 
19
  image = Image.open(image_path)
20
- inputs = feature_extractor(images=image, return_tensors="pt")
21
- outputs = model(**inputs)
22
- logits = outputs.logits
23
- predicted_class_idx = logits.argmax(-1).item()
24
- predicted_label = model.config.id2label[predicted_class_idx]
25
- food_name = predicted_label.split(',')[0]
26
- return food_name
27
 
28
  def get_calories(food_name):
29
  """Get the calorie information of the identified food item."""
 
1
  from PIL import Image
 
2
  import warnings
3
  import requests
4
  import gradio as gr
5
 
6
  warnings.filterwarnings('ignore')
7
 
 
 
 
 
 
8
  # API key for the nutrition information
9
  api_key = 'XshljGSwf/pq3GcgBdHtOg==G9X2wnPqW5c6vp0F'
10
 
11
  def identify_image(image_path):
12
  """Identify the food item in the image."""
13
+ API_URL = "https://api-inference.huggingface.co/models/nateraw/food"
14
+ headers = {"Authorization": huggingface_api}
15
  image = Image.open(image_path)
16
+ response = requests.post(API_URL, headers=headers, data=image)
17
+ return response.json()[0]['label']
 
 
 
 
 
18
 
19
  def get_calories(food_name):
20
  """Get the calorie information of the identified food item."""