File size: 10,771 Bytes
e4f9cbe
 
 
 
 
 
 
 
ce592f8
e4f9cbe
 
8c1a93e
e4f9cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55dc3dd
e4f9cbe
 
55dc3dd
e4f9cbe
 
 
 
 
 
 
55dc3dd
e4f9cbe
54369d2
e4f9cbe
 
 
 
 
 
 
 
 
 
54369d2
e4f9cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce592f8
 
 
e4f9cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
ce592f8
 
e4f9cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce592f8
 
 
e4f9cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55dc3dd
 
e4f9cbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c1a93e
 
 
 
 
 
 
 
 
 
 
 
ce592f8
 
 
8c1a93e
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Router for the dataset database."""
from typing import Optional, Sequence, Union, cast
from urllib.parse import unquote

from fastapi import APIRouter, Response
from fastapi.responses import ORJSONResponse
from pydantic import BaseModel, validator

from .config import CONFIG, data_path
from .data.dataset import BinaryOp
from .data.dataset import Column as DBColumn
from .data.dataset import DatasetManifest, DatasetSettings, FeatureListValue, FeatureValue
from .data.dataset import Filter as PyFilter
from .data.dataset import (
  GroupsSortBy,
  ListOp,
  Search,
  SelectGroupsResult,
  SelectRowsSchemaResult,
  SortOrder,
  StatsResult,
  UnaryOp,
)
from .data.dataset_duckdb import DatasetDuckDB
from .db_manager import get_dataset, remove_dataset_from_cache, set_default_dataset_cls
from .router_utils import RouteErrorHandler
from .schema import Bin, Path, normalize_path
from .signals.concept_labels import ConceptLabelsSignal
from .signals.concept_scorer import ConceptScoreSignal
from .signals.default_signals import register_default_signals
from .signals.semantic_similarity import SemanticSimilaritySignal
from .signals.signal import (
  Signal,
  TextEmbeddingModelSignal,
  TextEmbeddingSignal,
  TextSignal,
  resolve_signal,
)
from .signals.substring_search import SubstringSignal
from .tasks import TaskId, task_manager
from .utils import DatasetInfo, list_datasets

router = APIRouter(route_class=RouteErrorHandler)

register_default_signals()
set_default_dataset_cls(DatasetDuckDB)


@router.get('/', response_model_exclude_none=True)
def get_datasets() -> list[DatasetInfo]:
  """List the datasets."""
  return list_datasets(data_path())


class WebManifest(BaseModel):
  """Information about a dataset."""
  dataset_manifest: DatasetManifest


@router.get('/{namespace}/{dataset_name}')
def get_manifest(namespace: str, dataset_name: str) -> WebManifest:
  """Get the web manifest for the dataset."""
  dataset = get_dataset(namespace, dataset_name)
  res = WebManifest(dataset_manifest=dataset.manifest())
  # Avoids the error that Signal abstract class is not serializable.
  return cast(WebManifest, ORJSONResponse(res.dict(exclude_none=True)))


class ComputeSignalOptions(BaseModel):
  """The request for the compute signal endpoint."""
  signal: Signal

  # The leaf path to compute the signal on.
  leaf_path: Path

  @validator('signal', pre=True)
  def parse_signal(cls, signal: dict) -> Signal:
    """Parse a signal to its specific subclass instance."""
    return resolve_signal(signal)


@router.delete('/{namespace}/{dataset_name}')
def delete_dataset(namespace: str, dataset_name: str) -> None:
  """Delete the dataset."""
  if CONFIG.get('LILAC_READONLY', False):
    raise ValueError('Server is in readonly mode. This disables dataset.delete_dataset.')

  dataset = get_dataset(namespace, dataset_name)
  dataset.delete()
  remove_dataset_from_cache(namespace, dataset_name)


class ComputeSignalResponse(BaseModel):
  """Response of the compute signal column endpoint."""
  task_id: TaskId


@router.post('/{namespace}/{dataset_name}/compute_signal')
def compute_signal(namespace: str, dataset_name: str,
                   options: ComputeSignalOptions) -> ComputeSignalResponse:
  """Compute a signal for a dataset."""
  if CONFIG.get('LILAC_READONLY', False):
    raise ValueError('Server is in readonly mode. This disables dataset.compute_signal.')

  def _task_compute_signal(namespace: str, dataset_name: str, options_dict: dict,
                           task_id: TaskId) -> None:
    # NOTE: We manually call .dict() to avoid the dask serializer, which doesn't call the underlying
    # pydantic serializer.
    options = ComputeSignalOptions(**options_dict)
    dataset = get_dataset(namespace, dataset_name)
    dataset.compute_signal(options.signal, options.leaf_path, task_step_id=(task_id, 0))

  path_str = '.'.join(map(str, options.leaf_path))
  task_id = task_manager().task_id(
    name=f'[{namespace}/{dataset_name}] Compute signal "{options.signal.name}" on "{path_str}"',
    description=f'Config: {options.signal}')
  task_manager().execute(task_id, _task_compute_signal, namespace, dataset_name, options.dict(),
                         task_id)

  return ComputeSignalResponse(task_id=task_id)


class DeleteSignalOptions(BaseModel):
  """The request for the delete signal endpoint."""
  # The signal path holding the data from the signal.
  signal_path: Path


class DeleteSignalResponse(BaseModel):
  """Response of the compute signal column endpoint."""
  completed: bool


@router.delete('/{namespace}/{dataset_name}/delete_signal')
def delete_signal(namespace: str, dataset_name: str,
                  options: DeleteSignalOptions) -> DeleteSignalResponse:
  """Delete a signal from a dataset."""
  if CONFIG.get('LILAC_READONLY', False):
    raise ValueError('Server is in readonly mode. This disables dataset.delete_signal.')

  dataset = get_dataset(namespace, dataset_name)
  dataset.delete_signal(options.signal_path)
  return DeleteSignalResponse(completed=True)


class GetStatsOptions(BaseModel):
  """The request for the get stats endpoint."""
  leaf_path: Path


@router.post('/{namespace}/{dataset_name}/stats')
def get_stats(namespace: str, dataset_name: str, options: GetStatsOptions) -> StatsResult:
  """Get the stats for the dataset."""
  dataset = get_dataset(namespace, dataset_name)
  return dataset.stats(options.leaf_path)


class BinaryFilter(BaseModel):
  """A filter on a column."""
  path: Path
  op: BinaryOp
  value: FeatureValue


class UnaryFilter(BaseModel):
  """A filter on a column."""
  path: Path
  op: UnaryOp
  value: None = None


class ListFilter(BaseModel):
  """A filter on a column."""
  path: Path
  op: ListOp
  value: FeatureListValue


Filter = Union[BinaryFilter, UnaryFilter, ListFilter]

AllSignalTypes = Union[ConceptScoreSignal, ConceptLabelsSignal, SubstringSignal,
                       SemanticSimilaritySignal, TextEmbeddingModelSignal, TextEmbeddingSignal,
                       TextSignal, Signal]


# We override the `Column` class so we can add explicitly all signal types for better OpenAPI spec.
class Column(DBColumn):
  """A column in the dataset."""
  signal_udf: Optional[AllSignalTypes] = None


class SelectRowsOptions(BaseModel):
  """The request for the select rows endpoint."""
  columns: Optional[Sequence[Union[Path, Column]]]
  searches: Optional[Sequence[Search]]
  filters: Optional[Sequence[Filter]]
  sort_by: Optional[Sequence[Path]]
  sort_order: Optional[SortOrder] = SortOrder.DESC
  limit: Optional[int]
  offset: Optional[int]
  combine_columns: Optional[bool]


class SelectRowsSchemaOptions(BaseModel):
  """The request for the select rows schema endpoint."""
  columns: Optional[Sequence[Union[Path, Column]]]
  searches: Optional[Sequence[Search]]
  sort_by: Optional[Sequence[Path]]
  sort_order: Optional[SortOrder] = SortOrder.DESC
  combine_columns: Optional[bool]


class SelectRowsResponse(BaseModel):
  """The response for the select rows endpoint."""
  rows: list[dict]
  total_num_rows: int


@router.get('/{namespace}/{dataset_name}/select_rows_download', response_model=None)
def select_rows_download(namespace: str, dataset_name: str, url_safe_options: str) -> list[dict]:
  """Select rows from the dataset database and downloads them."""
  options = SelectRowsOptions.parse_raw(unquote(url_safe_options))
  return select_rows(namespace, dataset_name, options).rows


@router.post('/{namespace}/{dataset_name}/select_rows', response_model_exclude_none=True)
def select_rows(namespace: str, dataset_name: str,
                options: SelectRowsOptions) -> SelectRowsResponse:
  """Select rows from the dataset database."""
  dataset = get_dataset(namespace, dataset_name)

  sanitized_filters = [
    PyFilter(path=normalize_path(f.path), op=f.op, value=f.value) for f in (options.filters or [])
  ]

  res = dataset.select_rows(
    columns=options.columns,
    searches=options.searches or [],
    filters=sanitized_filters,
    sort_by=options.sort_by,
    sort_order=options.sort_order,
    limit=options.limit,
    offset=options.offset,
    combine_columns=options.combine_columns or False)

  return SelectRowsResponse(rows=list(res), total_num_rows=res.total_num_rows)


@router.post('/{namespace}/{dataset_name}/select_rows_schema', response_model_exclude_none=True)
def select_rows_schema(namespace: str, dataset_name: str,
                       options: SelectRowsSchemaOptions) -> SelectRowsSchemaResult:
  """Select rows from the dataset database."""
  dataset = get_dataset(namespace, dataset_name)
  return dataset.select_rows_schema(
    columns=options.columns,
    searches=options.searches or [],
    sort_by=options.sort_by,
    sort_order=options.sort_order,
    combine_columns=options.combine_columns or False)


class SelectGroupsOptions(BaseModel):
  """The request for the select groups endpoint."""
  leaf_path: Path
  filters: Optional[Sequence[Filter]]
  sort_by: Optional[GroupsSortBy] = GroupsSortBy.COUNT
  sort_order: Optional[SortOrder] = SortOrder.DESC
  limit: Optional[int] = 100
  bins: Optional[list[Bin]]


@router.post('/{namespace}/{dataset_name}/select_groups')
def select_groups(namespace: str, dataset_name: str,
                  options: SelectGroupsOptions) -> SelectGroupsResult:
  """Select groups from the dataset database."""
  dataset = get_dataset(namespace, dataset_name)
  sanitized_filters = [
    PyFilter(path=normalize_path(f.path), op=f.op, value=f.value) for f in (options.filters or [])
  ]
  return dataset.select_groups(options.leaf_path, sanitized_filters, options.sort_by,
                               options.sort_order, options.limit, options.bins)


@router.get('/{namespace}/{dataset_name}/media')
def get_media(namespace: str, dataset_name: str, item_id: str, leaf_path: str) -> Response:
  """Get the media for the dataset."""
  dataset = get_dataset(namespace, dataset_name)
  path = tuple(leaf_path.split('.'))
  result = dataset.media(item_id, path)
  # Return the response via HTTP.
  return Response(content=result.data)


@router.get('/{namespace}/{dataset_name}/settings')
def get_settings(namespace: str, dataset_name: str) -> DatasetSettings:
  """Get the media for the dataset."""
  dataset = get_dataset(namespace, dataset_name)
  return dataset.settings()


@router.post('/{namespace}/{dataset_name}/settings', response_model_exclude_none=True)
def update_settings(namespace: str, dataset_name: str, settings: DatasetSettings) -> None:
  """Get the media for the dataset."""
  if CONFIG.get('LILAC_READONLY', False):
    raise ValueError('Server is in readonly mode. This disables dataset.update_settings.')

  dataset = get_dataset(namespace, dataset_name)
  dataset.update_settings(settings)
  return None