Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,18 +14,19 @@ app.add_middleware(
|
|
| 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 |
-
|
|
|
|
|
|
|
| 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):
|
|
@@ -37,7 +38,7 @@ async def poe_bot_endpoint(request: Request):
|
|
| 37 |
data = await request.json()
|
| 38 |
req_type = data.get("type")
|
| 39 |
|
| 40 |
-
# 2. POE हेल्थ चेक (Settings)
|
| 41 |
if req_type == "settings":
|
| 42 |
return JSONResponse(content={
|
| 43 |
"server_bot_dependencies": {},
|
|
@@ -45,14 +46,13 @@ async def poe_bot_endpoint(request: Request):
|
|
| 45 |
"introduction_message": "नमस्ते! मैं Aura Gen 2.0 हूँ। मैं आपकी कैसे सहायता कर सकता हूँ?"
|
| 46 |
})
|
| 47 |
|
| 48 |
-
# 3. यूज़र का मैसेज (Query)
|
| 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 |
|
|
@@ -67,42 +67,49 @@ async def poe_bot_endpoint(request: Request):
|
|
| 67 |
|
| 68 |
async def stream_generator():
|
| 69 |
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 108 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 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 |
+
|
| 22 |
+
# आपके द्वारा चुना गया नया डिफ़ॉल्ट मॉडल
|
| 23 |
+
MODEL_NAME = os.getenv("SECRET_MODEL_NAME", "google/diffusiongemma-26b-a4b-it")
|
| 24 |
SYSTEM_PROMPT = os.getenv("SECRET_SYSTEM_PROMPT", "You are Aura Gen 2.0, a state-of-the-art AI assistant...")
|
| 25 |
|
| 26 |
@app.get("/")
|
| 27 |
async def root_health_check():
|
| 28 |
return {"status": "Aura Gen 2.0 is online"}
|
| 29 |
|
|
|
|
| 30 |
@app.post("/v1/chat/completions")
|
| 31 |
@app.post("/")
|
| 32 |
async def poe_bot_endpoint(request: Request):
|
|
|
|
| 38 |
data = await request.json()
|
| 39 |
req_type = data.get("type")
|
| 40 |
|
| 41 |
+
# 2. POE हेल्थ चेक (Settings)
|
| 42 |
if req_type == "settings":
|
| 43 |
return JSONResponse(content={
|
| 44 |
"server_bot_dependencies": {},
|
|
|
|
| 46 |
"introduction_message": "नमस्ते! मैं Aura Gen 2.0 हूँ। मैं आपकी कैसे सहायता कर सकता हूँ?"
|
| 47 |
})
|
| 48 |
|
| 49 |
+
# 3. यूज़र का मैसेज (Query)
|
| 50 |
if req_type == "query":
|
| 51 |
poe_messages = data.get("query", [])
|
| 52 |
|
| 53 |
# Poe के फॉर्मेट को NVIDIA (OpenAI) फॉर्मेट में बदलना
|
| 54 |
openai_messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 55 |
for msg in poe_messages:
|
|
|
|
| 56 |
role = "assistant" if msg.get("role") == "bot" else msg.get("role", "user")
|
| 57 |
openai_messages.append({"role": role, "content": msg.get("content", "")})
|
| 58 |
|
|
|
|
| 67 |
|
| 68 |
async def stream_generator():
|
| 69 |
async with httpx.AsyncClient(timeout=120.0) as client:
|
| 70 |
+
try:
|
| 71 |
+
async with client.stream(
|
| 72 |
+
"POST",
|
| 73 |
+
API_URL,
|
| 74 |
+
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
|
| 75 |
+
json=payload
|
| 76 |
+
) as response:
|
| 77 |
+
|
| 78 |
+
# यदि NVIDIA से कोई एरर आता है
|
| 79 |
+
if response.status_code != 200:
|
| 80 |
+
error_text = await response.aread()
|
| 81 |
+
yield f"event: text\ndata: {json.dumps({'text': f'⚠️ NVIDIA API Error ({response.status_code}): {error_text.decode()}'})}\n\n"
|
| 82 |
+
yield "event: done\ndata: {}\n\n"
|
| 83 |
+
return
|
| 84 |
|
| 85 |
+
# स्ट्रीमिंग को Poe के फॉर्मेट में बदलना
|
| 86 |
+
async for line in response.aiter_lines():
|
| 87 |
+
if line.startswith("data: "):
|
| 88 |
+
data_str = line[6:].strip()
|
| 89 |
+
if data_str == "[DONE]":
|
| 90 |
+
continue
|
| 91 |
+
try:
|
| 92 |
+
parsed = json.loads(data_str)
|
| 93 |
+
# पायथन डिक्शनरी सिंटैक्स
|
| 94 |
+
if "choices" in parsed and len(parsed["choices"]) > 0:
|
| 95 |
+
delta = parsed["choices"][0].get("delta", {})
|
| 96 |
+
token = delta.get("content", "")
|
| 97 |
+
|
| 98 |
+
if token:
|
| 99 |
+
poe_event = {"text": token}
|
| 100 |
+
yield f"event: text\ndata: {json.dumps(poe_event)}\n\n"
|
| 101 |
+
except Exception as e:
|
| 102 |
+
pass # आंशिक डेटा को इग्नोर करें
|
| 103 |
+
except Exception as req_e:
|
| 104 |
+
yield f"event: text\ndata: {json.dumps({'text': f'⚠️ Server Connection Error: {str(req_e)}'})}\n\n"
|
| 105 |
+
|
| 106 |
+
# स्ट्रीम खत्म होने का संकेत
|
| 107 |
+
yield "event: done\ndata: {}\n\n"
|
| 108 |
|
|
|
|
| 109 |
return StreamingResponse(stream_generator(), media_type="text/event-stream")
|
| 110 |
|
| 111 |
return JSONResponse(status_code=400, content={"error": "Unsupported request type"})
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
import uvicorn
|
| 115 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|