dbx-model-catalog-server 0.1.8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from .app import app
2
+
3
+ __all__ = ["app"]
@@ -0,0 +1,142 @@
1
+ import os
2
+ from collections.abc import AsyncIterator
3
+ from pathlib import Path
4
+ from typing import Literal
5
+
6
+ from chanx.core.decorators import channel, ws_handler
7
+ from chanx.fast_channels import asyncapi_docs, asyncapi_spec_json
8
+ from chanx.fast_channels.type_defs import AsyncAPIConfig
9
+ from chanx.messages.base import BaseMessage
10
+ from dbx_model_catalog import (
11
+ CatalogFilters,
12
+ CatalogModelRecord,
13
+ CatalogSearchIndexPayload,
14
+ ModelCatalog,
15
+ ModelCatalogClient,
16
+ )
17
+ from fastapi import FastAPI, Request
18
+ from fastapi.responses import HTMLResponse, JSONResponse
19
+ from fastapi.staticfiles import StaticFiles
20
+ from pynect import AsyncJsonWebsocketConsumer
21
+
22
+
23
+ class GetCatalogRequest(BaseMessage):
24
+ action: Literal["get_catalog"] = "get_catalog"
25
+ payload: CatalogFilters
26
+
27
+
28
+ class GetCatalogResponse(BaseMessage):
29
+ action: Literal["catalog"] = "catalog"
30
+ payload: ModelCatalog
31
+
32
+
33
+ class GetSearchIndexRequest(BaseMessage):
34
+ action: Literal["get_search_index"] = "get_search_index"
35
+ payload: CatalogFilters
36
+
37
+
38
+ class GetSearchIndexResponse(BaseMessage):
39
+ action: Literal["search_index"] = "search_index"
40
+ payload: CatalogSearchIndexPayload
41
+
42
+
43
+ class ListModelsRequest(BaseMessage):
44
+ action: Literal["list_models"] = "list_models"
45
+ payload: CatalogFilters
46
+
47
+
48
+ class ListModelsResponse(BaseMessage):
49
+ action: Literal["models"] = "models"
50
+ payload: list[CatalogModelRecord]
51
+
52
+
53
+ class StreamModelsRequest(BaseMessage):
54
+ action: Literal["stream_models"] = "stream_models"
55
+ payload: CatalogFilters
56
+
57
+
58
+ class StreamModelsResponse(BaseMessage):
59
+ action: Literal["model"] = "model"
60
+ payload: CatalogModelRecord
61
+
62
+
63
+ def _catalog_path() -> Path | None:
64
+ configured = os.environ.get("DBX_MODEL_CATALOG_PATH")
65
+ return Path(configured).expanduser().resolve() if configured else None
66
+
67
+
68
+ def _catalog_service() -> ModelCatalogClient:
69
+ path = _catalog_path()
70
+ return (
71
+ ModelCatalogClient.from_json(path.read_bytes())
72
+ if path is not None
73
+ else ModelCatalogClient.from_published()
74
+ )
75
+
76
+
77
+ catalog = _catalog_service()
78
+
79
+
80
+ @channel(name="models", description="Read and filter the model catalog")
81
+ class ModelsConsumer(AsyncJsonWebsocketConsumer):
82
+ @ws_handler
83
+ async def get_catalog(self, _message: GetCatalogRequest) -> GetCatalogResponse:
84
+ return GetCatalogResponse(payload=catalog.catalog)
85
+
86
+ @ws_handler
87
+ async def get_search_index(
88
+ self,
89
+ _message: GetSearchIndexRequest,
90
+ ) -> GetSearchIndexResponse:
91
+ return GetSearchIndexResponse(payload=catalog.search_index)
92
+
93
+ @ws_handler
94
+ async def list_models(self, message: ListModelsRequest) -> ListModelsResponse:
95
+ return ListModelsResponse(payload=catalog.filter(message.payload))
96
+
97
+ @ws_handler
98
+ async def stream_models(
99
+ self,
100
+ message: StreamModelsRequest,
101
+ ) -> AsyncIterator[StreamModelsResponse]:
102
+ for model in catalog.filter(message.payload):
103
+ yield StreamModelsResponse(payload=model)
104
+
105
+
106
+ app = FastAPI(title="dbx-model-catalog", version="1.0.0")
107
+ app.router.add_websocket_route("/ws/models", ModelsConsumer.as_asgi())
108
+
109
+ asyncapi_config = AsyncAPIConfig(
110
+ description="Python-owned dbx-model-catalog API",
111
+ version="1.0.0",
112
+ )
113
+
114
+
115
+ @app.get("/health")
116
+ async def health() -> dict[str, str]:
117
+ return {"status": "ok"}
118
+
119
+
120
+ @app.get("/asyncapi")
121
+ async def asyncapi_documentation(request: Request) -> HTMLResponse:
122
+ return await asyncapi_docs(request=request, app=app, config=asyncapi_config)
123
+
124
+
125
+ @app.get("/asyncapi.json")
126
+ async def asyncapi_json(request: Request) -> JSONResponse:
127
+ return await asyncapi_spec_json(
128
+ request=request,
129
+ app=app,
130
+ config=asyncapi_config,
131
+ )
132
+
133
+
134
+ web_directory = (
135
+ Path(__file__).resolve().parents[5]
136
+ / "packages"
137
+ / "js"
138
+ / "web"
139
+ / "dist"
140
+ )
141
+ if (web_directory / "index.html").is_file():
142
+ app.mount("/", StaticFiles(directory=web_directory, html=True), name="web")
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.5
2
+ Name: dbx-model-catalog-server
3
+ Version: 0.1.8
4
+ Summary: Chanx server for dbx-model-catalog
5
+ Requires-Python: >=3.12
6
+ Requires-Dist: chanx[fast-channels]<3,>=2.13
7
+ Requires-Dist: dbx-model-catalog
8
+ Requires-Dist: fastapi<1,>=0.120
9
+ Requires-Dist: pynect<2,>=1.0.0
10
+ Requires-Dist: uvicorn[standard]<1,>=0.40
@@ -0,0 +1,5 @@
1
+ dbx_model_catalog_server/__init__.py,sha256=jArxVms_ZS6eug_h3jSt4pbNpU0XFAAkZ_qbrA9Xrk8,40
2
+ dbx_model_catalog_server/app.py,sha256=zGGJTo8jTp3WiEH-7z4um92bAbyo8q_pOufx2DNBqrU,3932
3
+ dbx_model_catalog_server-0.1.8.dist-info/METADATA,sha256=2kQhbo0QmPSfMBdU-dG2KCXIhP_Jlc_UUjinK8D36nI,321
4
+ dbx_model_catalog_server-0.1.8.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
5
+ dbx_model_catalog_server-0.1.8.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any