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