dindizz commited on
Commit
0d8b8d6
1 Parent(s): eda6db8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -29
app.py CHANGED
@@ -3,6 +3,8 @@ import os
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()
@@ -10,17 +12,17 @@ load_dotenv()
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?
@@ -28,35 +30,20 @@ def analyze_ad(image):
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
 
3
  import gradio as gr
4
  from dotenv import load_dotenv
5
  import io
6
+ from PIL import Image
7
+ import pytesseract # Import Tesseract OCR
8
 
9
  # Load environment variables (where your OpenAI key will be stored)
10
  load_dotenv()
 
12
  # Load the OpenAI API key from environment variables and strip any trailing newlines or spaces
13
  openai.api_key = os.getenv("OPENAI_API_KEY").strip()
14
 
15
+ # Function to analyze the ad and generate marketing personas + scoring
16
  def analyze_ad(image):
17
+ # Extract text from the image using Tesseract OCR
18
+ ad_copy = pytesseract.image_to_string(image)
 
 
19
 
20
+ if not ad_copy.strip(): # Check if OCR extracted any text
21
+ return "No text was detected in the image. Please upload a clearer ad image."
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: Is the ad appealing to the intended demographic?
28
  2. Emotional Engagement: Does the ad evoke the right emotional response?
 
30
  4. Creativity: How unique or innovative is the ad's design and text approach?
31
  5. Persuasiveness: Does the ad motivate action, such as clicking or purchasing?
32
 
33
+ Ad Copy: {ad_copy}
34
+
35
  Provide the persona description and the scores in table form with a final score.
36
  """
37
 
38
+ # Send the prompt to GPT-4o-mini for analysis
39
  response = openai.ChatCompletion.create(
40
+ model="gpt-4o-mini", # Use the gpt-4o-mini model as requested
41
  messages=[
42
  {"role": "system", "content": "You are a marketing expert analyzing an advertisement."},
43
  {"role": "user", "content": prompt}
44
  ],
45
  temperature=0.7,
46
+ max_tokens=400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  )
48
 
49
  # Extract the response text from the API output