# Recipe Creation with Python: Cook up a Storm!

Welcome to the ultimate recipe creation experience! With Flask and OpenAI, we have created a website that will revolutionize the way you cook. Now, anyone can become a master chef with just a few clicks. Our easy-to-use interface, powered by Python, allows you to create your own personalized recipes in no time. With an endless array of ingredients and flavors to choose from, you’ll be able to create unique dishes that are sure to impress. Whether you’re a seasoned chef or a beginner in the kitchen, our website will help you unleash your culinary creativity. So, what are you waiting for? Let’s get cooking!

# **Step 1\] Seting Up A Flask Environment and Installing Modules**

Open the Command Prompt and Type

```bash
mkdir Receipe_website 
cd Receipe_website
```

Open the above project with the help of VS Code to do further coding:

Open the terminal of the VS Code and Type the below code for the setting up the flask environment

```bash
python -m menv menv
```

Now Install the Flask and OpenAi with the help of Pip Command below in the terminal

```bash
pip install flask, openai
```

# **2\] Get the API Key From OpenAi**

To get an OpenAI API key, you will need to sign up for an OpenAI account by following these steps:

1. Go to the OpenAI website: [https://openai.com/](https://openai.com/)
    
2. Click on the “Log in” button in the upper right-hand corner of the page.
    
3. If you already have an OpenAI account, enter your email and password to log in. Otherwise, click on the “Sign up” button and fill out the required information to create a new account.
    
4. Once you are logged in to your account, go to the OpenAI API page: [https://beta.openai.com/docs/api-reference/introduction](https://beta.openai.com/docs/api-reference/introduction).
    
5. Click on the “Get API Key” button to request an API key.
    
6. Follow the instructions to provide any necessary information, such as your credit card information if required.
    
7. Once you have provided all the necessary information, your API key will be generated and displayed on the page.
    

Make sure to keep your API key confidential and secure, as it allows access to your OpenAI account and any associated resources.

# **3\] Creating the Files To Code**

Once You Get Your API KEY Now Its time to code the website so let’s go.

Create [app.py](http://app.py) file in Directory and paste the below code in that file :-

```python
from flask import Flask, render_template, request
import openai

app = Flask(__name__)

# Set up OpenAI API key
openai.api_key = "YOUR_API_KEY_HERE"


def generate_recipe(ingredients):
    prompt = "Generate a recipe using the following ingredients:\n\n" + "\n".join("- " + ing for ing in ingredients) + "\n\nInstructions:"
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0,
    )
    recipe = response.choices[0].text.strip()
    return recipe


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/', methods=['POST'])
def generate():
    ingredients = request.form.get('ingredients')
    ingredients_list = ingredients.split(',')
    recipe = generate_recipe(ingredients_list)
    return render_template('index.html', recipe=recipe)


if __name__ == '__main__':
    app.run(debug=True)
```

This code is a Flask web application that uses OpenAI’s GPT-3 language model to generate a recipe. It consists of a generate\_recipe function that creates a prompt for the language model using a list of ingredients and returns the generated recipe, and two routes defined using the @app.route decorator. The first route returns the HTML template for the index page, and the second route handles form submissions, retrieves the list of ingredients, generates a recipe using the generate\_recipe function, and returns the HTML template for the index page with the generated recipe included. The OpenAI API key is set using the openai.api\_key variable, and the Flask application is started in debug mode using the if name == "main": statement.

Now create the Templates folder in the directory, add the index.html file to the Templates folder, and paste the below code into the index.html file: -

```xml
<!DOCTYPE html>
<html>
<head>
 <title>Recipe Generator</title>
</head>
<body>
 <h1>Recipe Generator</h1>
 <form method="POST" action="/">
  <label for="ingredients"> Enter ingredients (comma-separated):</label>
  <input type="text" name="ingredients" id="ingredients">
  <input type="submit" value="Generate Recipe">
 </form>
 {% if recipe %}
  <h2>Recipe:</h2>
  <p>{{ recipe }}</p>
 {% endif %}
</body>
</html>
```

This code is an HTML template used to render the index page of a Flask web application. The template contains an HTML form that allows users to input a list of ingredients and generate a recipe. The template has a title, a main heading, and a form that takes a list of ingredients as input. The input field has a label that is associated with the field using the “for” attribute, and a submit button is provided to submit the form.

If the Flask application generates a recipe, it is displayed below the form in a paragraph element with a subheading.

# **3\] Running the Server**

Open the VS Code Terminal and Run :-

```xml
python app.py
```

You will get the result like below hence your Recipe Generator Website is ready to Create a Recipe.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679748325869/884b9c5b-7282-40a1-ae75-4cc8cf3af53d.png align="center")

# **Conclusion**

In conclusion, the recipe creating website developed using Flask, OpenAI, and Python provides an efficient and user-friendly platform for creating and sharing recipes. With the integration of OpenAI’s natural language processing capabilities, users can easily search for recipes, generate recipe suggestions based on their preferences, and receive personalized recipe recommendations. The Flask framework provides a robust and scalable infrastructure for the website, enabling it to handle a large volume of user traffic. The use of Python as the primary programming language ensures that the website is highly efficient and reliable. Overall, the recipe creating website is a great example of how technology can be leveraged to simplify everyday tasks and enhance the user experience.
