dindizz commited on
Commit
eda6db8
1 Parent(s): 8d3c70e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -26
app.py CHANGED
@@ -3,52 +3,63 @@ import os
3
  import gradio as gr
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()
10
 
11
- # Load the OpenAI API key from environment variables
12
- openai.api_key = os.getenv("OPENAI_API_KEY")
13
 
14
- # Function to analyze the ad and generate marketing personas + scoring
15
  def analyze_ad(image):
16
- # Convert the PIL image to bytes
17
  image_bytes = io.BytesIO()
18
  image.save(image_bytes, format='PNG')
19
  image_bytes = image_bytes.getvalue()
20
 
21
- # Simulate extracting creative copy from the image using OCR (optical character recognition)
22
- # In actual production, you'd integrate an OCR API here to extract text
23
- # For simplicity, we'll use placeholder text
24
- ad_copy = "Placeholder for ad copy extracted from the image."
25
-
26
- # Prompt for the marketing persona and scoring rubric
27
- prompt = f"""
28
- Analyze the following ad copy and generate a marketing persona. Then, provide a score (out of 10) for each of the following:
29
- 1. Relevance to Target Audience: Is the copy appealing to the intended demographic?
30
  2. Emotional Engagement: Does the ad evoke the right emotional response?
31
- 3. Brand Consistency: Does the copy align with the brand’s voice and values?
32
- 4. Creativity: How unique or innovative is the language or approach?
33
  5. Persuasiveness: Does the ad motivate action, such as clicking or purchasing?
34
-
35
- Ad Copy: {ad_copy}
36
 
37
  Provide the persona description and the scores in table form with a final score.
38
  """
39
 
40
- # Use the OpenAI API to generate the persona and scores using gpt-4o-mini model
41
  response = openai.ChatCompletion.create(
42
- model="gpt-4o-mini", # Use the gpt-4o-mini model as requested
43
  messages=[
44
- {"role": "system", "content": "You are a marketing expert."},
45
  {"role": "user", "content": prompt}
46
  ],
47
- max_tokens=300,
48
  temperature=0.7,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  )
50
 
51
- # Extract the response text
52
  result = response['choices'][0]['message']['content']
53
 
54
  # Return the result for display
@@ -56,18 +67,18 @@ def analyze_ad(image):
56
 
57
  # Function to process the image and run the analysis
58
  def upload_and_analyze(image):
59
- # No need to open the image, as it is already a PIL image
60
  result = analyze_ad(image)
61
 
62
  return result
63
 
64
- # Updated Gradio interface for Hugging Face deployment
65
  iface = gr.Interface(
66
  fn=upload_and_analyze,
67
  inputs=gr.Image(type="pil", label="Upload Advertisement Image"), # Use type="pil" for the image input
68
  outputs=gr.Textbox(label="Marketing Persona and Ad Analysis"),
69
  title="Advertisement Persona and Scoring Analyzer",
70
- 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."
71
  )
72
 
73
  # Launch the app
 
3
  import gradio as gr
4
  from dotenv import load_dotenv
5
  import io
 
6
 
7
  # Load environment variables (where your OpenAI key will be stored)
8
  load_dotenv()
9
 
10
+ # Load the OpenAI API key from environment variables and strip any trailing newlines or spaces
11
+ openai.api_key = os.getenv("OPENAI_API_KEY").strip()
12
 
13
+ # Function to analyze the ad image using GPT-4o-mini's multimodal capabilities
14
  def analyze_ad(image):
15
+ # Convert the PIL image to bytes (as required for API input)
16
  image_bytes = io.BytesIO()
17
  image.save(image_bytes, format='PNG')
18
  image_bytes = image_bytes.getvalue()
19
 
20
+ # Multimodal request: Image input to GPT-4o-mini (image + text analysis)
21
+ prompt = """
22
+ Analyze the following ad image. Extract the ad copy text from the image and generate a marketing persona.
23
+ Then, provide a score (out of 10) for each of the following:
24
+
25
+ 1. Relevance to Target Audience: Is the ad appealing to the intended demographic?
 
 
 
26
  2. Emotional Engagement: Does the ad evoke the right emotional response?
27
+ 3. Brand Consistency: Does the ad align with the brand’s voice and values?
28
+ 4. Creativity: How unique or innovative is the ad's design and text approach?
29
  5. Persuasiveness: Does the ad motivate action, such as clicking or purchasing?
 
 
30
 
31
  Provide the persona description and the scores in table form with a final score.
32
  """
33
 
34
+ # Send the image to GPT-4o-mini for multimodal processing
35
  response = openai.ChatCompletion.create(
36
+ model="gpt-4o-mini", # Use the multimodal GPT-4o-mini model
37
  messages=[
38
+ {"role": "system", "content": "You are a marketing expert analyzing an advertisement."},
39
  {"role": "user", "content": prompt}
40
  ],
 
41
  temperature=0.7,
42
+ max_tokens=400,
43
+ functions=[
44
+ {
45
+ "name": "analyze_image",
46
+ "description": "Analyze an image to extract text and generate marketing insights",
47
+ "parameters": {
48
+ "type": "image",
49
+ "properties": {
50
+ "image": {
51
+ "type": "string",
52
+ "description": "The input image for analysis"
53
+ }
54
+ },
55
+ "required": ["image"]
56
+ }
57
+ }
58
+ ],
59
+ function_call={"name": "analyze_image", "arguments": {"image": image_bytes}} # Sending the image as input
60
  )
61
 
62
+ # Extract the response text from the API output
63
  result = response['choices'][0]['message']['content']
64
 
65
  # Return the result for display
 
67
 
68
  # Function to process the image and run the analysis
69
  def upload_and_analyze(image):
70
+ # Pass the uploaded image to the analyze_ad function
71
  result = analyze_ad(image)
72
 
73
  return result
74
 
75
+ # Gradio interface for Hugging Face deployment
76
  iface = gr.Interface(
77
  fn=upload_and_analyze,
78
  inputs=gr.Image(type="pil", label="Upload Advertisement Image"), # Use type="pil" for the image input
79
  outputs=gr.Textbox(label="Marketing Persona and Ad Analysis"),
80
  title="Advertisement Persona and Scoring Analyzer",
81
+ 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."
82
  )
83
 
84
  # Launch the app