userIdc2024 commited on
Commit
b80a4a4
·
verified ·
1 Parent(s): 2945545

Update generator_function/image_function.py

Browse files
Files changed (1) hide show
  1. generator_function/image_function.py +33 -36
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
- if model == "GPT"
334
- api_key = os.getenv("OPENAI_API_KEY")
335
- if not api_key:
336
- logger.critical("OPENAI_API_KEY is not set.")
337
- raise RuntimeError("OPENAI_API_KEY is missing")
338
- client = OpenAI(api_key=api_key)
339
-
340
- if platform == "Google Display Network":
341
- size_messages = {0: "The aspect ratio of the image should be 1024x1024",
342
- 1: "The aspect ratio of the image should be 1536x1024.",
343
- 2: "The aspect ratio of the image should be 1024x1536."}
344
- img_size = size_messages.get(i, "")
345
- else:
346
- img_size = ""
347
-
348
- with open(file_path, "rb") as img_file:
349
- background = "blurred background." if blur else " not blurred background."
350
- result = client.images.edit(
351
- model="gpt-image-1",
352
- prompt=(
353
- f"You are a top-tier performance digital marketer and creative strategist with 15+ years of expertise in affiliate marketing.\n"
354
- 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"
355
- 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."
356
- 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"
357
- 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}"
358
- ),
359
- image=img_file,
360
- size=size,
361
- quality=quality,
362
- )
363
- image_base64 = result.data[0].b64_json
364
- image_bytes = base64.b64decode(image_base64)
365
- logger.info(f"Successfully generated image for {file_path}")
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