Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import httpx
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# CORS इनेबल करना ताकि आपका फ्रंटएंड इसे एक्सेस कर सके
|
| 9 |
+
app.add_middleware(
|
| 10 |
+
CORSMiddleware,
|
| 11 |
+
allow_origins=["*"],
|
| 12 |
+
allow_methods=["*"],
|
| 13 |
+
allow_headers=["*"],
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
NVIDIA_API_KEY = os.getenv("NVIDIA_API_KEY")
|
| 17 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "microsoft/phi-4-multimodal-instruct")
|
| 18 |
+
API_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
|
| 19 |
+
|
| 20 |
+
@app.post("/v1/chat/completions")
|
| 21 |
+
async def chat_proxy(request: Request):
|
| 22 |
+
data = await request.json()
|
| 23 |
+
|
| 24 |
+
async with httpx.AsyncClient() as client:
|
| 25 |
+
response = await client.post(
|
| 26 |
+
API_URL,
|
| 27 |
+
headers={
|
| 28 |
+
"Authorization": f"Bearer {NVIDIA_API_KEY}",
|
| 29 |
+
"Content-Type": "application/json"
|
| 30 |
+
},
|
| 31 |
+
json={
|
| 32 |
+
"model": MODEL_NAME,
|
| 33 |
+
"messages": data.get("messages"),
|
| 34 |
+
"max_tokens": data.get("max_tokens", 4096),
|
| 35 |
+
"temperature": data.get("temperature", 0.6),
|
| 36 |
+
"top_p": 0.70
|
| 37 |
+
},
|
| 38 |
+
timeout=60.0
|
| 39 |
+
)
|
| 40 |
+
return response.json()
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
import uvicorn
|
| 44 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|