Spaces:
Runtime error
Runtime error
add: basic template
Browse files- app.py +109 -0
- requirements.txt +61 -0
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
3 |
+
import warnings
|
4 |
+
import requests
|
5 |
+
import gradio as gr
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
import os
|
8 |
+
|
9 |
+
load_dotenv() # Load .env file
|
10 |
+
# API key for the nutrition information
|
11 |
+
api_key =os.getenv("API_KEY")
|
12 |
+
|
13 |
+
|
14 |
+
warnings.filterwarnings('ignore')
|
15 |
+
|
16 |
+
# Load the pre-trained Vision Transformer model and feature extractor
|
17 |
+
model_name = "google/vit-base-patch16-224"
|
18 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
19 |
+
model = ViTForImageClassification.from_pretrained(model_name)
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
def identify_image(image_path):
|
24 |
+
"""Identify the food item in the image."""
|
25 |
+
image = Image.open(image_path)
|
26 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
27 |
+
outputs = model(**inputs)
|
28 |
+
logits = outputs.logits
|
29 |
+
predicted_class_idx = logits.argmax(-1).item()
|
30 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
31 |
+
food_name = predicted_label.split(',')[0]
|
32 |
+
return food_name
|
33 |
+
|
34 |
+
def get_calories(food_name):
|
35 |
+
"""Get the calorie information of the identified food item."""
|
36 |
+
api_url = 'https://api.api-ninjas.com/v1/nutrition?query={}'.format(food_name)
|
37 |
+
response = requests.get(api_url, headers={'X-Api-Key': api_key})
|
38 |
+
if response.status_code == requests.codes.ok:
|
39 |
+
nutrition_info = response.json()
|
40 |
+
else:
|
41 |
+
nutrition_info = {"Error": response.status_code, "Message": response.text}
|
42 |
+
return nutrition_info
|
43 |
+
|
44 |
+
def format_nutrition_info(nutrition_info):
|
45 |
+
"""Format the nutritional information into an HTML table."""
|
46 |
+
if "Error" in nutrition_info:
|
47 |
+
return f"Error: {nutrition_info['Error']} - {nutrition_info['Message']}"
|
48 |
+
|
49 |
+
if len(nutrition_info) == 0:
|
50 |
+
return "No nutritional information found."
|
51 |
+
|
52 |
+
nutrition_data = nutrition_info[0]
|
53 |
+
table = f"""
|
54 |
+
<table border="1" style="width: 100%; border-collapse: collapse;">
|
55 |
+
<tr><th colspan="4" style="text-align: center;"><b>Nutrition Facts</b></th></tr>
|
56 |
+
<tr><td colspan="4" style="text-align: center;"><b>Food Name: {nutrition_data['name']}</b></td></tr>
|
57 |
+
<tr>
|
58 |
+
<td style="text-align: left;"><b>Calories</b></td><td style="text-align: right;">{nutrition_data['calories']}</td>
|
59 |
+
<td style="text-align: left;"><b>Serving Size (g)</b></td><td style="text-align: right;">{nutrition_data['serving_size_g']}</td>
|
60 |
+
</tr>
|
61 |
+
<tr>
|
62 |
+
<td style="text-align: left;"><b>Total Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_total_g']}</td>
|
63 |
+
<td style="text-align: left;"><b>Saturated Fat (g)</b></td><td style="text-align: right;">{nutrition_data['fat_saturated_g']}</td>
|
64 |
+
</tr>
|
65 |
+
<tr>
|
66 |
+
<td style="text-align: left;"><b>Protein (g)</b></td><td style="text-align: right;">{nutrition_data['protein_g']}</td>
|
67 |
+
<td style="text-align: left;"><b>Sodium (mg)</b></td><td style="text-align: right;">{nutrition_data['sodium_mg']}</td>
|
68 |
+
</tr>
|
69 |
+
<tr>
|
70 |
+
<td style="text-align: left;"><b>Potassium (mg)</b></td><td style="text-align: right;">{nutrition_data['potassium_mg']}</td>
|
71 |
+
<td style="text-align: left;"><b>Cholesterol (mg)</b></td><td style="text-align: right;">{nutrition_data['cholesterol_mg']}</td>
|
72 |
+
</tr>
|
73 |
+
<tr>
|
74 |
+
<td style="text-align: left;"><b>Total Carbohydrates (g)</b></td><td style="text-align: right;">{nutrition_data['carbohydrates_total_g']}</td>
|
75 |
+
<td style="text-align: left;"><b>Fiber (g)</b></td><td style="text-align: right;">{nutrition_data['fiber_g']}</td>
|
76 |
+
</tr>
|
77 |
+
<tr>
|
78 |
+
<td style="text-align: left;"><b>Sugar (g)</b></td><td style="text-align: right;">{nutrition_data['sugar_g']}</td>
|
79 |
+
<td></td><td></td>
|
80 |
+
</tr>
|
81 |
+
</table>
|
82 |
+
"""
|
83 |
+
return table
|
84 |
+
|
85 |
+
def main_process(image_path):
|
86 |
+
"""Identify the food item and fetch its calorie information."""
|
87 |
+
food_name = identify_image(image_path)
|
88 |
+
nutrition_info = get_calories(food_name)
|
89 |
+
formatted_nutrition_info = format_nutrition_info(nutrition_info)
|
90 |
+
return formatted_nutrition_info
|
91 |
+
|
92 |
+
# Define the Gradio interface
|
93 |
+
def gradio_interface(image):
|
94 |
+
formatted_nutrition_info = main_process(image)
|
95 |
+
return formatted_nutrition_info
|
96 |
+
|
97 |
+
# Create the Gradio UI
|
98 |
+
iface = gr.Interface(
|
99 |
+
fn=gradio_interface,
|
100 |
+
inputs=gr.Image(type="filepath"),
|
101 |
+
outputs="html",
|
102 |
+
title="Food Identification and Nutrition Info",
|
103 |
+
description="Upload an image of food to get nutritional information.",
|
104 |
+
allow_flagging="never" # Disable flagging
|
105 |
+
)
|
106 |
+
|
107 |
+
# Launch the Gradio app
|
108 |
+
if __name__ == "__main__":
|
109 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
annotated-types==0.7.0
|
3 |
+
anyio==4.6.2.post1
|
4 |
+
certifi==2024.8.30
|
5 |
+
charset-normalizer==3.4.0
|
6 |
+
click==8.1.7
|
7 |
+
colorama==0.4.6
|
8 |
+
fastapi==0.115.5
|
9 |
+
ffmpy==0.4.0
|
10 |
+
filelock==3.16.1
|
11 |
+
fsspec==2024.10.0
|
12 |
+
gradio==5.6.0
|
13 |
+
gradio_client==1.4.3
|
14 |
+
h11==0.14.0
|
15 |
+
httpcore==1.0.7
|
16 |
+
httpx==0.27.2
|
17 |
+
huggingface-hub==0.26.2
|
18 |
+
idna==3.10
|
19 |
+
Jinja2==3.1.4
|
20 |
+
markdown-it-py==3.0.0
|
21 |
+
MarkupSafe==2.1.5
|
22 |
+
mdurl==0.1.2
|
23 |
+
mpmath==1.3.0
|
24 |
+
networkx==3.4.2
|
25 |
+
numpy==2.1.3
|
26 |
+
orjson==3.10.12
|
27 |
+
packaging==24.2
|
28 |
+
pandas==2.2.3
|
29 |
+
pillow==11.0.0
|
30 |
+
pydantic==2.10.1
|
31 |
+
pydantic_core==2.27.1
|
32 |
+
pydub==0.25.1
|
33 |
+
Pygments==2.18.0
|
34 |
+
python-dateutil==2.9.0.post0
|
35 |
+
python-multipart==0.0.12
|
36 |
+
pytz==2024.2
|
37 |
+
PyYAML==6.0.2
|
38 |
+
regex==2024.11.6
|
39 |
+
requests==2.32.3
|
40 |
+
rich==13.9.4
|
41 |
+
ruff==0.8.0
|
42 |
+
safehttpx==0.1.1
|
43 |
+
safetensors==0.4.5
|
44 |
+
semantic-version==2.10.0
|
45 |
+
shellingham==1.5.4
|
46 |
+
six==1.16.0
|
47 |
+
sniffio==1.3.1
|
48 |
+
starlette==0.41.3
|
49 |
+
sympy==1.13.1
|
50 |
+
tokenizers==0.20.3
|
51 |
+
tomlkit==0.12.0
|
52 |
+
torch==2.5.1
|
53 |
+
torchvision==0.20.1
|
54 |
+
tqdm==4.67.1
|
55 |
+
transformers==4.46.3
|
56 |
+
typer==0.13.1
|
57 |
+
typing_extensions==4.12.2
|
58 |
+
tzdata==2024.2
|
59 |
+
urllib3==2.2.3
|
60 |
+
uvicorn==0.32.1
|
61 |
+
websockets==12.0
|