File size: 2,391 Bytes
16d4da6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load the recipe generation model
generator = pipeline("text2text-generation", model="flax-community/t5-recipe-generation")

def generate_recipe(ingredients):
    """
    Generate a recipe based on the provided ingredients
    """
    # Check if ingredients are provided
    if not ingredients.strip():
        return "Please enter some ingredients!"
    
    try:
        # Format the prompt for the model
        # The model expects format: "items: ingredient1, ingredient2, ingredient3"
        prompt = f"items: {ingredients}"
        
        # Generate the recipe
        result = generator(prompt, max_length=512, do_sample=True)
        
        # Extract the generated recipe
        recipe = result[0]['generated_text']
        
        # Format the output nicely
        output = "#Generated Recipe\n\n"
        output += "---\n\n"
        output += recipe
        output += "\n\n---\n\n"
        output += "**Tip**: Try different ingredient combinations for variety!"
        
        return output
        
    except Exception as e:
        return f"Error generating recipe: {str(e)}\n\nPlease try again with different ingredients."

# Example ingredient combinations for users to try
examples = [
    ["chicken, rice, garlic, tomatoes, onion"],
    ["pasta, cream, parmesan cheese, mushrooms, spinach"],
    ["eggs, milk, flour, sugar, vanilla extract"],
    ["salmon, lemon, dill, potatoes, butter"],
    ["beef, beans, chili powder, onion, tomatoes"],
    ["tofu, soy sauce, ginger, broccoli, sesame oil"]
]

# Create the Gradio interface
demo = gr.Interface(
    fn=generate_recipe,
    inputs=gr.Textbox(
        label="Enter Your Ingredients",
        placeholder="chicken, rice, garlic, tomatoes, onion",
        lines=3,
        info="Enter ingredients separated by commas"
    ),
    outputs=gr.Markdown(label="Your Recipe"),
    title="AI Recipe Generator",
    description="Enter the ingredients you have, and I'll generate a delicious recipe for you!",
    examples=examples,
    theme=gr.themes.Soft(),
    article="""
    ### How to use:
    1. Enter your available ingredients separated by commas
    2. Click 'Submit' or press Enter
    3. Get a complete recipe with instructions!
    
    **Powered by T5 Recipe Generation Model from Hugging Face**
    """
)

if __name__ == "__main__":
    demo.launch()