Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import gradio as gr
|
|
4 |
from dotenv import load_dotenv
|
5 |
import io
|
6 |
from PIL import Image
|
7 |
-
import pytesseract # Optional: Using Tesseract OCR to extract text from the image
|
8 |
|
9 |
# Load environment variables (where your OpenAI key will be stored)
|
10 |
load_dotenv()
|
@@ -12,31 +11,28 @@ load_dotenv()
|
|
12 |
# Load the OpenAI API key from environment variables
|
13 |
openai.api_key = os.getenv("OPENAI_API_KEY").strip()
|
14 |
|
15 |
-
# Function to analyze the ad image
|
16 |
def analyze_ad(image):
|
17 |
-
#
|
18 |
-
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
# Prompt for the marketing persona and scoring rubric
|
24 |
-
prompt = f"""
|
25 |
-
Analyze the following ad copy and generate a marketing persona. Then, provide a score (out of 10) for each of the following:
|
26 |
|
27 |
-
1. Relevance to Target Audience
|
28 |
-
2. Emotional Engagement
|
29 |
-
3. Brand Consistency
|
30 |
-
4. Creativity
|
31 |
-
5. Persuasiveness
|
32 |
-
|
33 |
-
Ad Copy: {ad_copy}
|
34 |
|
35 |
-
|
36 |
"""
|
37 |
|
38 |
# Send the prompt to GPT-4-turbo for analysis
|
39 |
-
response = openai.
|
40 |
model="gpt-4-turbo",
|
41 |
messages=[
|
42 |
{"role": "system", "content": "You are a marketing expert analyzing an advertisement."},
|
@@ -54,18 +50,17 @@ def analyze_ad(image):
|
|
54 |
|
55 |
# Function to process the image and run the analysis
|
56 |
def upload_and_analyze(image):
|
57 |
-
#
|
58 |
result = analyze_ad(image)
|
59 |
-
|
60 |
return result
|
61 |
|
62 |
# Gradio interface for Hugging Face deployment
|
63 |
iface = gr.Interface(
|
64 |
fn=upload_and_analyze,
|
65 |
-
inputs=gr.Image(type="pil", label="Upload Advertisement Image"), #
|
66 |
outputs=gr.Textbox(label="Marketing Persona and Ad Analysis"),
|
67 |
title="Advertisement Persona and Scoring Analyzer",
|
68 |
-
description="Upload an advertisement image
|
69 |
)
|
70 |
|
71 |
# Launch the app
|
|
|
4 |
from dotenv import load_dotenv
|
5 |
import io
|
6 |
from PIL import Image
|
|
|
7 |
|
8 |
# Load environment variables (where your OpenAI key will be stored)
|
9 |
load_dotenv()
|
|
|
11 |
# Load the OpenAI API key from environment variables
|
12 |
openai.api_key = os.getenv("OPENAI_API_KEY").strip()
|
13 |
|
14 |
+
# Function to analyze the ad image (text analysis with GPT-4)
|
15 |
def analyze_ad(image):
|
16 |
+
# Convert the image to bytes (you can still keep this for future multimodal capabilities)
|
17 |
+
image_bytes = io.BytesIO()
|
18 |
+
image.save(image_bytes, format='PNG')
|
19 |
+
image_bytes = image_bytes.getvalue()
|
20 |
|
21 |
+
# Placeholder: Using a simple prompt for text-based analysis (future multimodal support)
|
22 |
+
prompt = """
|
23 |
+
This is an advertisement. Please generate a marketing persona based on this ad and provide scores (out of 10) on the following:
|
|
|
|
|
|
|
24 |
|
25 |
+
1. Relevance to Target Audience
|
26 |
+
2. Emotional Engagement
|
27 |
+
3. Brand Consistency
|
28 |
+
4. Creativity
|
29 |
+
5. Persuasiveness
|
|
|
|
|
30 |
|
31 |
+
Explain your reasoning for each score and provide a final persona description.
|
32 |
"""
|
33 |
|
34 |
# Send the prompt to GPT-4-turbo for analysis
|
35 |
+
response = openai.ChatCompletion.create(
|
36 |
model="gpt-4-turbo",
|
37 |
messages=[
|
38 |
{"role": "system", "content": "You are a marketing expert analyzing an advertisement."},
|
|
|
50 |
|
51 |
# Function to process the image and run the analysis
|
52 |
def upload_and_analyze(image):
|
53 |
+
# Call the analyze_ad function to generate marketing insights
|
54 |
result = analyze_ad(image)
|
|
|
55 |
return result
|
56 |
|
57 |
# Gradio interface for Hugging Face deployment
|
58 |
iface = gr.Interface(
|
59 |
fn=upload_and_analyze,
|
60 |
+
inputs=gr.Image(type="pil", label="Upload Advertisement Image"), # Upload the ad image
|
61 |
outputs=gr.Textbox(label="Marketing Persona and Ad Analysis"),
|
62 |
title="Advertisement Persona and Scoring Analyzer",
|
63 |
+
description="Upload an advertisement image and get a marketing persona along with scores for Relevance, Emotional Engagement, Brand Consistency, Creativity, and Persuasiveness."
|
64 |
)
|
65 |
|
66 |
# Launch the app
|