| |
| |
| |
|
|
| from fastapi import FastAPI, Request, Response |
| from fastapi.responses import StreamingResponse |
| import httpx |
| import json |
| import os |
|
|
| app = FastAPI() |
|
|
| |
| HF_ENDPOINT = os.environ.get("HF_ENDPOINT", "") |
| HF_TOKEN = os.environ.get("HF_TOKEN", "") |
| PROXY_SECRET = os.environ.get("PROXY_SECRET", "") |
|
|
| @app.get("/health") |
| async def health(): |
| return {"status": "ok", "service": "Nova-1-XL Proxy"} |
|
|
| @app.post("/v1/chat/completions") |
| async def proxy_chat(request: Request): |
| |
| auth = request.headers.get("Authorization", "") |
| if not auth.startswith("Bearer ") or auth[7:] != PROXY_SECRET: |
| return Response(content=json.dumps({"error": "Unauthorized"}), |
| status_code=401, media_type="application/json") |
| |
| body = await request.body() |
| data = json.loads(body) |
| stream = data.get("stream", False) |
| |
| headers = { |
| "Authorization": f"Bearer {HF_TOKEN}", |
| "Content-Type": "application/json", |
| } |
| |
| if stream: |
| async def stream_gen(): |
| async with httpx.AsyncClient(timeout=120) as client: |
| async with client.stream( |
| "POST", HF_ENDPOINT + "chat/completions", |
| headers=headers, json=data |
| ) as resp: |
| async for chunk in resp.aiter_bytes(): |
| yield chunk |
| return StreamingResponse(stream_gen(), media_type="text/event-stream", |
| headers={"X-Accel-Buffering": "no", |
| "Cache-Control": "no-cache"}) |
| else: |
| async with httpx.AsyncClient(timeout=120) as client: |
| resp = await client.post( |
| HF_ENDPOINT + "chat/completions", |
| headers=headers, json=data |
| ) |
| return Response(content=resp.content, |
| status_code=resp.status_code, |
| media_type="application/json") |
|
|
| |
| if __name__ == "__main__": |
| import uvicorn |
| uvicorn.run(app, host="0.0.0.0", port=7860) |