Spaces:
Running
Running
Sheelu Katiyar commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,21 @@
|
|
| 1 |
import os
|
| 2 |
-
import json
|
| 3 |
import requests
|
| 4 |
from flask import Flask, request, Response, stream_with_context, render_template_string
|
| 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 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
@app.route('/')
|
| 19 |
def home():
|
|
@@ -26,6 +28,10 @@ def home():
|
|
| 26 |
|
| 27 |
@app.route('/api/chat', methods=['POST'])
|
| 28 |
def chat():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
data = request.get_json() or {}
|
| 30 |
user_message = data.get("message", "")
|
| 31 |
attachments = data.get("attachments", [])
|
|
@@ -35,11 +41,11 @@ def chat():
|
|
| 35 |
|
| 36 |
messages = []
|
| 37 |
|
| 38 |
-
# 1. सिस्टम प्रॉम्प्ट
|
| 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})
|
|
@@ -51,13 +57,11 @@ def chat():
|
|
| 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 = {
|
|
@@ -74,16 +78,20 @@ def chat():
|
|
| 74 |
"stream": True
|
| 75 |
}
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
if
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if __name__ == '__main__':
|
| 89 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
from flask import Flask, request, Response, stream_with_context, render_template_string
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# 🔐 100% सुरक्षित: सब कुछ केवल 'Secrets' से कॉल होगा, कोई हार्डकोडेड डेटा नहीं।
|
| 8 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 9 |
BASE_URL = os.environ.get("BASE_URL")
|
| 10 |
+
MODEL_ID = os.environ.get("MODEL_ID")
|
| 11 |
|
| 12 |
+
# URL को सुरक्षित रूप से बनाना ताकि कोई डिफ़ॉल्ट लिंक न दिखे
|
| 13 |
+
INVOKE_URL = ""
|
| 14 |
+
if BASE_URL:
|
| 15 |
+
if not BASE_URL.endswith("/chat/completions"):
|
| 16 |
+
INVOKE_URL = f"{BASE_URL.rstrip('/')}/chat/completions"
|
| 17 |
+
else:
|
| 18 |
+
INVOKE_URL = BASE_URL
|
| 19 |
|
| 20 |
@app.route('/')
|
| 21 |
def home():
|
|
|
|
| 28 |
|
| 29 |
@app.route('/api/chat', methods=['POST'])
|
| 30 |
def chat():
|
| 31 |
+
# सुरक्षा जांच: अगर सीक्रेट्स सेट नहीं हैं, तो रिक्वेस्ट आगे नहीं जाएगी
|
| 32 |
+
if not API_KEY or not INVOKE_URL or not MODEL_ID:
|
| 33 |
+
return Response("Server Error: Configuration secrets are missing.", status=500)
|
| 34 |
+
|
| 35 |
data = request.get_json() or {}
|
| 36 |
user_message = data.get("message", "")
|
| 37 |
attachments = data.get("attachments", [])
|
|
|
|
| 41 |
|
| 42 |
messages = []
|
| 43 |
|
| 44 |
+
# 1. सिस्टम प्रॉम्प्ट
|
| 45 |
if system_prompt.strip():
|
| 46 |
messages.append({"role": "system", "content": system_prompt})
|
| 47 |
|
| 48 |
+
# 2. मल्टीमोडल इनपुट
|
| 49 |
content_payload = []
|
| 50 |
if user_message.strip():
|
| 51 |
content_payload.append({"type": "text", "text": user_message})
|
|
|
|
| 57 |
if att_type == "image":
|
| 58 |
content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
|
| 59 |
elif att_type in ["audio", "file"]:
|
|
|
|
| 60 |
content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
|
| 61 |
|
| 62 |
if not content_payload:
|
| 63 |
content_payload.append({"type": "text", "text": "Hello"})
|
| 64 |
|
|
|
|
| 65 |
messages.append({"role": "user", "content": content_payload})
|
| 66 |
|
| 67 |
headers = {
|
|
|
|
| 78 |
"stream": True
|
| 79 |
}
|
| 80 |
|
| 81 |
+
try:
|
| 82 |
+
# सुरक्षित स्ट्रीमिंग रिक्वेस्ट
|
| 83 |
+
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
|
| 84 |
+
|
| 85 |
+
def generate():
|
| 86 |
+
for line in response.iter_lines():
|
| 87 |
+
if line:
|
| 88 |
+
decoded_line = line.decode("utf-8")
|
| 89 |
+
if decoded_line.startswith("data: "):
|
| 90 |
+
yield decoded_line + "\n\n"
|
| 91 |
+
|
| 92 |
+
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return Response("Internal Error: Unable to process request securely.", status=500)
|
| 95 |
|
| 96 |
if __name__ == '__main__':
|
| 97 |
app.run(host='0.0.0.0', port=7860)
|