File size: 3,048 Bytes
32ed852
 
542a039
 
68331a9
 
542a039
32ed852
 
 
 
 
 
1ba9158
 
 
 
32ed852
68331a9
4f66dbd
 
 
 
 
 
1ba9158
3a7e3d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542a039
 
1ba9158
542a039
 
 
1ba9158
 
 
 
 
 
542a039
32ed852
542a039
 
1ba9158
 
542a039
 
 
 
1ba9158
 
542a039
 
1ba9158
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
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):
    url = "https://gustar-io-deutsche-rezepte.p.rapidapi.com/generateRecipe"
    payload = { "text": meal.replace("_"," ")}
    headers = {
        "x-rapidapi-key": "f2703cb7b0msh6f8e7a071e404d7p1e3f67jsnb8855a98ffce",
        "x-rapidapi-host": "gustar-io-deutsche-rezepte.p.rapidapi.com",
        "Content-Type": "application/json"
    }
    try:
        response = requests.post(url, json=payload, headers=headers)
        response.raise_for_status()  # Raise an error for bad status codes

        data = response.json()

        # Format ingredients list
        ingredients_list = "Zutaten:\n"
        for ingredient in data['ingredients']:
            amount = ingredient.get('amount', '')
            unit = ingredient.get('unit', '')
            name = ingredient['name']
            ingredients_list += f"- {amount} {unit} {name}\n".strip() + '\n'

        # Format instructions list
        instructions_list = "\nZubereitung:\n"
        for step in data['instructions']:
            instructions_list += f"{step}\n"

        # Format the entire recipe
        formatted_recipe = f"{data['title']}\n"
        formatted_recipe += f"Portionen: {data['portions']}\n"
        formatted_recipe += f"Gesamtzeit: {data['totalTime'] // 60} Minuten\n\n"
        formatted_recipe += ingredients_list + instructions_list

        return formatted_recipe

    except requests.exceptions.RequestException as e:
        return f"An error occurred while fetching the recipe: {str(e)}"

    except json.decoder.JSONDecodeError:
        return "Error: Unable to decode the JSON response from the API."

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()