Spaces:
Running
Running
Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 1 |
import os
|
| 2 |
-
import asyncio
|
| 3 |
-
import tempfile
|
| 4 |
import requests
|
| 5 |
-
import
|
| 6 |
-
from flask import Flask, request, Response, stream_with_context, render_template_string, send_file
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
# 🔐 100%
|
| 11 |
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
|
| 12 |
BASE_URL = os.environ.get("BASE_URL")
|
| 13 |
MODEL_ID = os.environ.get("MODEL_ID")
|
| 14 |
|
|
|
|
| 15 |
INVOKE_URL = ""
|
| 16 |
if BASE_URL:
|
| 17 |
if not BASE_URL.endswith("/chat/completions"):
|
|
@@ -30,6 +28,7 @@ def home():
|
|
| 30 |
|
| 31 |
@app.route('/api/chat', methods=['POST'])
|
| 32 |
def chat():
|
|
|
|
| 33 |
if not API_KEY or not INVOKE_URL or not MODEL_ID:
|
| 34 |
return Response("Server Error: Configuration secrets are missing.", status=500)
|
| 35 |
|
|
@@ -37,20 +36,24 @@ def chat():
|
|
| 37 |
user_message = data.get("message", "")
|
| 38 |
attachments = data.get("attachments", [])
|
| 39 |
system_prompt = data.get("system_prompt", "")
|
| 40 |
-
history = data.get("history", [])
|
| 41 |
max_tokens = data.get("max_tokens", 4096)
|
| 42 |
-
temperature = data.get("temperature", 0.
|
| 43 |
|
| 44 |
messages = []
|
|
|
|
|
|
|
| 45 |
if system_prompt.strip():
|
| 46 |
messages.append({"role": "system", "content": system_prompt})
|
| 47 |
|
|
|
|
| 48 |
for msg in history:
|
| 49 |
role = msg.get("role", "user")
|
| 50 |
content = msg.get("content", "")
|
| 51 |
if content:
|
| 52 |
messages.append({"role": role, "content": content})
|
| 53 |
|
|
|
|
| 54 |
content_payload = []
|
| 55 |
if user_message.strip():
|
| 56 |
content_payload.append({"type": "text", "text": user_message})
|
|
@@ -58,6 +61,7 @@ def chat():
|
|
| 58 |
for att in attachments:
|
| 59 |
att_type = att.get("type")
|
| 60 |
b64_data = att.get("data")
|
|
|
|
| 61 |
if att_type == "image":
|
| 62 |
content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
|
| 63 |
elif att_type in ["audio", "file"]:
|
|
@@ -83,38 +87,19 @@ def chat():
|
|
| 83 |
}
|
| 84 |
|
| 85 |
try:
|
|
|
|
| 86 |
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
|
|
|
|
| 87 |
def generate():
|
| 88 |
for line in response.iter_lines():
|
| 89 |
if line:
|
| 90 |
decoded_line = line.decode("utf-8")
|
| 91 |
if decoded_line.startswith("data: "):
|
| 92 |
yield decoded_line + "\n\n"
|
|
|
|
| 93 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
| 94 |
except Exception as e:
|
| 95 |
return Response("Internal Error: Unable to process request securely.", status=500)
|
| 96 |
|
| 97 |
-
# 🎙️ PRABHAT NEURAL TTS ENDPOINT (भारी आवाज़ के लिए)
|
| 98 |
-
@app.route('/api/tts', methods=['POST'])
|
| 99 |
-
def tts():
|
| 100 |
-
data = request.get_json() or {}
|
| 101 |
-
text = data.get("text", "")
|
| 102 |
-
|
| 103 |
-
if not text:
|
| 104 |
-
return Response("No text provided", status=400)
|
| 105 |
-
|
| 106 |
-
temp_path = tempfile.mktemp(suffix=".mp3")
|
| 107 |
-
|
| 108 |
-
# प्रभात न्यूरल (India Male) - Pitch और Rate एडजस्ट किया गया है ताकि भारी और प्रोफेशनल लगे।
|
| 109 |
-
async def generate_audio():
|
| 110 |
-
communicate = edge_tts.Communicate(text, "en-IN-PrabhatNeural", pitch="-15%", rate="-5%")
|
| 111 |
-
await communicate.save(temp_path)
|
| 112 |
-
|
| 113 |
-
try:
|
| 114 |
-
asyncio.run(generate_audio())
|
| 115 |
-
return send_file(temp_path, mimetype="audio/mpeg", as_attachment=False)
|
| 116 |
-
except Exception as e:
|
| 117 |
-
return Response(f"TTS Error: {str(e)}", status=500)
|
| 118 |
-
|
| 119 |
if __name__ == '__main__':
|
| 120 |
-
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"):
|
|
|
|
| 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 |
|
|
|
|
| 36 |
user_message = data.get("message", "")
|
| 37 |
attachments = data.get("attachments", [])
|
| 38 |
system_prompt = data.get("system_prompt", "")
|
| 39 |
+
history = data.get("history", []) # <-- यहाँ मैंने हिस्ट्री को रिसीव करने का कोड जोड़ा है
|
| 40 |
max_tokens = data.get("max_tokens", 4096)
|
| 41 |
+
temperature = data.get("temperature", 0.6)
|
| 42 |
|
| 43 |
messages = []
|
| 44 |
+
|
| 45 |
+
# 1. सिस्टम प्रॉम्प्ट
|
| 46 |
if system_prompt.strip():
|
| 47 |
messages.append({"role": "system", "content": system_prompt})
|
| 48 |
|
| 49 |
+
# 2. पुरानी हिस्ट्री (Memory) जोड़ना ताकि अर्जुन पुरानी बातें याद रखे <-- यह ब्लॉक नया जोड़ा गया है
|
| 50 |
for msg in history:
|
| 51 |
role = msg.get("role", "user")
|
| 52 |
content = msg.get("content", "")
|
| 53 |
if content:
|
| 54 |
messages.append({"role": role, "content": content})
|
| 55 |
|
| 56 |
+
# 3. मल्टीमोडल इनपुट (करेंट मैसेज)
|
| 57 |
content_payload = []
|
| 58 |
if user_message.strip():
|
| 59 |
content_payload.append({"type": "text", "text": user_message})
|
|
|
|
| 61 |
for att in attachments:
|
| 62 |
att_type = att.get("type")
|
| 63 |
b64_data = att.get("data")
|
| 64 |
+
|
| 65 |
if att_type == "image":
|
| 66 |
content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
|
| 67 |
elif att_type in ["audio", "file"]:
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
try:
|
| 90 |
+
# सुरक्षित स्ट्रीमिंग रिक्वेस्ट
|
| 91 |
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
|
| 92 |
+
|
| 93 |
def generate():
|
| 94 |
for line in response.iter_lines():
|
| 95 |
if line:
|
| 96 |
decoded_line = line.decode("utf-8")
|
| 97 |
if decoded_line.startswith("data: "):
|
| 98 |
yield decoded_line + "\n\n"
|
| 99 |
+
|
| 100 |
return Response(stream_with_context(generate()), mimetype='text/event-stream')
|
| 101 |
except Exception as e:
|
| 102 |
return Response("Internal Error: Unable to process request securely.", status=500)
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
if __name__ == '__main__':
|
| 105 |
+
app.run(host='0.0.0.0', port=7860)
|