Spaces:
Sleeping
Sleeping
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 | |
openai.api_key = os.getenv("OPENAI_API_KEY") | |
# Function to analyze the ad and generate marketing personas + scoring | |
def analyze_ad(image): | |
# Convert the image to bytes | |
image_bytes = io.BytesIO() | |
image.save(image_bytes, format='PNG') | |
image_bytes = image_bytes.getvalue() | |
# Simulate extracting creative copy from the image using OCR (optical character recognition) | |
# In actual production, you'd integrate an OCR API here to extract text | |
# For simplicity, we'll use placeholder text | |
ad_copy = "Placeholder for ad copy extracted from the image." | |
# Prompt for the marketing persona and scoring rubric | |
prompt = f""" | |
Analyze the following ad copy and generate a marketing persona. Then, provide a score (out of 10) for each of the following: | |
1. Relevance to Target Audience: Is the copy appealing to the intended demographic? | |
2. Emotional Engagement: Does the ad evoke the right emotional response? | |
3. Brand Consistency: Does the copy align with the brand’s voice and values? | |
4. Creativity: How unique or innovative is the language or approach? | |
5. Persuasiveness: Does the ad motivate action, such as clicking or purchasing? | |
Ad Copy: {ad_copy} | |
Provide the persona description and the scores in table form with a final score. | |
""" | |
# Use the OpenAI API to generate the persona and scores using gpt-4o-mini model | |
response = openai.ChatCompletion.create( | |
model="gpt-4o-mini", # Use the gpt-4o-mini model as requested | |
messages=[ | |
{"role": "system", "content": "You are a marketing expert."}, | |
{"role": "user", "content": prompt} | |
], | |
max_tokens=300, | |
temperature=0.7, | |
) | |
# Extract the response text | |
result = response['choices'][0]['message']['content'] | |
# Return the result for display | |
return result | |
# Function to load and display the image | |
def upload_and_analyze(image): | |
# Open the image and display | |
ad_image = Image.open(image) | |
# Analyze the ad | |
result = analyze_ad(ad_image) | |
return result | |
# Interface using Gradio for Hugging Face deployment | |
# Simple UI that takes an image upload | |
iface = gr.Interface( | |
fn=upload_and_analyze, | |
inputs=gr.inputs.Image(type="file", label="Upload Advertisement Image"), | |
outputs=gr.outputs.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 copy based on Relevance, Emotional Engagement, Brand Consistency, Creativity, and Persuasiveness." | |
) | |
# Launch the app | |
if __name__ == "__main__": | |
iface.launch() | |