from transformers import pipeline from PIL import Image import gradio as gr import os import requests import json model_name = "larimei/food-classification-ai" classifier = pipeline("image-classification", model=model_name) def predict_image(image): predictions = classifier(image) meal_name = predictions[0]['label'] recipe_text = getRecipe(meal_name) meal_info = f"This is {meal_name.replace('_', ' ')}." return meal_info, recipe_text def getRecipe(meal): app_id = "24bf0913" app_key = "03c60f26520f9d25b0d0617e50993aaa" field = ["uri", "label", "image", "ingredientLines", "source", "url"] url = "https://api.edamam.com/api/recipes/v2" querystring = { "type": "public", "q": meal.replace("_", " "), "app_id": app_id, "app_key": app_key, "field": field } response = requests.get(url, params=querystring) json_object = response.json() returnString = "It is made out of the following ingredients: \n\n" for line in json_object["hits"][0]["recipe"]["ingredientLines"]: returnString += line + "\n" returnString += "\n You can make " + json_object["hits"][0]["recipe"]["label"] + " yourself by following the steps of this instruction: " + json_object["hits"][0]["recipe"]["url"] return returnString title = "Recipifier" description = "Discover the world of recipes effortlessly with Recipifier, using advanced AI trained on the extensive Food-101 dataset. Simply upload a photo of any dish, and our application identifies it accurately, providing detailed recipes and cooking instructions sourced from a vast collection. Perfect for food enthusiasts and home chefs alike, Recipifier makes exploring new culinary creations intuitive and inspiring. Start transforming everyday ingredients into extraordinary meals today!" example_list = [["examples/" + example] for example in os.listdir("examples")] css = """ #component-13 { display: none; } """ demo = gr.Interface( fn=predict_image, inputs=gr.Image(type="pil"), outputs=[ gr.Textbox(label="Meal", elem_id="meal"), gr.Textbox(label="Recipe") ], examples=example_list, title=title, description=description, css=css, flagging_options=None ) demo.launch()