📚
Aditya Kumar
  • About me
    • Resume
  • Automating Amazon EBS
  • ECS Bluegreen with Lambda SWAP
  • First drive of AWS Chatbot
  • Talking COVID-19 with Amazon Lex
  • Managed Jupyter on AWS SageMaker
  • Extending GitOps to reliability-as-code with GitHub and StackPulse
  • HashiCorp Packer for Machine Images
  • Deleted My Web App and App Service Plan, Whats Next ?
  • Using Terraform for zero downtime updates of an Auto Scaling group in AWS
  • Automating Instagram Posts with AI: A Step-by-Step Guide
  • Merge Commit Message Validator for Pull Requests
  • Automating Jenkins Data Backup with rsync and Slack Notifications
  • Creating a One-Time Setup for Kubernetes Cluster with Worker Nodes Using HAProxy
Powered by GitBook
On this page

Was this helpful?

Automating Instagram Posts with AI: A Step-by-Step Guide

PreviousUsing Terraform for zero downtime updates of an Auto Scaling group in AWSNextMerge Commit Message Validator for Pull Requests

Last updated 6 months ago

Was this helpful?

Are you looking to leverage AI to create engaging Instagram posts? This blog explores how to integrate multiple tools for caption generation, music addition, and automated posting to Instagram using Python. By the end of this guide, you'll have a deeper understanding of the code that powers this bot.

Code Breakdown

1. Setting Up the Environment

The script uses popular Python libraries and APIs to achieve its functionality:

  • Hugging Face Transformers: To generate captions from images using the BLIP model.

  • Google Generative AI (GenAI): To make captions more engaging and platform-appropriate.

  • Pillow: For image manipulation.

Make sure to install these dependencies:

pip install transformers instagrapi pillow google-generativeai

2. AI-Powered Caption Generation

BLIP for Image Captioning

The BlipProcessor and BlipForConditionalGeneration models from Hugging Face are used to generate captions directly from images. Here's how it works:

# Load the BLIP model and processor
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

def generate_caption_blip(image_path):
    image = Image.open(image_path).convert("RGB")
    inputs = processor(image, return_tensors="pt")
    caption_ids = model.generate(**inputs)
    return processor.decode(caption_ids[0], skip_special_tokens=True)

This generates a base caption describing the content of the image.

Enhancing Captions with Google GenAI

To make captions engaging and tailored for Instagram:

def enhance_caption_with_google(blip_caption, post_type="post"):
    prompt = (
        f"Make this caption more creative, fun, and Instagram-friendly: '{blip_caption}'" 
        if post_type == "post" else
        f"Make this caption short, catchy, and suitable for an Instagram story: '{blip_caption}'"
    )
    response = generative_model.generate_content(prompt)
    return response.text.strip() if response and response.text else blip_caption

This uses generative AI to create captions filled with emojis and hashtags, perfect for social media.


3. Instagram Integration

Automating Posts with Instagrapi

Instagrapi simplifies Instagram interactions, including logging in and posting:

def post_to_instagram(image_path, caption, post_type="post"):
    cl = Client()
    cl.login(os.getenv("INSTAGRAM_USERNAME"), os.getenv("INSTAGRAM_PASSWORD"))
    if post_type == "story":
        cl.photo_upload_to_story(image_path, caption)
    else:
        cl.photo_upload(image_path, caption)

Environment variables store sensitive credentials to keep them secure.

4. User Interaction

Post Type Selection

Before posting, the script prompts you to choose the type of post:

  • Post: A feed post with a detailed caption.

  • Story: A brief caption with catchy visuals.

  • Reel: A short video with music.

def prompt_post_type():
    print("1. Post\n2. Story\n3. Reel")
    choice = input("Choose your post type: ")
    return {"1": "post", "2": "story", "3": "reel"}.get(choice, "post")

5. Adding Music

While adding music is a placeholder in this script, you can integrate tools like FFMPEG to overlay audio on media.

def add_music_to_media(media_type, media_path):
    music_path = "background_music.mp3"
    print(f"Adding music to {media_type}: {music_path}")
    return media_path

6. Putting It All Together

The script automates the posting process with the following steps:

  1. Randomly selects an image from a directory.

  2. Generates a base caption using BLIP.

  3. Enhances the caption using Google GenAI.

  4. Posts the image with the caption to Instagram.

The main function:

 post_immediately():
    images = [f for f in os.listdir("pics/") if f.endswith(('.jpg', '.png'))]
    if not images:
        print("No images to post.")
        return
    selected_image = os.path.join("pics/", random.choice(images))
    blip_caption = generate_caption_blip(selected_image)
    post_type = prompt_post_type()
    enhanced_caption = enhance_caption_with_google(blip_caption, post_type)
    post_to_instagram(selected_image, enhanced_caption, post_type)

7. How to Run

  1. Set Environment Variables:

    • API_KEY for Google GenAI.

    • Instagram INSTAGRAM_USERNAME and INSTAGRAM_PASSWORD.

  2. Organize Your Media: Place your images in a folder named pics/.

  3. Run the Script:

    python script_name.py

8. Enhancements to Consider

  • Music Integration: Automate background music addition for videos.

  • Scheduled Posting: Use a scheduler like cron or Python's schedule library.

  • Content Moderation: Validate and filter generated captions for brand consistency.

import os
import random
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
from instagrapi import Client
import google.generativeai as genai

# Initialize BLIP model for image captioning
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

# Configure Google Generative AI API
genai.configure(api_key=os.environ.get("API_KEY"))  # Make sure to set API_KEY in your environment variables
generative_model = genai.GenerativeModel("gemini-1.5-flash")

# Instagram credentials
INSTAGRAM_USERNAME = os.getenv("INSTAGRAM_USERNAME")  # Your Instagram username
INSTAGRAM_PASSWORD = os.getenv("INSTAGRAM_PASSWORD")  # Your Instagram password
IMAGE_DIRECTORY = "pics/"

# Generate caption using BLIP
def generate_caption_blip(image_path):
    try:
        image = Image.open(image_path).convert("RGB")
    except IOError:
        print(f"Error opening image: {image_path}")
        return None  # Return None if image is invalid
    inputs = processor(image, return_tensors="pt")
    caption_ids = model.generate(**inputs)
    caption = processor.decode(caption_ids[0], skip_special_tokens=True)
    return caption

# Enhance caption using Google Generative AI
def enhance_caption_with_google(blip_caption, post_type="post"):
    try:
        # Create the prompt to generate a creative, Instagram-friendly caption with hashtags
        if post_type == "story":
            prompt = f"Make this caption short and catchy for an Instagram story: '{blip_caption}'. Add emojis and hashtags. Keep it brief and fun."
        else:
            prompt = f"Make this caption more creative, fun, engaging, genz, and Instagram-friendly with hashtags: '{blip_caption}'. Ensure the caption is less than 100 words and includes relevant hashtags."

        response = generative_model.generate_content(prompt)
        enhanced_caption = response.text.strip() if response and response.text else blip_caption
        # Ensure caption is within 100 words
        words = enhanced_caption.split()
        if len(words) > 100:
            enhanced_caption = ' '.join(words[:100])
        return enhanced_caption
    except Exception as e:
        print(f"Failed to enhance caption with Google Generative AI: {e}")
        return blip_caption  # Fallback to BLIP caption

# Function to prompt user for type of post (story, post, or reel)
def prompt_post_type():
    print("Choose post type:")
    print("1. Post (with caption and music)")
    print("2. Story (with minimal text, background music)")
    print("3. Reel (with engaging caption and background music)")
    choice = input("Enter the number corresponding to your choice: ")

    if choice == "1":
        return "post"
    elif choice == "2":
        return "story"
    elif choice == "3":
        return "reel"
    else:
        print("Invalid choice. Defaulting to 'post'.")
        return "post"

# Add music to media
def add_music_to_media(media_type, media_path):
    # Placeholder function: here, you would integrate the functionality to add music
    # depending on the media type (story, reel, or post).
    music_path = "background_music.mp3"  # This should be an appropriate audio file
    print(f"Adding music '{music_path}' to {media_type}.")
    return media_path  # Return the path with the music added (you will need external tools for this)

# Post to Instagram using instagrapi
def post_to_instagram(image_path, caption, post_type="post"):
    cl = Client()

    # Login to Instagram
    try:
        cl.login(INSTAGRAM_USERNAME, INSTAGRAM_PASSWORD)
        print("Successfully logged in to Instagram.")
    except Exception as e:
        print(f"Failed to log in to Instagram: {e}")
        return

    # Add music to post (if applicable)
    media_path_with_music = add_music_to_media(post_type, image_path)

    # Upload and post the image
    try:
        if post_type == "story":
            # For story: use the `photo_upload_to_story` method
            media = cl.photo_upload_to_story(media_path_with_music, caption)
        elif post_type == "reel":
            # For reels, use `video_upload` method if video is present
            media = cl.video_upload(media_path_with_music, caption)
        else:
            # Regular feed post
            media = cl.photo_upload(media_path_with_music, caption)

        print(f"Post published successfully. Media ID: {media.pk}")
    except Exception as e:
        print(f"Failed to post to Instagram: {e}")

# Post immediately
def post_immediately():
    if not os.path.exists(IMAGE_DIRECTORY):
        print(f"Directory {IMAGE_DIRECTORY} does not exist.")
        return

    images = [f for f in os.listdir(IMAGE_DIRECTORY) if f.endswith(('.jpg', '.png'))]
    if not images:
        print("No images available for posting.")
        return

    # Choose random image
    selected_image = os.path.join(IMAGE_DIRECTORY, random.choice(images))

    # Generate caption using BLIP
    try:
        blip_caption = generate_caption_blip(selected_image)  # Will raise an IOError if the image is invalid
    except Exception as e:
        print(f"Error with the selected image: {e}")
        return  # Skip if image cannot be opened

    if blip_caption:
        print(f"BLIP Generated Caption: {blip_caption}")

        # Prompt user for post type (story, reel, or post)
        post_type = prompt_post_type()

        # Enhance caption with Google Generative AI based on post type
        enhanced_caption = enhance_caption_with_google(blip_caption, post_type)
        print(f"Enhanced Caption: {enhanced_caption}")

        # Post to Instagram
        post_to_instagram(selected_image, enhanced_caption, post_type)
    else:
        print("Failed to generate a caption.")

# Run the bot
post_immediately()

Conclusion

This AI-powered bot demonstrates how to automate Instagram posting while maintaining creative control over captions. With models like BLIP and Google GenAI, generating engaging, platform-ready content has never been easier.

: To post directly to Instagram.

Instagrapi
Kalpa,Himachal Pradesh