Skip to content

Commit 45bfa07

Browse files
committed
cleanup
1 parent 5ab0010 commit 45bfa07

File tree

2 files changed

+8
-53
lines changed

2 files changed

+8
-53
lines changed

llama_cpp/server/app.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import numpy.typing as npt
2727

2828
from llama_cpp.server.model import get_llama, llama_outer_lock, MultiLlama as Llama
29-
from llama_cpp.server.model import router as models_router
30-
#from llama_cpp.server.model import MultiLlama as Llama
31-
from llama_cpp.server.settings import Settings, SETTINGS, set_settings, get_settings
29+
from llama_cpp.server.settings import Settings, set_settings, get_settings
3230

3331
class ErrorResponse(TypedDict):
3432
"""OpenAI style error response"""
@@ -233,7 +231,6 @@ def create_app(settings: Optional[Settings] = None):
233231
allow_headers=["*"],
234232
)
235233
app.include_router(router)
236-
app.include_router(models_router)
237234

238235
set_settings(settings)
239236
return app
@@ -249,7 +246,7 @@ async def get_event_publisher(
249246
await inner_send_chan.send(dict(data=json.dumps(chunk)))
250247
if await request.is_disconnected():
251248
raise anyio.get_cancelled_exc_class()()
252-
if SETTINGS.interrupt_requests and llama_outer_lock.locked():
249+
if next(get_settings()).interrupt_requests and llama_outer_lock.locked():
253250
await inner_send_chan.send(dict(data="[DONE]"))
254251
raise anyio.get_cancelled_exc_class()()
255252
await inner_send_chan.send(dict(data="[DONE]"))

llama_cpp/server/model.py

Lines changed: 6 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import os
2-
import shutil
3-
import tempfile
4-
from pathlib import Path
52
from typing import Any, Optional
63
from threading import Lock
7-
from fastapi import APIRouter, UploadFile, Depends, HTTPException
8-
from fastapi.background import BackgroundTasks
94
import 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

149
class 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

9186
LLAMA: 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

9792
llama_outer_lock = Lock()
9893
llama_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

Comments
 (0)