File size: 3,408 Bytes
ff93ad0
 
 
 
 
0d8b8d6
ff93ad0
 
 
 
eda6db8
 
ff93ad0
abeac40
ff93ad0
abeac40
 
 
 
0d8b8d6
 
abeac40
 
 
 
eda6db8
ff93ad0
eda6db8
 
ff93ad0
abeac40
ff93ad0
 
 
abeac40
ff93ad0
abeac40
ff93ad0
eda6db8
ff93ad0
 
abeac40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff93ad0
abeac40
ff93ad0
 
eda6db8
ff93ad0
 
 
 
 
24747b5
ff93ad0
eda6db8
24747b5
ff93ad0
 
 
eda6db8
ff93ad0
 
1d8e0f9
 
ff93ad0
eda6db8
ff93ad0
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import openai
import os
import gradio as gr
from dotenv import load_dotenv
import io
from PIL import Image

# Load environment variables (where your OpenAI key will be stored)
load_dotenv()

# Load the OpenAI API key from environment variables and strip any trailing newlines or spaces
openai.api_key = os.getenv("OPENAI_API_KEY").strip()

# Function to analyze the ad image using GPT-4-turbo's multimodal capabilities
def analyze_ad(image):
    # Convert the PIL image to bytes
    image_bytes = io.BytesIO()
    image.save(image_bytes, format='PNG')
    image_bytes = image_bytes.getvalue()

    # Prompt for the marketing persona and scoring rubric
    prompt = """
    Analyze this advertisement image. Extract any text present in the image and generate a marketing persona.
    Then, provide a score (out of 10) for each of the following:

    1. Relevance to Target Audience: Is the ad appealing to the intended demographic?
    2. Emotional Engagement: Does the ad evoke the right emotional response?
    3. Brand Consistency: Does the ad align with the brand’s voice and values?
    4. Creativity: How unique or innovative is the ad's design and text approach?
    5. Persuasiveness: Does the ad motivate action, such as clicking or purchasing?

    Provide the persona description and the scores in table form with a final score.
    """

    # Send the image and prompt to GPT-4-turbo for analysis
    response = openai.ChatCompletion.create(
        model="gpt-4-turbo-vision",  # Use the multimodal GPT-4-turbo model
        messages=[
            {"role": "system", "content": "You are a marketing expert analyzing an advertisement."},
            {"role": "user", "content": prompt}
        ],
        functions=[
            {
                "name": "analyze_image",
                "description": "Analyze an image and generate marketing insights",
                "parameters": {
                    "type": "image",
                    "properties": {
                        "image": {
                            "type": "string",
                            "description": "The input advertisement image for analysis"
                        }
                    },
                    "required": ["image"]
                }
            }
        ],
        function_call={"name": "analyze_image", "arguments": {"image": image_bytes}},  # Sending the image as input
        temperature=0.7,
        max_tokens=500
    )

    # Extract the response text from the API output
    result = response['choices'][0]['message']['content']

    # Return the result for display
    return result

# Function to process the image and run the analysis
def upload_and_analyze(image):
    # Pass the uploaded image to the analyze_ad function
    result = analyze_ad(image)
    
    return result

# Gradio interface for Hugging Face deployment
iface = gr.Interface(
    fn=upload_and_analyze,
    inputs=gr.Image(type="pil", label="Upload Advertisement Image"),  # Use type="pil" for the image input
    outputs=gr.Textbox(label="Marketing Persona and Ad Analysis"),
    title="Advertisement Persona and Scoring Analyzer",
    description="Upload an advertisement image, and the app will generate marketing personas and evaluate the ad based on Relevance, Emotional Engagement, Brand Consistency, Creativity, and Persuasiveness."
)

# Launch the app
if __name__ == "__main__":
    iface.launch()