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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -34
app.py CHANGED
@@ -1,11 +1,13 @@
 
 
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=["*"],
@@ -13,44 +15,47 @@ app.add_middleware(
13
  allow_headers=["*"],
14
  )
15
 
16
- # कॉनफ़िगरेशन - Secrets से
17
- NVIDIA_API_KEY = os.getenv("NVIDIA_API_KEY")
18
- MODEL_NAME = os.getenv("MODEL_NAME", "microsoft/phi-4-multimodal-instruct")
19
  API_URL = "https://integrate.api.nvidia.com/v1/chat/completions"
20
 
21
- # सिस्टम प्ॉम जो हमशा सर्वर पर रहेगा
22
- SYSTEM_PROMPT = "You are Aura Gen 2.0, a state-of-the-art AI assistant developed by Divy Bhai. 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. Use Markdown formatting extensively (code blocks, tables, lists) to structure your answers clearly and effectively."
23
 
24
- @app.post("/v1/chat/completions")
25
- async def chat_proxy(request: Request):
26
  data = await request.json()
27
  user_messages = data.get("messages", [])
28
 
29
- # हमेश सिस्टम प्रॉम्ट को सबसे ऊप जोड़ें
30
- full_messages = [{"role": "system", "content": SYSTEM_PROMPT}] + user_messages
31
 
32
- async with httpx.AsyncClient() as client:
33
- response = await client.post(
34
- API_URL,
35
- headers={
36
- "Authorization": f"Bearer {NVIDIA_API_KEY}",
37
- "Content-Type": "application/json"
38
- },
39
- json={
40
- "model": MODEL_NAME,
41
- "messages": full_messages,
42
- "max_tokens": data.get("max_tokens", 4096),
43
- "temperature": data.get("temperature", 0.6),
44
- "top_p": 0.70,
45
- "frequency_penalty": 0.00,
46
- "presence_penalty": 0.00,
47
- "stream": False
48
- },
49
- timeout=60.0
50
- )
51
- return response.json()
 
 
 
 
52
 
53
  if __name__ == "__main__":
54
  import uvicorn
55
- # Hugging Face Spaces 7860 पोर्ट का उपयोग करता है
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)