File size: 1,021 Bytes
71079a3 |
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 |
from fastapi import FastAPI, Request, HTTPException
from pydantic import BaseModel
import os
import requests
app = FastAPI(title="Hakayti Proxy API")
UPSTREAM_URL = os.getenv("UPSTREAM_URL", "https://placeholder.invalid")
API_KEY = os.getenv("API_KEY", "my_secret_key_123")
class StoryRequest(BaseModel):
name: str
hobby: str
@app.post("/generate")
async def generate(req: StoryRequest, request: Request):
header_key = request.headers.get("x-api-key") or request.headers.get("authorization")
if API_KEY and header_key not in (API_KEY, f"Bearer {API_KEY}"):
raise HTTPException(status_code=401, detail="Unauthorized")
try:
r = requests.post(
f"{UPSTREAM_URL}/generate",
headers={"x-api-key": API_KEY},
json=req.dict(),
timeout=120
)
r.raise_for_status()
except Exception as e:
raise HTTPException(status_code=502, detail=f"Kaggle upstream error: {e}")
return r.json()
|