Spaces:
Running
Running
Create app/main.py
Browse files- app/main.py +35 -0
app/main.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# /app/main.py
|
2 |
+
from fastapi import FastAPI, HTTPException, Header
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from playwright.async_api import async_playwright
|
5 |
+
import os
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Load Gemini API key from env
|
10 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
11 |
+
|
12 |
+
class CommandRequest(BaseModel):
|
13 |
+
command: str
|
14 |
+
|
15 |
+
@app.post("/run-command")
|
16 |
+
async def run_command(req: CommandRequest, authorization: str = Header(None)):
|
17 |
+
if authorization != f"Bearer {GEMINI_API_KEY}":
|
18 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
19 |
+
|
20 |
+
# Here you would call Gemini API with req.command and get parsed instructions
|
21 |
+
# For demo, we'll simulate: "Go to example.com and take screenshot"
|
22 |
+
if "example.com" in req.command.lower():
|
23 |
+
async with async_playwright() as p:
|
24 |
+
browser = await p.chromium.launch(headless=True)
|
25 |
+
page = await browser.new_page()
|
26 |
+
await page.goto("https://example.com")
|
27 |
+
screenshot = await page.screenshot()
|
28 |
+
await browser.close()
|
29 |
+
return {"status": "done", "screenshot_bytes": len(screenshot)}
|
30 |
+
|
31 |
+
return {"status": "command not recognized"}
|
32 |
+
|
33 |
+
@app.get("/")
|
34 |
+
async def root():
|
35 |
+
return {"message": "Playwright + Gemini API running. POST /run-command to automate."}
|