Skip to content

Commit 0cdfa99

Browse files
committed
Cubic
1 parent d0be266 commit 0cdfa99

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

examples/apps/ad-use/ad_generator.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
from datetime import datetime
88
from pathlib import Path
9+
from typing import Optional
910

1011

1112
def setup_environment(debug: bool):
@@ -44,7 +45,6 @@ def __init__(self, debug: bool = False):
4445
async def analyze_landing_page(self, url: str) -> dict:
4546
browser_session = BrowserSession(
4647
headless=not self.debug, # headless=False only when debug=True
47-
disable_security=True,
4848
)
4949

5050
agent = Agent(
@@ -128,8 +128,8 @@ def create_ad_prompt(self, browser_analysis: str) -> str:
128128
Style: Modern Instagram advertisement, (1:1), scroll-stopping, professional but playful, conversion-focused"""
129129
return prompt
130130

131-
async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = None) -> bytes:
132-
"""Generate ad image bytes using Gemini. Returns *empty bytes* on failure."""
131+
async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = None) -> Optional[bytes]:
132+
"""Generate ad image bytes using Gemini. Returns None on failure."""
133133

134134
try:
135135
from typing import Any
@@ -149,7 +149,7 @@ async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = No
149149

150150
contents = [prompt + screenshot_prompt, img]
151151

152-
response = self.client.models.generate_content(
152+
response = await self.client.aio.models.generate_content(
153153
model='gemini-2.5-flash-image-preview',
154154
contents=contents,
155155
)
@@ -164,7 +164,7 @@ async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = No
164164
except Exception as e:
165165
print(f'❌ Image generation failed: {e}')
166166

167-
return b''
167+
return None
168168

169169
async def save_results(self, ad_image: bytes, prompt: str, analysis: str, url: str, timestamp: str) -> str:
170170
image_path = self.output_dir / f'ad_{timestamp}.png'
@@ -208,6 +208,8 @@ async def create_ad_from_landing_page(url: str, debug: bool = False):
208208

209209
prompt = generator.create_ad_prompt(page_data['analysis'])
210210
ad_image = await generator.generate_ad_image(prompt, page_data.get('screenshot_path'))
211+
if ad_image is None:
212+
raise RuntimeError('Ad image generation failed')
211213
result_path = await generator.save_results(ad_image, prompt, page_data['analysis'], url, page_data['timestamp'])
212214

213215
print(f'🎨 Generated ad: {result_path}')

0 commit comments

Comments
 (0)