File size: 9,165 Bytes
d51e88d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"import gradio as gr\n",
"import requests\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 20,
"outputs": [],
"source": [
"def list_to_dict(data):\n",
" results = {}\n",
"\n",
" for i in range(len(data)):\n",
" # Access the i-th dictionary in the list using an integer index\n",
" d = data[i]\n",
" # Assign the value of the 'label' key to the 'score' value in the results dictionary\n",
" results[d['label']] = d['score']\n",
"\n",
" # The results dictionary will now contain the label-score pairs from the data list\n",
" return results"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 21,
"outputs": [],
"source": [
"\n",
"\n",
"API_URL = \"https://api-inference.huggingface.co/models/nateraw/food\"\n",
"headers = {\"Authorization\": \"Bearer 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\"}\n",
"\n",
"def query(filename):\n",
" with open(filename, \"rb\") as f:\n",
" data = f.read()\n",
" response = requests.request(\"POST\", API_URL, headers=headers, data=data)\n",
" output = json.loads(response.content.decode(\"utf-8\"))\n",
" return list_to_dict(output),json.dumps(output, indent=2, sort_keys=True)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 27,
"outputs": [],
"source": [
"def get_nutrition_info(food_name):\n",
" #Make request to Nutritionix API\n",
" response = requests.get(\n",
" \"https://trackapi.nutritionix.com/v2/search/instant\",\n",
" params={\"query\": food_name},\n",
" headers={\n",
" \"x-app-id\": \"63a710ef\",\n",
" \"x-app-key\": \"3ddc7e3feda88e1cf6dd355fb26cb261\"\n",
" }\n",
" )\n",
" #Parse response and return relevant information\n",
" data = response.json()\n",
" response = data[\"branded\"][0][\"photo\"][\"thumb\"]\n",
"\n",
" # Open the image using PIL\n",
"\n",
" return {\n",
" \"food_name\": data[\"branded\"][0][\"food_name\"],\n",
" \"calories\": data[\"branded\"][0][\"nf_calories\"],\n",
" \"serving_size\": data[\"branded\"][0][\"serving_qty\"],\n",
" \"serving_unit\": data[\"branded\"][0][\"serving_unit\"],\n",
" #\"images\": data[\"branded\"][0][\"photo\"]\n",
" },response"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 28,
"outputs": [
{
"data": {
"text/plain": "({'food_name': 'Hamburger',\n 'calories': 340,\n 'serving_size': 1,\n 'serving_unit': 'sandwich'},\n 'https://d2eawub7utcl6.cloudfront.net/images/nix-apple-grey.png')"
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"get_nutrition_info(\"Hamburger\")"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 22,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 22,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7869\n",
"Running on public URL: https://f7f1e48778aede65.gradio.app\n",
"\n",
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n"
]
},
{
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"text/html": "<div><iframe src=\"https://f7f1e48778aede65.gradio.app\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"Food-Classification-Calorie-Estimation and Volume-Estimation\")\n",
" with gr.Tab(\"Food Classification\"):\n",
" text_input = gr.Image(type=\"filepath\")\n",
" text_output = [gr.Label(num_top_classes=6),\n",
" gr.Textbox()\n",
" ]\n",
" text_button = gr.Button(\"Food Classification\")\n",
" with gr.Tab(\"Food Calorie Estimation\"):\n",
" image_input = gr.Textbox(label=\"Please enter the name of the Food you want to get calorie\")\n",
" image_output = [gr.Textbox(),\n",
" gr.Image(type=\"filepath\")\n",
" ]\n",
" image_button = gr.Button(\"Estimate Calories!\")\n",
" with gr.Tab(\"Volume Estimation\"):\n",
" _image_input = gr.Textbox(label=\"Please enter the name of the Food you want to get calorie\")\n",
" _image_output = [gr.Textbox(),\n",
" gr.Image()\n",
" ]\n",
" _image_button = gr.Button(\"Volume Calculation\")\n",
" with gr.Tab(\"Future Works\"):\n",
" gr.Markdown(\"Future work on Food Classification\")\n",
" gr.Markdown(\n",
" \"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\")\n",
" gr.Markdown(\"Future work on Volume Estimation\")\n",
" gr.Markdown(\n",
" \"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\")\n",
" gr.Markdown(\"Future work on Calorie Estimation\")\n",
" gr.Markdown(\n",
" \"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\")\n",
" gr.Markdown(\"https://github.com/Ali-Maq/Food-Classification-Volume-Estimation-and-Calorie-Estimation/blob/main/README.md\")\n",
"\n",
" text_button.click(query, inputs=text_input, outputs=text_output)\n",
" image_button.click(get_nutrition_info, inputs=image_input, outputs=image_output)\n",
" _image_button.click(get_nutrition_info, inputs=_image_input, outputs=_image_output)\n",
" with gr.Accordion(\"Open for More!\"):\n",
" gr.Markdown(\"π Designed and built by Ali Under the Guidance of Professor Dennis Shasha\")\n",
" gr.Markdown(\"Contact me at ali.quidwai@nyu.edu π\")\n",
"\n",
"demo.launch(share=True, debug=True)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"is_executing": true
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import numpy as np\n",
"import gradio as gr\n",
"\n",
"def flip_text(x):\n",
" return x[::-1]\n",
"\n",
"def flip_image(x):\n",
" return np.fliplr(x)\n",
"\n",
"with gr.Blocks() as demo:\n",
" gr.Markdown(\"Flip text or image files using this demo.\")\n",
" with gr.Tab(\"Flip Text\"):\n",
" text_input = gr.Textbox()\n",
" text_output = gr.Textbox()\n",
" text_button = gr.Button(\"Flip\")\n",
" with gr.Tab(\"Flip Image\"):\n",
" with gr.Row():\n",
" image_input = gr.Image()\n",
" image_output = gr.Image()\n",
" image_button = gr.Button(\"Flip\")\n",
"\n",
" with gr.Accordion(\"Open for More!\"):\n",
" gr.Markdown(\"Look at me...\")\n",
"\n",
" text_button.click(get_nutrition_info, inputs=text_input, outputs=text_output)\n",
" image_button.click(query, inputs=image_input, outputs=image_output)\n",
"\n",
"demo.launch()"
],
"metadata": {
"collapsed": false,
"pycharm": {
"is_executing": true
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|