dindizz commited on
Commit
ff93ad0
1 Parent(s): df30f5a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ 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 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
55
+ return result
56
+
57
+ # Function to load and display the image
58
+ def upload_and_analyze(image):
59
+ # Open the image and display
60
+ ad_image = Image.open(image)
61
+
62
+ # Analyze the ad
63
+ result = analyze_ad(ad_image)
64
+
65
+ return result
66
+
67
+ # Interface using Gradio for Hugging Face deployment
68
+ # Simple UI that takes an image upload
69
+ iface = gr.Interface(
70
+ fn=upload_and_analyze,
71
+ inputs=gr.inputs.Image(type="file", label="Upload Advertisement Image"),
72
+ outputs=gr.outputs.Textbox(label="Marketing Persona and Ad Analysis"),
73
+ title="Advertisement Persona and Scoring Analyzer",
74
+ 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."
75
+ )
76
+
77
+ # Launch the app
78
+ if __name__ == "__main__":
79
+ iface.launch()