File size: 1,334 Bytes
897dc04 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import gradio as gr
from transformers import pipeline
from PIL import Image
# Initialize models
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
def analyze_image_sentiment(image):
if image is None:
return None, "Please upload an image.", "No sentiment analysis available."
# Generate caption from image
caption_result = captioner(image)[0]["generated_text"]
# Analyze sentiment of the caption
sentiment_result = sentiment_analyzer(caption_result)[0]
sentiment = f"Sentiment: {sentiment_result['label']} (Confidence: {sentiment_result['score']:.2f})"
return image, caption_result, sentiment
# Create Gradio interface
demo = gr.Interface(
fn=analyze_image_sentiment,
inputs=gr.Image(type="pil", label="Upload an Image"),
outputs=[
gr.Image(type="pil", label="Uploaded Image"),
gr.Textbox(label="Generated Caption"),
gr.Textbox(label="Sentiment Analysis")
],
title="Image Caption Sentiment Analyzer",
description="Upload an image to generate a caption and analyze its sentiment (positive or negative). Powered by Salesforce BLIP and DistilBERT."
)
if __name__ == "__main__":
demo.launch() |