from transformers import Blip2Processor, Blip2ForConditionalGeneration
import torch
from PIL import Picture
import requests
import matplotlib.pyplot as plt
import numpy as np
from io import BytesIO
# Load BLIP-2 mannequin
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
mannequin = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", torch_dtype=torch.float16)
if torch.cuda.is_available():
mannequin = mannequin.to("cuda")
# Operate to obtain picture from URL
def download_image(url):
response = requests.get(url, stream=True)
return Picture.open(BytesIO(response.content material)).convert('RGB')
# Operate for picture captioning
def generate_caption(image_path):
# Load picture from path or URL
if isinstance(image_path, str):
if image_path.startswith(('http://', 'https://')):
picture = download_image(image_path)
else:
picture = Picture.open(image_path).convert('RGB')
else:
picture = image_path
inputs = processor(photographs=picture, return_tensors="pt")
if torch.cuda.is_available():
inputs = {okay: v.to("cuda") for okay, v in inputs.gadgets()}
generated_ids = mannequin.generate(**inputs, max_new_tokens=50)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
return generated_text
# Operate for visible query answering
def visual_qa(image_path, query):
# Load picture from path or URL
if isinstance(image_path, str):
if image_path.startswith(('http://', 'https://')):
picture = download_image(image_path)
else:
picture = Picture.open(image_path).convert('RGB')
else:
picture = image_path
# FIX: Correctly format the query for the mannequin
# BLIP-2 wants a particular immediate format for QA
immediate = f"Query: {query} Reply:"
inputs = processor(photographs=picture, textual content=immediate, return_tensors="pt")
if torch.cuda.is_available():
inputs = {okay: v.to("cuda") for okay, v in inputs.gadgets()}
generated_ids = mannequin.generate(
**inputs,
max_new_tokens=30,
do_sample=False # Use grasping decoding for extra exact solutions
)
reply = processor.batch_decode(generated_ids, skip_special_tokens=True)[0].strip()
# Take away the immediate half from the reply
reply = reply.change(immediate, "").strip()
return reply
# Operate to visualise picture with caption and QA
def visualize_product_analysis(image_path, questions=None):
# Load picture
if isinstance(image_path, str):
if image_path.startswith(('http://', 'https://')):
picture = download_image(image_path)
else:
picture = Picture.open(image_path).convert('RGB')
else:
picture = image_path
# Generate caption
caption = generate_caption(picture)
# Default questions if none offered
if questions is None:
questions = [
"What color is this product?",
"What material is this product made of?",
"What is the target demographic for this product?",
"What is a key feature of this product?"
]
# Get solutions
solutions = []
for query in questions:
reply = visual_qa(picture, query)
solutions.append((query, reply))
# Create visualization
plt.determine(figsize=(12, 10))
# Show picture
plt.subplot(2, 1, 1)
plt.imshow(np.array(picture))
plt.title("Product Picture", fontsize=14)
plt.axis('off')
# Show caption and Q&A
plt.subplot(2, 1, 2)
plt.axis('off')
text_content = f"Generated Description: {caption}nn"
text_content += "Product Evaluation:n"
for q, a in solutions:
text_content += f"Q: {q}nA: {a}nn"
plt.textual content(0.01, 0.99, text_content, rework=plt.gca().transAxes,
fontsize=12, verticalalignment="prime", wrap=True)
plt.tight_layout()
plt.present()
return caption, solutions
# Enterprise utility: Automated product itemizing
def create_product_listing(image_path):
# Load picture
if isinstance(image_path, str):
if image_path.startswith(('http://', 'https://')):
picture = download_image(image_path)
else:
picture = Picture.open(image_path).convert('RGB')
else:
picture = image_path
# Get fundamental caption
caption = generate_caption(picture)
# Extract product attributes with extra particular prompting
colour = visual_qa(picture, "What colours are seen on this product?")
materials = visual_qa(picture, "What materials does this product seem like manufactured from?")
use_case = visual_qa(picture, "What could be the principle use case for this product?")
unique_features = visual_qa(picture, "What are any distinctive or notable options of this product?")
# Create structured itemizing
itemizing = {
"title": caption,
"attributes": {
"colour": colour,
"materials": materials,
"primary_use": use_case,
"unique_features": unique_features
}
}
# Visualize the itemizing
plt.determine(figsize=(14, 10))
# Show picture
plt.subplot(1, 2, 1)
plt.imshow(np.array(picture))
plt.title("Product Picture", fontsize=14)
plt.axis('off')
# Show itemizing particulars
plt.subplot(1, 2, 2)
plt.axis('off')
listing_text = f"PRODUCT LISTINGnn"
listing_text += f"Title: {itemizing['title']}nn"
listing_text += "Product Attributes:n"
for attr, worth in itemizing['attributes'].gadgets():
listing_text += f"{attr.change('_', ' ').title()}: {worth}n"
plt.textual content(0.01, 0.99, listing_text, rework=plt.gca().transAxes,
fontsize=12, verticalalignment="prime")
plt.tight_layout()
plt.present()
return itemizing
# Operate for advertising content material evaluation
def analyze_marketing_content(image_path):
# Load picture
if isinstance(image_path, str):
if image_path.startswith(('http://', 'https://')):
picture = download_image(image_path)
else:
picture = Picture.open(image_path).convert('RGB')
else:
picture = image_path
# Advertising-specific questions
marketing_questions = [
"What emotions does this image evoke?",
"What brand values are communicated in this image?",
"What target audience would this image appeal to?",
"What call to action would pair well with this image?",
"What marketing channel would this image be most effective on?"
]
# Get solutions
marketing_insights = {}
for query in marketing_questions:
reply = visual_qa(picture, query)
key = query.cut up("?")[0].strip().decrease().change(" ", "_")
marketing_insights[key] = reply
# Visualize the evaluation
plt.determine(figsize=(14, 10))
# Show picture
plt.subplot(1, 2, 1)
plt.imshow(np.array(picture))
plt.title("Advertising Visible", fontsize=14)
plt.axis('off')
# Show advertising insights
plt.subplot(1, 2, 2)
plt.axis('off')
insights_text = "MARKETING CONTENT ANALYSISnn"
for query, key in zip(marketing_questions, marketing_insights.keys()):
insights_text += f"{query}n{marketing_insights[key]}nn"
plt.textual content(0.01, 0.99, insights_text, rework=plt.gca().transAxes,
fontsize=12, verticalalignment="prime")
plt.tight_layout()
plt.present()
return marketing_insights
# Operate for social media understanding
def analyze_social_media_content(image_path):
# Load picture
if isinstance(image_path, str):
if image_path.startswith(('http://', 'https://')):
picture = download_image(image_path)
else:
picture = Picture.open(image_path).convert('RGB')
else:
picture = image_path
# Generate caption
caption = generate_caption(picture)
# Social media particular evaluation
engagement_potential = visual_qa(picture, "How probably is that this picture to have interaction viewers on social media?")
suggested_hashtags = visual_qa(picture, "What hashtags could be acceptable for this picture on social media?")
platform_fit = visual_qa(picture, "Which social media platform would this picture carry out finest on?")
content_type = visual_qa(picture, "What sort of social media submit would this picture be appropriate for?")
# Create evaluation dict
social_analysis = {
"caption": caption,
"engagement_potential": engagement_potential,
"suggested_hashtags": suggested_hashtags,
"platform_fit": platform_fit,
"content_type": content_type
}
# Visualize the evaluation
plt.determine(figsize=(14, 10))
# Show picture
plt.subplot(1, 2, 1)
plt.imshow(np.array(picture))
plt.title("Social Media Content material", fontsize=14)
plt.axis('off')
# Show social media insights
plt.subplot(1, 2, 2)
plt.axis('off')
insights_text = "SOCIAL MEDIA CONTENT ANALYSISnn"
insights_text += f"Caption: {social_analysis['caption']}nn"
insights_text += f"Engagement Potential: {social_analysis['engagement_potential']}nn"
insights_text += f"Prompt Hashtags: {social_analysis['suggested_hashtags']}nn"
insights_text += f"Greatest Platform: {social_analysis['platform_fit']}nn"
insights_text += f"Content material Kind: {social_analysis['content_type']}n"
plt.textual content(0.01, 0.99, insights_text, rework=plt.gca().transAxes,
fontsize=12, verticalalignment="prime")
plt.tight_layout()
plt.present()
return social_analysis
# Instance utilization
if __name__ == "__main__":
# Instance: E-commerce product evaluation
product_url = "https://photographs.unsplash.com/photo-1598033129183-c4f50c736f10?w=800"
print("1. Fundamental Product Evaluation")
caption, qa_results = visualize_product_analysis(product_url)
print("n2. Creating Automated Product Itemizing")
product_listing = create_product_listing(product_url)
print("n3. Advertising Content material Evaluation")
marketing_url = "https://photographs.unsplash.com/photo-1581252584837-9f0b1d3bf82c?ixlib=rb-4.0.3&q=80"
marketing_insights = analyze_marketing_content(marketing_url)
print("n4. Social Media Content material Evaluation")
social_url = "https://photographs.unsplash.com/photo-1534442072653-dbbf80c5e1ae?ixlib=rb-4.0.3&q=80"
social_analysis = analyze_social_media_content(social_url)