Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeier committed Dec 11, 2024
1 parent a305adf commit 9dde033
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: ragna-deploy-dev
name: ragna-dev
channels:
- conda-forge
dependencies:
Expand Down
16 changes: 10 additions & 6 deletions ragna/deploy/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import TYPE_CHECKING, Annotated, Awaitable, Callable, Optional, Union, cast

import httpx
import panel as pn
import pydantic
from fastapi import Depends, FastAPI, Request, status
from fastapi.responses import HTMLResponse, RedirectResponse, Response
Expand Down Expand Up @@ -38,6 +39,11 @@ class Session(pydantic.BaseModel):


class SessionMiddleware(BaseHTTPMiddleware):
# panel uses cookies to transfer user information (see _cookie_dispatch() below) and
# signs them for security. However, since this happens after our authentication
# check, we can use an arbitrary, hardcoded value here.
_PANEL_COOKIE_SECRET = "ragna"

def __init__(
self, app: FastAPI, *, config: Config, engine: Engine, api: bool, ui: bool
) -> None:
Expand All @@ -48,6 +54,9 @@ def __init__(
self._ui = ui
self._sessions: KeyValueStore[Session] = config.key_value_store()

if ui:
pn.config.cookie_secret = self._PANEL_COOKIE_SECRET # type: ignore[misc]

_COOKIE_NAME = "ragna"

async def dispatch(self, request: Request, call_next: CallNext) -> Response:
Expand Down Expand Up @@ -97,11 +106,6 @@ async def _api_token_dispatch(
request.state.session = session
return await call_next(request)

# panel uses cookies to transfer user information (see _cookie_dispatch() below) and
# signs them for security. However, since this happens after our authentication
# check, we can use an arbitrary, hardcoded value here.
PANEL_COOKIE_SECRET = "ragna"

async def _cookie_dispatch(
self, request: Request, call_next: CallNext, *, cookie: str
) -> Response:
Expand All @@ -128,7 +132,7 @@ async def _cookie_dispatch(
(
f"{key}=".encode()
+ create_signed_value(
self.PANEL_COOKIE_SECRET, key, value, version=1
self._PANEL_COOKIE_SECRET, key, value, version=1
)
)
for key, value in extra_cookies.items()
Expand Down
3 changes: 1 addition & 2 deletions ragna/deploy/_templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@
background-color: white;
padding: 20px;
box-shadow: 20px;
background-color: red;
}

{{ __template_css__ }}
</style>
<body>
<div class="auth">
<div class="auth border rounded bg-light">
{% block content %}{% endblock %}
</div>
</body>
Expand Down
2 changes: 2 additions & 0 deletions ragna/deploy/_ui/left_sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class LeftSidebar(pn.viewable.Viewer):
def __init__(self, api_wrapper, **params):
super().__init__(**params)

self.api_wrapper = api_wrapper
self.on_click_chat = None
self.on_click_new_chat = None

Expand Down Expand Up @@ -104,6 +105,7 @@ def __panel__(self):
+ self.chat_buttons
+ [
pn.layout.VSpacer(),
pn.pane.HTML(f"user: {self.api_wrapper._user}"),
pn.pane.HTML(f"version: {ragna_version}"),
# self.footer()
]
Expand Down
2 changes: 1 addition & 1 deletion ragna/source_storages/_vector_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _chunk_pages(
):
tokens, page_numbers = zip(*window)
yield Chunk(
text=self._tokenizer.decode(tokens), # type: ignore[arg-type]
text=self._tokenizer.decode(tokens),
page_numbers=list(filter(lambda n: n is not None, page_numbers))
or None,
num_tokens=len(tokens),
Expand Down

0 comments on commit 9dde033

Please sign in to comment.