File size: 2,276 Bytes
8f79a29
 
 
 
 
 
 
 
 
 
 
 
 
f29a4da
 
 
8f79a29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# app.py - Deploy this to HuggingFace Space (Free CPU)
# Set HF Space to use "Docker" or "Gradio" SDK
# Requirements: fastapi uvicorn httpx

from fastapi import FastAPI, Request, Response
from fastapi.responses import StreamingResponse
import httpx
import json
import os

app = FastAPI()

# Your actual HF endpoint - stored as HF Space secret
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):
    # Verify proxy secret
    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")

# Required for HF Spaces
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)