ai-ad-demo / app.py
zq13648's picture
Upload folder using huggingface_hub
4591ab4 verified
# import os
from PIL import Image
from io import BytesIO
from openai import OpenAI
import gradio as gr
import base64
import textract
from dotenv import load_dotenv
load_dotenv()
demo = gr.Blocks(
css='''
footer {visibility: hidden}
''')
with demo:
client = OpenAI()
gr.Markdown('# AI Advertising Copywriter Demo')
with gr.Row():
with gr.Column():
product_name = gr.Textbox(label='Brand name or Product')
core_message = gr.Textbox(
label='Core Message'
)
product_details = gr.Textbox(
label='Product Details (Optional)',
info='Please provide product details and features.'
)
product_document = gr.File(
label='Product Document'
)
content_restrictions = gr.Textbox(
label='Content Restrictions',
info='Use phrases like "Do not ... ..." to enforce model behavior.'
)
with gr.Row():
length = gr.Number(label='Length (words)', value=50)
tone = gr.Dropdown(label='Tone',
allow_custom_value=True,
choices=[
'Factual and Straightforward',
'Sentimental and Human-centric',
'Daring and Stimulating',
'Graceful and Extended',
'Dialogic and Clever',
'Perceptive and Simplistic',
'Creative and Convincing',
'Stimulating and Traditional'],
info='Input custom value by type and press Enter'
)
with gr.Row():
channel = gr.Dropdown(
label='Channel',
allow_custom_value=True,
choices=[
"Social Media",
"Websites/Blogs",
"Email Marketing",
"Video Marketing",
"Influencer Marketing",
"Podcast Marketing",
"Print Advertising",
"TV and Radio Advertising",
"Community Marketing",
"Digital Ads"
],
info='Input custom value by type and press Enter')
# gr.Textbox(label='Format')
target_audience = gr.Dropdown(
label='Target audience',
allow_custom_value=True,
choices=[
'Male',
'Female',
'Working Professionals',
'Middle Age',
'Student',
'Elder',
'25 to 50 years old',
'15 to 24 years old'],
multiselect=True,
info='Input custom value by type and press Enter')
# img = gr.Image()
submit_btn = gr.Button('Submit', variant='primary')
with gr.Row():
with gr.Group():
output = gr.HTML()
gr.Markdown('Version 0.0.1 | Updated: 2024-Mar-29')
gpt_model = 'gpt-4-0125-preview' # 'gpt-3.5-turbo' 'gpt-4-vision-preview'
def img_to_b64(img):
try:
if not img:
return False
except Exception:
print(Exception)
pil_img = Image.fromarray(img)
buff = BytesIO()
pil_img.save(buff, format="JPEG")
base64_image = base64.b64encode(buff.getvalue()).decode("utf-8")
# b64 = base64.urlsafe_b64encode(img)
return f"data:image/jpeg;base64,{base64_image}"
def submit_text(
product_name,
target_audience,
core_message,
product_details,
product_document,
length,
tone,
channel,
content_restrictions,
# img=None
):
# img_b64 = img_to_b64(img)
product_details = product_details + read_doc_file(product_document)
messages = [
{
'role': 'system',
'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.'
},
{
'role': 'user',
'content': [
{
'type': 'text',
'text': f'''
PRODUCT_NAME:{product_name},
TARGET_AUDIENCES:{target_audience},
CORE_MESSAGE: {core_message},
PRODUCT_DETAILS:{product_details},
LENGTH: {length} words,
TONE:{tone},
AVERTISING_CHANNEL:{channel},
CONTENT_RESTRICTIONS:{content_restrictions}
'''
},
]
},
]
# if img_b64:
# messages[1]['content'].append(
# {
# 'type': 'image_url',
# 'image_url': {
# 'url': img_b64
# }
# }
# )
completion = client.chat.completions.create(
model=gpt_model,
messages=messages
)
return gr.HTML(f'''
<p style="font-size:20px; margin:20px">
{completion.choices[0].message.content}
</p>
''')
def read_doc_file(file):
try:
doc_text = textract.process(file)
except Exception:
return ''
return doc_text.decode("utf-8")
submit_btn.click(
submit_text,
[
product_name,
target_audience,
core_message,
product_details,
product_document,
length,
tone,
channel,
content_restrictions,
],
[output])
demo.launch()