dindizz commited on
Commit
37255b7
1 Parent(s): f722a57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
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 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.chat_completions.create(
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
- # Pass the uploaded image to the analyze_ad function
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"), # Use type="pil" for the image input
66
  outputs=gr.Textbox(label="Marketing Persona and Ad Analysis"),
67
  title="Advertisement Persona and Scoring Analyzer",
68
- 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."
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