Spaces:
No application file
No application file
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from browser_use import Agent | |
| app = FastAPI() | |
| class Task(BaseModel): | |
| task: str | |
| def make_llm(): | |
| # LangChain helper; will raise if GOOGLE_API_KEY missing | |
| # return ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp") | |
| # Allow override from HF Secrets, but fall back to a stable, supported model | |
| model_id = os.getenv("GEMINI_MODEL", "gemini-1.5-pro-latest") | |
| return ChatGoogleGenerativeAI( | |
| model=model_id, | |
| max_retries=3, # built-in exponential back-off | |
| temperature=0.2, | |
| ) | |
| async def run_task(t: Task): | |
| try: | |
| llm = make_llm() | |
| agent = Agent(task=t.task, llm=llm) # Browser-Use agent | |
| result = await agent.run_async() # async version | |
| return result # Browser-Use returns dict | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |