Spaces:
Sleeping
Sleeping
File size: 5,292 Bytes
7f3d153 d172eee 0083f4e d172eee 0083f4e 5e89b71 0083f4e fd666bc 0083f4e 5e89b71 fd666bc 0083f4e 5e89b71 fd666bc 0083f4e d0796d1 d172eee d0796d1 0083f4e d0796d1 fd666bc d0796d1 d172eee 5f7bedc 5e89b71 18db9b3 5e89b71 18db9b3 5e89b71 d0796d1 0083f4e 5e89b71 0083f4e d0796d1 0083f4e fd666bc 5e89b71 fd666bc 5e89b71 0083f4e d172eee 0083f4e d172eee | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | import os
import asyncio
import tempfile
import requests
import edge_tts
from flask import Flask, request, Response, stream_with_context, render_template_string, send_file
app = Flask(__name__)
# 🔐 100% सुरक्षित: सब कुछ केवल 'Secrets' से कॉल होगा, कोई हार्डकोडेड डेटा नहीं।
API_KEY = os.environ.get("YOUR_VEDIKA_API_KEY")
BASE_URL = os.environ.get("BASE_URL")
MODEL_ID = os.environ.get("MODEL_ID")
# URL को सुरक्षित रूप से बनाना ताकि कोई डिफ़ॉल्ट लिंक न दिखे
INVOKE_URL = ""
if BASE_URL:
if not BASE_URL.endswith("/chat/completions"):
INVOKE_URL = f"{BASE_URL.rstrip('/')}/chat/completions"
else:
INVOKE_URL = BASE_URL
@app.route('/')
def home():
try:
with open('index.html', 'r', encoding='utf-8') as f:
html_content = f.read()
return render_template_string(html_content)
except Exception as e:
return f"index.html file missing! Error: {str(e)}"
@app.route('/api/chat', methods=['POST'])
def chat():
# सुरक्षा जांच: अगर सीक्रेट्स सेट नहीं हैं, तो रिक्वेस्ट आगे नहीं जाएगी
if not API_KEY or not INVOKE_URL or not MODEL_ID:
return Response("Server Error: Configuration secrets are missing.", status=500)
data = request.get_json() or {}
user_message = data.get("message", "")
attachments = data.get("attachments", [])
system_prompt = data.get("system_prompt", "")
history = data.get("history", []) # पुरानी हिस्ट्री (Memory)
max_tokens = data.get("max_tokens", 4096)
temperature = data.get("temperature", 0.6)
messages = []
# 1. सिस्टम प्रॉम्प्ट
if system_prompt.strip():
messages.append({"role": "system", "content": system_prompt})
# 2. पुरानी हिस्ट्री (Memory) जोड़ना ताकि अर्जुन पुरानी बातें याद रखे
for msg in history:
role = msg.get("role", "user")
content = msg.get("content", "")
if content:
messages.append({"role": role, "content": content})
# 3. मल्टीमोडल इनपुट (करेंट मैसेज)
content_payload = []
if user_message.strip():
content_payload.append({"type": "text", "text": user_message})
for att in attachments:
att_type = att.get("type")
b64_data = att.get("data")
if att_type == "image":
content_payload.append({"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_data}"}})
elif att_type in ["audio", "file"]:
content_payload.append({"type": "input_audio", "input_audio": {"data": b64_data, "format": "wav"}})
if not content_payload:
content_payload.append({"type": "text", "text": "Hello"})
messages.append({"role": "user", "content": content_payload})
headers = {
"Authorization": f"Bearer {API_KEY}",
"Accept": "text/event-stream"
}
payload = {
"model": MODEL_ID,
"messages": messages,
"max_tokens": int(max_tokens),
"temperature": float(temperature),
"top_p": 0.70,
"stream": True
}
try:
# सुरक्षित स्ट्रीमिंग रिक्वेस्ट
response = requests.post(INVOKE_URL, headers=headers, json=payload, stream=True)
def generate():
for line in response.iter_lines():
if line:
decoded_line = line.decode("utf-8")
if decoded_line.startswith("data: "):
yield decoded_line + "\n\n"
return Response(stream_with_context(generate()), mimetype='text/event-stream')
except Exception as e:
return Response("Internal Error: Unable to process request securely.", status=500)
# 🎙️ नया TTS एंडपॉइंट (Microsoft Edge - Prabhat Neural)
@app.route('/api/tts', methods=['POST'])
def tts():
data = request.get_json() or {}
text = data.get("text", "")
if not text:
return Response("No text provided", status=400)
# एक टेम्परेरी MP3 फाइल बनाना
temp_path = tempfile.mktemp(suffix=".mp3")
# आवाज़ को भारी (Heavy/Deep) करने के लिए pitch को कम किया गया है (-20%)
# Prabhat Neural की आवाज सेट की गई है।
async def generate_audio():
communicate = edge_tts.Communicate(text, "en-IN-PrabhatNeural", pitch="-20%", rate="-5%")
await communicate.save(temp_path)
try:
asyncio.run(generate_audio())
return send_file(temp_path, mimetype="audio/mpeg", as_attachment=False)
except Exception as e:
return Response(f"TTS Error: {str(e)}", status=500)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860) |