foodrecipe-ai / app.py
larimei's picture
updated recipeAI return
db088a7 verified
raw
history blame
2.08 kB
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)
return getRecipe(predictions[0]['label'])
def getRecipe(meal):
#meal = "hamburger"
app_id = "24bf0913"
app_key = "03c60f26520f9d25b0d0617e50993aaa"
#field = ["label"]
field = ["uri","label","image","ingredientLines","source","url"]
url = "https://api.edamam.com/api/recipes/v2"
#url2 = "https://api.edamam.com/api/recipes/v2?type=public&q=chicken%20curry&app_id=24bf0913&app_key=03c60f26520f9d25b0d0617e50993aaa"
querystring = {"type":"public",
"q": meal.replace("_"," "),
"app_id": app_id,
"app_key": app_key,
"field": field}
response = requests.get(url, params=querystring)
#print(response.content)
json_object = response.json()
json_formatted_str = json.dumps(json_object["hits"][0], indent=2) #nur das erste der 20 aus der liste
print(json_formatted_str)
#print(json_object)
#whole response
#return json_object
#just one result
#return json_object["hits"][0]
returnString = "This is " + json_object["hits"][0]["recipe"]["label"] + ". \n\n It is Made out of following ingredients: \n\n"
for line in json_object["hits"][0]["recipe"]["ingredientLines"]:
returnString += line + "\n"
returnString += "\n You can make it yourself by following the steps of this instruction: " + json_object["hits"][0]["recipe"]["url"]
return returnString
title = "Recipifier"
description = "blablabla"
example_list = [["examples/" + example] for example in os.listdir("examples")]
demo = gr.Interface(
fn=predict_image,
inputs=gr.Image(type="pil"),
outputs=[
gr.Textbox(label="Recipe")
],
examples=example_list,
title=title,
description=description,
)
demo.launch()