11import os
2- import shutil
3- import tempfile
4- from pathlib import Path
52from typing import Any , Optional
63from threading import Lock
7- from fastapi import APIRouter , UploadFile , Depends , HTTPException
8- from fastapi .background import BackgroundTasks
94import llama_cpp
105
11- from llama_cpp .server .util import remove_file , models_root_dir
12- from llama_cpp .server .settings import Settings , SETTINGS
6+ from llama_cpp .server .util import models_root_dir
7+ from llama_cpp .server .settings import Settings , get_settings
138
149class MultiLlama :
1510 _model : Optional [llama_cpp .Llama ] = None
@@ -27,7 +22,7 @@ def __call__(self, model: str, **kwargs: Any) -> llama_cpp.Llama:
2722 model_path = self ._models [model ]
2823 except KeyError :
2924 # TODO server raises 500 ?
30- raise HTTPException (404 , f"Model file for { model } NOT found" )
25+ raise Exception (404 , f"Model file for { model } NOT found" )
3126
3227 if self ._model :
3328 if self ._model .model_path == model_path :
@@ -90,9 +85,9 @@ def __setitem__(self, model, path):
9085
9186LLAMA : Optional [MultiLlama ] = None
9287
93- def init_llama ( ):
88+ def _set_llama ( settings : Optional [ Settings ] = None ):
9489 global LLAMA
95- LLAMA = MultiLlama (SETTINGS )
90+ LLAMA = MultiLlama (settings or next ( get_settings ()) )
9691
9792llama_outer_lock = Lock ()
9893llama_inner_lock = Lock ()
@@ -107,7 +102,7 @@ def get_llama():
107102 llama_inner_lock .acquire ()
108103 try :
109104 if not LLAMA :
110- init_llama ()
105+ _set_llama ()
111106 llama_outer_lock .release ()
112107 release_outer_lock = False
113108 yield LLAMA
@@ -116,40 +111,3 @@ def get_llama():
116111 finally :
117112 if release_outer_lock :
118113 llama_outer_lock .release ()
119-
120-
121- router = APIRouter (
122- prefix = "/models" ,
123- tags = ["Model" ],
124- responses = {404 : {"description" : "Not found" }},
125- )
126-
127- @router .put ("/" )
128- async def api_update_model (
129- file : UploadFile ,
130- background_tasks : BackgroundTasks
131- # user: User = Depends(RBAC(settings.auth_role)),
132- ):
133- ext = "" .join (Path (file .filename ).suffixes ) if file .filename else ".gguf"
134- model_file = tempfile .NamedTemporaryFile (suffix = ext ).name
135- with open (model_file , "wb" ) as buffer :
136- shutil .copyfileobj (file .file , buffer )
137- background_tasks .add_task (remove_file , model_file )
138- models_dir = os .path .dirname (os .path .abspath (os .environ .get ('MODEL' , '/' )))
139- target_path = os .path .join (models_dir , file .filename )
140- shutil .copy (model_file , target_path )
141- LLAMA [file .filename ] = target_path
142- return {"model" : target_path }
143-
144- @router .delete ("/{model}" )
145- async def api_delete_model (
146- model : str ,
147- background_tasks : BackgroundTasks
148- # user: User = Depends(RBAC(settings.auth_role)),
149- ):
150- models_dir = models_root_dir ()
151- target_path = os .path .join (models_dir , LLAMA [model ])
152- if not os .path .exists (target_path ):
153- raise HTTPException (status_code = 404 , detail = f"Model File NOT Found for { model } " )
154- background_tasks .add_task (remove_file , target_path )
155- return 'success'
0 commit comments