Hanuman2 commited on
Commit
3914a72
·
verified ·
1 Parent(s): e1f24b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -40
app.py CHANGED
@@ -2,12 +2,12 @@ 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,47 +15,73 @@ app.add_middleware(
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)
 
2
  import httpx
3
  from fastapi import FastAPI, Request
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ from fastapi.responses import StreamingResponse, JSONResponse
6
 
7
+ # सवर की मुख्य नींव
8
  app = FastAPI()
9
 
10
+ # क्रॉस-ओरिजिन (CORS) नीतिय ताि आपका सुरक्षित फ्रंटंड इससे जुड़
11
  app.add_middleware(
12
  CORSMiddleware,
13
  allow_origins=["*"],
 
15
  allow_headers=["*"],
16
  )
17
 
18
+ # स संवेदनशील जानारी सीधे सीक्रेट्स (Environment Variables) से ली एगी
19
+ SECRET_API_KEY = os.getenv("SECRET_API_KEY")
20
+ SECRET_API_URL = os.getenv("SECRET_API_URL")
21
+ SECRET_MODEL_NAME = os.getenv("SECRET_MODEL_NAME")
22
+ SECRET_SYSTEM_PROMPT = os.getenv("SECRET_SYSTEM_PROMPT")
 
 
23
 
24
  @app.post("/chat")
25
  async def process_chat(request: Request):
26
+ # जांच लें कि सीक्रेट्स सही से लोड हुए हैं या नहीं
27
+ if not SECRET_API_KEY or not SECRET_API_URL or not SECRET_MODEL_NAME:
28
+ return JSONResponse(
29
+ status_code=500,
30
+ content={"error": "सर्वर त्रुटि: कृपया हगिंग फेस स्पेस के Secrets में API_KEY, API_URL और MODEL_NAME सेट करें।"}
31
+ )
32
+
33
+ try:
34
+ data = await request.json()
35
+ user_messages = data.get("messages", [])
36
+
37
+ # संदेशों को तैयार करना (अगर सिस्टम प्रॉम्प्ट सीक्रेट्स में है, तो उसे सबसे ऊपर जोड़ें)
38
+ full_messages = []
39
+ if SECRET_SYSTEM_PROMPT:
40
+ full_messages.append({"role": "system", "content": SECRET_SYSTEM_PROMPT})
41
+
42
+ full_messages.extend(user_messages)
43
+
44
+ # पेलोड का निर्माण (स्ट्रीमिंग और इमेजेस के सपोर्ट के साथ)
45
+ payload = {
46
+ "model": SECRET_MODEL_NAME,
47
+ "messages": full_messages,
48
+ "max_tokens": data.get("max_tokens", 4096),
49
+ "temperature": data.get("temperature", 1.00),
50
+ "top_p": data.get("top_p", 0.95),
51
+ "stream": True,
52
+ "chat_template_kwargs": {"enable_thinking": True}
53
+ }
54
+
55
+ headers = {
56
+ "Authorization": f"Bearer {SECRET_API_KEY}",
57
+ "Content-Type": "application/json",
58
+ "Accept": "text/event-stream"
59
+ }
60
+
61
+ # स्ट्रीमिंग डेटा जनरेटर (Stream Generator)
62
+ async def event_stream_generator():
63
+ async with httpx.AsyncClient(timeout=120.0) as client:
64
+ async with client.stream("POST", SECRET_API_URL, headers=headers, json=payload) as response:
65
+
66
+ # यदि कोई त्रुटि आती है तो उसे पकड़कर फ्रंटएंड को भेजें
67
+ if response.status_code != 200:
68
+ error_detail = await response.aread()
69
+ yield f"data: {{\"error\": \"बाहरी सर्वर से संपर्क में त्रुटि, कोड: {response.status_code}\"}}\n\n"
70
+ return
71
+
72
+ # सफलता पूर्वक स्ट्रीम प्राप्त करना
73
+ async for line in response.aiter_lines():
74
+ if line:
75
+ # डेटा को सही SSE (Server-Sent Events) प्रारूप में भेजें
76
+ yield f"{line}\n\n"
77
+
78
+ # स्ट्रीमिंग रिस्पॉन्स को सीधे फ्रंटएंड की ओर प्रवाहित करें
79
+ return StreamingResponse(event_stream_generator(), media_type="text/event-stream")
80
+
81
+ except Exception as e:
82
+ return JSONResponse(status_code=500, content={"error": f"आंतरिक सर्वर त्रुटि: {str(e)}"})
83
 
84
  if __name__ == "__main__":
85
  import uvicorn
86
+ # पोर्ट 7860 का उपयोग (जो कि इस प्लेटफ़ॉर्म का मानक है)
87
  uvicorn.run(app, host="0.0.0.0", port=7860)