Skip to content

added authentication for "/init" #162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file.

## [0.5.2 - 2023-11-xx]

### Fixed

- AppAPI: added authentication to `/init` endpoint. #162

## [0.5.1 - 2023-11-12]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion nc_py_api/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of nc_py_api."""

__version__ = "0.5.1"
__version__ = "0.5.2.dev0"
17 changes: 10 additions & 7 deletions nc_py_api/ex_app/integration_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def set_handlers(
fast_api_app: FastAPI,
enabled_handler: typing.Callable[[bool, NextcloudApp], str],
heartbeat_handler: typing.Optional[typing.Callable[[], str]] = None,
init_handler: typing.Optional[typing.Callable[[], None]] = None,
init_handler: typing.Optional[typing.Callable[[NextcloudApp], None]] = None,
models_to_fetch: typing.Optional[list[str]] = None,
models_download_params: typing.Optional[dict] = None,
):
Expand All @@ -75,15 +75,15 @@ def set_handlers(
:param models_download_params: Parameters to pass to ``snapshot_download`` function from **huggingface_hub**.
"""

def fetch_models_task(models: list[str]) -> None:
def fetch_models_task(nc: NextcloudApp, models: list[str]) -> None:
if models:
from huggingface_hub import snapshot_download # noqa isort:skip pylint: disable=C0415 disable=E0401
from tqdm import tqdm # noqa isort:skip pylint: disable=C0415 disable=E0401

class TqdmProgress(tqdm):
def display(self, msg=None, pos=None):
if init_handler is None:
NextcloudApp().set_init_status(min(int((self.n * 100 / self.total) / len(models)), 100))
nc.set_init_status(min(int((self.n * 100 / self.total) / len(models)), 100))
return super().display(msg, pos)

params = models_download_params if models_download_params else {}
Expand All @@ -94,9 +94,9 @@ def display(self, msg=None, pos=None):
for model in models:
snapshot_download(model, tqdm_class=TqdmProgress, **params) # noqa
if init_handler is None:
NextcloudApp().set_init_status(100)
nc.set_init_status(100)
else:
init_handler()
init_handler(nc)

@fast_api_app.put("/enabled")
def enabled_callback(
Expand All @@ -114,6 +114,9 @@ def heartbeat_callback():
return responses.JSONResponse(content={"status": return_status}, status_code=200)

@fast_api_app.post("/init")
def init_callback(background_tasks: BackgroundTasks):
background_tasks.add_task(fetch_models_task, models_to_fetch if models_to_fetch else [])
def init_callback(
background_tasks: BackgroundTasks,
nc: typing.Annotated[NextcloudApp, Depends(nc_app)],
):
background_tasks.add_task(fetch_models_task, nc, models_to_fetch if models_to_fetch else [])
return responses.JSONResponse(content={}, status_code=200)
4 changes: 2 additions & 2 deletions tests/_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def enabled_handler(enabled: bool, nc: NextcloudApp) -> str:
return ""


def init_handler():
NextcloudApp().set_init_status(100)
def init_handler(nc: NextcloudApp):
nc.set_init_status(100)


def heartbeat_callback():
Expand Down