import numpy as np import gradio as gr import requests import json def list_to_dict(data): results = {} for i in range(len(data)): # Access the i-th dictionary in the list using an integer index d = data[i] # Assign the value of the 'label' key to the 'score' value in the results dictionary results[d['label']] = d['score'] # The results dictionary will now contain the label-score pairs from the data list return results API_URL = "https://api-inference.huggingface.co/models/nateraw/food" headers = {"Authorization": "Bearer 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"} def query(filename): with open(filename, "rb") as f: data = f.read() response = requests.request("POST", API_URL, headers=headers, data=data) output = json.loads(response.content.decode("utf-8")) return list_to_dict(output),json.dumps(output, indent=2, sort_keys=True) def get_nutrition_info(food_name): #Make request to Nutritionix API response = requests.get( "https://trackapi.nutritionix.com/v2/search/instant", params={"query": food_name}, headers={ "x-app-id": "63a710ef", "x-app-key": "3ddc7e3feda88e1cf6dd355fb26cb261" } ) #Parse response and return relevant information data = response.json() response = data["branded"][0]["photo"]["thumb"] # Open the image using PIL return { "food_name": data["branded"][0]["food_name"], "calories": data["branded"][0]["nf_calories"], "serving_size": data["branded"][0]["serving_qty"], "serving_unit": data["branded"][0]["serving_unit"], #"images": data["branded"][0]["photo"] },response with gr.Blocks() as demo: gr.Markdown("Food-Classification-Calorie-Estimation and Volume-Estimation") with gr.Tab("Food Classification"): text_input = gr.Image(type="filepath") text_output = [gr.Label(num_top_classes=6), gr.Textbox() ] text_button = gr.Button("Food Classification") with gr.Tab("Food Calorie Estimation"): image_input = gr.Textbox(label="Please enter the name of the Food you want to get calorie") image_output = [gr.Textbox(), gr.Image(type="filepath") ] image_button = gr.Button("Estimate Calories!") with gr.Tab("Volume Estimation"): _image_input = gr.Textbox(label="Please enter the name of the Food you want to get calorie") _image_output = [gr.Textbox(), gr.Image() ] _image_button = gr.Button("Volume Calculation") with gr.Tab("Future Works"): gr.Markdown("Future work on Food Classification") gr.Markdown( "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") gr.Markdown("Future work on Volume Estimation") gr.Markdown( "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") gr.Markdown("Future work on Calorie Estimation") gr.Markdown( "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") gr.Markdown("https://github.com/Ali-Maq/Food-Classification-Volume-Estimation-and-Calorie-Estimation/blob/main/README.md") text_button.click(query, inputs=text_input, outputs=text_output) image_button.click(get_nutrition_info, inputs=image_input, outputs=image_output) _image_button.click(get_nutrition_info, inputs=_image_input, outputs=_image_output) with gr.Accordion("Open for More!"): gr.Markdown("🍎 Designed and built by Ali Under the Guidance of Professor Dennis Shasha") gr.Markdown("Contact me at ali.quidwai@nyu.edu 😊") demo.launch(share=True, debug=True)