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