-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
96 lines (71 loc) · 2.4 KB
/
api.py
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
import traceback
from contextlib import asynccontextmanager
from browser_use.browser.browser import Browser
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel
from langchain_openai import ChatOpenAI
from browser_use import Agent
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
browser = Browser()
context = None
@asynccontextmanager
async def lifespan(app: FastAPI):
global context
context = await browser.new_context()
logger.info("Browser context initialized")
yield
await context.close()
logger.info("Browser context closed")
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class TaskRequest(BaseModel):
api_key: str
task: str
# API routes
@app.post("/api/run-task")
async def run_task(request: TaskRequest):
try:
logger.info(f"Starting task: {request.task}")
llm = ChatOpenAI(
model="gpt-4o",
api_key=request.api_key
)
agent = Agent(
task=request.task,
llm=llm,
browser_context=context
)
try:
result = await agent.run()
logger.info("Task completed successfully")
if result is None:
return {"status": "success", "result": "Task completed but no results were returned"}
if not isinstance(result, str):
result = str(result.history[-1].result[0].extracted_content)
return {"status": "success", "result": result}
except Exception as agent_error:
logger.error(f"Agent error: {str(agent_error)}")
logger.error(f"Full error: {traceback.print_exc()}")
raise HTTPException(status_code=500, detail=f"Agent error: {str(agent_error)}")
except Exception as e:
logger.error(f"API error: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
# Serve static files
@app.get("/")
async def read_index():
return FileResponse('index.html')
app.mount("/static", StaticFiles(directory="."), name="static")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)