-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
686 lines (583 loc) · 22.9 KB
/
api.py
File metadata and controls
686 lines (583 loc) · 22.9 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
from typing import Optional, TypeVar, Union
from contextlib import contextmanager
import base64
import os
import json
from lavender_data.serialize import deserialize_sample
from openapi_lavender_data_rest import Client, AuthenticatedClient
from openapi_lavender_data_rest.types import Response
# apis
from openapi_lavender_data_rest.api.root import version_version_get
from openapi_lavender_data_rest.api.datasets import (
get_dataset_datasets_dataset_id_get,
get_datasets_datasets_get,
get_shardset_datasets_dataset_id_shardsets_shardset_id_get,
create_dataset_datasets_post,
delete_dataset_datasets_dataset_id_delete,
create_shardset_datasets_dataset_id_shardsets_post,
delete_shardset_datasets_dataset_id_shardsets_shardset_id_delete,
sync_shardset_datasets_dataset_id_shardsets_shardset_id_sync_post,
get_sync_status_datasets_dataset_id_shardsets_shardset_id_sync_get,
preprocess_dataset_datasets_dataset_id_preprocess_post,
)
from openapi_lavender_data_rest.api.iterations import (
create_iteration_iterations_post,
get_next_iterations_iteration_id_next_get,
submit_next_iterations_iteration_id_next_post,
get_submitted_result_iterations_iteration_id_next_cache_key_get,
get_iteration_iterations_iteration_id_get,
get_iterations_iterations_get,
complete_index_iterations_iteration_id_complete_index_post,
pushback_iterations_iteration_id_pushback_post,
get_progress_iterations_iteration_id_progress_get,
)
from openapi_lavender_data_rest.api.cluster import (
get_nodes_cluster_nodes_get,
)
from openapi_lavender_data_rest.api.background_tasks import (
get_tasks_background_tasks_get,
)
# models
from openapi_lavender_data_rest.models.http_validation_error import HTTPValidationError
from openapi_lavender_data_rest.models.create_dataset_params import CreateDatasetParams
from openapi_lavender_data_rest.models.create_shardset_params import (
CreateShardsetParams,
)
from openapi_lavender_data_rest.models.sync_shardset_params import SyncShardsetParams
from openapi_lavender_data_rest.models.dataset_column_options import (
DatasetColumnOptions,
)
from openapi_lavender_data_rest.models.get_dataset_response import GetDatasetResponse
from openapi_lavender_data_rest.models.create_iteration_params import (
CreateIterationParams,
)
from openapi_lavender_data_rest.models.get_iteration_response import (
GetIterationResponse,
)
from openapi_lavender_data_rest.models.dataset_public import DatasetPublic
from openapi_lavender_data_rest.models.dataset_column_public import DatasetColumnPublic
from openapi_lavender_data_rest.models.shardset_public import ShardsetPublic
from openapi_lavender_data_rest.models.shardset_with_shards import ShardsetWithShards
from openapi_lavender_data_rest.models.shard_public import ShardPublic
from openapi_lavender_data_rest.models.iteration_filter import IterationFilter
from openapi_lavender_data_rest.models.iteration_categorizer import IterationCategorizer
from openapi_lavender_data_rest.models.iteration_collater import IterationCollater
from openapi_lavender_data_rest.models.iteration_preprocessor import (
IterationPreprocessor,
)
from openapi_lavender_data_rest.models.preprocess_dataset_params import (
PreprocessDatasetParams,
)
class LavenderDataApiError(Exception):
pass
class LavenderDataSampleProcessingError(LavenderDataApiError):
current: int
msg: str
def __init__(self, current: int, msg: str):
self.current = current
self.msg = msg
def __str__(self):
return self.msg
_T = TypeVar("T")
class LavenderDataClient:
def __init__(
self,
api_url: Optional[str] = None,
api_key: Optional[str] = None,
):
self.api_url = api_url or os.getenv("LAVENDER_DATA_API_URL")
self.api_key = api_key or os.getenv("LAVENDER_DATA_API_KEY")
try:
self.version = self.get_version().version
except Exception as e:
raise ValueError(
"Failed to initialize lavender_data client. Please check if the server is running."
) from e
@contextmanager
def _get_client(self):
if self.api_key is None:
_client = Client(base_url=self.api_url)
else:
_client = AuthenticatedClient(
base_url=self.api_url,
token=base64.b64encode(self.api_key.encode()).decode(),
prefix="Basic",
)
with _client as client:
yield client
def _check_response(self, response: Response[Union[_T, HTTPValidationError]]) -> _T:
if response.headers.get("X-Lavender-Data-Error") == "SAMPLE_PROCESSING_ERROR":
raise LavenderDataSampleProcessingError(
current=int(response.headers.get("X-Lavender-Data-Sample-Current")),
msg=json.loads(response.content)["detail"],
)
if response.status_code >= 400:
try:
json_content = json.loads(response.content)
msg = json_content["detail"]
except Exception:
msg = response.content.decode("utf-8")
raise LavenderDataApiError(msg)
if isinstance(response.parsed, HTTPValidationError):
raise LavenderDataApiError(response.parsed)
return response.parsed
def get_version(self):
with self._get_client() as client:
response = version_version_get.sync_detailed(
client=client,
)
return self._check_response(response)
def get_dataset(
self,
dataset_id: Optional[str] = None,
name: Optional[str] = None,
):
if dataset_id is None and name is None:
raise ValueError("Either dataset_id or name must be provided")
if dataset_id is not None and name is not None:
raise ValueError("Only one of dataset_id or name can be provided")
if name is not None:
datasets = self.get_datasets(name=name)
if len(datasets) == 0:
raise ValueError(f"Dataset {name} not found")
if len(datasets) > 1:
raise ValueError(
f"Multiple datasets found for name {name}: {', '.join([d.id for d in datasets])}\n"
"This should never happen since the dataset name is unique. "
"Please contact the Lavender Data team if you see this error."
)
dataset_id = datasets[0].id
with self._get_client() as client:
response = get_dataset_datasets_dataset_id_get.sync_detailed(
client=client,
dataset_id=dataset_id,
)
return self._check_response(response)
def get_datasets(self, name: Optional[str] = None):
with self._get_client() as client:
response = get_datasets_datasets_get.sync_detailed(
client=client,
name=name,
)
return self._check_response(response)
def create_dataset(
self,
name: str,
uid_column_name: Optional[str] = None,
shardset_location: Optional[str] = None,
):
with self._get_client() as client:
response = create_dataset_datasets_post.sync_detailed(
client=client,
body=CreateDatasetParams(
name=name,
uid_column_name=uid_column_name,
shardset_location=shardset_location,
),
)
return self._check_response(response)
def delete_dataset(self, dataset_id: str):
with self._get_client() as client:
response = delete_dataset_datasets_dataset_id_delete.sync_detailed(
client=client,
dataset_id=dataset_id,
)
return self._check_response(response)
def get_shardset(self, dataset_id: str, shardset_id: str):
with self._get_client() as client:
response = get_shardset_datasets_dataset_id_shardsets_shardset_id_get.sync_detailed(
client=client,
dataset_id=dataset_id,
shardset_id=shardset_id,
)
return self._check_response(response)
def create_shardset(
self, dataset_id: str, location: str, columns: list[DatasetColumnOptions] = []
):
with self._get_client() as client:
response = create_shardset_datasets_dataset_id_shardsets_post.sync_detailed(
client=client,
dataset_id=dataset_id,
body=CreateShardsetParams(location=location, columns=columns),
)
return self._check_response(response)
def delete_shardset(self, dataset_id: str, shardset_id: str):
with self._get_client() as client:
response = delete_shardset_datasets_dataset_id_shardsets_shardset_id_delete.sync_detailed(
client=client,
dataset_id=dataset_id,
shardset_id=shardset_id,
)
return self._check_response(response)
def sync_shardset(self, dataset_id: str, shardset_id: str, overwrite: bool = False):
with self._get_client() as client:
response = sync_shardset_datasets_dataset_id_shardsets_shardset_id_sync_post.sync_detailed(
client=client,
dataset_id=dataset_id,
shardset_id=shardset_id,
body=SyncShardsetParams(
overwrite=overwrite,
),
)
return self._check_response(response)
def get_sync_shardset_status(self, dataset_id: str, shardset_id: str):
with self._get_client() as client:
response = get_sync_status_datasets_dataset_id_shardsets_shardset_id_sync_get.sync_detailed(
client=client,
dataset_id=dataset_id,
shardset_id=shardset_id,
)
return self._check_response(response)
def preprocess_dataset(
self,
dataset_id: str,
shardset_location: str,
source_shardset_ids: Optional[list[str]] = None,
preprocessors: Optional[list[IterationPreprocessor]] = None,
export_columns: Optional[list[str]] = None,
batch_size: Optional[int] = None,
overwrite: bool = False,
):
with self._get_client() as client:
response = (
preprocess_dataset_datasets_dataset_id_preprocess_post.sync_detailed(
client=client,
dataset_id=dataset_id,
body=PreprocessDatasetParams(
shardset_location=shardset_location,
source_shardset_ids=source_shardset_ids,
preprocessors=preprocessors,
export_columns=export_columns,
batch_size=batch_size,
overwrite=overwrite,
),
)
)
return self._check_response(response)
def create_iteration(
self,
dataset_id: str,
shardsets: Optional[list[str]] = None,
shuffle: bool = False,
shuffle_seed: Optional[int] = None,
shuffle_block_size: Optional[int] = None,
batch_size: Optional[int] = None,
replication_pg: Optional[list[list[int]]] = None,
filters: Optional[list[IterationFilter]] = None,
categorizer: Optional[IterationCategorizer] = None,
collater: Optional[IterationCollater] = None,
preprocessors: Optional[list[IterationPreprocessor]] = None,
rank: int = 0,
world_size: Optional[int] = None,
wait_participant_threshold: Optional[float] = None,
cluster_sync: bool = False,
):
with self._get_client() as client:
response = create_iteration_iterations_post.sync_detailed(
client=client,
body=CreateIterationParams(
dataset_id=dataset_id,
shardsets=shardsets,
shuffle=shuffle,
shuffle_seed=shuffle_seed,
shuffle_block_size=shuffle_block_size,
batch_size=batch_size,
filters=filters,
categorizer=categorizer,
collater=collater,
preprocessors=preprocessors,
replication_pg=replication_pg,
rank=rank,
world_size=world_size,
wait_participant_threshold=wait_participant_threshold,
cluster_sync=cluster_sync,
),
)
return self._check_response(response)
def get_iterations(
self, dataset_id: Optional[str] = None, dataset_name: Optional[str] = None
):
if dataset_id is None and dataset_name is None:
raise ValueError("Either dataset_id or dataset_name must be provided")
if dataset_id is not None and dataset_name is not None:
raise ValueError("Only one of dataset_id or dataset_name can be provided")
if dataset_name is not None:
dataset = self.get_dataset(name=dataset_name)
if dataset is None:
raise ValueError(f"Dataset {dataset_name} not found")
dataset_id = dataset.id
with self._get_client() as client:
response = get_iterations_iterations_get.sync_detailed(
client=client,
dataset_id=dataset_id,
)
return self._check_response(response)
def get_iteration(self, iteration_id: str):
with self._get_client() as client:
response = get_iteration_iterations_iteration_id_get.sync_detailed(
client=client,
iteration_id=iteration_id,
)
return self._check_response(response)
def get_next_item(
self,
iteration_id: str,
rank: int = 0,
no_cache: bool = False,
max_retry_count: int = 0,
):
with self._get_client() as client:
response = get_next_iterations_iteration_id_next_get.sync_detailed(
client=client,
iteration_id=iteration_id,
rank=rank,
no_cache=no_cache,
max_retry_count=max_retry_count,
)
return self._check_response(response).payload.read()
def submit_next_item(
self,
iteration_id: str,
rank: int = 0,
no_cache: bool = False,
max_retry_count: int = 0,
):
with self._get_client() as client:
response = submit_next_iterations_iteration_id_next_post.sync_detailed(
client=client,
iteration_id=iteration_id,
rank=rank,
no_cache=no_cache,
max_retry_count=max_retry_count,
)
return self._check_response(response)
def get_submitted_result(self, iteration_id: str, cache_key: str):
with self._get_client() as client:
response = get_submitted_result_iterations_iteration_id_next_cache_key_get.sync_detailed(
client=client,
iteration_id=iteration_id,
cache_key=cache_key,
)
if response.status_code == 202:
raise LavenderDataApiError(response.content.decode("utf-8"))
return self._check_response(response).payload.read()
def complete_index(self, iteration_id: str, index: int):
with self._get_client() as client:
response = complete_index_iterations_iteration_id_complete_index_post.sync_detailed(
client=client,
iteration_id=iteration_id,
index=index,
)
return self._check_response(response)
def pushback(self, iteration_id: str):
with self._get_client() as client:
response = pushback_iterations_iteration_id_pushback_post.sync_detailed(
client=client,
iteration_id=iteration_id,
)
return self._check_response(response)
def get_progress(self, iteration_id: str):
with self._get_client() as client:
response = get_progress_iterations_iteration_id_progress_get.sync_detailed(
client=client,
iteration_id=iteration_id,
)
return self._check_response(response)
def get_node_statuses(self):
with self._get_client() as client:
response = get_nodes_cluster_nodes_get.sync_detailed(
client=client,
)
return self._check_response(response)
def get_tasks(self):
with self._get_client() as client:
response = get_tasks_background_tasks_get.sync_detailed(
client=client,
)
return self._check_response(response)
_client_instance = None
@contextmanager
def ensure_client():
global _client_instance
if _client_instance is None:
try:
init(
api_url=os.getenv("LAVENDER_DATA_API_URL", "http://localhost:8000"),
api_key=os.getenv("LAVENDER_DATA_API_KEY", None),
)
except Exception as e:
raise e
yield _client_instance
def init(api_url: str = "http://localhost:8000", api_key: Optional[str] = None):
"""Initialize and return a LavenderDataClient instance.
This function maintains backwards compatibility with the old API.
"""
global _client_instance
_client_instance = LavenderDataClient(api_url=api_url, api_key=api_key)
return _client_instance
def get_client():
global _client_instance
if _client_instance is None:
raise ValueError(
"Lavender Data client is not initialized. Please call lavender_data.client.api.init() first."
)
return _client_instance
@ensure_client()
def get_version():
return _client_instance.get_version()
@ensure_client()
def get_dataset(
dataset_id: Optional[str] = None,
name: Optional[str] = None,
):
return _client_instance.get_dataset(dataset_id=dataset_id, name=name)
@ensure_client()
def get_datasets(name: Optional[str] = None):
return _client_instance.get_datasets(name=name)
@ensure_client()
def create_dataset(
name: str,
uid_column_name: Optional[str] = None,
shardset_location: Optional[str] = None,
):
return _client_instance.create_dataset(
name=name, uid_column_name=uid_column_name, shardset_location=shardset_location
)
@ensure_client()
def delete_dataset(dataset_id: str):
return _client_instance.delete_dataset(dataset_id=dataset_id)
@ensure_client()
def get_shardset(dataset_id: str, shardset_id: str):
return _client_instance.get_shardset(dataset_id=dataset_id, shardset_id=shardset_id)
@ensure_client()
def create_shardset(
dataset_id: str, location: str, columns: list[DatasetColumnOptions] = []
):
return _client_instance.create_shardset(
dataset_id=dataset_id, location=location, columns=columns
)
@ensure_client()
def delete_shardset(dataset_id: str, shardset_id: str):
return _client_instance.delete_shardset(
dataset_id=dataset_id, shardset_id=shardset_id
)
@ensure_client()
def sync_shardset(dataset_id: str, shardset_id: str, overwrite: bool = False):
return _client_instance.sync_shardset(
dataset_id=dataset_id, shardset_id=shardset_id, overwrite=overwrite
)
@ensure_client()
def get_sync_shardset_status(dataset_id: str, shardset_id: str):
return _client_instance.get_sync_shardset_status(
dataset_id=dataset_id, shardset_id=shardset_id
)
@ensure_client()
def preprocess_dataset(
dataset_id: str,
shardset_location: str,
source_shardset_ids: Optional[list[str]] = None,
preprocessors: Optional[list[IterationPreprocessor]] = None,
export_columns: Optional[list[str]] = None,
batch_size: Optional[int] = None,
overwrite: bool = False,
):
return _client_instance.preprocess_dataset(
dataset_id=dataset_id,
shardset_location=shardset_location,
source_shardset_ids=source_shardset_ids,
preprocessors=preprocessors,
export_columns=export_columns,
batch_size=batch_size,
overwrite=overwrite,
)
@ensure_client()
def create_iteration(
dataset_id: str,
shardsets: Optional[list[str]] = None,
shuffle: bool = False,
shuffle_seed: Optional[int] = None,
shuffle_block_size: Optional[int] = None,
batch_size: Optional[int] = None,
replication_pg: Optional[list[list[int]]] = None,
filters: Optional[list[IterationFilter]] = None,
categorizer: Optional[IterationCategorizer] = None,
collater: Optional[IterationCollater] = None,
preprocessors: Optional[list[IterationPreprocessor]] = None,
rank: int = 0,
world_size: Optional[int] = None,
wait_participant_threshold: Optional[float] = None,
cluster_sync: bool = False,
):
return _client_instance.create_iteration(
dataset_id=dataset_id,
shardsets=shardsets,
shuffle=shuffle,
shuffle_seed=shuffle_seed,
shuffle_block_size=shuffle_block_size,
batch_size=batch_size,
replication_pg=replication_pg,
filters=filters,
categorizer=categorizer,
collater=collater,
preprocessors=preprocessors,
rank=rank,
world_size=world_size,
wait_participant_threshold=wait_participant_threshold,
cluster_sync=cluster_sync,
)
@ensure_client()
def get_iterations(
dataset_id: Optional[str] = None, dataset_name: Optional[str] = None
):
return _client_instance.get_iterations(
dataset_id=dataset_id, dataset_name=dataset_name
)
@ensure_client()
def get_iteration(iteration_id: str):
return _client_instance.get_iteration(iteration_id=iteration_id)
@ensure_client()
def get_next_item(
iteration_id: str,
rank: int = 0,
no_cache: bool = False,
max_retry_count: int = 0,
):
return _client_instance.get_next_item(
iteration_id=iteration_id,
rank=rank,
no_cache=no_cache,
max_retry_count=max_retry_count,
)
@ensure_client()
def submit_next_item(
iteration_id: str,
rank: int = 0,
no_cache: bool = False,
max_retry_count: int = 0,
):
return _client_instance.submit_next_item(
iteration_id=iteration_id,
rank=rank,
no_cache=no_cache,
max_retry_count=max_retry_count,
)
@ensure_client()
def get_submitted_result(iteration_id: str, cache_key: str):
return _client_instance.get_submitted_result(
iteration_id=iteration_id, cache_key=cache_key
)
@ensure_client()
def complete_index(iteration_id: str, index: int):
return _client_instance.complete_index(iteration_id=iteration_id, index=index)
@ensure_client()
def pushback(iteration_id: str):
return _client_instance.pushback(iteration_id=iteration_id)
@ensure_client()
def get_progress(iteration_id: str):
return _client_instance.get_progress(iteration_id=iteration_id)
@ensure_client()
def get_node_statuses():
return _client_instance.get_node_statuses()
@ensure_client()
def get_tasks():
return _client_instance.get_tasks()