Abhithebob commited on
Commit
ed3e754
1 Parent(s): 766e72b

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +103 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 'wWHUTkkQ+/0QdXfCSKE9jA==bRif7gdSnEdaq4j9'
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."""
30
+ api_url = 'https://api.api-ninjas.com/v1/nutrition?query={}'.format(food_name)
31
+ response = requests.get(api_url, headers={'X-Api-Key': api_key})
32
+ if response.status_code == requests.codes.ok:
33
+ nutrition_info = response.json()
34
+ else:
35
+ nutrition_info = {"Error": response.status_code, "Message": response.text}
36
+ return nutrition_info
37
+
38
+ def format_nutrition_info(nutrition_info):
39
+ """Format the nutritional information into an HTML table."""
40
+ if "Error" in nutrition_info:
41
+ return f"Error: {nutrition_info['Error']} - {nutrition_info['Message']}"
42
+
43
+ if len(nutrition_info) == 0:
44
+ return "No nutritional information found."
45
+
46
+ nutrition_data = nutrition_info[0]
47
+ table = f"""
48
+ <table border="1" style="width: 100%; border-collapse: collapse;">
49
+ <tr><th colspan="4" style="text-align: center;"><b>Nutrition Facts</b></th></tr>
50
+ <tr><td colspan="4" style="text-align: center;"><b>Food Name: {nutrition_data['name']}</b></td></tr>
51
+ <tr>
52
+ <td style="text-align: left;"><b>Calories</b></td><td style="text-align: right;">{nutrition_data['calories']}</td>
53
+ <td style="text-align: left;"><b>Serving Size (g)</b></td><td style="text-align: right;">{nutrition_data['serving_size_g']}</td>
54
+ </tr>
55
+ <tr>
56
+ <td style="text-align: left;"><b>Total Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_total_g']}</td>
57
+ <td style="text-align: left;"><b>Saturated Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_saturated_g']}</td>
58
+ </tr>
59
+ <tr>
60
+ <td style="text-align: left;"><b>Protein (g)</b></td><td style="text-align: right;">{nutrition_data['protein_g']}</td>
61
+ <td style="text-align: left;"><b>Sodium (mg)</b></td><td style="text-align: right;">{nutrition_data['sodium_mg']}</td>
62
+ </tr>
63
+ <tr>
64
+ <td style="text-align: left;"><b>Potassium (mg)</b></td><td style="text-align: right;">{nutrition_data['potassium_mg']}</td>
65
+ <td style="text-align: left;"><b>Cholesterol (mg)</b></td><td style="text-align: right;">{nutrition_data['cholesterol_mg']}</td>
66
+ </tr>
67
+ <tr>
68
+ <td style="text-align: left;"><b>Total Carbohydrates (g)</b></td><td style="text-align: right;">{nutrition_data['carbohydrates_total_g']}</td>
69
+ <td style="text-align: left;"><b>Fiber (g)</b></td><td style="text-align: right;">{nutrition_data['fiber_g']}</td>
70
+ </tr>
71
+ <tr>
72
+ <td style="text-align: left;"><b>Sugar (g)</b></td><td style="text-align: right;">{nutrition_data['sugar_g']}</td>
73
+ <td></td><td></td>
74
+ </tr>
75
+ </table>
76
+ """
77
+ return table
78
+
79
+ def main_process(image_path):
80
+ """Identify the food item and fetch its calorie information."""
81
+ food_name = identify_image(image_path)
82
+ nutrition_info = get_calories(food_name)
83
+ formatted_nutrition_info = format_nutrition_info(nutrition_info)
84
+ return formatted_nutrition_info
85
+
86
+ # Define the Gradio interface
87
+ def gradio_interface(image):
88
+ formatted_nutrition_info = main_process(image)
89
+ return formatted_nutrition_info
90
+
91
+ # Create the Gradio UI
92
+ iface = gr.Interface(
93
+ fn=gradio_interface,
94
+ inputs=gr.Image(type="filepath"),
95
+ outputs="html",
96
+ title="Food Identification and Nutrition Info",
97
+ description="Upload an image of food to get nutritional information.",
98
+ allow_flagging="never" # Disable flagging
99
+ )
100
+
101
+ # Launch the Gradio app
102
+ if __name__ == "__main__":
103
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ torchvision
5
+ requests
6
+ python-dotenv