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
pip install transformers instagrapi pillow google-generativeai# 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)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_captiondef 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)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")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 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)python script_name.pyimport 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()