Spaces:
Running
Running
Update generator_function/image_function.py
Browse files
generator_function/image_function.py
CHANGED
|
@@ -330,42 +330,39 @@ def handle_image_generation_optimized(
|
|
| 330 |
|
| 331 |
def generate_image(file_path, size, quality, category, sentiment, user_prompt, platform, blur, i=None):
|
| 332 |
try:
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
return image_bytes
|
| 367 |
-
else:
|
| 368 |
-
|
| 369 |
except Exception as e:
|
| 370 |
logger.exception(f"Failed to generate image for {file_path}: {e}")
|
| 371 |
raise
|
|
|
|
| 330 |
|
| 331 |
def generate_image(file_path, size, quality, category, sentiment, user_prompt, platform, blur, i=None):
|
| 332 |
try:
|
| 333 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 334 |
+
if not api_key:
|
| 335 |
+
logger.critical("OPENAI_API_KEY is not set.")
|
| 336 |
+
raise RuntimeError("OPENAI_API_KEY is missing")
|
| 337 |
+
client = OpenAI(api_key=api_key)
|
| 338 |
+
|
| 339 |
+
if platform == "Google Display Network":
|
| 340 |
+
size_messages = {0: "The aspect ratio of the image should be 1024x1024",
|
| 341 |
+
1: "The aspect ratio of the image should be 1536x1024.",
|
| 342 |
+
2: "The aspect ratio of the image should be 1024x1536."}
|
| 343 |
+
img_size = size_messages.get(i, "")
|
| 344 |
+
else:
|
| 345 |
+
img_size = ""
|
| 346 |
+
|
| 347 |
+
with open(file_path, "rb") as img_file:
|
| 348 |
+
background = "blurred background." if blur else " not blurred background."
|
| 349 |
+
result = client.images.edit(
|
| 350 |
+
model="gpt-image-1",
|
| 351 |
+
prompt=(
|
| 352 |
+
f"You are a top-tier performance digital marketer and creative strategist with 15+ years of expertise in affiliate marketing.\n"
|
| 353 |
+
f"Your objective is to analyze the provided winning ad image, deconstruct its concept, visual composition, and color scheme, and generate a fresh, conversion-focused ad visual tailored for the {category} niche.\n"
|
| 354 |
+
f"The new design should convey a {sentiment} sentiment and incorporate the user instruction: \n {user_prompt}.\n If user has given multple choices or options to be include in the image so choose randomly relevant to the reference image."
|
| 355 |
+
f"Create a visually compelling ad optimized for {platform} Ads that is scroll-stopping, pattern-interrupting, and designed to drive high CTR and Conversion Rate. Utilize striking color combinations, dynamic contrast levels, and strategic layout compositions to command attention while aligning with the target audience avatar.\n"
|
| 356 |
+
f"Make sure the images should be realistic, not be stocky at all and raw which should look like they are shot from an iPhone with {background}.{img_size}"
|
| 357 |
+
),
|
| 358 |
+
image=img_file,
|
| 359 |
+
size=size,
|
| 360 |
+
quality=quality,
|
| 361 |
+
)
|
| 362 |
+
image_base64 = result.data[0].b64_json
|
| 363 |
+
image_bytes = base64.b64decode(image_base64)
|
| 364 |
+
logger.info(f"Successfully generated image for {file_path}")
|
| 365 |
+
return image_bytes
|
|
|
|
|
|
|
|
|
|
| 366 |
except Exception as e:
|
| 367 |
logger.exception(f"Failed to generate image for {file_path}: {e}")
|
| 368 |
raise
|