6
6
import sys
7
7
from datetime import datetime
8
8
from pathlib import Path
9
+ from typing import Optional
9
10
10
11
11
12
def setup_environment (debug : bool ):
@@ -44,7 +45,6 @@ def __init__(self, debug: bool = False):
44
45
async def analyze_landing_page (self , url : str ) -> dict :
45
46
browser_session = BrowserSession (
46
47
headless = not self .debug , # headless=False only when debug=True
47
- disable_security = True ,
48
48
)
49
49
50
50
agent = Agent (
@@ -128,8 +128,8 @@ def create_ad_prompt(self, browser_analysis: str) -> str:
128
128
Style: Modern Instagram advertisement, (1:1), scroll-stopping, professional but playful, conversion-focused"""
129
129
return prompt
130
130
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."""
133
133
134
134
try :
135
135
from typing import Any
@@ -149,7 +149,7 @@ async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = No
149
149
150
150
contents = [prompt + screenshot_prompt , img ]
151
151
152
- response = self .client .models .generate_content (
152
+ response = await self .client . aio .models .generate_content (
153
153
model = 'gemini-2.5-flash-image-preview' ,
154
154
contents = contents ,
155
155
)
@@ -164,7 +164,7 @@ async def generate_ad_image(self, prompt: str, screenshot_path: Path | None = No
164
164
except Exception as e :
165
165
print (f'❌ Image generation failed: { e } ' )
166
166
167
- return b''
167
+ return None
168
168
169
169
async def save_results (self , ad_image : bytes , prompt : str , analysis : str , url : str , timestamp : str ) -> str :
170
170
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):
208
208
209
209
prompt = generator .create_ad_prompt (page_data ['analysis' ])
210
210
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' )
211
213
result_path = await generator .save_results (ad_image , prompt , page_data ['analysis' ], url , page_data ['timestamp' ])
212
214
213
215
print (f'🎨 Generated ad: { result_path } ' )
0 commit comments