Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +122 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from PIL import Image
|
3 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
4 |
+
import warnings
|
5 |
+
import requests
|
6 |
+
import gradio as gr
|
7 |
+
import logging
|
8 |
+
|
9 |
+
warnings.filterwarnings('ignore')
|
10 |
+
|
11 |
+
# Configure logging
|
12 |
+
logging.basicConfig(level=logging.INFO)
|
13 |
+
|
14 |
+
# Load the pre-trained Vision Transformer model and feature extractor
|
15 |
+
model_name = "google/vit-base-patch16-224"
|
16 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
17 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
18 |
+
|
19 |
+
# Load the API key from environment variables
|
20 |
+
api_key = os.getenv('NUTRITION_API_KEY')
|
21 |
+
|
22 |
+
if not api_key:
|
23 |
+
logging.error("API key for nutrition information is not set.")
|
24 |
+
raise ValueError("API key for nutrition information is not set. Please set the NUTRITION_API_KEY environment variable.")
|
25 |
+
|
26 |
+
def identify_image(image_path):
|
27 |
+
"""Identify the food item in the image."""
|
28 |
+
try:
|
29 |
+
image = Image.open(image_path)
|
30 |
+
except Exception as e:
|
31 |
+
logging.error(f"Failed to open image: {e}")
|
32 |
+
return "Invalid image"
|
33 |
+
|
34 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
35 |
+
outputs = model(**inputs)
|
36 |
+
logits = outputs.logits
|
37 |
+
predicted_class_idx = logits.argmax(-1).item()
|
38 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
39 |
+
food_name = predicted_label.split(',')[0]
|
40 |
+
logging.info(f"Identified food: {food_name}")
|
41 |
+
return food_name
|
42 |
+
|
43 |
+
def get_calories(food_name):
|
44 |
+
"""Get the calorie information of the identified food item."""
|
45 |
+
api_url = f'https://api.api-ninjas.com/v1/nutrition?query={food_name}'
|
46 |
+
try:
|
47 |
+
response = requests.get(api_url, headers={'X-Api-Key': api_key})
|
48 |
+
response.raise_for_status()
|
49 |
+
nutrition_info = response.json()
|
50 |
+
except requests.RequestException as e:
|
51 |
+
logging.error(f"API request failed: {e}")
|
52 |
+
nutrition_info = {"Error": response.status_code, "Message": str(e)}
|
53 |
+
return nutrition_info
|
54 |
+
|
55 |
+
def format_nutrition_info(nutrition_info):
|
56 |
+
"""Format the nutritional information into an HTML table."""
|
57 |
+
if "Error" in nutrition_info:
|
58 |
+
return f"Error: {nutrition_info['Error']} - {nutrition_info['Message']}"
|
59 |
+
|
60 |
+
if not nutrition_info:
|
61 |
+
return "No nutritional information found."
|
62 |
+
|
63 |
+
nutrition_data = nutrition_info[0]
|
64 |
+
table = f"""
|
65 |
+
<table border="1" style="width: 100%; border-collapse: collapse;">
|
66 |
+
<tr><th colspan="4" style="text-align: center;"><b>Nutrition Facts</b></th></tr>
|
67 |
+
<tr><td colspan="4" style="text-align: center;"><b>Food Name: {nutrition_data['name']}</b></td></tr>
|
68 |
+
<tr>
|
69 |
+
<td style="text-align: left;"><b>Calories</b></td><td style="text-align: right;">{nutrition_data['calories']}</td>
|
70 |
+
<td style="text-align: left;"><b>Serving Size (g)</b></td><td style="text-align: right;">{nutrition_data['serving_size_g']}</td>
|
71 |
+
</tr>
|
72 |
+
<tr>
|
73 |
+
<td style="text-align: left;"><b>Total Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_total_g']}</td>
|
74 |
+
<td style="text-align: left;"><b>Saturated Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_saturated_g']}</td>
|
75 |
+
</tr>
|
76 |
+
<tr>
|
77 |
+
<td style="text-align: left;"><b>Protein (g)</b></td><td style="text-align: right;">{nutrition_data['protein_g']}</td>
|
78 |
+
<td style="text-align: left;"><b>Sodium (mg)</b></td><td style="text-align: right;">{nutrition_data['sodium_mg']}</td>
|
79 |
+
</tr>
|
80 |
+
<tr>
|
81 |
+
<td style="text-align: left;"><b>Potassium (mg)</b></td><td style="text-align: right;">{nutrition_data['potassium_mg']}</td>
|
82 |
+
<td style="text-align: left;"><b>Cholesterol (mg)</b></td><td style="text-align: right;">{nutrition_data['cholesterol_mg']}</td>
|
83 |
+
</tr>
|
84 |
+
<tr>
|
85 |
+
<td style="text-align: left;"><b>Total Carbohydrates (g)</b></td><td style="text-align: right;">{nutrition_data['carbohydrates_total_g']}</td>
|
86 |
+
<td style="text-align: left;"><b>Fiber (g)</b></td><td style="text-align: right;">{nutrition_data['fiber_g']}</td>
|
87 |
+
</tr>
|
88 |
+
<tr>
|
89 |
+
<td style="text-align: left;"><b>Sugar (g)</b></td><td style="text-align: right;">{nutrition_data['sugar_g']}</td>
|
90 |
+
<td></td><td></td>
|
91 |
+
</tr>
|
92 |
+
</table>
|
93 |
+
"""
|
94 |
+
return table
|
95 |
+
|
96 |
+
def main_process(image_path):
|
97 |
+
"""Identify the food item and fetch its calorie information."""
|
98 |
+
food_name = identify_image(image_path)
|
99 |
+
if food_name == "Invalid image":
|
100 |
+
return food_name
|
101 |
+
nutrition_info = get_calories(food_name)
|
102 |
+
formatted_nutrition_info = format_nutrition_info(nutrition_info)
|
103 |
+
return formatted_nutrition_info
|
104 |
+
|
105 |
+
# Define the Gradio interface
|
106 |
+
def gradio_interface(image):
|
107 |
+
formatted_nutrition_info = main_process(image)
|
108 |
+
return formatted_nutrition_info
|
109 |
+
|
110 |
+
# Create the Gradio UI
|
111 |
+
iface = gr.Interface(
|
112 |
+
fn=gradio_interface,
|
113 |
+
inputs=gr.Image(type="filepath"),
|
114 |
+
outputs="html",
|
115 |
+
title="Food Identification and Nutrition Info",
|
116 |
+
description="Upload an image of food to get nutritional information.",
|
117 |
+
allow_flagging="never" # Disable flagging
|
118 |
+
)
|
119 |
+
|
120 |
+
# Launch the Gradio app
|
121 |
+
if __name__ == "__main__":
|
122 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio~=4.40.0
|
2 |
+
transformers~=4.44.0
|
3 |
+
torch~=2.4.0
|
4 |
+
torchvision~=0.19.0
|
5 |
+
requests~=2.32.3
|
6 |
+
python-dotenv~=1.0.1
|