AI in the Kitchen: Building a Recipe Builder with Gemini and Python

AI in the Kitchen: Building a Recipe Builder with Gemini and Python

·

4 min read

In today’s fast-paced world, where time is precious and creativity in the kitchen can often take a backseat, what if you could simply take a photo of the vegetables in your fridge, share it with an AI, and get a delicious recipe tailored to those ingredients? Sounds like a dream, right? With the power of Gemini AI and Streamlit, this dream can become a reality. In this blog, I’ll walk you through how to build a Recipe Builder app that turns your fridge ingredients into culinary masterpieces.


The Recipe Builder App

The Recipe Builder app we’re building will allow users to:

  1. Take a photo of the vegetables or ingredients in their fridge (or upload an existing image).

  2. Provide an optional text prompt (e.g., "Make a vegetarian dish" or "Suggest a quick dinner recipe").

  3. Generate a recipe based on the image and prompt using Gemini AI.

Let’s dive into the code and see how it works!


Step 1: Setting Up the Environment

Before we start coding, we need to set up our environment. Here’s what you’ll need:

  • Python installed on your machine.

  • A Google Cloud API key for Gemini AI (you can get this from the Google AI studio).

  • Install the required Python libraries.

Run the following commands to install the necessary libraries:

pip install streamlit python-dotenv google-generativeai

Step 2: Writing the Code

Here’s the complete code for our Recipe Builder app:

from dotenv import load_dotenv
load_dotenv()
import streamlit as st
import os
from PIL import Image
import google.generativeai as genai

genai.configure(api_key=os.getenv("GENAI_API_KEY"))

#gemini call
def get_gemini_response(input,image):
    model = genai.GenerativeModel('gemini-2.0-flash')
    if input!="":
       response = model.generate_content([input,image])
    else:
       response = model.generate_content(image)
    return response.text

##initialize streamlit app
st.set_page_config(page_title="Gemini Recipe Builder")
st.header("Gemini Recipe Builder")
input=st.text_input("Input Prompt: ",key="input")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

image=""   
if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image.", use_column_width=True)
submit=st.button("Gnerate Recipe")

## Button click
if submit: 
    response=get_gemini_response(input,image)
    st.subheader("The Response text:")
    st.write(response)

Github:

https://github.com/vipinputhanveetil/gemini_recipe_builder


Step 3: Breaking Down the Code

  1. Environment Setup:

    • We use dotenv to load environment variables, including the Gemini API key.

    • The google-generativeai library is used to interact with the Gemini AI model.

  2. Gemini AI Configuration:

    • The genai.configure function sets up the API key for Gemini.
  3. Streamlit App:

    • The app has a title (Gemini Recipe Builder) and a header.

    • Users can input a text prompt and upload an image of ingredients.

    • The get_gemini_response function sends the input and image to the Gemini model and retrieves the generated recipe.

  4. Image Handling:

    • The app uses the PIL library to open and display the uploaded image.
  5. Recipe Generation:

    • When the user clicks the "Generate Recipe" button, the app sends the input and image to Gemini AI and displays the generated recipe.

Step 4: Running the App

To run the app, save the code in a file (e.g., recipe_builder.py) and run the following command in your terminal:

streamlit run gemini_recipe_builder.py

This will start the Streamlit app, and you can access it in your browser at http://localhost:8504.


Step 5: Testing the App

  1. Take a photo of the vegetables or ingredients in your fridge (or upload an existing image).

  2. Optionally, provide a text prompt like "Make a pasta dish" or "Suggest a quick dinner recipe."

  3. Click "Generate Recipe" and watch as Gemini AI creates a recipe tailored to your ingredients!


Why This App is Useful

  • Personalized Recipes: The app generates recipes based on the ingredients you have, reducing food waste and inspiring creativity in the kitchen.

  • Ease of Use: Simply take a photo of your fridge contents, and let AI do the rest.

  • AI-Powered: Gemini AI ensures that the recipes are relevant and tailored to your inputs.

  • Time-Saving: No more searching for recipes—just snap a photo and get cooking!


Future Enhancements

This app is just the beginning! Here are some ideas to make it even better:

  • Add dietary preferences (e.g., vegan, gluten-free).

  • Include step-by-step cooking instructions.

  • Integrate a shopping list generator for missing ingredients.

  • Allow users to save and share their favorite recipes.


Conclusion

Building a Recipe Builder with Gemini AI and Streamlit is a fun and practical way to explore the capabilities of generative AI. With just a few lines of code, you can create an app that helps people cook delicious meals using the ingredients they already have. Whether you’re a beginner or an experienced developer, this project is a great way to dive into the world of AI and web development.