Skip to content

Commit 2299fed

Browse files
authored
fix: Ad-Use demo (#3050)
Handle errors better, remove disable security. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Improves stability and security in the Ad-Use demo by removing the insecure browser flag, switching to async image generation, and clearly surfacing failures. Also fixes image opening on Windows. - **Bug Fixes** - Use subprocess "start" on Windows to open images reliably. - Raise an error when image generation fails instead of returning empty bytes. - **Refactors** - Remove disable_security=True from BrowserSession. - Switch to aio models.generate_content and return None on failure (updated type hints and docstring). <!-- End of auto-generated description by cubic. -->
2 parents bb15a75 + e1b3d4a commit 2299fed

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

examples/apps/ad-use/ad_generator.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def __init__(self, debug: bool = False):
4444
async def analyze_landing_page(self, url: str) -> dict:
4545
browser_session = BrowserSession(
4646
headless=not self.debug, # headless=False only when debug=True
47-
disable_security=True,
4847
)
4948

5049
agent = Agent(
@@ -128,8 +127,8 @@ def create_ad_prompt(self, browser_analysis: str) -> str:
128127
Style: Modern Instagram advertisement, (1:1), scroll-stopping, professional but playful, conversion-focused"""
129128
return prompt
130129

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."""
130+
async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = None) -> bytes | None:
131+
"""Generate ad image bytes using Gemini. Returns None on failure."""
133132

134133
try:
135134
from typing import Any
@@ -149,7 +148,7 @@ async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = No
149148

150149
contents = [prompt + screenshot_prompt, img]
151150

152-
response = self.client.models.generate_content(
151+
response = await self.client.aio.models.generate_content(
153152
model='gemini-2.5-flash-image-preview',
154153
contents=contents,
155154
)
@@ -164,7 +163,7 @@ async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = No
164163
except Exception as e:
165164
print(f'❌ Image generation failed: {e}')
166165

167-
return b''
166+
return None
168167

169168
async def save_results(self, ad_image: bytes, prompt: str, analysis: str, url: str, timestamp: str) -> str:
170169
image_path = self.output_dir / f'ad_{timestamp}.png'
@@ -190,7 +189,7 @@ def open_image(image_path: str):
190189
subprocess.run(['open', image_path], check=True)
191190
elif sys.platform.startswith('win'):
192191
# Windows
193-
os.startfile(image_path)
192+
subprocess.run(['cmd', '/c', 'start', '', image_path], check=True)
194193
else:
195194
# Linux
196195
subprocess.run(['xdg-open', image_path], check=True)
@@ -208,6 +207,8 @@ async def create_ad_from_landing_page(url: str, debug: bool = False):
208207

209208
prompt = generator.create_ad_prompt(page_data['analysis'])
210209
ad_image = await generator.generate_ad_image(prompt, page_data.get('screenshot_path'))
210+
if ad_image is None:
211+
raise RuntimeError('Ad image generation failed')
211212
result_path = await generator.save_results(ad_image, prompt, page_data['analysis'], url, page_data['timestamp'])
212213

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

0 commit comments

Comments
 (0)