Skip to content

A curated collection of practical AI projects implementing OCR systems, RAG, AI agents, and other AI use cases.

audit: fix browser_automation_agent - fix em dashes, add docstring, add streaming progress updates

Tiioluwanie0b882dfb941
diff --git a/ai_agents/browser_automation_agent/README.md b/ai_agents/browser_automation_agent/README.md
index 7c9a737..9488343 100644
--- a/ai_agents/browser_automation_agent/README.md
+++ b/ai_agents/browser_automation_agent/README.md
@@ -12,12 +12,13 @@ Browser Automation Agent is a Gradio app that turns plain-English instructions i
## Features
-- **Natural language tasks** — describe what you want in plain English; no scripting required.
-- **Autonomous browsing** — the agent plans and executes real browser actions (navigate, click, read, extract) step by step.
-- **Structured results** — returns a clear summary plus the pages visited and a collapsible action log.
-- **Resilient error handling** — surfaces provider/model errors in the UI and tolerates transient step retries.
-- **Configurable model** — point at any Orq.ai-supported model via a single environment variable.
-- **Clean chat UI** — a Gradio chat interface with ready-to-run example prompts.
+- **Natural language tasks:** describe what you want in plain English; no scripting required.
+- **Autonomous browsing:** the agent plans and executes real browser actions (navigate, click, read, extract) step by step.
+- **Live progress updates:** streams step-by-step progress to the chat as the agent browses, so you can follow along in real time.
+- **Structured results:** returns a clear summary plus the pages visited and a collapsible action log.
+- **Resilient error handling:** surfaces provider/model errors in the UI and tolerates transient step retries.
+- **Configurable model:** point at any Orq.ai-supported model via a single environment variable.
+- **Clean chat UI:** a Gradio chat interface with ready-to-run example prompts.
## Tech Stack
diff --git a/ai_agents/browser_automation_agent/agent.py b/ai_agents/browser_automation_agent/agent.py
index 75a67fb..73286d3 100644
--- a/ai_agents/browser_automation_agent/agent.py
+++ b/ai_agents/browser_automation_agent/agent.py
@@ -10,7 +10,7 @@ from __future__ import annotations
import os
from dataclasses import dataclass, field
-from typing import List, Optional
+from typing import Callable, List, Optional
from dotenv import load_dotenv
@@ -168,7 +168,9 @@ def _extract_history(history) -> AgentResult:
async def run_browser_agent(
- instruction: str, max_steps: int = DEFAULT_MAX_STEPS
+ instruction: str,
+ max_steps: int = DEFAULT_MAX_STEPS,
+ on_step: Optional[Callable] = None,
) -> AgentResult:
"""Run the browser-use agent for a single natural language instruction.
@@ -198,7 +200,7 @@ async def run_browser_agent(
)
try:
- agent = Agent(task=instruction, llm=llm)
+ agent = Agent(task=instruction, llm=llm, register_new_step_callback=on_step)
history = await agent.run(max_steps=max_steps)
result = _extract_history(history)
result.instruction = instruction
diff --git a/ai_agents/browser_automation_agent/app.py b/ai_agents/browser_automation_agent/app.py
index f47fcd7..f844f1d 100644
--- a/ai_agents/browser_automation_agent/app.py
+++ b/ai_agents/browser_automation_agent/app.py
@@ -8,6 +8,8 @@ returning a structured summary in the chat.
from __future__ import annotations
import asyncio
+import queue
+import threading
import gradio as gr
@@ -27,16 +29,41 @@ DESCRIPTION = (
)
-def respond(message: str, history: list) -> str:
- """Synchronous Gradio handler that drives the async browser agent."""
- try:
- result = asyncio.run(run_browser_agent(message))
- except Exception as exc: # noqa: BLE001 - never crash the UI thread
- return f"**Error:** `{exc}`"
- return result.to_markdown()
+def respond(message: str, history: list):
+ """Streaming Gradio handler that yields step updates then the final result."""
+ step_queue: queue.Queue = queue.Queue()
+
+ def on_step(state, output, step_num: int) -> None:
+ url = getattr(state, "url", "") or ""
+ msg = f"Step {step_num}: Navigating to {url}..." if url else f"Step {step_num}: Processing..."
+ step_queue.put(("step", msg))
+
+ def run_agent() -> None:
+ try:
+ result = asyncio.run(run_browser_agent(message, on_step=on_step))
+ step_queue.put(("done", result))
+ except Exception as exc: # noqa: BLE001 - never crash the UI thread
+ step_queue.put(("error", str(exc)))
+
+ thread = threading.Thread(target=run_agent, daemon=True)
+ thread.start()
+
+ yield "Starting browser agent..."
+
+ while True:
+ kind, value = step_queue.get()
+ if kind == "step":
+ yield value
+ elif kind == "done":
+ yield value.to_markdown()
+ break
+ else:
+ yield f"**Error:** `{value}`"
+ break
def build_demo() -> gr.ChatInterface:
+ """Construct and return the Gradio ChatInterface for the browser automation agent."""
return gr.ChatInterface(
fn=respond,
title="🌐 Browser Automation Agent",