Spaces:
Running
Running
Vedika commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,17 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
-
# 🔐 100%
|
| 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,7 +30,6 @@ 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 |
|
|
@@ -36,16 +37,20 @@ def chat():
|
|
| 36 |
user_message = data.get("message", "")
|
| 37 |
attachments = data.get("attachments", [])
|
| 38 |
system_prompt = data.get("system_prompt", "")
|
|
|
|
| 39 |
max_tokens = data.get("max_tokens", 4096)
|
| 40 |
-
temperature = data.get("temperature", 0.
|
| 41 |
|
| 42 |
messages = []
|
| 43 |
-
|
| 44 |
-
# 1. सिस्टम प्रॉम्प्ट
|
| 45 |
if system_prompt.strip():
|
| 46 |
messages.append({"role": "system", "content": system_prompt})
|
| 47 |
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
content_payload = []
|
| 50 |
if user_message.strip():
|
| 51 |
content_payload.append({"type": "text", "text": user_message})
|
|
@@ -53,7 +58,6 @@ def chat():
|
|
| 53 |
for att in attachments:
|
| 54 |
att_type = att.get("type")
|
| 55 |
b64_data = att.get("data")
|
| 56 |
-
|
| 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"]:
|
|
@@ -79,19 +83,38 @@ def chat():
|
|
| 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)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import asyncio
|
| 3 |
+
import tempfile
|
| 4 |
import requests
|
| 5 |
+
import edge_tts
|
| 6 |
+
from flask import Flask, request, Response, stream_with_context, render_template_string, send_file
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
+
# 🔐 100% Secure Secrets
|
| 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 |
|
| 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 |
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.7)
|
| 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 |
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 |
}
|
| 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)
|