Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from huggingface_hub import hf_hub_download
|
|
| 6 |
import gc
|
| 7 |
import os
|
| 8 |
|
| 9 |
-
app = FastAPI(title="
|
| 10 |
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
|
@@ -17,12 +17,13 @@ app.add_middleware(
|
|
| 17 |
)
|
| 18 |
|
| 19 |
REPO_ID = "Rid3/xtime-v1beta-gguf-storage"
|
|
|
|
| 20 |
current_llm = None
|
| 21 |
current_model_name = ""
|
| 22 |
|
| 23 |
MODELS = {
|
| 24 |
"medium": "xtime-v1beta-n-m_1p.gguf",
|
| 25 |
-
"large": "xtime-v1beta-q4_K_M.gguf",
|
| 26 |
"small": "xtime-v1beta-xp-r_2.gguf"
|
| 27 |
}
|
| 28 |
|
|
@@ -31,60 +32,35 @@ def load_model(model_key: str):
|
|
| 31 |
filename = MODELS.get(model_key)
|
| 32 |
if not filename:
|
| 33 |
raise HTTPException(status_code=404, detail="Model not found")
|
| 34 |
-
|
| 35 |
-
if current_model_name == model_key:
|
| 36 |
return
|
| 37 |
|
| 38 |
-
print(f"--- Loading
|
| 39 |
-
|
| 40 |
if current_llm is not None:
|
| 41 |
del current_llm
|
| 42 |
gc.collect()
|
| 43 |
|
| 44 |
try:
|
| 45 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=filename)
|
| 46 |
-
|
| 47 |
-
# Для mllama важно использовать свежий движок
|
| 48 |
current_llm = Llama(
|
| 49 |
model_path=model_path,
|
| 50 |
-
n_ctx=
|
| 51 |
-
n_threads=4,
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
)
|
| 55 |
current_model_name = model_key
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
-
print(f"Error loading model: {e}")
|
| 58 |
raise HTTPException(status_code=500, detail=str(e))
|
| 59 |
|
| 60 |
@app.on_event("startup")
|
| 61 |
async def startup_event():
|
| 62 |
-
#
|
| 63 |
-
# Но с новым Dockerfile 'large' должен завестись
|
| 64 |
-
load_model("large")
|
| 65 |
-
|
| 66 |
-
class ChatRequest(BaseModel):
|
| 67 |
-
prompt: str
|
| 68 |
-
model_type: str = "large"
|
| 69 |
-
|
| 70 |
-
@app.post("/chat")
|
| 71 |
-
async def chat(request: ChatRequest):
|
| 72 |
-
if request.model_type != current_model_name:
|
| 73 |
-
load_model(request.model_type)
|
| 74 |
-
|
| 75 |
-
try:
|
| 76 |
-
# Используем метод create_chat_completion для лучшей совместимости с Llama 3
|
| 77 |
-
output = current_llm.create_chat_completion(
|
| 78 |
-
messages=[
|
| 79 |
-
{"role": "system", "content": "You are a helpful assistant."},
|
| 80 |
-
{"role": "user", "content": request.prompt}
|
| 81 |
-
],
|
| 82 |
-
max_tokens=512
|
| 83 |
-
)
|
| 84 |
-
return {"response": output["choices"][0]["message"]["content"].strip()}
|
| 85 |
-
except Exception as e:
|
| 86 |
-
return {"error": str(e)}
|
| 87 |
|
| 88 |
-
|
| 89 |
-
async def health():
|
| 90 |
-
return {"status": "online", "model": current_model_name}
|
|
|
|
| 6 |
import gc
|
| 7 |
import os
|
| 8 |
|
| 9 |
+
app = FastAPI(title="Xtime AI API")
|
| 10 |
|
| 11 |
app.add_middleware(
|
| 12 |
CORSMiddleware,
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
REPO_ID = "Rid3/xtime-v1beta-gguf-storage"
|
| 20 |
+
|
| 21 |
current_llm = None
|
| 22 |
current_model_name = ""
|
| 23 |
|
| 24 |
MODELS = {
|
| 25 |
"medium": "xtime-v1beta-n-m_1p.gguf",
|
| 26 |
+
"large": "xtime-v1beta-q4_K_M.gguf",
|
| 27 |
"small": "xtime-v1beta-xp-r_2.gguf"
|
| 28 |
}
|
| 29 |
|
|
|
|
| 32 |
filename = MODELS.get(model_key)
|
| 33 |
if not filename:
|
| 34 |
raise HTTPException(status_code=404, detail="Model not found")
|
| 35 |
+
|
| 36 |
+
if current_model_name == model_key and current_llm is not None:
|
| 37 |
return
|
| 38 |
|
| 39 |
+
print(f"--- Loading model: {filename} ({model_key}) ---")
|
| 40 |
+
|
| 41 |
if current_llm is not None:
|
| 42 |
del current_llm
|
| 43 |
gc.collect()
|
| 44 |
|
| 45 |
try:
|
| 46 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=filename)
|
| 47 |
+
|
|
|
|
| 48 |
current_llm = Llama(
|
| 49 |
model_path=model_path,
|
| 50 |
+
n_ctx=4096, # увеличил, если позволяет память
|
| 51 |
+
n_threads=os.cpu_count() or 4,
|
| 52 |
+
n_gpu_layers=0, # явно CPU
|
| 53 |
+
verbose=False,
|
| 54 |
+
chat_format="llama-3"
|
| 55 |
)
|
| 56 |
current_model_name = model_key
|
| 57 |
+
print(f"✅ Model {model_key} loaded successfully")
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"❌ Error loading model: {e}")
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
|
| 62 |
@app.on_event("startup")
|
| 63 |
async def startup_event():
|
| 64 |
+
load_model("large") # или "medium", если large слишком тяжёлый
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# ... остальной код без изменений ...
|
|
|
|
|
|