Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
import json
|
5 |
+
|
6 |
+
def list_to_dict(data):
|
7 |
+
results = {}
|
8 |
+
|
9 |
+
for i in range(len(data)):
|
10 |
+
# Access the i-th dictionary in the list using an integer index
|
11 |
+
d = data[i]
|
12 |
+
# Assign the value of the 'label' key to the 'score' value in the results dictionary
|
13 |
+
results[d['label']] = d['score']
|
14 |
+
|
15 |
+
# The results dictionary will now contain the label-score pairs from the data list
|
16 |
+
return results
|
17 |
+
|
18 |
+
API_URL = "https://api-inference.huggingface.co/models/nateraw/food"
|
19 |
+
headers = {"Authorization": "Bearer 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"}
|
20 |
+
|
21 |
+
def query(filename):
|
22 |
+
with open(filename, "rb") as f:
|
23 |
+
data = f.read()
|
24 |
+
response = requests.request("POST", API_URL, headers=headers, data=data)
|
25 |
+
output = json.loads(response.content.decode("utf-8"))
|
26 |
+
return list_to_dict(output),json.dumps(output, indent=2, sort_keys=True)
|
27 |
+
|
28 |
+
def get_nutrition_info(food_name):
|
29 |
+
#Make request to Nutritionix API
|
30 |
+
response = requests.get(
|
31 |
+
"https://trackapi.nutritionix.com/v2/search/instant",
|
32 |
+
params={"query": food_name},
|
33 |
+
headers={
|
34 |
+
"x-app-id": "63a710ef",
|
35 |
+
"x-app-key": "3ddc7e3feda88e1cf6dd355fb26cb261"
|
36 |
+
}
|
37 |
+
)
|
38 |
+
#Parse response and return relevant information
|
39 |
+
data = response.json()
|
40 |
+
response = data["branded"][0]["photo"]["thumb"]
|
41 |
+
|
42 |
+
# Open the image using PIL
|
43 |
+
|
44 |
+
return {
|
45 |
+
"food_name": data["branded"][0]["food_name"],
|
46 |
+
"calories": data["branded"][0]["nf_calories"],
|
47 |
+
"serving_size": data["branded"][0]["serving_qty"],
|
48 |
+
"serving_unit": data["branded"][0]["serving_unit"],
|
49 |
+
#"images": data["branded"][0]["photo"]
|
50 |
+
},response
|
51 |
+
|
52 |
+
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("Food-Classification-Calorie-Estimation and Volume-Estimation")
|
55 |
+
with gr.Tab("Food Classification"):
|
56 |
+
text_input = gr.Image(type="filepath")
|
57 |
+
text_output = [gr.Label(num_top_classes=6),
|
58 |
+
gr.Textbox()
|
59 |
+
]
|
60 |
+
text_button = gr.Button("Food Classification")
|
61 |
+
with gr.Tab("Food Calorie Estimation"):
|
62 |
+
image_input = gr.Textbox(label="Please enter the name of the Food you want to get calorie")
|
63 |
+
image_output = [gr.Textbox(),
|
64 |
+
gr.Image(type="filepath")
|
65 |
+
]
|
66 |
+
image_button = gr.Button("Estimate Calories!")
|
67 |
+
with gr.Tab("Volume Estimation"):
|
68 |
+
_image_input = gr.Textbox(label="Please enter the name of the Food you want to get calorie")
|
69 |
+
_image_output = [gr.Textbox(),
|
70 |
+
gr.Image()
|
71 |
+
]
|
72 |
+
_image_button = gr.Button("Volume Calculation")
|
73 |
+
with gr.Tab("Future Works"):
|
74 |
+
gr.Markdown("Future work on Food Classification")
|
75 |
+
gr.Markdown(
|
76 |
+
"Currently the Model is trained on food-101 Dataset, which has 100 classes, In the future iteration of the project we would like to train the model on UNIMIB Dataset with 256 Food Classes")
|
77 |
+
gr.Markdown("Future work on Volume Estimation")
|
78 |
+
gr.Markdown(
|
79 |
+
"The volume model has been trained on Apple AR Toolkit and thus can be executred only on Apple devices ie a iOS platform, In futur we would like to train the volume model such that it is Platform independent")
|
80 |
+
gr.Markdown("Future work on Calorie Estimation")
|
81 |
+
gr.Markdown(
|
82 |
+
"The Calorie Estimation currently relies on Nutritionix API , In Future Iteration we would like to build our own Custom Database of Major Food Product across New York Restaurent")
|
83 |
+
gr.Markdown("https://github.com/Ali-Maq/Food-Classification-Volume-Estimation-and-Calorie-Estimation/blob/main/README.md")
|
84 |
+
|
85 |
+
text_button.click(query, inputs=text_input, outputs=text_output)
|
86 |
+
image_button.click(get_nutrition_info, inputs=image_input, outputs=image_output)
|
87 |
+
_image_button.click(get_nutrition_info, inputs=_image_input, outputs=_image_output)
|
88 |
+
with gr.Accordion("Open for More!"):
|
89 |
+
gr.Markdown("π Designed and built by Ali Under the Guidance of Professor Dennis Shasha")
|
90 |
+
gr.Markdown("Contact me at ali.quidwai@nyu.edu π")
|
91 |
+
|
92 |
+
demo.launch(share=True, debug=True)
|