Update main.py
Browse files
main.py
CHANGED
|
@@ -1,37 +1,46 @@
|
|
| 1 |
-
from
|
| 2 |
-
from fastapi.responses import PlainTextResponse
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
from typing import List, Optional, Literal
|
| 5 |
from gradio_client import Client
|
| 6 |
-
import uvicorn
|
| 7 |
-
import time
|
| 8 |
import uuid
|
|
|
|
| 9 |
import logging
|
| 10 |
|
| 11 |
-
# ===
|
| 12 |
-
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
| 13 |
-
logger = logging.getLogger(__name__)
|
| 14 |
-
|
| 15 |
-
# === Задаём модель (используется глобально) ===
|
| 16 |
AI_MODEL = "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
# === Подключаемся к Gradio Space по URL ===
|
| 19 |
try:
|
| 20 |
-
gr_client = Client(src=
|
| 21 |
-
logger.info("✅ Успешно подключено к Gradio Space")
|
| 22 |
except Exception as e:
|
| 23 |
-
|
| 24 |
gr_client = None
|
| 25 |
|
| 26 |
-
# ===
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
max_tokens=512,
|
| 36 |
temperature=0.7,
|
| 37 |
top_p=0.95,
|
|
@@ -42,58 +51,10 @@ def ask(user_prompt, system_prompt):
|
|
| 42 |
selected_model=AI_MODEL,
|
| 43 |
api_name="/bot"
|
| 44 |
)
|
| 45 |
-
return result
|
| 46 |
except Exception as e:
|
| 47 |
-
|
| 48 |
-
return f"[Ошибка: {str(e)}]"
|
| 49 |
-
|
| 50 |
-
# === FastAPI приложение ===
|
| 51 |
-
app = FastAPI()
|
| 52 |
-
|
| 53 |
-
# === Модели запроса ===
|
| 54 |
-
class Message(BaseModel):
|
| 55 |
-
role: Literal["user", "assistant", "system"]
|
| 56 |
-
content: str
|
| 57 |
-
|
| 58 |
-
class ChatRequest(BaseModel):
|
| 59 |
-
model: str # Заглушка — не используется
|
| 60 |
-
messages: List[Message]
|
| 61 |
-
temperature: Optional[float] = 0.7
|
| 62 |
-
top_p: Optional[float] = 0.95
|
| 63 |
-
max_tokens: Optional[int] = 512
|
| 64 |
|
| 65 |
-
|
| 66 |
-
@app.get("/", response_class=PlainTextResponse)
|
| 67 |
-
async def root():
|
| 68 |
-
return "Proxy free test"
|
| 69 |
-
|
| 70 |
-
# === Основной маршрут OpenAI-совместимый ===
|
| 71 |
-
@app.post("/v1/chat/completions")
|
| 72 |
-
async def chat_completion(request: Request):
|
| 73 |
-
headers = dict(request.headers)
|
| 74 |
-
body = await request.body()
|
| 75 |
-
|
| 76 |
-
logger.info("📥 Запрос получен")
|
| 77 |
-
logger.info(f"🔸 Заголовки: {headers}")
|
| 78 |
-
logger.info(f"🔸 Тело: {body.decode('utf-8')}")
|
| 79 |
-
|
| 80 |
-
try:
|
| 81 |
-
data = await request.json()
|
| 82 |
-
chat_request = ChatRequest(**data)
|
| 83 |
-
except Exception as e:
|
| 84 |
-
logger.error(f"❌ Ошибка разбора JSON: {e}")
|
| 85 |
-
return {"error": "Некорректный JSON"}
|
| 86 |
-
|
| 87 |
-
# Игнорируем model из запроса и используем глобальный AI_MODEL
|
| 88 |
-
user_msg = next((m.content for m in reversed(chat_request.messages) if m.role == "user"), None)
|
| 89 |
-
system_msg = next((m.content for m in chat_request.messages if m.role == "system"), "You are a helpful AI assistant.")
|
| 90 |
-
|
| 91 |
-
if not user_msg:
|
| 92 |
-
return {"error": "User message not found."}
|
| 93 |
-
|
| 94 |
-
assistant_reply = ask(user_msg, system_msg)
|
| 95 |
-
|
| 96 |
-
response = {
|
| 97 |
"id": f"chatcmpl-{uuid.uuid4().hex[:12]}",
|
| 98 |
"object": "chat.completion",
|
| 99 |
"created": int(time.time()),
|
|
@@ -103,7 +64,7 @@ async def chat_completion(request: Request):
|
|
| 103 |
"index": 0,
|
| 104 |
"message": {
|
| 105 |
"role": "assistant",
|
| 106 |
-
"content":
|
| 107 |
},
|
| 108 |
"finish_reason": "stop"
|
| 109 |
}
|
|
@@ -113,10 +74,8 @@ async def chat_completion(request: Request):
|
|
| 113 |
"completion_tokens": 0,
|
| 114 |
"total_tokens": 0
|
| 115 |
}
|
| 116 |
-
}
|
| 117 |
-
|
| 118 |
-
return response
|
| 119 |
|
| 120 |
# === Запуск сервера ===
|
| 121 |
if __name__ == "__main__":
|
| 122 |
-
|
|
|
|
| 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,
|
|
|
|
| 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()),
|
|
|
|
| 64 |
"index": 0,
|
| 65 |
"message": {
|
| 66 |
"role": "assistant",
|
| 67 |
+
"content": output
|
| 68 |
},
|
| 69 |
"finish_reason": "stop"
|
| 70 |
}
|
|
|
|
| 74 |
"completion_tokens": 0,
|
| 75 |
"total_tokens": 0
|
| 76 |
}
|
| 77 |
+
})
|
|
|
|
|
|
|
| 78 |
|
| 79 |
# === Запуск сервера ===
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|