File size: 1,258 Bytes
47d9c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# /app/main.py
from fastapi import FastAPI, HTTPException, Header
from pydantic import BaseModel
from playwright.async_api import async_playwright
import os

app = FastAPI()

# Load Gemini API key from env
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")

class CommandRequest(BaseModel):
    command: str

@app.post("/run-command")
async def run_command(req: CommandRequest, authorization: str = Header(None)):
    if authorization != f"Bearer {GEMINI_API_KEY}":
        raise HTTPException(status_code=401, detail="Unauthorized")

    # Here you would call Gemini API with req.command and get parsed instructions
    # For demo, we'll simulate: "Go to example.com and take screenshot"
    if "example.com" in req.command.lower():
        async with async_playwright() as p:
            browser = await p.chromium.launch(headless=True)
            page = await browser.new_page()
            await page.goto("https://example.com")
            screenshot = await page.screenshot()
            await browser.close()
            return {"status": "done", "screenshot_bytes": len(screenshot)}

    return {"status": "command not recognized"}

@app.get("/")
async def root():
    return {"message": "Playwright + Gemini API running. POST /run-command to automate."}