forked from VRAutomatize/browser-use
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
142 lines (117 loc) · 4.33 KB
/
server.py
File metadata and controls
142 lines (117 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import logging
from fastapi import FastAPI, HTTPException, Body, Depends
from fastapi.middleware.cors import CORSMiddleware
from dotenv import load_dotenv
from api import router
from database import get_db, init_db
from logging_config import log_info, log_error, log_debug, log_warning
from browser_use import Agent, BrowserConfig, Browser
from settings import get_llm, AgentResponse, TaskRequest
# Logging configuration
logger = logging.getLogger('browser-use.server')
# Load environment variables
load_dotenv()
# Database configuration
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
raise ValueError("DATABASE_URL is not defined in environment variables")
log_info(logger, f"Connecting to database at: {DATABASE_URL}")
# Initialize database
init_db()
# Initialize FastAPI application
app = FastAPI(
title="Browser-use API",
description="API to control Browser-use"
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include API routes
app.include_router(router)
@app.post("/run", response_model=AgentResponse)
async def run_agent(
request: TaskRequest = Body(...),
db = Depends(get_db)
):
log_info(logger, "Starting agent execution", {
"task": request.task,
"provider": request.llm_config.provider,
"model": request.llm_config.model_name
})
try:
# Configure LLM model
llm = get_llm(request.llm_config)
# Configure browser
browser_config = BrowserConfig(
headless=request.browser_config.headless if request.browser_config else True,
disable_security=request.browser_config.disable_security if request.browser_config else True,
extra_chromium_args=request.browser_config.extra_chromium_args if request.browser_config else []
)
log_debug(logger, "Browser configuration", {
"headless": browser_config.headless,
"disable_security": browser_config.disable_security
})
# Initialize browser
browser = Browser(config=browser_config)
tool_calling_method = "auto"
if "deepseek-r1" in request.llm_config.model_name:
tool_calling_method = "json_mode"
# Initialize and run agent
agent = Agent(
task=request.task,
llm=llm,
browser=browser,
use_vision=request.use_vision,
generate_gif=request.generate_gif,
max_failures=request.max_failures,
memory_interval=request.memory_interval,
planner_interval=request.planner_interval,
tool_calling_method=tool_calling_method
)
result = await agent.run(max_steps=request.max_steps)
# Extract result
success = False
content = "Task not completed"
videopath = agent.videopath
if result and result.history and len(result.history) > 0:
last_item = result.history[-1]
if last_item.result and len(last_item.result) > 0:
last_result = last_item.result[-1]
content = last_result.extracted_content or "No content extracted"
success = last_result.is_done
# Close browser after use
await browser.close()
return AgentResponse(
task=request.task,
result=content,
success=success,
steps_executed=len(result.history) if result and result.history else 0,
videopath=videopath
)
except Exception as e:
log_error(logger, "Error during agent execution", {
"task": request.task,
"error": str(e)
}, exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
@app.get("/health")
async def health_check():
"""Endpoint to check API health"""
log_debug(logger, "Checking API health")
return {"status": "healthy"}
if __name__ == "__main__":
import uvicorn
# Get port from environment or use 8000 as default
port = int(os.getenv("PORT", 8000))
log_info(logger, "Starting FastAPI server", {
"host": "0.0.0.0",
"port": port
})
# Start server
uvicorn.run("server:app", host="0.0.0.0", port=port, log_level="info")