dindizz's picture
Update app.py
c8406ec verified
raw
history blame
2.34 kB
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").strip()
# Function to analyze the ad image (text analysis with GPT-4)
def analyze_ad(image):
# Convert the image to bytes (you can keep this for future multimodal capabilities)
image_bytes = io.BytesIO()
image.save(image_bytes, format='PNG')
image_bytes = image_bytes.getvalue()
# Placeholder: Using a simple prompt for text-based analysis (future multimodal support)
prompt = """
This is an advertisement. Please generate a marketing persona based on this ad and provide scores (out of 10) on the following:
1. Relevance to Target Audience
2. Emotional Engagement
3. Brand Consistency
4. Creativity
5. Persuasiveness
Explain your reasoning for each score and provide a final persona description.
"""
# Send the prompt to GPT-4-turbo for analysis
response = openai.chat_completions.create(
model="gpt-4-turbo",
messages=[
{"role": "system", "content": "You are a marketing expert analyzing an advertisement."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=400
)
# 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):
# Call the analyze_ad function to generate marketing insights
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"), # Upload the ad image
outputs=gr.Textbox(label="Marketing Persona and Ad Analysis"),
title="Advertisement Persona and Scoring Analyzer",
description="Upload an advertisement image and get a marketing persona along with scores for Relevance, Emotional Engagement, Brand Consistency, Creativity, and Persuasiveness."
)
# Launch the app
if __name__ == "__main__":
iface.launch()