| import uuid |
| import time |
| import logging |
| from flask import Flask, request, jsonify |
| import requests |
|
|
| |
| AI_MODEL = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO" |
| HF_API_URL = "https://nymbo-serverless-textgen-hub.hf.space/run/bot" |
|
|
| |
| logging.basicConfig(level=logging.INFO) |
| logger = logging.getLogger(__name__) |
|
|
| |
| app = Flask(__name__) |
|
|
| |
| @app.route("/", methods=["GET"]) |
| def index(): |
| return "Proxy free test", 200 |
|
|
| |
| @app.route("/v1/chat/completions", methods=["POST"]) |
| def chat_completion(): |
| |
| logger.info(f"📥 Запрос от клиента: {request.remote_addr}") |
| logger.info(f"Headers:\n{dict(request.headers)}") |
| logger.info(f"Body:\n{request.get_data(as_text=True)}") |
|
|
| try: |
| payload = request.get_json() |
| messages = payload.get("messages", []) |
|
|
| |
| user_msg = next((m["content"] for m in reversed(messages) if m["role"] == "user"), None) |
| system_msg = next((m["content"] for m in messages if m["role"] == "system"), "You are a helpful AI assistant.") |
|
|
| if not user_msg: |
| return jsonify({"error": "Missing user message"}), 400 |
|
|
| |
| hf_payload = { |
| "data": [ |
| [[user_msg, None]], |
| system_msg, |
| 512, |
| 0.7, |
| 0.95, |
| 0, |
| -1, |
| AI_MODEL, |
| "", |
| AI_MODEL |
| ] |
| } |
|
|
| |
| hf_response = requests.post(HF_API_URL, json=hf_payload, timeout=60) |
| hf_response.raise_for_status() |
| result = hf_response.json() |
|
|
| output_text = result["data"][0] if "data" in result else "❌ Unexpected response format" |
|
|
| return jsonify({ |
| "id": f"chatcmpl-{uuid.uuid4().hex[:12]}", |
| "object": "chat.completion", |
| "created": int(time.time()), |
| "model": AI_MODEL, |
| "choices": [ |
| { |
| "index": 0, |
| "message": { |
| "role": "assistant", |
| "content": output_text |
| }, |
| "finish_reason": "stop" |
| } |
| ], |
| "usage": { |
| "prompt_tokens": 0, |
| "completion_tokens": 0, |
| "total_tokens": 0 |
| } |
| }) |
|
|
| except Exception as e: |
| logger.exception("❌ Ошибка обработки запроса:") |
| return jsonify({"error": str(e)}), 500 |
|
|
| |
| if __name__ == "__main__": |
| app.run(host="0.0.0.0", port=7860, debug=True) |
|
|