Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,58 +3,68 @@ from huggingface_hub import hf_hub_download, login
|
|
| 3 |
from llama_cpp import Llama
|
| 4 |
from fastapi import FastAPI
|
| 5 |
import uvicorn
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# ===
|
| 8 |
token = os.getenv("TOKEN_FACE_GRAINED")
|
| 9 |
if token:
|
| 10 |
login(token=token)
|
| 11 |
-
print("TOKEN_FACE_GRAINED подключен")
|
| 12 |
-
else:
|
| 13 |
-
print("Warning: TOKEN_FACE_GRAINED не найден! Убедитесь, что добавили его в Settings → Secrets")
|
| 14 |
|
| 15 |
-
# ===
|
| 16 |
print("Скачивание модели...")
|
| 17 |
model_path = hf_hub_download(
|
| 18 |
repo_id="Sanya2025/Proxima_B-instruct-Q5_0-GGUF",
|
| 19 |
filename="proxima_b-instruct-q5_0.gguf",
|
| 20 |
-
token=token,
|
| 21 |
-
local_dir="/tmp/model",
|
| 22 |
local_dir_use_symlinks=False
|
| 23 |
)
|
| 24 |
-
print(f"Модель
|
| 25 |
|
| 26 |
-
# === 3. Загрузка в llama.cpp ===
|
| 27 |
print("Инициализация llama.cpp...")
|
| 28 |
llm = Llama(
|
| 29 |
model_path=model_path,
|
| 30 |
-
n_ctx=4096,
|
| 31 |
-
n_threads=4,
|
| 32 |
-
verbose=
|
| 33 |
)
|
|
|
|
| 34 |
|
| 35 |
-
# ===
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
-
async def chat(request: dict):
|
| 40 |
-
messages = request.get("messages", [])
|
| 41 |
-
response = llm.create_chat_completion(
|
| 42 |
-
messages=messages,
|
| 43 |
-
temperature=request.get("temperature", 0.7),
|
| 44 |
-
max_tokens=request.get("max_tokens", 512)
|
| 45 |
-
)
|
| 46 |
-
return response
|
| 47 |
|
| 48 |
@app.get("/")
|
| 49 |
-
|
| 50 |
return {"status": "ok", "model": "proxima-b-instruct-q5_0"}
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# === 5. ЗАПУСК СЕРВЕРА ===
|
| 57 |
-
# uvicorn.run — это БЛОКИРУЮЩИЙ вызов, он держит контейнер открытым
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
print("Запуск uvicorn на 0.0.0.0:7860...")
|
| 60 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 3 |
from llama_cpp import Llama
|
| 4 |
from fastapi import FastAPI
|
| 5 |
import uvicorn
|
| 6 |
+
import time
|
| 7 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
|
| 9 |
+
# === Авторизация ===
|
| 10 |
token = os.getenv("TOKEN_FACE_GRAINED")
|
| 11 |
if token:
|
| 12 |
login(token=token)
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# === Загрузка модели ===
|
| 15 |
print("Скачивание модели...")
|
| 16 |
model_path = hf_hub_download(
|
| 17 |
repo_id="Sanya2025/Proxima_B-instruct-Q5_0-GGUF",
|
| 18 |
filename="proxima_b-instruct-q5_0.gguf",
|
| 19 |
+
token=token,
|
| 20 |
+
local_dir="/tmp/model",
|
| 21 |
local_dir_use_symlinks=False
|
| 22 |
)
|
| 23 |
+
print(f"Модель: {model_path}")
|
| 24 |
|
|
|
|
| 25 |
print("Инициализация llama.cpp...")
|
| 26 |
llm = Llama(
|
| 27 |
model_path=model_path,
|
| 28 |
+
n_ctx=4096,
|
| 29 |
+
n_threads=4,
|
| 30 |
+
verbose=False
|
| 31 |
)
|
| 32 |
+
print("Модель готова!")
|
| 33 |
|
| 34 |
+
# === ThreadPool для неблокирующей генерации ===
|
| 35 |
+
executor = ThreadPoolExecutor(max_workers=1)
|
| 36 |
|
| 37 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
@app.get("/")
|
| 40 |
+
def health():
|
| 41 |
return {"status": "ok", "model": "proxima-b-instruct-q5_0"}
|
| 42 |
|
| 43 |
+
# !!! СИНХРОННЫЙ endpoint (не async) !!!
|
| 44 |
+
@app.post("/v1/chat/completions")
|
| 45 |
+
def chat(request: dict):
|
| 46 |
+
print(f"[{time.strftime('%H:%M:%S')}] Запрос: {str(request)[:200]}")
|
| 47 |
+
|
| 48 |
+
messages = request.get("messages", [])
|
| 49 |
+
max_tokens = request.get("max_tokens", 128)
|
| 50 |
+
|
| 51 |
+
print(f"Генерация начата (max_tokens={max_tokens})...")
|
| 52 |
+
start = time.time()
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
response = llm.create_chat_completion(
|
| 56 |
+
messages=messages,
|
| 57 |
+
max_tokens=max_tokens,
|
| 58 |
+
temperature=request.get("temperature", 0.7),
|
| 59 |
+
stop=request.get("stop", []),
|
| 60 |
+
stream=False
|
| 61 |
+
)
|
| 62 |
+
print(f"Генерация завершена за {time.time()-start:.1f} сек")
|
| 63 |
+
return response
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
print(f"ОШИБКА генерации: {e}")
|
| 67 |
+
return {"error": str(e)}
|
| 68 |
|
|
|
|
|
|
|
| 69 |
if __name__ == "__main__":
|
|
|
|
| 70 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|