Lazy imports for browser-use deps + restore missing bug-report route
browser-use-service/main.py: Made browser_use and langchain_ollama imports lazy (imported only in functions that use them) so the scraper starts without these optional packages.
This commit is contained in:
+20
-15
@@ -5,16 +5,11 @@ from fastapi import FastAPI, Query, Body
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
import uvicorn
|
||||
from playwright.async_api import async_playwright
|
||||
from browser_use import Agent, Browser
|
||||
|
||||
|
||||
# ── Lazy Imports for browser-use (not always available) ──────────────
|
||||
def _make_ollama(model: str | None = None, **kwargs):
|
||||
from langchain_ollama import ChatOllama
|
||||
|
||||
|
||||
# ── Helpers ──────────────────────────────────────────────────────────
|
||||
def make_ollama(model: str | None = None, **kwargs) -> ChatOllama:
|
||||
"""
|
||||
Create ChatOllama with required attrs for browser-use Agent compatibility.
|
||||
The browser-use Agent expects llm.name, llm.model_name, and llm.provider attrs.
|
||||
"""
|
||||
llm = ChatOllama(model=model or CLASSIFY_MODEL, **kwargs)
|
||||
object.__setattr__(llm, 'provider', 'ollama')
|
||||
object.__setattr__(llm, 'name', 'ChatOllama')
|
||||
@@ -22,6 +17,16 @@ def make_ollama(model: str | None = None, **kwargs) -> ChatOllama:
|
||||
return llm
|
||||
|
||||
|
||||
def _make_browser(**kwargs):
|
||||
from browser_use import Browser
|
||||
return Browser(**kwargs)
|
||||
|
||||
|
||||
def _make_agent(task: str, llm, browser, **kwargs):
|
||||
from browser_use import Agent
|
||||
return Agent(task=task, llm=llm, browser=browser, **kwargs)
|
||||
|
||||
|
||||
# ── Logging & App Setup ──────────────────────────────────────────────
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -1263,7 +1268,7 @@ async def _scrape_with_agent(force: bool = False) -> dict:
|
||||
profile_dir = copy_chrome_profile(CHROME_PROFILE)
|
||||
user_data_dir = profile_dir
|
||||
|
||||
browser = Browser(
|
||||
browser = _make_browser(
|
||||
headless=True,
|
||||
args=CHROME_LAUNCH_ARGS,
|
||||
user_data_dir=user_data_dir,
|
||||
@@ -1273,7 +1278,7 @@ async def _scrape_with_agent(force: bool = False) -> dict:
|
||||
|
||||
all_posts = []
|
||||
for query in random.sample(FB_SEARCHES, k=random.randint(2, 4)):
|
||||
agent = Agent(
|
||||
agent = _make_agent(
|
||||
task=f"""You are logged into Facebook. Do the following:
|
||||
1. Navigate to facebook.com and make sure you are on the homepage
|
||||
2. Use the Facebook search to find: {query}
|
||||
@@ -1287,7 +1292,7 @@ async def _scrape_with_agent(force: bool = False) -> dict:
|
||||
6. Collect as many posts as you can (aim for 5-10 per search)
|
||||
|
||||
When done, return the data as a JSON list with keys: content, author, url, date.""",
|
||||
llm=make_ollama(num_ctx=32000, temperature=0.3),
|
||||
llm=_make_ollama(num_ctx=32000, temperature=0.3),
|
||||
browser=browser,
|
||||
max_actions_per_step=3,
|
||||
use_vision=False,
|
||||
@@ -1517,11 +1522,11 @@ async def setup_check_login(body: dict):
|
||||
async def agent_run(task: str = Body(..., embed=True)):
|
||||
"""Run a browser-use Agent with ChatOllama (free/local)."""
|
||||
try:
|
||||
browser = Browser(headless=True, args=CHROME_LAUNCH_ARGS)
|
||||
browser = _make_browser(headless=True, args=CHROME_LAUNCH_ARGS)
|
||||
await browser.start()
|
||||
agent = Agent(
|
||||
agent = _make_agent(
|
||||
task=task,
|
||||
llm=make_ollama(num_ctx=32000, temperature=0.3),
|
||||
llm=_make_ollama(num_ctx=32000, temperature=0.3),
|
||||
browser=browser,
|
||||
use_vision=False,
|
||||
max_actions_per_step=5,
|
||||
|
||||
Reference in New Issue
Block a user