dindizz commited on
Commit
c30f297
1 Parent(s): 517f94a

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 # 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,42 +11,57 @@ 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 image by first extracting the text with pytesseract
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(): # If OCR doesn't extract text, return an error message
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-4-turbo for analysis
39
  response = openai.ChatCompletion.create(
40
- model="gpt-4-turbo",
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
50
- result = response['choices'][0]['message']['content']
51
 
52
  # Return the result for display
53
  return result
 
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 Vision's multimodal capabilities
15
  def analyze_ad(image):
16
+ # Convert the PIL image to bytes for GPT-4 Vision input
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 and extract any text present in the image.
24
+ Then, generate a marketing persona based on the ad. 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 multimodal analysis
36
  response = openai.ChatCompletion.create(
37
+ model="gpt-4-turbo", # Use the GPT-4 Vision-enabled 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
64
+ result = response['choices'][0]['message']['content'].strip()
65
 
66
  # Return the result for display
67
  return result