dindizz commited on
Commit
abeac40
1 Parent(s): 197048e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
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 # Import Tesseract OCR
8
 
9
  # Load environment variables (where your OpenAI key will be stored)
10
  load_dotenv()
@@ -12,38 +11,53 @@ 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?
29
  3. Brand Consistency: Does the ad align with the brand’s voice and values?
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
 
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 and strip any trailing newlines or spaces
12
  openai.api_key = os.getenv("OPENAI_API_KEY").strip()
13
 
14
+ # Function to analyze the ad image using GPT-4-turbo's multimodal capabilities
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
  # Prompt for the marketing persona and scoring rubric
22
+ prompt = """
23
+ Analyze this advertisement image. Extract any text present in the image and generate a marketing persona.
24
+ Then, provide a score (out of 10) for each of the following:
25
+
26
  1. Relevance to Target Audience: Is the ad appealing to the intended demographic?
27
  2. Emotional Engagement: Does the ad evoke the right emotional response?
28
  3. Brand Consistency: Does the ad align with the brand’s voice and values?
29
  4. Creativity: How unique or innovative is the ad's design and text approach?
30
  5. Persuasiveness: Does the ad motivate action, such as clicking or purchasing?
31
+
 
 
32
  Provide the persona description and the scores in table form with a final score.
33
  """
34
 
35
+ # Send the image and prompt to GPT-4-turbo for analysis
36
  response = openai.ChatCompletion.create(
37
+ model="gpt-4-turbo-vision", # Use the multimodal GPT-4-turbo model
38
  messages=[
39
  {"role": "system", "content": "You are a marketing expert analyzing an advertisement."},
40
  {"role": "user", "content": prompt}
41
  ],
42
+ functions=[
43
+ {
44
+ "name": "analyze_image",
45
+ "description": "Analyze an image and generate marketing insights",
46
+ "parameters": {
47
+ "type": "image",
48
+ "properties": {
49
+ "image": {
50
+ "type": "string",
51
+ "description": "The input advertisement image for analysis"
52
+ }
53
+ },
54
+ "required": ["image"]
55
+ }
56
+ }
57
+ ],
58
+ function_call={"name": "analyze_image", "arguments": {"image": image_bytes}}, # Sending the image as input
59
  temperature=0.7,
60
+ max_tokens=500
61
  )
62
 
63
  # Extract the response text from the API output