Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
import
|
| 4 |
-
import os
|
| 5 |
|
|
|
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
app.add_middleware(
|
| 10 |
CORSMiddleware,
|
| 11 |
allow_origins=["*"],
|
|
@@ -13,44 +15,47 @@ app.add_middleware(
|
|
| 13 |
allow_headers=["*"],
|
| 14 |
)
|
| 15 |
|
| 16 |
-
# क
|
| 17 |
-
|
| 18 |
-
MODEL_NAME = os.getenv("MODEL_NAME", "
|
| 19 |
API_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
|
| 20 |
|
| 21 |
-
# सिस्टम
|
| 22 |
-
|
| 23 |
|
| 24 |
-
@app.post("/
|
| 25 |
-
async def
|
| 26 |
data = await request.json()
|
| 27 |
user_messages = data.get("messages", [])
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
full_messages = [{"role": "system", "content":
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
import uvicorn
|
| 55 |
-
|
| 56 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import httpx
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.responses import StreamingResponse
|
|
|
|
| 6 |
|
| 7 |
+
# सिस्टम की नींव: FastAPI एप्लीकेशन
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# सुरक्षा के लिए CORS का सेटअप
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
| 13 |
allow_origins=["*"],
|
|
|
|
| 15 |
allow_headers=["*"],
|
| 16 |
)
|
| 17 |
|
| 18 |
+
# सीक्रेट्स से डेटा प्राप्त करना
|
| 19 |
+
API_KEY = os.getenv("API_KEY")
|
| 20 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "meta/llama-4-maverick-17b-128e-instruct")
|
| 21 |
API_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
|
| 22 |
|
| 23 |
+
# सिस्टम निर्देश - आपके द्वारा बताए गए नाम के साथ
|
| 24 |
+
SYSTEM_INSTRUCTION = "You are Aura Gen 2.0, a state-of-the-art AI assistant developed by Divy. You possess world-class expertise in software engineering, advanced algorithmic logic, and complex problem-solving. Always provide highly optimized, secure, and precise solutions in English by default. You are capable of analyzing both text and images provided by the user. Use Markdown formatting extensively."
|
| 25 |
|
| 26 |
+
@app.post("/chat")
|
| 27 |
+
async def process_chat(request: Request):
|
| 28 |
data = await request.json()
|
| 29 |
user_messages = data.get("messages", [])
|
| 30 |
|
| 31 |
+
# संदेशों को व्यवस्थित करना
|
| 32 |
+
full_messages = [{"role": "system", "content": SYSTEM_INSTRUCTION}] + user_messages
|
| 33 |
|
| 34 |
+
# पेलोड बनाना (इमेज और टेक्स्ट दोनों के लिए तैयार)
|
| 35 |
+
payload = {
|
| 36 |
+
"model": MODEL_NAME,
|
| 37 |
+
"messages": full_messages,
|
| 38 |
+
"max_tokens": 1024,
|
| 39 |
+
"temperature": 0.7,
|
| 40 |
+
"top_p": 0.9,
|
| 41 |
+
"stream": True
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# स्ट्रीमिंग क्लाइंट बनाना
|
| 45 |
+
async def generate_stream():
|
| 46 |
+
async with httpx.AsyncClient(timeout=60.0) as client:
|
| 47 |
+
async with client.stream(
|
| 48 |
+
"POST",
|
| 49 |
+
API_URL,
|
| 50 |
+
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
|
| 51 |
+
json=payload
|
| 52 |
+
) as response:
|
| 53 |
+
async for line in response.aiter_lines():
|
| 54 |
+
if line:
|
| 55 |
+
yield line + "\n"
|
| 56 |
+
|
| 57 |
+
return StreamingResponse(generate_stream(), media_type="text/event-stream")
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
import uvicorn
|
| 61 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|