|
{ |
|
"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 |
|
} |
|
|