Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import asyncio
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
import uvicorn
|
| 6 |
+
from playwright.async_api import async_playwright
|
| 7 |
+
from openclaw import Agent, Tool
|
| 8 |
+
|
| 9 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 10 |
+
SPACE_HOST = os.getenv("SPACE_HOST")
|
| 11 |
+
STATIC_DIR = "static"
|
| 12 |
+
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 15 |
+
|
| 16 |
+
async def take_screenshot(url: str) -> str:
|
| 17 |
+
filepath = f"{STATIC_DIR}/screenshot.png"
|
| 18 |
+
async with async_playwright() as p:
|
| 19 |
+
browser = await p.chromium.launch(headless=True)
|
| 20 |
+
page = await browser.new_page()
|
| 21 |
+
await page.goto(url)
|
| 22 |
+
await page.screenshot(path=filepath, full_page=True)
|
| 23 |
+
await browser.close()
|
| 24 |
+
return f"Screenshot saved. View here: https://{SPACE_HOST}/static/screenshot.png"
|
| 25 |
+
|
| 26 |
+
tools = [Tool(name="take_screenshot", func=take_screenshot, description="Takes a screenshot of a URL.")]
|
| 27 |
+
|
| 28 |
+
agent = Agent(
|
| 29 |
+
model="gemini/gemini-2.5-pro",
|
| 30 |
+
tools=tools,
|
| 31 |
+
system_prompt="Expert DevOps agent. Concise replies (<1000 chars). Explain before acting.",
|
| 32 |
+
model_kwargs={"temperature": 0.2, "max_tokens": 8192, "top_p": 0.95}
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
@app.get("/health")
|
| 36 |
+
async def health(): return {"status": "online"}
|
| 37 |
+
|
| 38 |
+
@app.post("/webhook")
|
| 39 |
+
async def wa_webhook(request: Request):
|
| 40 |
+
data = await request.json()
|
| 41 |
+
user_msg = data.get("message", "")
|
| 42 |
+
if user_msg.lower().startswith("claw "):
|
| 43 |
+
prompt = user_msg[5:].strip()
|
| 44 |
+
response = await agent.arun(prompt)
|
| 45 |
+
return {"replies": [{"message": response}]}
|
| 46 |
+
return {"replies": []}
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|