EmoCube commited on
Commit
4d30faf
·
verified ·
1 Parent(s): 6fe7868

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +68 -56
main.py CHANGED
@@ -1,80 +1,92 @@
1
- from flask import Flask, request, jsonify
2
- from gradio_client import Client
3
  import uuid
4
  import time
5
  import logging
 
 
6
 
7
  # === Конфигурация ===
8
  AI_MODEL = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"
9
- HF_SPACE_URL = "https://nymbo-serverless-textgen-hub.hf.space"
10
 
11
- # === Инициализация ===
12
- app = Flask(__name__)
13
  logging.basicConfig(level=logging.INFO)
 
14
 
15
- try:
16
- gr_client = Client(src=HF_SPACE_URL)
17
- except Exception as e:
18
- logging.error(f"Ошибка подключения к HuggingFace Space: {e}")
19
- gr_client = None
20
 
21
- # === Корневой эндпоинт ===
22
  @app.route("/", methods=["GET"])
23
- def home():
24
  return "Proxy free test", 200
25
 
26
- # === Основной OpenAI-подобный endpoint ===
27
  @app.route("/v1/chat/completions", methods=["POST"])
28
- def chat():
 
 
 
 
 
29
  try:
30
- data = request.get_json()
31
- messages = data.get("messages", [])
 
 
32
  user_msg = next((m["content"] for m in reversed(messages) if m["role"] == "user"), None)
33
  system_msg = next((m["content"] for m in messages if m["role"] == "system"), "You are a helpful AI assistant.")
34
- except Exception as e:
35
- return jsonify({"error": f"Невалидный JSON: {e}"}), 400
36
 
37
- if not user_msg or not gr_client:
38
- return jsonify({"error": "Отсутствует сообщение пользователя или не подключена модель."}), 400
39
 
40
- try:
41
- output = gr_client.predict(
42
- history=[[user_msg, None]],
43
- system_msg=system_msg,
44
- max_tokens=512,
45
- temperature=0.7,
46
- top_p=0.95,
47
- freq_penalty=0,
48
- seed=-1,
49
- custom_model=AI_MODEL,
50
- search_term="",
51
- selected_model=AI_MODEL,
52
- api_name="/bot"
53
- )
54
- except Exception as e:
55
- return jsonify({"error": f"Ошибка при вызове модели: {e}"}), 500
 
 
 
 
 
 
56
 
57
- return jsonify({
58
- "id": f"chatcmpl-{uuid.uuid4().hex[:12]}",
59
- "object": "chat.completion",
60
- "created": int(time.time()),
61
- "model": AI_MODEL,
62
- "choices": [
63
- {
64
- "index": 0,
65
- "message": {
66
- "role": "assistant",
67
- "content": output
68
- },
69
- "finish_reason": "stop"
 
 
 
 
 
 
70
  }
71
- ],
72
- "usage": {
73
- "prompt_tokens": 0,
74
- "completion_tokens": 0,
75
- "total_tokens": 0
76
- }
77
- })
78
 
79
  # === Запуск сервера ===
80
  if __name__ == "__main__":
 
 
 
1
  import uuid
2
  import time
3
  import logging
4
+ from flask import Flask, request, jsonify
5
+ import requests
6
 
7
  # === Конфигурация ===
8
  AI_MODEL = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"
9
+ HF_API_URL = "https://nymbo-serverless-textgen-hub.hf.space/run/bot"
10
 
11
+ # === Логгер ===
 
12
  logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
 
15
+ # === Инициализация Flask ===
16
+ app = Flask(__name__)
 
 
 
17
 
18
+ # === Корневой маршрут для проверки работоспособности ===
19
  @app.route("/", methods=["GET"])
20
+ def index():
21
  return "Proxy free test", 200
22
 
23
+ # === Основной OpenAI-совместимый endpoint ===
24
  @app.route("/v1/chat/completions", methods=["POST"])
25
+ def chat_completion():
26
+ # Логируем входной запрос
27
+ logger.info(f"📥 Запрос от клиента: {request.remote_addr}")
28
+ logger.info(f"Headers:\n{dict(request.headers)}")
29
+ logger.info(f"Body:\n{request.get_data(as_text=True)}")
30
+
31
  try:
32
+ payload = request.get_json()
33
+ messages = payload.get("messages", [])
34
+
35
+ # Извлекаем последнее сообщение пользователя
36
  user_msg = next((m["content"] for m in reversed(messages) if m["role"] == "user"), None)
37
  system_msg = next((m["content"] for m in messages if m["role"] == "system"), "You are a helpful AI assistant.")
 
 
38
 
39
+ if not user_msg:
40
+ return jsonify({"error": "Missing user message"}), 400
41
 
42
+ # Подготавливаем запрос к Hugging Face Space
43
+ hf_payload = {
44
+ "data": [
45
+ [[user_msg, None]], # history
46
+ system_msg, # system_msg
47
+ 512, # max_tokens
48
+ 0.7, # temperature
49
+ 0.95, # top_p
50
+ 0, # freq_penalty
51
+ -1, # seed
52
+ AI_MODEL, # custom_model
53
+ "", # search_term
54
+ AI_MODEL # selected_model
55
+ ]
56
+ }
57
+
58
+ # Выполняем запрос
59
+ hf_response = requests.post(HF_API_URL, json=hf_payload, timeout=60)
60
+ hf_response.raise_for_status()
61
+ result = hf_response.json()
62
+
63
+ output_text = result["data"][0] if "data" in result else "❌ Unexpected response format"
64
 
65
+ return jsonify({
66
+ "id": f"chatcmpl-{uuid.uuid4().hex[:12]}",
67
+ "object": "chat.completion",
68
+ "created": int(time.time()),
69
+ "model": AI_MODEL,
70
+ "choices": [
71
+ {
72
+ "index": 0,
73
+ "message": {
74
+ "role": "assistant",
75
+ "content": output_text
76
+ },
77
+ "finish_reason": "stop"
78
+ }
79
+ ],
80
+ "usage": {
81
+ "prompt_tokens": 0,
82
+ "completion_tokens": 0,
83
+ "total_tokens": 0
84
  }
85
+ })
86
+
87
+ except Exception as e:
88
+ logger.exception("❌ Ошибка обработки запроса:")
89
+ return jsonify({"error": str(e)}), 500
 
 
90
 
91
  # === Запуск сервера ===
92
  if __name__ == "__main__":