Skip to content

Commit 66f5145

Browse files
Merge pull request #2506 from andoriyaprashant/branch18
AI-Powered Recipe Generator Script Added
2 parents 6343fa1 + ffefad4 commit 66f5145

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

AI-Powered Recipe Generator/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# AI-Powered Recipe Generator
2+
3+
## Description
4+
AI-Powered Recipe Generator is a Python script that uses natural language processing (NLP) to analyze a database of recipes. Users can input a list of ingredients they have, and the script generates unique recipes based on their preferences and available ingredients. The generated recipes come with instructions and can be customized based on dietary preferences.
5+
6+
## Features
7+
- Generate unique recipes based on user-provided ingredients and dietary preferences.
8+
- Provide instructions for each generated recipe.
9+
- Suggest ingredient substitutions if some ingredients are missing.
10+
- Fetch nutritional information of the generated recipe (work in progress).
11+
- Allow users to rate the generated recipe for feedback and improvement.
12+
- Suggest similar recipes for more options.
13+
14+
## Requirements
15+
- Python 3.6+
16+
- nltk library (Natural Language Toolkit)
17+
18+
## Installation
19+
1. Clone the repository or download the script (`recipe_generator.py`) to your local machine.
20+
2. Install the required libraries using pip:
21+
22+
```bash
23+
pip install nltk
24+
```
25+
26+
## Usage
27+
1. Run the script using Python:
28+
29+
```bash
30+
python recipe_generator.py
31+
```
32+
33+
2. Follow the on-screen prompts to enter your list of ingredients and dietary preferences.
34+
3. The script will generate a unique recipe based on your inputs, along with instructions and a link to the original source.
35+
4. Optionally, the script will ask for ingredient substitutions and user ratings.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import nltk
2+
import random
3+
from nltk.corpus import stopwords
4+
from nltk.tokenize import word_tokenize
5+
6+
# Sample database of recipes
7+
recipes = {
8+
"Spaghetti Carbonara": {
9+
"ingredients": "pasta, eggs, bacon, parmesan cheese, black pepper",
10+
"source_link": "https://www.example.com/spaghetti-carbonara",
11+
"preferences": ["non-vegetarian"],
12+
"instructions": "1. Cook pasta according to package instructions.\n2. In a separate bowl, whisk together eggs, grated parmesan cheese, and black pepper.\n3. In a skillet, cook bacon until crispy, then remove from heat.\n4. Toss cooked pasta with the bacon and bacon fat.\n5. Add the egg mixture to the pasta, tossing quickly to avoid scrambling the eggs. The heat from the pasta will cook the eggs and create a creamy sauce. Serve immediately.",
13+
},
14+
"Chicken Stir-Fry": {
15+
"ingredients": "chicken, broccoli, carrots, soy sauce, garlic, ginger",
16+
"source_link": "https://www.example.com/chicken-stir-fry",
17+
"preferences": ["non-vegetarian"],
18+
"instructions": "1. Heat oil in a wok or large skillet over high heat.\n2. Add chopped chicken and cook until browned and cooked through.\n3. Add minced garlic and grated ginger, stir for a minute.\n4. Add chopped vegetables (broccoli, carrots) and stir-fry until tender-crisp.\n5. Pour in soy sauce and toss everything together. Serve hot.",
19+
},
20+
"Caprese Salad": {
21+
"ingredients": "tomatoes, mozzarella cheese, basil, olive oil, balsamic vinegar",
22+
"source_link": "https://www.example.com/caprese-salad",
23+
"preferences": ["vegetarian"],
24+
"instructions": "1. Slice tomatoes and mozzarella cheese.\n2. Arrange tomato and mozzarella slices on a plate, alternating them.\n3. Drizzle with olive oil and balsamic vinegar.\n4. Sprinkle fresh basil leaves over the top. Serve as a refreshing salad.",
25+
},
26+
"Chocolate Brownies": {
27+
"ingredients": "chocolate, butter, sugar, eggs, flour, vanilla extract",
28+
"source_link": "https://www.example.com/chocolate-brownies",
29+
"preferences": ["vegetarian"],
30+
"instructions": "1. Preheat oven to 350°F (175°C). Grease a baking pan.\n2. In a microwave-safe bowl, melt chocolate and butter together.\n3. Stir in sugar, eggs, flour, and vanilla extract until well combined.\n4. Pour the batter into the prepared pan.\n5. Bake for about 25 minutes or until a toothpick inserted in the center comes out with moist crumbs. Let it cool before cutting into squares.",
31+
},
32+
"Mushroom Risotto": {
33+
"ingredients": "arborio rice, mushrooms, vegetable broth, onion, garlic, parmesan cheese",
34+
"source_link": "https://www.example.com/mushroom-risotto",
35+
"preferences": ["vegetarian"],
36+
"instructions": "1. In a large skillet, sauté chopped onion and minced garlic in olive oil until translucent.\n2. Add arborio rice and stir until coated with oil.\n3. Gradually add vegetable broth, one ladleful at a time, stirring until the liquid is absorbed.\n4. Stir in sliced mushrooms and continue adding broth until the rice is creamy and cooked.\n5. Stir in grated parmesan cheese and serve hot.",
37+
},
38+
"Grilled Salmon": {
39+
"ingredients": "salmon fillet, lemon, olive oil, garlic, dill, salt, pepper",
40+
"source_link": "https://www.example.com/grilled-salmon",
41+
"preferences": ["non-vegetarian"],
42+
"instructions": "1. Preheat grill to medium-high heat.\n2. Rub salmon fillet with olive oil and season with salt, pepper, and minced garlic.\n3. Place the salmon on the grill and cook for a few minutes on each side until it flakes easily with a fork.\n4. Squeeze fresh lemon juice over the salmon and sprinkle with chopped dill before serving.",
43+
},
44+
"Vegetable Curry": {
45+
"ingredients": "mixed vegetables, coconut milk, curry paste, onion, garlic, ginger",
46+
"source_link": "https://www.example.com/vegetable-curry",
47+
"preferences": ["vegetarian", "vegan"],
48+
"instructions": "1. In a large pot, sauté chopped onion, minced garlic, and grated ginger in oil until fragrant.\n2. Add mixed vegetables and cook for a few minutes.\n3. Stir in curry paste and cook for another minute.\n4. Pour in coconut milk and bring to a simmer until the vegetables are tender.\n5. Serve the vegetable curry over cooked rice.",
49+
},
50+
"Apple Pie": {
51+
"ingredients": "apples, sugar, flour, butter, cinnamon, pie crust",
52+
"source_link": "https://www.example.com/apple-pie",
53+
"preferences": ["vegetarian"],
54+
"instructions": "1. Preheat oven to 375°F (190°C).\n2. Peel and slice apples, then mix with sugar, cinnamon, and flour.\n3. Roll out pie crust and place it in a pie dish.\n4. Fill the pie crust with the apple mixture.\n5. Add small pieces of butter on top of the apples.\n6. Roll out another pie crust for the top, seal the edges, and cut slits for venting.\n7. Bake for about 45 minutes or until the crust is golden brown and the apples are tender.",
55+
},
56+
}
57+
# Instructions for each recipe
58+
recipe_instructions = {
59+
"Spaghetti Carbonara": "...", # Instructions for Spaghetti Carbonara
60+
"Chicken Stir-Fry": "...", # Instructions for Chicken Stir-Fry
61+
"Caprese Salad": "...", # Instructions for Caprese Salad
62+
"Chocolate Brownies": "...", # Instructions for Chocolate Brownies
63+
"Mushroom Risotto": "...", # Instructions for Mushroom Risotto
64+
"Grilled Salmon": "...", # Instructions for Grilled Salmon
65+
"Vegetable Curry": "...", # Instructions for Vegetable Curry
66+
"Apple Pie": "...", # Instructions for Apple Pie
67+
}
68+
69+
# Function to preprocess the recipes
70+
def preprocess_recipe(recipe):
71+
stop_words = set(stopwords.words("english"))
72+
tokens = word_tokenize(recipe.lower())
73+
return [word for word in tokens if word.isalpha() and word not in stop_words]
74+
75+
# Function to generate a unique recipe
76+
def generate_recipe(ingredients, preferences=None):
77+
processed_ingredients = preprocess_recipe(ingredients)
78+
79+
matching_recipes = []
80+
for name, recipe_info in recipes.items():
81+
if all(ingredient in recipe_info["ingredients"] for ingredient in processed_ingredients):
82+
if not preferences or any(pref in preferences for pref in recipe_info["preferences"]):
83+
matching_recipes.append(name)
84+
85+
if not matching_recipes:
86+
return "Sorry, no recipe found with those ingredients and preferences. Try something else."
87+
88+
generated_recipe_name = random.choice(matching_recipes)
89+
return generated_recipe_name, recipes[generated_recipe_name]["source_link"]
90+
91+
# Function to get user ingredient substitutions
92+
93+
# Function to suggest similar recipes
94+
def suggest_similar_recipes(generated_recipe_name):
95+
similar_recipes = random.sample([name for name in recipes.keys() if name != generated_recipe_name], 2)
96+
return similar_recipes
97+
98+
# Main function
99+
def main():
100+
print("AI-Powered Recipe Generator")
101+
print("Available Recipes:")
102+
for idx, recipe_name in enumerate(recipes.keys(), start=1):
103+
print(f"{idx}. {recipe_name}")
104+
105+
user_ingredients = input("Enter the list of ingredients you have (comma-separated): ")
106+
user_preferences = input("Enter your dietary preferences (comma-separated, or press Enter to skip): ").split(",")
107+
108+
generated_recipe_name, source_link = generate_recipe(user_ingredients, user_preferences)
109+
110+
print("\nGenerated Recipe:")
111+
print(f"Recipe: {generated_recipe_name}")
112+
print("Ingredients:", recipes[generated_recipe_name]["ingredients"])
113+
print("Instructions:")
114+
print(recipe_instructions[generated_recipe_name])
115+
print("Source Link:", source_link)
116+
117+
118+
similar_recipes = suggest_similar_recipes(generated_recipe_name)
119+
print("\nYou may also like these recipes:")
120+
for idx, recipe_name in enumerate(similar_recipes, start=1):
121+
print(f"{idx}. {recipe_name}")
122+
123+
if __name__ == "__main__":
124+
nltk.download("punkt")
125+
nltk.download("stopwords")
126+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nltk

0 commit comments

Comments
 (0)