Spaces:
No application file
No application file
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from fastapi import FastAPI, HTTPException
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 5 |
+
from browser_use import Agent
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
class Task(BaseModel):
|
| 10 |
+
task: str
|
| 11 |
+
|
| 12 |
+
def make_llm():
|
| 13 |
+
# LangChain helper; will raise if GOOGLE_API_KEY missing
|
| 14 |
+
return ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")
|
| 15 |
+
|
| 16 |
+
@app.post("/run")
|
| 17 |
+
async def run_task(t: Task):
|
| 18 |
+
try:
|
| 19 |
+
llm = make_llm()
|
| 20 |
+
agent = Agent(task=t.task, llm=llm) # Browser-Use agent
|
| 21 |
+
result = await agent.run_async() # async version
|
| 22 |
+
return result # Browser-Use returns dict
|
| 23 |
+
except Exception as e:
|
| 24 |
+
raise HTTPException(status_code=500, detail=str(e))
|