Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import httpx
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
-
from fastapi.responses import StreamingResponse
|
| 6 |
|
| 7 |
-
# एप्लीकेशन की नींव
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# CORS पॉलिसी
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
allow_origins=["*"],
|
|
@@ -15,54 +14,94 @@ app.add_middleware(
|
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# सीक्रेट्स
|
| 19 |
-
API_KEY = os.getenv("SECRET_API_KEY")
|
| 20 |
-
API_URL = os.getenv("SECRET_API_URL")
|
| 21 |
-
MODEL_NAME = os.getenv("SECRET_MODEL_NAME")
|
| 22 |
-
SYSTEM_PROMPT = os.getenv("SECRET_SYSTEM_PROMPT")
|
| 23 |
SECRET_TOKEN = "uISssvLLmXK0bsrcFICh14o7aj4q2ZT4"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
# 1. रूट हेल्थ चेक (ताकि वह प्लेटफ़ॉर्म सर्वर को ढूंढ सके)
|
| 26 |
@app.get("/")
|
| 27 |
-
async def
|
| 28 |
return {"status": "Aura Gen 2.0 is online"}
|
| 29 |
|
| 30 |
-
#
|
| 31 |
@app.post("/v1/chat/completions")
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
auth_header = request.headers.get("Authorization")
|
| 35 |
if auth_header != f"Bearer {SECRET_TOKEN}":
|
| 36 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid Token")
|
| 37 |
|
| 38 |
data = await request.json()
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
async with client.stream(
|
| 56 |
-
"POST",
|
| 57 |
-
API_URL,
|
| 58 |
-
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
|
| 59 |
-
json=payload
|
| 60 |
-
) as response:
|
| 61 |
-
async for line in response.aiter_lines():
|
| 62 |
-
if line:
|
| 63 |
-
yield f"{line}\n\n"
|
| 64 |
|
| 65 |
-
return
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
import uvicorn
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import httpx
|
| 4 |
from fastapi import FastAPI, Request, HTTPException
|
| 5 |
from fastapi.middleware.cors import CORSMiddleware
|
| 6 |
+
from fastapi.responses import StreamingResponse, JSONResponse
|
| 7 |
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 10 |
app.add_middleware(
|
| 11 |
CORSMiddleware,
|
| 12 |
allow_origins=["*"],
|
|
|
|
| 14 |
allow_headers=["*"],
|
| 15 |
)
|
| 16 |
|
| 17 |
+
# टोकन और सीक्रेट्स
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
SECRET_TOKEN = "uISssvLLmXK0bsrcFICh14o7aj4q2ZT4"
|
| 19 |
+
API_KEY = os.getenv("SECRET_API_KEY")
|
| 20 |
+
API_URL = os.getenv("SECRET_API_URL", "https://integrate.api.nvidia.com/v1/chat/completions")
|
| 21 |
+
MODEL_NAME = os.getenv("SECRET_MODEL_NAME", "meta/llama-4-maverick-17b-128e-instruct")
|
| 22 |
+
SYSTEM_PROMPT = os.getenv("SECRET_SYSTEM_PROMPT", "You are Aura Gen 2.0, a state-of-the-art AI assistant...")
|
| 23 |
|
|
|
|
| 24 |
@app.get("/")
|
| 25 |
+
async def root_health_check():
|
| 26 |
return {"status": "Aura Gen 2.0 is online"}
|
| 27 |
|
| 28 |
+
# आप URL में जो भी डालें, यह उसे पकड़ लेगा
|
| 29 |
@app.post("/v1/chat/completions")
|
| 30 |
+
@app.post("/")
|
| 31 |
+
async def poe_bot_endpoint(request: Request):
|
| 32 |
+
# 1. सुरक्षा: आपके टोकन की जांच
|
| 33 |
auth_header = request.headers.get("Authorization")
|
| 34 |
if auth_header != f"Bearer {SECRET_TOKEN}":
|
| 35 |
raise HTTPException(status_code=401, detail="Unauthorized: Invalid Token")
|
| 36 |
|
| 37 |
data = await request.json()
|
| 38 |
+
req_type = data.get("type")
|
| 39 |
+
|
| 40 |
+
# 2. POE हेल्थ चेक (Settings) - यही 'Run Check' को पास करेगा!
|
| 41 |
+
if req_type == "settings":
|
| 42 |
+
return JSONResponse(content={
|
| 43 |
+
"server_bot_dependencies": {},
|
| 44 |
+
"allow_attachments": True,
|
| 45 |
+
"introduction_message": "नमस्ते! मैं Aura Gen 2.0 हूँ। मैं आपकी कैसे सहायता कर सकता हूँ?"
|
| 46 |
+
})
|
| 47 |
+
|
| 48 |
+
# 3. यूज़र का मैसेज (Query) - जब कोई Poe पर आपसे बात करेगा
|
| 49 |
+
if req_type == "query":
|
| 50 |
+
poe_messages = data.get("query", [])
|
| 51 |
+
|
| 52 |
+
# Poe के फॉर्मेट को NVIDIA (OpenAI) फॉर्मेट में बदलना
|
| 53 |
+
openai_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 54 |
+
for msg in poe_messages:
|
| 55 |
+
# Poe में AI को 'bot' कहते हैं, NVIDIA में 'assistant'
|
| 56 |
+
role = "assistant" if msg.get("role") == "bot" else msg.get("role", "user")
|
| 57 |
+
openai_messages.append({"role": role, "content": msg.get("content", "")})
|
| 58 |
+
|
| 59 |
+
payload = {
|
| 60 |
+
"model": MODEL_NAME,
|
| 61 |
+
"messages": openai_messages,
|
| 62 |
+
"max_tokens": 4096,
|
| 63 |
+
"temperature": 1.0,
|
| 64 |
+
"stream": True,
|
| 65 |
+
"chat_template_kwargs": {"enable_thinking": True}
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
async def stream_generator():
|
| 69 |
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 70 |
+
async with client.stream(
|
| 71 |
+
"POST",
|
| 72 |
+
API_URL,
|
| 73 |
+
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
|
| 74 |
+
json=payload
|
| 75 |
+
) as response:
|
| 76 |
+
# यदि NVIDIA से कोई एरर आता है
|
| 77 |
+
if response.status_code != 200:
|
| 78 |
+
yield f"event: text\ndata: {json.dumps({'text': f'⚠️ NVIDIA Server Error: {response.status_code}'})}\n\n"
|
| 79 |
+
yield "event: done\ndata: {}\n\n"
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
+
# स्ट्रीमिंग को Poe के फॉर्मेट में बदलना
|
| 83 |
+
async for line in response.aiter_lines():
|
| 84 |
+
if line.startswith("data: "):
|
| 85 |
+
data_str = line[6:].strip()
|
| 86 |
+
if data_str == "[DONE]":
|
| 87 |
+
continue
|
| 88 |
+
try:
|
| 89 |
+
parsed = json.loads(data_str)
|
| 90 |
+
token = parsed.choices[0].delta.get("content", "")
|
| 91 |
+
if token:
|
| 92 |
+
# Poe को यह खास फॉर्मेट चाहिए होता है
|
| 93 |
+
poe_event = {"text": token}
|
| 94 |
+
yield f"event: text\ndata: {json.dumps(poe_event)}\n\n"
|
| 95 |
+
except Exception:
|
| 96 |
+
pass
|
| 97 |
+
|
| 98 |
+
# स्ट्रीम खत्म होने का संकेत
|
| 99 |
+
yield "event: done\ndata: {}\n\n"
|
| 100 |
|
| 101 |
+
# Poe के लिए सही मीडिया टाइप
|
| 102 |
+
return StreamingResponse(stream_generator(), media_type="text/event-stream")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
+
return JSONResponse(status_code=400, content={"error": "Unsupported request type"})
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
import uvicorn
|