Spaces:
Sleeping
Sleeping
Commit ·
111fd8c
1
Parent(s): 3208e8a
Debug
Browse files- Dockerfile +14 -3
- app/main.py +26 -11
Dockerfile
CHANGED
|
@@ -1,10 +1,21 @@
|
|
| 1 |
FROM python:3.11-slim
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
WORKDIR /app
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
|
|
|
| 10 |
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 1 |
FROM python:3.11-slim
|
| 2 |
|
| 3 |
+
# Отключаем буферизацию Python вывода
|
| 4 |
+
ENV PYTHONUNBUFFERED=1
|
| 5 |
+
|
| 6 |
+
# Создаем пользователя с UID 1000
|
| 7 |
+
RUN useradd -m -u 1000 user
|
| 8 |
+
USER user
|
| 9 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 10 |
+
|
| 11 |
WORKDIR /app
|
| 12 |
|
| 13 |
+
# Копируем и устанавливаем зависимости
|
| 14 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 15 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 16 |
|
| 17 |
+
# Копируем остальные файлы
|
| 18 |
+
COPY --chown=user . /app
|
| 19 |
|
| 20 |
+
# Запускаем приложение
|
| 21 |
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/main.py
CHANGED
|
@@ -1,27 +1,31 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
from typing import List, Optional, Dict, Any
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from .auth import verify_api_key
|
| 7 |
from .factory import ProviderFactory
|
| 8 |
from .models import ChatRequest, ChatResponse
|
| 9 |
|
| 10 |
app = FastAPI(title="LLM API Proxy", version="1.0.0")
|
|
|
|
| 11 |
|
| 12 |
@app.get("/")
|
| 13 |
async def root():
|
|
|
|
| 14 |
return {"message": "LLM API Proxy is running", "version": "1.0.1"}
|
| 15 |
|
| 16 |
@app.get("/v1/models")
|
| 17 |
async def list_models(api_key: str = Depends(verify_api_key)):
|
| 18 |
"""Возвращает список доступных моделей/провайдеров."""
|
| 19 |
-
|
| 20 |
return {
|
| 21 |
"models": [
|
| 22 |
{"id": "zhipu-flash", "name": "GLM-4.7-Flash", "provider": "zhipu", "type": "free"},
|
| 23 |
-
# В будущем:
|
| 24 |
-
# {"id": "openai-gpt3", "name": "GPT-3.5 Turbo", "provider": "openai", "type": "paid"},
|
| 25 |
]
|
| 26 |
}
|
| 27 |
|
|
@@ -31,30 +35,41 @@ async def chat_completion(
|
|
| 31 |
api_key: str = Depends(verify_api_key)
|
| 32 |
):
|
| 33 |
"""Основной эндпоинт для генерации текста."""
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
# 1. Получаем провайдера по имени модели из запроса
|
|
|
|
| 36 |
provider = ProviderFactory.get_provider(request.model)
|
|
|
|
| 37 |
|
| 38 |
# 2. Генерируем ответ
|
|
|
|
| 39 |
result = await provider.generate(
|
| 40 |
messages=[m.dict() for m in request.messages],
|
| 41 |
max_tokens=request.max_tokens,
|
| 42 |
temperature=request.temperature,
|
| 43 |
-
model=request.model
|
| 44 |
)
|
|
|
|
| 45 |
|
| 46 |
# 3. Возвращаем в стандартном формате
|
| 47 |
-
|
| 48 |
id=f"chat-{hash(str(request.messages))}",
|
| 49 |
choices=[{"message": {"content": result["content"]}}],
|
| 50 |
usage={"total_tokens": result.get("total_tokens", 0)},
|
| 51 |
model=request.model
|
| 52 |
)
|
|
|
|
|
|
|
|
|
|
| 53 |
except ValueError as e:
|
| 54 |
-
|
| 55 |
raise HTTPException(status_code=400, detail=str(e))
|
| 56 |
except Exception as e:
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
raise HTTPException(status_code=502, detail=f"Provider error: {str(e)}")
|
| 59 |
-
|
| 60 |
-
print(
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
+
from fastapi import FastAPI, Depends, HTTPException
|
| 4 |
from typing import List, Optional, Dict, Any
|
| 5 |
|
| 6 |
+
# Отключаем буферизацию вывода сразу при старте
|
| 7 |
+
sys.stdout.reconfigure(line_buffering=True)
|
| 8 |
+
print("🚀 Starting application initialization...", flush=True)
|
| 9 |
+
|
| 10 |
from .auth import verify_api_key
|
| 11 |
from .factory import ProviderFactory
|
| 12 |
from .models import ChatRequest, ChatResponse
|
| 13 |
|
| 14 |
app = FastAPI(title="LLM API Proxy", version="1.0.0")
|
| 15 |
+
print("✅ FastAPI app created", flush=True)
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
async def root():
|
| 19 |
+
print("🌐 Root endpoint accessed", flush=True)
|
| 20 |
return {"message": "LLM API Proxy is running", "version": "1.0.1"}
|
| 21 |
|
| 22 |
@app.get("/v1/models")
|
| 23 |
async def list_models(api_key: str = Depends(verify_api_key)):
|
| 24 |
"""Возвращает список доступных моделей/провайдеров."""
|
| 25 |
+
print("📋 Models endpoint accessed", flush=True)
|
| 26 |
return {
|
| 27 |
"models": [
|
| 28 |
{"id": "zhipu-flash", "name": "GLM-4.7-Flash", "provider": "zhipu", "type": "free"},
|
|
|
|
|
|
|
| 29 |
]
|
| 30 |
}
|
| 31 |
|
|
|
|
| 35 |
api_key: str = Depends(verify_api_key)
|
| 36 |
):
|
| 37 |
"""Основной эндпоинт для генерации текста."""
|
| 38 |
+
print(f"💬 Chat completion requested with model: {request.model}", flush=True)
|
| 39 |
+
|
| 40 |
try:
|
| 41 |
# 1. Получаем провайдера по имени модели из запроса
|
| 42 |
+
print(f"🔍 Getting provider for model: {request.model}", flush=True)
|
| 43 |
provider = ProviderFactory.get_provider(request.model)
|
| 44 |
+
print(f"✅ Provider obtained: {type(provider).__name__}", flush=True)
|
| 45 |
|
| 46 |
# 2. Генерируем ответ
|
| 47 |
+
print("🔄 Calling provider.generate()...", flush=True)
|
| 48 |
result = await provider.generate(
|
| 49 |
messages=[m.dict() for m in request.messages],
|
| 50 |
max_tokens=request.max_tokens,
|
| 51 |
temperature=request.temperature,
|
| 52 |
+
model=request.model
|
| 53 |
)
|
| 54 |
+
print(f"✅ Generation complete, tokens: {result.get('total_tokens', 0)}", flush=True)
|
| 55 |
|
| 56 |
# 3. Возвращаем в стандартном формате
|
| 57 |
+
response = ChatResponse(
|
| 58 |
id=f"chat-{hash(str(request.messages))}",
|
| 59 |
choices=[{"message": {"content": result["content"]}}],
|
| 60 |
usage={"total_tokens": result.get("total_tokens", 0)},
|
| 61 |
model=request.model
|
| 62 |
)
|
| 63 |
+
print("✅ Response prepared, sending...", flush=True)
|
| 64 |
+
return response
|
| 65 |
+
|
| 66 |
except ValueError as e:
|
| 67 |
+
print(f"❌ ValueError: {e}", flush=True)
|
| 68 |
raise HTTPException(status_code=400, detail=str(e))
|
| 69 |
except Exception as e:
|
| 70 |
+
print(f"❌ Exception: {type(e).__name__}: {e}", flush=True)
|
| 71 |
+
import traceback
|
| 72 |
+
traceback.print_exc()
|
| 73 |
raise HTTPException(status_code=502, detail=f"Provider error: {str(e)}")
|
| 74 |
+
|
| 75 |
+
print("🎉 App started successfully!", flush=True)
|