Full Tutorial: How to Use Google Generative AI for Text and Image Content Creation in Python

Introduction:

KoshurAI
3 min readSep 8, 2024

In this tutorial, you will learn how to use Google Generative AI to create engaging content, from text to images. The AI-powered Gemini Model can generate text based on prompts and descriptions from images. We’ll cover the complete process of setting up the environment, generating content, and working with images in Python. By the end of this guide, you’ll be equipped to create content that could go viral.

Step-by-Step Instructions:

Step 1: Install Necessary Libraries

To start using Google Generative AI, you’ll need to install the google-generativeai package. We'll also be using Pillow for image handling.

  1. Open Google Colab or set up your local Python environment.
  2. Run the following command to install the required libraries.
# Install Google Generative AI library
!pip install -U google-generativeai

# Import required libraries
import google.generativeai as genai
import os

Step 2: Configure Google Generative AI API Key

You need an API key to use Google Generative AI. Follow these steps to get the key:

  1. Go to the Google Cloud Console.
  2. Create a new project (or select an existing one).
  3. Enable the Google Generative AI API.
  4. Generate an API key and copy it.
  5. Replace your_api_key_here with your actual key in the code below:
# Set up API key for Google Generative AI
key = 'your_api_key_here'
genai.configure(api_key=key)

Step 3: Generate Text Content

Now that the API is set up, you can start generating text. We’ll use the Gemini model to complete a simple sentence.

# Initialize the model
model = genai.GenerativeModel('gemini-1.5-flash')

# Generate text with a prompt
response = model.generate_content("The opposite of hot is")
print(response.text)

Explanation:

  • The generate_content method takes a prompt and completes it based on the AI’s knowledge.
  • The model will complete the sentence, e.g., “The opposite of hot is cold.”

Step 4: Handling and Displaying Images

You can also use an image as part of your content creation process. In this step, we’ll use Pillow to handle images.

  1. Upload an image file (for example, in Google Colab).
  2. Use the following code to open and display it.
# Import Pillow library
import PIL.Image
# Load and display an image
img = PIL.Image.open('/content/your_image_file.png')
img.show() # To display the image

Step 5: Generate Blog Content Based on an Image

We will now generate text content based on the image and a description prompt. This is useful for generating blog posts or social media captions.

# Generate content from an image and prompt
response = model.generate_content([
"Write a short, engaging blog post based on this picture. It should include a description of the meal in the photo and talk about my journey meal prepping.",
img
], stream=True)

response.resolve() # Wait for response

# Print the generated content
print(response.text)

Explanation:

  • The AI generates a blog post describing the image and adding narrative based on the prompt. You can customize the prompt according to the content you need.

Instructions Recap:

  1. Install the google-generativeai package and import necessary libraries.
  2. Configure your API key from Google Cloud.
  3. Generate text content using the Gemini model with simple prompts.
  4. Load and display images with Pillow.
  5. Generate descriptive content based on the provided image.

--

--

KoshurAI
KoshurAI

Written by KoshurAI

Passionate about Data Science? I offer personalized data science training and mentorship. Join my course today to unlock your true potential in Data Science.

No responses yet