Spaces:
Sleeping
Sleeping
File size: 5,276 Bytes
524187e d080bf0 524187e d080bf0 524187e d080bf0 524187e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
from openai import OpenAI
import base64
import io
from dotenv import load_dotenv
import os
load_dotenv()
def _encode_image_to_base64(image_path) -> str:
"""
Helper to read an image file from disk and encode it to base64.
"""
buffer = io.BytesIO()
image_path.save(buffer, format="JPEG")
image_bytes = buffer.getvalue()
image_string = base64.b64encode(image_bytes).decode("utf-8")
final_string = f"data:image/jpeg;base64,{image_string}"
return final_string
def generate_gpt_prompt(
pos_img_path: str,
neg_img_path: str,
category: str,
story_instruction: str,
sentiment, image_elements, is_text, text_elements, non_compliant, emotion
):
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Prepare base64 images
base64_pos = _encode_image_to_base64(pos_img_path)
base64_neg = _encode_image_to_base64(neg_img_path) if neg_img_path else None
# 1) ANALYSIS
if base64_neg:
# Two images
user_content_analysis = [
{
"type": "text",
"text": f"""These are the images of {category} ad category.
The first image is winning creative and the other is losing creative.
Think and Analyse these images and list down the winning and losing elements.
Make sure you give response in markdown."""
},
{
"type": "image_url",
"image_url": {"url": f"{base64_pos}"}
},
{
"type": "image_url",
"image_url": {"url": f"{base64_neg}"}
},
]
else:
# Single image
user_content_analysis = [
{
"type": "text",
"text": f"""This is the winning image of {category} ad category.
Think and Analyse this image and list down the possible winning elements.
Make sure you give response in markdown."""
},
{
"type": "image_url",
"image_url": {"url": f"{base64_pos}"}
}
]
response_analyses = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": user_content_analysis}],
)
analyses = response_analyses.choices[0].message.content
story_prompt = f"Based on this analysis, generate me the new image story for the {category} ad whose Sentiment of the new image story MUST be {sentiment}."
if image_elements:
story_prompt += f"You MUST creatively add these {image_elements} image elements."
if is_text is True:
story_prompt += f"Also, MUST add these '{text_elements}' text in new image story."
else:
story_prompt += "DON'T add any text overlays on the image."
if non_compliant:
story_prompt += f"DON'T add the mentioned stuff in the image story {non_compliant}."
if emotion:
story_prompt += f"The image should reflect {emotion} emotion."
story_prompt += f"Blend everything with new image story {story_instruction}"
# 2) STORY
user_content_story = [
{
"type": "text",
"text": f"""{story_prompt}"""
}
]
response_story = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": user_content_analysis,
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": analyses
}
]
},
{
"role": "user",
"content": user_content_story
}
],
)
story = response_story.choices[0].message.content
# 3) PROMPT
final_user_content_prompt = [
{
"type": "text",
"text": """Now convert this image description to the prompt for the image generation through ideogram model.
The prompt should be short and crisp for it to understand really well.
Please also consider the background colors, text colors, text overlays and any other elements in the image to be included in prompt.
Make sure you only return the prompt."""
}
]
response_prompt = client.chat.completions.create(
model="gpt-4o",
messages=[
{
"role": "user",
"content": user_content_analysis,
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": analyses
}
]
},
{
"role": "user",
"content": user_content_story
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": story
}
]
},
{
"role": "user",
"content": final_user_content_prompt
}
],
)
final_prompt = response_prompt.choices[0].message.content
return final_prompt |