Spaces:
Sleeping
Sleeping
File size: 1,135 Bytes
c1d6bf9 |
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 |
import json
import os
from fastapi import FastAPI, HTTPException, Request
from huggingface_hub import HfApi, login
app = FastAPI()
KEY = os.environ.get("WEBHOOK_SECRET")
HF_READ = os.environ.get("HF_READ")
api = HfApi(token=HF_READ)
login(HF_READ)
LATEST_PAYLOAD = "latest_payload.json"
@app.get("/")
def greet_json():
try:
with open(LATEST_PAYLOAD, "r") as file:
payload = json.load(file)
except FileNotFoundError:
payload = {"message": "No payload received yet"}
except json.JSONDecodeError:
payload = {"message": "Error reading payload file"}
return payload
@app.post("/benchmark")
async def handle_webhook(request: Request):
secret = request.headers.get("X-Webhook-Secret")
if secret != KEY:
raise HTTPException(status_code=403, detail="Invalid webhook secret")
try:
payload = await request.json()
except Exception:
raise HTTPException(status_code=400, detail="Invalid JSON payload")
with open(LATEST_PAYLOAD, "w") as file:
json.dump(payload, file)
return {"status": "success", "received": payload}
|