import os import requests from flask import Flask, request, Response, stream_with_context, render_template_string 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") 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", []) 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. рдкреБрд░рд╛рдиреА рд╣рд┐рд╕реНрдЯреНрд░реА рд▓реЛрдб рдХрд░рдирд╛ (Safe format рдореЗрдВ) for msg in history: role = "assistant" if msg.get("role") == "bot" else "user" content = msg.get("content", "") if content: messages.append({"role": role, "content": str(content)}) # 3. рдорд▓реНрдЯреАрдореЛрдбрд▓ рдЗрдирдкреБрдЯ (рдХрд░рдВрдЯ рдореИрд╕реЗрдЬ) - рдУрд░рд┐рдЬрд┐рдирд▓ рд▓реЙрдЬрд┐рдХ if attachments: # рдЕрдЧрд░ рдЗрдореЗрдЬ рдпрд╛ рдлрд╛рдЗрд▓ рд╣реИ, рддреЛ рд▓рд┐рд╕реНрдЯ рд╡рд╛рд▓рд╛ рдлреЙрд░реНрдореЗрдЯ рдЗрд╕реНрддреЗрдорд╛рд▓ рд╣реЛрдЧрд╛ 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"}}) messages.append({"role": "user", "content": content_payload}) else: # рдЕрдЧрд░ рдХреЛрдИ рдлрд╛рдЗрд▓ рдирд╣реАрдВ рд╣реИ, рддреЛ рд╕рд┐рдВрдкрд▓ рдЯреЗрдХреНрд╕реНрдЯ рднреЗрдЬрд╛ рдЬрд╛рдПрдЧрд╛ (рдпрд╣реА API рдХреНрд░реИрд╢ рдХреЛ рд░реЛрдХреЗрдЧрд╛) safe_text = user_message.strip() if user_message.strip() else "Hello" messages.append({"role": "user", "content": safe_text}) headers = { "Authorization": f"Bearer {API_KEY}", "Accept": "text/event-stream", "Content-Type": "application/json" } 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) # рдЕрдЧрд░ API рд╕реЗ рдХреЛрдИ рдПрд░рд░ рдЖрддрд╛ рд╣реИ, рддреЛ рд╡рд╣ рдбреЙрдХрд░ (Hugging Face) рдХреЗ рд▓реЙрдЧреНрд╕ рдореЗрдВ рдкреНрд░рд┐рдВрдЯ рд╣реЛ рдЬрд╛рдПрдЧрд╛ if response.status_code != 200: print(f"API Error ({response.status_code}): {response.text}", flush=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: print(f"Backend Exception: {str(e)}", flush=True) return Response("Internal Error: Unable to process request.", status=500) if __name__ == '__main__': # рдбреЙрдХрд░ рд╕реНрдкреЗрд╕ рдХреЗ рд▓рд┐рдП рдкреЛрд░реНрдЯ 7860 app.run(host='0.0.0.0', port=7860)