| 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()
|
|
|