Sanya2025 commited on
Commit
b8a4d48
·
verified ·
1 Parent(s): ae57780

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -10
app.py CHANGED
@@ -1,29 +1,60 @@
 
 
1
  from llama_cpp import Llama
2
  from fastapi import FastAPI
3
- from huggingface_hub import hf_hub_download
4
- import os
5
 
6
- app = FastAPI()
 
 
 
 
 
 
7
 
8
- # ✅ Исправленное имя файла — точно как в репозитории
 
9
  model_path = hf_hub_download(
10
  repo_id="Sanya2025/Proxima_B-instruct-Q5_0-GGUF",
11
- filename="proxima_b-instruct-q5_0.gguf" # ← нижний регистр, подчёркивания!
 
 
 
12
  )
 
13
 
 
 
14
  llm = Llama(
15
  model_path=model_path,
16
- n_ctx=4096,
17
- n_threads=4,
18
- verbose=False
19
  )
20
 
 
 
 
21
  @app.post("/v1/chat/completions")
22
  async def chat(request: dict):
23
  messages = request.get("messages", [])
24
- response = llm.create_chat_completion(messages=messages)
 
 
 
 
25
  return response
26
 
27
  @app.get("/")
28
  async def health():
29
- return {"status": "ok", "model": "proxima-b-instruct"}
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import hf_hub_download, login
3
  from llama_cpp import Llama
4
  from fastapi import FastAPI
5
+ import uvicorn
 
6
 
7
+ # === 1. Авторизация в HF Hub ===
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
+ # === 2. Скачивание модели ===
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, # явно передаём токен для gated репозитория
21
+ local_dir="/tmp/model", # чтобы не качать повторно при рестарте
22
+ local_dir_use_symlinks=False
23
  )
24
+ print(f"Модель загружена: {model_path}")
25
 
26
+ # === 3. Загрузка в llama.cpp ===
27
+ print("Инициализация llama.cpp...")
28
  llm = Llama(
29
  model_path=model_path,
30
+ n_ctx=4096, # можно увеличить до 8192 если хватит RAM
31
+ n_threads=4, # для CPU Space
32
+ verbose=True
33
  )
34
 
35
+ # === 4. FastAPI приложение ===
36
+ app = FastAPI()
37
+
38
  @app.post("/v1/chat/completions")
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
  async def health():
50
+ return {"status": "ok", "model": "proxima-b-instruct-q5_0"}
51
+
52
+ @app.get("/health")
53
+ async def health_check():
54
+ return {"status": "alive"}
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)