Update main.py
Browse files
main.py
CHANGED
|
@@ -1,61 +1,73 @@
|
|
| 1 |
-
from fastapi import FastAPI, Request, BackgroundTasks
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from fastapi.exceptions import HTTPException
|
| 4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from agent import run_agent
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
-
import uvicorn
|
| 8 |
-
import os
|
| 9 |
-
from shared_store import url_time, BASE64_STORE
|
| 10 |
-
import time
|
| 11 |
-
|
| 12 |
-
load_dotenv()
|
| 13 |
-
|
| 14 |
-
EMAIL = os.getenv("EMAIL")
|
| 15 |
-
SECRET = os.getenv("SECRET")
|
| 16 |
-
|
| 17 |
-
app = FastAPI()
|
| 18 |
-
app.add_middleware(
|
| 19 |
-
CORSMiddleware,
|
| 20 |
-
allow_origins=["*"], # or specific domains
|
| 21 |
-
allow_credentials=True,
|
| 22 |
-
allow_methods=["*"],
|
| 23 |
-
allow_headers=["*"],
|
| 24 |
-
)
|
| 25 |
-
START_TIME = time.time()
|
| 26 |
-
@app.get("/healthz")
|
| 27 |
-
def healthz():
|
| 28 |
-
"""Simple liveness check."""
|
| 29 |
-
return {
|
| 30 |
-
"status": "ok",
|
| 31 |
-
"uptime_seconds": int(time.time() - START_TIME)
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, BackgroundTasks
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.exceptions import HTTPException
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from agent import run_agent
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import uvicorn
|
| 8 |
+
import os
|
| 9 |
+
from shared_store import url_time, BASE64_STORE
|
| 10 |
+
import time
|
| 11 |
+
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
EMAIL = os.getenv("EMAIL")
|
| 15 |
+
SECRET = os.getenv("SECRET")
|
| 16 |
+
|
| 17 |
+
app = FastAPI()
|
| 18 |
+
app.add_middleware(
|
| 19 |
+
CORSMiddleware,
|
| 20 |
+
allow_origins=["*"], # or specific domains
|
| 21 |
+
allow_credentials=True,
|
| 22 |
+
allow_methods=["*"],
|
| 23 |
+
allow_headers=["*"],
|
| 24 |
+
)
|
| 25 |
+
START_TIME = time.time()
|
| 26 |
+
@app.get("/healthz")
|
| 27 |
+
def healthz():
|
| 28 |
+
"""Simple liveness check."""
|
| 29 |
+
return {
|
| 30 |
+
"status": "ok",
|
| 31 |
+
"uptime_seconds": int(time.time() - START_TIME)
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
import requests
|
| 35 |
+
|
| 36 |
+
@app.get("/test_google")
|
| 37 |
+
def test_google():
|
| 38 |
+
try:
|
| 39 |
+
url = "https://speech.googleapis.com/v1/speech:recognize"
|
| 40 |
+
r = requests.post(url, timeout=5)
|
| 41 |
+
return {"status": r.status_code, "text": r.text[:200]}
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return {"error": str(e)}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
@app.post("/solve")
|
| 47 |
+
async def solve(request: Request, background_tasks: BackgroundTasks):
|
| 48 |
+
try:
|
| 49 |
+
data = await request.json()
|
| 50 |
+
except Exception:
|
| 51 |
+
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 52 |
+
if not data:
|
| 53 |
+
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 54 |
+
url = data.get("url")
|
| 55 |
+
secret = data.get("secret")
|
| 56 |
+
if not url or not secret:
|
| 57 |
+
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 58 |
+
|
| 59 |
+
if secret != SECRET:
|
| 60 |
+
raise HTTPException(status_code=403, detail="Invalid secret")
|
| 61 |
+
url_time.clear()
|
| 62 |
+
BASE64_STORE.clear()
|
| 63 |
+
print("Verified starting the task...")
|
| 64 |
+
os.environ["url"] = url
|
| 65 |
+
os.environ["offset"] = "0"
|
| 66 |
+
url_time[url] = time.time()
|
| 67 |
+
background_tasks.add_task(run_agent, url)
|
| 68 |
+
|
| 69 |
+
return JSONResponse(status_code=200, content={"status": "ok"})
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|