Spaces:
Running
Running
Sheelu Katiyar commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,16 +5,15 @@ from flask import Flask, request, Response, stream_with_context, render_template
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
-
#
|
| 9 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 10 |
BASE_URL = os.environ.get("BASE_URL")
|
| 11 |
-
MODEL_ID = os.environ.get("MODEL_ID")
|
| 12 |
|
| 13 |
-
# URL को सुरक्षित बनाना (अगर BASE_URL में अंत में /chat/completions नहीं है तो उसे जोड़ना)
|
| 14 |
if BASE_URL and not BASE_URL.endswith("/chat/completions"):
|
| 15 |
INVOKE_URL = f"{BASE_URL.rstrip('/')}/chat/completions"
|
| 16 |
else:
|
| 17 |
-
INVOKE_URL = BASE_URL
|
| 18 |
|
| 19 |
@app.route('/')
|
| 20 |
def home():
|
|
@@ -30,50 +29,51 @@ def chat():
|
|
| 30 |
data = request.get_json() or {}
|
| 31 |
user_message = data.get("message", "")
|
| 32 |
attachments = data.get("attachments", [])
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
content_payload = []
|
| 36 |
-
|
| 37 |
-
# 1. टेक्स्ट इनपुट को जोड़ना
|
| 38 |
if user_message.strip():
|
| 39 |
content_payload.append({"type": "text", "text": user_message})
|
| 40 |
|
| 41 |
-
# 2. इमेज या ऑडियो (Base64) को जोड़ना
|
| 42 |
for att in attachments:
|
| 43 |
att_type = att.get("type")
|
| 44 |
b64_data = att.get("data")
|
| 45 |
|
| 46 |
if att_type == "image":
|
| 47 |
-
content_payload.append({
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
})
|
| 51 |
-
elif att_type == "audio":
|
| 52 |
-
content_payload.append({
|
| 53 |
-
"type": "input_audio",
|
| 54 |
-
"input_audio": {"data": b64_data, "format": "wav"}
|
| 55 |
-
})
|
| 56 |
|
| 57 |
-
# डिफ़ॉल्ट हैंडलिंग
|
| 58 |
if not content_payload:
|
| 59 |
content_payload.append({"type": "text", "text": "Hello"})
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
headers = {
|
| 62 |
"Authorization": f"Bearer {API_KEY}",
|
| 63 |
"Accept": "text/event-stream"
|
| 64 |
}
|
| 65 |
|
| 66 |
-
# डायनामिक मॉडल आईडी का उपयोग
|
| 67 |
payload = {
|
| 68 |
"model": MODEL_ID,
|
| 69 |
-
"messages":
|
| 70 |
-
"max_tokens":
|
| 71 |
-
"temperature":
|
| 72 |
"top_p": 0.70,
|
| 73 |
"stream": True
|
| 74 |
}
|
| 75 |
|
| 76 |
-
# API पर स्ट्रीमिंग रिक्वेस्ट भेजना
|
| 77 |
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
|
| 78 |
|
| 79 |
def generate():
|
|
|
|
| 5 |
|
| 6 |
app = Flask(__name__)
|
| 7 |
|
| 8 |
+
# हगिंग फेस 'Secrets'
|
| 9 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 10 |
BASE_URL = os.environ.get("BASE_URL")
|
| 11 |
+
MODEL_ID = os.environ.get("MODEL_ID", "google/gemma-3n-e2b-it")
|
| 12 |
|
|
|
|
| 13 |
if BASE_URL and not BASE_URL.endswith("/chat/completions"):
|
| 14 |
INVOKE_URL = f"{BASE_URL.rstrip('/')}/chat/completions"
|
| 15 |
else:
|
| 16 |
+
INVOKE_URL = BASE_URL or "https://integrate.api.nvidia.com/v1/chat/completions"
|
| 17 |
|
| 18 |
@app.route('/')
|
| 19 |
def home():
|
|
|
|
| 29 |
data = request.get_json() or {}
|
| 30 |
user_message = data.get("message", "")
|
| 31 |
attachments = data.get("attachments", [])
|
| 32 |
+
system_prompt = data.get("system_prompt", "")
|
| 33 |
+
max_tokens = data.get("max_tokens", 4096)
|
| 34 |
+
temperature = data.get("temperature", 0.6)
|
| 35 |
|
| 36 |
+
messages = []
|
| 37 |
+
|
| 38 |
+
# 1. सिस्टम प्रॉम्प्ट (System Instruction) सेट करना
|
| 39 |
+
if system_prompt.strip():
|
| 40 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 41 |
+
|
| 42 |
+
# 2. मल्टीमोडल कंटेंट एरे तैयार करना
|
| 43 |
content_payload = []
|
|
|
|
|
|
|
| 44 |
if user_message.strip():
|
| 45 |
content_payload.append({"type": "text", "text": user_message})
|
| 46 |
|
|
|
|
| 47 |
for att in attachments:
|
| 48 |
att_type = att.get("type")
|
| 49 |
b64_data = att.get("data")
|
| 50 |
|
| 51 |
if att_type == "image":
|
| 52 |
+
content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
|
| 53 |
+
elif att_type in ["audio", "file"]:
|
| 54 |
+
# ऑडियो या अन्य फ़ाइलों के लिए
|
| 55 |
+
content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 57 |
if not content_payload:
|
| 58 |
content_payload.append({"type": "text", "text": "Hello"})
|
| 59 |
|
| 60 |
+
# यूज़र का मैसेज जोड़ना
|
| 61 |
+
messages.append({"role": "user", "content": content_payload})
|
| 62 |
+
|
| 63 |
headers = {
|
| 64 |
"Authorization": f"Bearer {API_KEY}",
|
| 65 |
"Accept": "text/event-stream"
|
| 66 |
}
|
| 67 |
|
|
|
|
| 68 |
payload = {
|
| 69 |
"model": MODEL_ID,
|
| 70 |
+
"messages": messages,
|
| 71 |
+
"max_tokens": int(max_tokens),
|
| 72 |
+
"temperature": float(temperature),
|
| 73 |
"top_p": 0.70,
|
| 74 |
"stream": True
|
| 75 |
}
|
| 76 |
|
|
|
|
| 77 |
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
|
| 78 |
|
| 79 |
def generate():
|