Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app/services/banner_service.py +146 -0
- app/services/bestnetwork_service.py +135 -0
- app/services/byop_service.py +135 -0
app/services/banner_service.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google import genai
|
| 2 |
+
from google.genai import types
|
| 3 |
+
from typing import Dict
|
| 4 |
+
from PIL import Image, ImageOps, ImageEnhance
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from app.config.settings import settings
|
| 7 |
+
from app.utils.s3_utils import upload_image_to_s3
|
| 8 |
+
|
| 9 |
+
def get_banner_prompts(brand_name: str, domain: str) -> list[str]:
|
| 10 |
+
prompts = [
|
| 11 |
+
f"Generate a captivating banner for {brand_name}, in the realm of {domain}. Design an abstract and thought-provoking composition that will crop well to various aspect ratios (desktop, tablet, mobile) without losing its core visual impact. Focus on non-representational visuals that communicate the core essence and values of the brand, ensuring key visual elements are centrally located or distributed in a way that remains effective when cropped.",
|
| 12 |
+
f"Create a modern and innovative banner featuring bold geometric shapes and dynamic patterns in a contemporary color palette, suitable for {brand_name} in the {domain} industry. The visual design should convey professionalism and a forward-thinking approach and be adaptable for different screen sizes, ensuring cropped versions retain a balanced and visually appealing composition. Consider how central elements and overall visual flow will be maintained across desktop, tablet, and mobile formats. No text should be included in the image."
|
| 13 |
+
]
|
| 14 |
+
return prompts
|
| 15 |
+
|
| 16 |
+
def process_image(image_data: bytes, target_width: int, target_height: int) -> bytes:
|
| 17 |
+
img = Image.open(BytesIO(image_data)).convert('RGB')
|
| 18 |
+
|
| 19 |
+
scale_factor = 4
|
| 20 |
+
intermediate_width = target_width * scale_factor
|
| 21 |
+
intermediate_height = target_height * scale_factor
|
| 22 |
+
|
| 23 |
+
upscaled_img = ImageOps.fit(
|
| 24 |
+
img,
|
| 25 |
+
(intermediate_width, intermediate_height),
|
| 26 |
+
method=Image.LANCZOS,
|
| 27 |
+
centering=(0.5, 0.5)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
enhancer = ImageEnhance.Sharpness(upscaled_img)
|
| 31 |
+
upscaled_img = enhancer.enhance(1.3)
|
| 32 |
+
|
| 33 |
+
contrast = ImageEnhance.Contrast(upscaled_img)
|
| 34 |
+
upscaled_img = contrast.enhance(1.1)
|
| 35 |
+
|
| 36 |
+
final_img = ImageOps.fit(
|
| 37 |
+
upscaled_img,
|
| 38 |
+
(target_width, target_height),
|
| 39 |
+
method=Image.LANCZOS,
|
| 40 |
+
centering=(0.5, 0.5)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
enhancer = ImageEnhance.Sharpness(final_img)
|
| 44 |
+
final_img = enhancer.enhance(1.1)
|
| 45 |
+
|
| 46 |
+
color_enhancer = ImageEnhance.Color(final_img)
|
| 47 |
+
final_img = color_enhancer.enhance(1.1)
|
| 48 |
+
|
| 49 |
+
buffer = BytesIO()
|
| 50 |
+
|
| 51 |
+
quality = 95
|
| 52 |
+
while True:
|
| 53 |
+
buffer.seek(0)
|
| 54 |
+
buffer.truncate()
|
| 55 |
+
|
| 56 |
+
if quality >= 90:
|
| 57 |
+
final_img.save(
|
| 58 |
+
buffer,
|
| 59 |
+
format='JPEG',
|
| 60 |
+
quality=quality,
|
| 61 |
+
optimize=True,
|
| 62 |
+
progressive=True,
|
| 63 |
+
subsampling=0
|
| 64 |
+
)
|
| 65 |
+
else:
|
| 66 |
+
final_img.save(
|
| 67 |
+
buffer,
|
| 68 |
+
format='JPEG',
|
| 69 |
+
quality=quality,
|
| 70 |
+
optimize=True,
|
| 71 |
+
progressive=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if buffer.tell() <= 500 * 1024 or quality <= 80:
|
| 75 |
+
break
|
| 76 |
+
quality -= 3
|
| 77 |
+
|
| 78 |
+
return buffer.getvalue()
|
| 79 |
+
|
| 80 |
+
def process_images_for_devices(image_data: bytes) -> Dict[str, bytes]:
|
| 81 |
+
"""Process images for different device types."""
|
| 82 |
+
return {
|
| 83 |
+
'desktop': process_image(image_data, 1440, 500),
|
| 84 |
+
'ipad': process_image(image_data, 768, 500),
|
| 85 |
+
'mobile': process_image(image_data, 375, 500)
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
def generate_banner(brand_name: str, domain: str) -> Dict:
|
| 89 |
+
api_key = settings.GEMINI_API_KEY
|
| 90 |
+
client = genai.Client(api_key=api_key)
|
| 91 |
+
prompts = get_banner_prompts(brand_name, domain)
|
| 92 |
+
all_banner_results = []
|
| 93 |
+
|
| 94 |
+
for prompt_index, prompt in enumerate(prompts, 1):
|
| 95 |
+
try:
|
| 96 |
+
response = client.models.generate_content(
|
| 97 |
+
model="gemini-2.0-flash-exp-image-generation",
|
| 98 |
+
contents=prompt,
|
| 99 |
+
config=types.GenerateContentConfig(
|
| 100 |
+
response_modalities=['TEXT', 'IMAGE']
|
| 101 |
+
)
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
for part in response.candidates[0].content.parts:
|
| 105 |
+
if part.inline_data is not None:
|
| 106 |
+
try:
|
| 107 |
+
mime_type = part.inline_data.mime_type
|
| 108 |
+
image_data = part.inline_data.data
|
| 109 |
+
|
| 110 |
+
image_format = "png"
|
| 111 |
+
if "jpeg" in mime_type or "jpg" in mime_type:
|
| 112 |
+
image_format = "jpeg"
|
| 113 |
+
elif "png" in mime_type:
|
| 114 |
+
image_format = "png"
|
| 115 |
+
elif "webp" in mime_type:
|
| 116 |
+
image_format = "webp"
|
| 117 |
+
|
| 118 |
+
processed_images = process_images_for_devices(image_data)
|
| 119 |
+
|
| 120 |
+
banner_results = {}
|
| 121 |
+
for device, img_data in processed_images.items():
|
| 122 |
+
result = upload_image_to_s3(
|
| 123 |
+
img_data,
|
| 124 |
+
f"banner_{brand_name}_{device}_v{prompt_index}",
|
| 125 |
+
image_format,
|
| 126 |
+
f"{device}_v{prompt_index}"
|
| 127 |
+
)
|
| 128 |
+
banner_results[device] = result
|
| 129 |
+
|
| 130 |
+
all_banner_results.append({
|
| 131 |
+
"variation": prompt_index,
|
| 132 |
+
"banners": banner_results
|
| 133 |
+
})
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
print(f"Error processing banner image for variation {prompt_index}: {str(e)}")
|
| 137 |
+
raise
|
| 138 |
+
|
| 139 |
+
except Exception as e:
|
| 140 |
+
print(f"Error generating banner for variation {prompt_index}: {str(e)}")
|
| 141 |
+
raise
|
| 142 |
+
|
| 143 |
+
if not all_banner_results:
|
| 144 |
+
raise Exception("No images were generated")
|
| 145 |
+
|
| 146 |
+
return {"variations": all_banner_results}
|
app/services/bestnetwork_service.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google import genai
|
| 2 |
+
from google.genai import types
|
| 3 |
+
from typing import Dict
|
| 4 |
+
from PIL import Image, ImageOps, ImageEnhance
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from app.config.settings import settings
|
| 7 |
+
from app.utils.s3_utils import upload_image_to_s3
|
| 8 |
+
|
| 9 |
+
def get_network_prompts(brand_name: str, domain: str) -> list[str]:
|
| 10 |
+
prompts = [
|
| 11 |
+
f"Generate a powerful banner for {brand_name}'s network coverage in the {domain} sector. Create a sophisticated visualization showing a cityscape or landscape overlaid with dynamic network signals, coverage maps, or signal strength indicators. Use a modern color scheme that emphasizes reliability and strength. The composition should be optimized for 530x352 dimensions while maintaining visual clarity.",
|
| 12 |
+
f"Design an impactful banner for {brand_name}'s network superiority in the {domain} industry. Create a bold, abstract representation of network strength using waves, particles, or geometric patterns that suggest widespread coverage and connectivity. Incorporate subtle depth and dimension to emphasize network reach. Ensure the design is effective at 530x352 resolution. No text should be included in the image."
|
| 13 |
+
]
|
| 14 |
+
return prompts
|
| 15 |
+
|
| 16 |
+
def process_image(image_data: bytes, width: int = 530, height: int = 352) -> bytes:
|
| 17 |
+
img = Image.open(BytesIO(image_data)).convert('RGB')
|
| 18 |
+
|
| 19 |
+
scale_factor = 4
|
| 20 |
+
intermediate_width = width * scale_factor
|
| 21 |
+
intermediate_height = height * scale_factor
|
| 22 |
+
|
| 23 |
+
upscaled_img = ImageOps.fit(
|
| 24 |
+
img,
|
| 25 |
+
(intermediate_width, intermediate_height),
|
| 26 |
+
method=Image.LANCZOS,
|
| 27 |
+
centering=(0.5, 0.5)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
enhancer = ImageEnhance.Sharpness(upscaled_img)
|
| 31 |
+
upscaled_img = enhancer.enhance(1.3)
|
| 32 |
+
|
| 33 |
+
contrast = ImageEnhance.Contrast(upscaled_img)
|
| 34 |
+
upscaled_img = contrast.enhance(1.1)
|
| 35 |
+
|
| 36 |
+
final_img = ImageOps.fit(
|
| 37 |
+
upscaled_img,
|
| 38 |
+
(width, height),
|
| 39 |
+
method=Image.LANCZOS,
|
| 40 |
+
centering=(0.5, 0.5)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
enhancer = ImageEnhance.Sharpness(final_img)
|
| 44 |
+
final_img = enhancer.enhance(1.1)
|
| 45 |
+
|
| 46 |
+
color_enhancer = ImageEnhance.Color(final_img)
|
| 47 |
+
final_img = color_enhancer.enhance(1.1)
|
| 48 |
+
|
| 49 |
+
buffer = BytesIO()
|
| 50 |
+
|
| 51 |
+
quality = 95
|
| 52 |
+
while True:
|
| 53 |
+
buffer.seek(0)
|
| 54 |
+
buffer.truncate()
|
| 55 |
+
|
| 56 |
+
if quality >= 90:
|
| 57 |
+
final_img.save(
|
| 58 |
+
buffer,
|
| 59 |
+
format='JPEG',
|
| 60 |
+
quality=quality,
|
| 61 |
+
optimize=True,
|
| 62 |
+
progressive=True,
|
| 63 |
+
subsampling=0
|
| 64 |
+
)
|
| 65 |
+
else:
|
| 66 |
+
final_img.save(
|
| 67 |
+
buffer,
|
| 68 |
+
format='JPEG',
|
| 69 |
+
quality=quality,
|
| 70 |
+
optimize=True,
|
| 71 |
+
progressive=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if buffer.tell() <= 500 * 1024 or quality <= 80:
|
| 75 |
+
break
|
| 76 |
+
quality -= 3
|
| 77 |
+
|
| 78 |
+
return buffer.getvalue()
|
| 79 |
+
|
| 80 |
+
def generate_network_banner(brand_name: str, domain: str) -> Dict:
|
| 81 |
+
api_key = settings.GEMINI_API_KEY
|
| 82 |
+
client = genai.Client(api_key=api_key)
|
| 83 |
+
prompts = get_network_prompts(brand_name, domain)
|
| 84 |
+
all_banner_results = []
|
| 85 |
+
|
| 86 |
+
for prompt_index, prompt in enumerate(prompts, 1):
|
| 87 |
+
try:
|
| 88 |
+
response = client.models.generate_content(
|
| 89 |
+
model="gemini-2.0-flash-exp-image-generation",
|
| 90 |
+
contents=prompt,
|
| 91 |
+
config=types.GenerateContentConfig(
|
| 92 |
+
response_modalities=['TEXT', 'IMAGE']
|
| 93 |
+
)
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
for part in response.candidates[0].content.parts:
|
| 97 |
+
if part.inline_data is not None:
|
| 98 |
+
try:
|
| 99 |
+
mime_type = part.inline_data.mime_type
|
| 100 |
+
image_data = part.inline_data.data
|
| 101 |
+
|
| 102 |
+
image_format = "png"
|
| 103 |
+
if "jpeg" in mime_type or "jpg" in mime_type:
|
| 104 |
+
image_format = "jpeg"
|
| 105 |
+
elif "png" in mime_type:
|
| 106 |
+
image_format = "png"
|
| 107 |
+
elif "webp" in mime_type:
|
| 108 |
+
image_format = "webp"
|
| 109 |
+
|
| 110 |
+
processed_image = process_image(image_data)
|
| 111 |
+
|
| 112 |
+
result = upload_image_to_s3(
|
| 113 |
+
processed_image,
|
| 114 |
+
f"network_banner_{brand_name}_v{prompt_index}",
|
| 115 |
+
image_format,
|
| 116 |
+
f"network_v{prompt_index}"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
all_banner_results.append({
|
| 120 |
+
"variation": prompt_index,
|
| 121 |
+
"banner": result
|
| 122 |
+
})
|
| 123 |
+
|
| 124 |
+
except Exception as e:
|
| 125 |
+
print(f"Error processing network banner image for variation {prompt_index}: {str(e)}")
|
| 126 |
+
raise
|
| 127 |
+
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"Error generating network banner for variation {prompt_index}: {str(e)}")
|
| 130 |
+
raise
|
| 131 |
+
|
| 132 |
+
if not all_banner_results:
|
| 133 |
+
raise Exception("No images were generated")
|
| 134 |
+
|
| 135 |
+
return {"variations": all_banner_results}
|
app/services/byop_service.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from google import genai
|
| 2 |
+
from google.genai import types
|
| 3 |
+
from typing import Dict
|
| 4 |
+
from PIL import Image, ImageOps, ImageEnhance
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from app.config.settings import settings
|
| 7 |
+
from app.utils.s3_utils import upload_image_to_s3
|
| 8 |
+
|
| 9 |
+
def get_byop_prompts(brand_name: str, domain: str) -> list[str]:
|
| 10 |
+
prompts = [
|
| 11 |
+
f"Create a modern, customer-centric banner for {brand_name}'s Bring Your Own Phone service in the {domain} industry. Design a clean, minimalist composition featuring a sleek smartphone with a seamless network transition effect. Incorporate abstract elements that suggest flexibility and freedom of choice. The image should be optimized for a 530x352 display while maintaining visual impact.",
|
| 12 |
+
f"Design a contemporary banner for {brand_name}'s BYOP program that emphasizes the ease of switching networks. Create a visually appealing composition showing multiple smartphones with dynamic connection lines or waves, symbolizing seamless network transition. Use a modern color palette that aligns with {domain} industry standards. Ensure the design works effectively in a 530x352 format. No text should be included in the image."
|
| 13 |
+
]
|
| 14 |
+
return prompts
|
| 15 |
+
|
| 16 |
+
def process_image(image_data: bytes, width: int = 530, height: int = 352) -> bytes:
|
| 17 |
+
img = Image.open(BytesIO(image_data)).convert('RGB')
|
| 18 |
+
|
| 19 |
+
scale_factor = 4
|
| 20 |
+
intermediate_width = width * scale_factor
|
| 21 |
+
intermediate_height = height * scale_factor
|
| 22 |
+
|
| 23 |
+
upscaled_img = ImageOps.fit(
|
| 24 |
+
img,
|
| 25 |
+
(intermediate_width, intermediate_height),
|
| 26 |
+
method=Image.LANCZOS,
|
| 27 |
+
centering=(0.5, 0.5)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
enhancer = ImageEnhance.Sharpness(upscaled_img)
|
| 31 |
+
upscaled_img = enhancer.enhance(1.3)
|
| 32 |
+
|
| 33 |
+
contrast = ImageEnhance.Contrast(upscaled_img)
|
| 34 |
+
upscaled_img = contrast.enhance(1.1)
|
| 35 |
+
|
| 36 |
+
final_img = ImageOps.fit(
|
| 37 |
+
upscaled_img,
|
| 38 |
+
(width, height),
|
| 39 |
+
method=Image.LANCZOS,
|
| 40 |
+
centering=(0.5, 0.5)
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
enhancer = ImageEnhance.Sharpness(final_img)
|
| 44 |
+
final_img = enhancer.enhance(1.1)
|
| 45 |
+
|
| 46 |
+
color_enhancer = ImageEnhance.Color(final_img)
|
| 47 |
+
final_img = color_enhancer.enhance(1.1)
|
| 48 |
+
|
| 49 |
+
buffer = BytesIO()
|
| 50 |
+
|
| 51 |
+
quality = 95
|
| 52 |
+
while True:
|
| 53 |
+
buffer.seek(0)
|
| 54 |
+
buffer.truncate()
|
| 55 |
+
|
| 56 |
+
if quality >= 90:
|
| 57 |
+
final_img.save(
|
| 58 |
+
buffer,
|
| 59 |
+
format='JPEG',
|
| 60 |
+
quality=quality,
|
| 61 |
+
optimize=True,
|
| 62 |
+
progressive=True,
|
| 63 |
+
subsampling=0
|
| 64 |
+
)
|
| 65 |
+
else:
|
| 66 |
+
final_img.save(
|
| 67 |
+
buffer,
|
| 68 |
+
format='JPEG',
|
| 69 |
+
quality=quality,
|
| 70 |
+
optimize=True,
|
| 71 |
+
progressive=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
if buffer.tell() <= 500 * 1024 or quality <= 80:
|
| 75 |
+
break
|
| 76 |
+
quality -= 3
|
| 77 |
+
|
| 78 |
+
return buffer.getvalue()
|
| 79 |
+
|
| 80 |
+
def generate_byop_banner(brand_name: str, domain: str) -> Dict:
|
| 81 |
+
api_key = settings.GEMINI_API_KEY
|
| 82 |
+
client = genai.Client(api_key=api_key)
|
| 83 |
+
prompts = get_byop_prompts(brand_name, domain)
|
| 84 |
+
all_banner_results = []
|
| 85 |
+
|
| 86 |
+
for prompt_index, prompt in enumerate(prompts, 1):
|
| 87 |
+
try:
|
| 88 |
+
response = client.models.generate_content(
|
| 89 |
+
model="gemini-2.0-flash-exp-image-generation",
|
| 90 |
+
contents=prompt,
|
| 91 |
+
config=types.GenerateContentConfig(
|
| 92 |
+
response_modalities=['TEXT', 'IMAGE']
|
| 93 |
+
)
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
for part in response.candidates[0].content.parts:
|
| 97 |
+
if part.inline_data is not None:
|
| 98 |
+
try:
|
| 99 |
+
mime_type = part.inline_data.mime_type
|
| 100 |
+
image_data = part.inline_data.data
|
| 101 |
+
|
| 102 |
+
image_format = "png"
|
| 103 |
+
if "jpeg" in mime_type or "jpg" in mime_type:
|
| 104 |
+
image_format = "jpeg"
|
| 105 |
+
elif "png" in mime_type:
|
| 106 |
+
image_format = "png"
|
| 107 |
+
elif "webp" in mime_type:
|
| 108 |
+
image_format = "webp"
|
| 109 |
+
|
| 110 |
+
processed_image = process_image(image_data)
|
| 111 |
+
|
| 112 |
+
result = upload_image_to_s3(
|
| 113 |
+
processed_image,
|
| 114 |
+
f"byop_banner_{brand_name}_v{prompt_index}",
|
| 115 |
+
image_format,
|
| 116 |
+
f"byop_v{prompt_index}"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
all_banner_results.append({
|
| 120 |
+
"variation": prompt_index,
|
| 121 |
+
"banner": result
|
| 122 |
+
})
|
| 123 |
+
|
| 124 |
+
except Exception as e:
|
| 125 |
+
print(f"Error processing BYOP banner image for variation {prompt_index}: {str(e)}")
|
| 126 |
+
raise
|
| 127 |
+
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"Error generating BYOP banner for variation {prompt_index}: {str(e)}")
|
| 130 |
+
raise
|
| 131 |
+
|
| 132 |
+
if not all_banner_results:
|
| 133 |
+
raise Exception("No images were generated")
|
| 134 |
+
|
| 135 |
+
return {"variations": all_banner_results}
|