Creating Your Own Recipe Generator Using Streamlit, Python, and OpenAI API
Tags: openai, streamlit, python, howto
Published: 2023-06-08 07:23:49

If you love food or are a tech enthusiast looking for a new way to discover quick and tasty recipes, then this is the blog for you.In this tutorial, I will walk you through the steps to create your own recipe generator using streamlit, Python, and the OpenAI API. By the end of this tutorial, you'll have a fully functional recipe generator that can provide you with unique recipe ideas at the click of a button!

## **Prerequisites **

To follow along with this tutorial, make sure you have the following prerequisites:

Python3 installed on your machine(Version 3.6 or higher)

Streamlit library installed ('pip install streamlit') 

OpenAI Python library installed ( 'pip install openai')

OpenAI API key (You can obtain it from the OpenAI website) 

**Step 1: Set up the Project**

Let's start by setting up the project structure and installing the required dependencies. Follow the steps below:

Create a new directory for your project and navigate into it using the command line.

Initialize a new Python virtual environment by running the following command:

python -m venv recipe-generator-env

Activate the virtual environment:

On Windows, run: recipe-generator-env\Scripts\activate

On macOS/Linux, run: source recipe-generator-env/bin/activate

Install the required libraries by running the following command:

pip install streamlit openai python-dotenv

Create a new Python file named recipe_generator.py in your project directory.

**Step 2: Set up the Environment Variables**

To securely store your OpenAI API key and load it into your application, we'll use environment variables. Follow these steps:

Create a new file named .env in the same directory as your recipe_generator.py file.

Open the .env file in a text editor and add the following line:

OPENAI_API_KEY=your_api_key

3.** Save the '.env' file.**

Step 3: Import the Required Libraries

In the recipe_generator.py file, start by importing the required libraries:

import os
import streamlit as st
import openai
from dotenv import load_dotenv

**Step 4: Load Environment Variables
**
Next, load the environment variables from the .env file:

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")

Step 5: Implement the Recipe Generator

Now, let's define the function to generate the recipe using the OpenAI API:

def generate_recipe(prompt):
    completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "Professional Chef", "content": prompt}
        ]
    )
    return completion.choices[0].message["content"]

**Step 6: Create the Streamlit Application
**
We will use Streamlit to create a simple web application for the recipe generator:

def main():
    st.title("Recipe Generator")

    prompt = st.text_area("Enter the ingredients and instructions:", height=150)
    if st.button("Generate Recipe"):
        recipe = generate_recipe(prompt)
        st.markdown(f"## Recipe\n\n{recipe}")

if __name__ == "__main__":
    main()

**Step 7: Run the Application
**
To run the application, open a command prompt, navigate to your project directory, and run the following command:

streamlit run recipe_generator.py

The application will start, and you can access it in your web browser at http://localhost:8501.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qee43ugg2uej8gigk0r3.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndwmb3bda73ed35glg2b.png)

**

## Conclusion
**

Congratulations! You have successfully built your own recipe generator using Streamlit, Python, and the OpenAI API. Now you can generate unique and exciting recipes by entering the ingredients and instructions. Feel free to customize and enhance the application further to suit your needs. Happy cooking!

Feel free to customize and add more features to your streamlit app! This is just a starting point to guide you in creating an amazing recipe generator.

Follow me on: Twitter or Linkedin!


References:

[Youtube Channel](https://youtu.be/oiQqRX2v7_Y)

[Follow My Substack](https://yourtechie.substack.com/p/create-your-own-recipe-generator?sd=pf)