Skip to content

Commit be89b90

Browse files
authored
Merge pull request #104 from vvincent1234/fix/requirements_update
update requirements
2 parents bd696d2 + ae47523 commit be89b90

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
browser-use>=0.1.18
2-
langchain-google-genai>=2.0.8
1+
browser-use==0.1.18
2+
langchain-google-genai==2.0.8
33
pyperclip
44
gradio
55
langchain-ollama

src/utils/utils.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -164,38 +164,39 @@ def get_latest_files(directory: str, file_types: list = ['.webm', '.zip']) -> Di
164164
print(f"Error getting latest {file_type} file: {e}")
165165

166166
return latest_files
167-
async def capture_screenshot(browser_context) -> str:
167+
async def capture_screenshot(browser_context):
168168
"""Capture and encode a screenshot"""
169-
try:
170-
# Extract the Playwright browser instance
171-
playwright_browser = browser_context.browser.playwright_browser # Ensure this is correct.
172-
173-
# Check if the browser instance is valid and if an existing context can be reused
174-
if playwright_browser and playwright_browser.contexts:
175-
playwright_context = playwright_browser.contexts[0]
176-
177-
# Access pages in the context
178-
if playwright_context:
179-
pages = playwright_context.pages
180-
181-
# Use an existing page or create a new one if none exist
182-
if pages:
183-
active_page = pages[0]
184-
for page in pages:
185-
if page.url != "about:blank":
186-
active_page = page
187-
188-
# Take screenshot
189-
try:
190-
screenshot = await active_page.screenshot(
191-
type='jpeg',
192-
quality=75,
193-
scale="css"
194-
)
195-
encoded = base64.b64encode(screenshot).decode('utf-8')
196-
return f'<img src="data:image/jpeg;base64,{encoded}" style="width:80vw; height:90vh ; border:1px solid #ccc;">'
197-
except Exception as e:
198-
return f"<h1 class='error' style='width:80vw; height:90vh'>Waiting for browser session...</h1>"
169+
# Extract the Playwright browser instance
170+
playwright_browser = browser_context.browser.playwright_browser # Ensure this is correct.
171+
172+
# Check if the browser instance is valid and if an existing context can be reused
173+
if playwright_browser and playwright_browser.contexts:
174+
playwright_context = playwright_browser.contexts[0]
175+
else:
176+
return None
199177

178+
# Access pages in the context
179+
pages = None
180+
if playwright_context:
181+
pages = playwright_context.pages
182+
183+
# Use an existing page or create a new one if none exist
184+
if pages:
185+
active_page = pages[0]
186+
for page in pages:
187+
if page.url != "about:blank":
188+
active_page = page
189+
else:
190+
return None
191+
192+
# Take screenshot
193+
try:
194+
screenshot = await active_page.screenshot(
195+
type='jpeg',
196+
quality=75,
197+
scale="css"
198+
)
199+
encoded = base64.b64encode(screenshot).decode('utf-8')
200+
return encoded
200201
except Exception as e:
201-
return f"<h1 class='error' style='width:80vw; height:90vh'>Waiting for browser session...</h1>"
202+
return None

webui.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ async def run_with_stream(
408408
max_actions_per_step,
409409
tool_call_in_content
410410
):
411+
stream_vw = 80
412+
stream_vh = int(80 * window_h // window_w)
411413
if not headless:
412414
result = await run_browser_agent(
413415
agent_type=agent_type,
@@ -433,7 +435,7 @@ async def run_with_stream(
433435
tool_call_in_content=tool_call_in_content
434436
)
435437
# Add HTML content at the start of the result array
436-
html_content = "<h1 style='width:80vw; height:90vh'>Using browser...</h1>"
438+
html_content = f"<h1 style='width:{stream_vw}vw; height:{stream_vh}vh'>Using browser...</h1>"
437439
yield [html_content] + list(result)
438440
else:
439441
try:
@@ -465,17 +467,21 @@ async def run_with_stream(
465467
)
466468

467469
# Initialize values for streaming
468-
html_content = "<h1 style='width:80vw; height:90vh'>Using browser...</h1>"
470+
html_content = f"<h1 style='width:{stream_vw}vw; height:{stream_vh}vh'>Using browser...</h1>"
469471
final_result = errors = model_actions = model_thoughts = ""
470472
latest_videos = trace = None
471473

472474

473475
# Periodically update the stream while the agent task is running
474476
while not agent_task.done():
475477
try:
476-
html_content = await capture_screenshot(_global_browser_context)
478+
encoded_screenshot = await capture_screenshot(_global_browser_context)
479+
if encoded_screenshot is not None:
480+
html_content = f'<img src="data:image/jpeg;base64,{encoded_screenshot}" style="width:{stream_vw}vw; height:{stream_vh}vh ; border:1px solid #ccc;">'
481+
else:
482+
html_content = f"<h1 style='width:{stream_vw}vw; height:{stream_vh}vh'>Waiting for browser session...</h1>"
477483
except Exception as e:
478-
html_content = f"<h1 style='width:80vw; height:90vh'>Waiting for browser session...</h1>"
484+
html_content = f"<h1 style='width:{stream_vw}vw; height:{stream_vh}vh'>Waiting for browser session...</h1>"
479485

480486
yield [
481487
html_content,
@@ -488,7 +494,7 @@ async def run_with_stream(
488494
gr.update(value="Stop", interactive=True), # Re-enable stop button
489495
gr.update(value="Run", interactive=True) # Re-enable run button
490496
]
491-
await asyncio.sleep(0.01)
497+
await asyncio.sleep(0.05)
492498

493499
# Once the agent task completes, get the results
494500
try:
@@ -515,7 +521,7 @@ async def run_with_stream(
515521
except Exception as e:
516522
import traceback
517523
yield [
518-
f"<h1 style='width:80vw; height:90vh'>Waiting for browser session...</h1>",
524+
f"<h1 style='width:{stream_vw}vw; height:{stream_vh}vh'>Waiting for browser session...</h1>",
519525
"",
520526
f"Error: {str(e)}\n{traceback.format_exc()}",
521527
"",
@@ -740,7 +746,7 @@ def create_ui(theme_name="Ocean"):
740746

741747
with gr.Row():
742748
browser_view = gr.HTML(
743-
value="<h1 style='width:80vw; height:90vh'>Waiting for browser session...</h1>",
749+
value="<h1 style='width:80vw; height:50vh'>Waiting for browser session...</h1>",
744750
label="Live Browser View",
745751
)
746752

0 commit comments

Comments
 (0)