trrithvik/llm-analysis-agent-api
0
1from fastapi import FastAPI, Request, BackgroundTasks2from fastapi.responses import JSONResponse3from fastapi.exceptions import HTTPException4from fastapi.middleware.cors import CORSMiddleware5from agent import run_agent6from dotenv import load_dotenv7import uvicorn8import os9import time10 11load_dotenv()12 13EMAIL = os.getenv("EMAIL") 14SECRET = os.getenv("SECRET")15 16app = FastAPI()17app.add_middleware(18 CORSMiddleware,19 allow_origins=["*"], # or specific domains20 allow_credentials=True,21 allow_methods=["*"],22 allow_headers=["*"],23)24START_TIME = time.time()25@app.get("/healthz")26def healthz():27 """Simple liveness check."""28 return {29 "status": "ok",30 "uptime_seconds": int(time.time() - START_TIME)31 }32 33@app.post("/solve")34async def solve(request: Request, background_tasks: BackgroundTasks):35 try:36 data = await request.json()37 except Exception:38 raise HTTPException(status_code=400, detail="Invalid JSON")39 if not data:40 raise HTTPException(status_code=400, detail="Invalid JSON")41 url = data.get("url")42 secret = data.get("secret")43 if not url or not secret:44 raise HTTPException(status_code=400, detail="Invalid JSON")45 46 if secret != SECRET:47 raise HTTPException(status_code=403, detail="Invalid secret")48 print("Verified starting the task...")49 background_tasks.add_task(run_agent, url)50 51 return JSONResponse(status_code=200, content={"status": "ok"})52 53 54if __name__ == "__main__":55 uvicorn.run(app, host="0.0.0.0", port=7860)