Spaces:
Sleeping
Sleeping
File size: 1,540 Bytes
df532e4 |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
from fastapi import FastAPI, Request, BackgroundTasks
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from fastapi.middleware.cors import CORSMiddleware
from agent import run_agent
from dotenv import load_dotenv
import uvicorn
import os
import time
load_dotenv()
EMAIL = os.getenv("EMAIL")
SECRET = os.getenv("SECRET")
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # or specific domains
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
START_TIME = time.time()
@app.get("/healthz")
def healthz():
"""Simple liveness check."""
return {
"status": "ok",
"uptime_seconds": int(time.time() - START_TIME)
}
@app.post("/solve")
async def solve(request: Request, background_tasks: BackgroundTasks):
try:
data = await request.json()
except Exception:
raise HTTPException(status_code=400, detail="Invalid JSON")
if not data:
raise HTTPException(status_code=400, detail="Invalid JSON")
url = data.get("url")
secret = data.get("secret")
if not url or not secret:
raise HTTPException(status_code=400, detail="Invalid JSON")
if secret != SECRET:
raise HTTPException(status_code=403, detail="Invalid secret")
print("Verified starting the task...")
background_tasks.add_task(run_agent, url)
return JSONResponse(status_code=200, content={"status": "ok"})
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) |