File size: 3,483 Bytes
d0e5fb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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/gradio_api/call/bot"

# === Логгер ===
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# === Инициализация Flask ===
app = Flask(__name__)

# === Корневой маршрут для проверки работоспособности ===
@app.route("/", methods=["GET"])
def index():
    return "Proxy free test", 200

# === Основной OpenAI-совместимый endpoint ===
@app.route("/use", 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

        # Подготавливаем запрос к Hugging Face Space
        hf_payload = {
            "data": [
                [[user_msg, None]],     # history
                system_msg,             # system_msg
                512,                    # max_tokens
                0.7,                    # temperature
                0.95,                   # top_p
                0,                      # freq_penalty
                -1,                     # seed
                AI_MODEL,               # custom_model
                "",                     # search_term
                AI_MODEL                # selected_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"
        print(f"\n🧠 Ответ модели:\n{output_text}\n")  # ⬅️ Вывод в консоль

        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)