Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .gitignore +2 -0
- README.md +3 -10
- __pycache__/app.cpython-310.pyc +0 -0
- app.py +180 -0
- deploy_gradio_demo.sh +22 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
__pycache__
|
2 |
+
.env
|
README.md
CHANGED
@@ -1,13 +1,6 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji: 👁
|
4 |
-
colorFrom: gray
|
5 |
-
colorTo: green
|
6 |
-
sdk: gradio
|
7 |
-
sdk_version: 4.22.0
|
8 |
app_file: app.py
|
9 |
-
|
10 |
-
|
11 |
---
|
12 |
-
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: ai-ad-demo
|
|
|
|
|
|
|
|
|
|
|
3 |
app_file: app.py
|
4 |
+
sdk: gradio
|
5 |
+
sdk_version: 4.7.1
|
6 |
---
|
|
|
|
__pycache__/app.cpython-310.pyc
ADDED
Binary file (3.94 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,180 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import os
|
2 |
+
from PIL import Image
|
3 |
+
from io import BytesIO
|
4 |
+
from openai import OpenAI
|
5 |
+
import gradio as gr
|
6 |
+
import base64
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
|
11 |
+
demo = gr.Blocks(
|
12 |
+
css='''
|
13 |
+
footer {visibility: hidden}
|
14 |
+
''')
|
15 |
+
|
16 |
+
with demo:
|
17 |
+
client = OpenAI()
|
18 |
+
gr.Markdown('# AI Advertising Copywriter Demo')
|
19 |
+
|
20 |
+
with gr.Row():
|
21 |
+
with gr.Column():
|
22 |
+
product_name = gr.Textbox(label='Brand name or Product')
|
23 |
+
|
24 |
+
core_message = gr.Textbox(
|
25 |
+
label='Core Message'
|
26 |
+
)
|
27 |
+
|
28 |
+
product_details = gr.Textbox(
|
29 |
+
label='Product Details (Optional)',
|
30 |
+
info='Please provide product details and features.'
|
31 |
+
)
|
32 |
+
|
33 |
+
with gr.Row():
|
34 |
+
length = gr.Number(label='Length (words)', value=50)
|
35 |
+
|
36 |
+
tone = gr.Dropdown(label='Tone',
|
37 |
+
allow_custom_value=True,
|
38 |
+
choices=[
|
39 |
+
'Factual and Straightforward',
|
40 |
+
'Sentimental and Human-centric',
|
41 |
+
'Daring and Stimulating',
|
42 |
+
'Graceful and Extended',
|
43 |
+
'Dialogic and Clever',
|
44 |
+
'Perceptive and Simplistic',
|
45 |
+
'Creative and Convincing',
|
46 |
+
'Stimulating and Traditional'],
|
47 |
+
info='Input custom value by type and press Enter'
|
48 |
+
)
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
channel = gr.Dropdown(
|
52 |
+
label='Channel',
|
53 |
+
allow_custom_value=True,
|
54 |
+
choices=[
|
55 |
+
"Social Media",
|
56 |
+
"Websites/Blogs",
|
57 |
+
"Email Marketing",
|
58 |
+
"Video Marketing",
|
59 |
+
"Influencer Marketing",
|
60 |
+
"Podcast Marketing",
|
61 |
+
"Print Advertising",
|
62 |
+
"TV and Radio Advertising",
|
63 |
+
"Community Marketing",
|
64 |
+
"Digital Ads"
|
65 |
+
],
|
66 |
+
info='Input custom value by type and press Enter')
|
67 |
+
# gr.Textbox(label='Format')
|
68 |
+
|
69 |
+
target_audience = gr.Dropdown(
|
70 |
+
label='Target audience',
|
71 |
+
allow_custom_value=True,
|
72 |
+
choices=[
|
73 |
+
'Male',
|
74 |
+
'Female',
|
75 |
+
'Working Professionals',
|
76 |
+
'Middle Age',
|
77 |
+
'Student',
|
78 |
+
'Elder',
|
79 |
+
'25 to 50 years old',
|
80 |
+
'15 to 24 years old'],
|
81 |
+
multiselect=True,
|
82 |
+
info='Input custom value by type and press Enter')
|
83 |
+
|
84 |
+
# img = gr.Image()
|
85 |
+
|
86 |
+
submit_btn = gr.Button('Submit', variant='primary')
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Group():
|
90 |
+
output = gr.HTML()
|
91 |
+
|
92 |
+
gr.Markdown('Version 0.0.0 | Updated: 2024-Mar-22')
|
93 |
+
|
94 |
+
gpt_model = 'gpt-4-0125-preview' # 'gpt-3.5-turbo' 'gpt-4-vision-preview'
|
95 |
+
|
96 |
+
def img_to_b64(img):
|
97 |
+
try:
|
98 |
+
if not img:
|
99 |
+
return False
|
100 |
+
except Exception:
|
101 |
+
print(Exception)
|
102 |
+
pil_img = Image.fromarray(img)
|
103 |
+
buff = BytesIO()
|
104 |
+
pil_img.save(buff, format="JPEG")
|
105 |
+
base64_image = base64.b64encode(buff.getvalue()).decode("utf-8")
|
106 |
+
# b64 = base64.urlsafe_b64encode(img)
|
107 |
+
return f"data:image/jpeg;base64,{base64_image}"
|
108 |
+
|
109 |
+
def submit_text(
|
110 |
+
product_name,
|
111 |
+
target_audience,
|
112 |
+
core_message,
|
113 |
+
product_details,
|
114 |
+
length,
|
115 |
+
tone,
|
116 |
+
channel,
|
117 |
+
# img=None
|
118 |
+
):
|
119 |
+
|
120 |
+
# img_b64 = img_to_b64(img)
|
121 |
+
|
122 |
+
messages = [
|
123 |
+
{
|
124 |
+
'role': 'system',
|
125 |
+
'content': 'You are an experienced and award wining advertising copywriter, you will provide ad copywriting based on the given information. Please also describe the product outlook if an image is presented. Do not describe the image with "The product in the image is ...", but use "The produce is ...". Make it sounds like a proper ad copywriting. Be very careful with the word limit, do not go exceed required amount of words.'
|
126 |
+
},
|
127 |
+
{
|
128 |
+
'role': 'user',
|
129 |
+
'content': [
|
130 |
+
{
|
131 |
+
'type': 'text',
|
132 |
+
'text': f'''
|
133 |
+
PRODUCT_NAME:{product_name},
|
134 |
+
TARGET_AUDIENCES:{target_audience},
|
135 |
+
CORE_MESSAGE: {core_message},
|
136 |
+
PRODUCT_DETAILS:{product_details},
|
137 |
+
LENGTH: {length} words,
|
138 |
+
TONE:{tone},
|
139 |
+
AVERTISING_CHANNEL:{channel}
|
140 |
+
'''
|
141 |
+
},
|
142 |
+
|
143 |
+
]
|
144 |
+
},
|
145 |
+
]
|
146 |
+
|
147 |
+
# if img_b64:
|
148 |
+
# messages[1]['content'].append(
|
149 |
+
# {
|
150 |
+
# 'type': 'image_url',
|
151 |
+
# 'image_url': {
|
152 |
+
# 'url': img_b64
|
153 |
+
# }
|
154 |
+
# }
|
155 |
+
# )
|
156 |
+
|
157 |
+
completion = client.chat.completions.create(
|
158 |
+
model=gpt_model,
|
159 |
+
messages=messages
|
160 |
+
)
|
161 |
+
return gr.HTML(f'''
|
162 |
+
<p style="font-size:20px; margin:20px">
|
163 |
+
{completion.choices[0].message.content}
|
164 |
+
</p>
|
165 |
+
''')
|
166 |
+
|
167 |
+
submit_btn.click(
|
168 |
+
submit_text,
|
169 |
+
[
|
170 |
+
product_name,
|
171 |
+
target_audience,
|
172 |
+
core_message,
|
173 |
+
product_details,
|
174 |
+
length,
|
175 |
+
tone,
|
176 |
+
channel,
|
177 |
+
],
|
178 |
+
[output])
|
179 |
+
|
180 |
+
demo.launch()
|
deploy_gradio_demo.sh
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# This script used to deploy the graio app
|
2 |
+
# last update 2024-Feb-1
|
3 |
+
|
4 |
+
echo '1. Update version number'
|
5 |
+
read -r -p "Have you checked all the testing items?(y/n)" response
|
6 |
+
echo # (optional) move to a new line
|
7 |
+
|
8 |
+
# response=${response,,} # tolower
|
9 |
+
if [ $response = "y" ]; then
|
10 |
+
echo 'Deploying ...'
|
11 |
+
|
12 |
+
# Store .env content to local variable
|
13 |
+
env_value=$(<.env)
|
14 |
+
|
15 |
+
# Remove .env file, because it will be publish to gradio
|
16 |
+
rm .env || echo 'no .env found'
|
17 |
+
|
18 |
+
gradio deploy
|
19 |
+
fi
|
20 |
+
|
21 |
+
# Save back .env file
|
22 |
+
echo "$env_value" > './.env'
|