File size: 2,585 Bytes
6fe7868 ab9cd81 6fe7868 21dd7fa ab9cd81 6fe7868 8b04581 6fe7868 8b04581 21dd7fa 6fe7868 21dd7fa 6fe7868 21dd7fa 6fe7868 8b04581 6fe7868 21dd7fa 6fe7868 21dd7fa 8b04581 21dd7fa 8b04581 21dd7fa 6fe7868 ab9cd81 6fe7868 ab9cd81 8b04581 ab9cd81 6fe7868 ab9cd81 21dd7fa ab9cd81 6fe7868 ab9cd81 21dd7fa ab9cd81 6fe7868 |
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 |
from flask import Flask, request, jsonify
from gradio_client import Client
import uuid
import time
import logging
# === Конфигурация ===
AI_MODEL = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"
HF_SPACE_URL = "https://nymbo-serverless-textgen-hub.hf.space"
# === Инициализация ===
app = Flask(__name__)
logging.basicConfig(level=logging.INFO)
try:
gr_client = Client(src=HF_SPACE_URL)
except Exception as e:
logging.error(f"Ошибка подключения к HuggingFace Space: {e}")
gr_client = None
# === Корневой эндпоинт ===
@app.route("/", methods=["GET"])
def home():
return "Proxy free test", 200
# === Основной OpenAI-подобный endpoint ===
@app.route("/v1/chat/completions", methods=["POST"])
def chat():
try:
data = request.get_json()
messages = data.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.")
except Exception as e:
return jsonify({"error": f"Невалидный JSON: {e}"}), 400
if not user_msg or not gr_client:
return jsonify({"error": "Отсутствует сообщение пользователя или не подключена модель."}), 400
try:
output = gr_client.predict(
history=[[user_msg, None]],
system_msg=system_msg,
max_tokens=512,
temperature=0.7,
top_p=0.95,
freq_penalty=0,
seed=-1,
custom_model=AI_MODEL,
search_term="",
selected_model=AI_MODEL,
api_name="/bot"
)
except Exception as e:
return jsonify({"error": f"Ошибка при вызове модели: {e}"}), 500
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
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0
}
})
# === Запуск сервера ===
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)
|