/
api.py
700 lines (582 loc) · 24.2 KB
/
api.py
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
# coding=utf-8
# Copyright 2021-present, the Recognai S.L. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import logging
import os
import re
import warnings
from asyncio import Future
from functools import wraps
from inspect import signature
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union
from tqdm.auto import tqdm
from rubrix._constants import (
DATASET_NAME_REGEX_PATTERN,
DEFAULT_API_KEY,
RUBRIX_WORKSPACE_HEADER_NAME,
)
from rubrix.client.apis.datasets import Datasets
from rubrix.client.apis.metrics import MetricsAPI
from rubrix.client.apis.searches import Searches
from rubrix.client.datasets import (
Dataset,
DatasetForText2Text,
DatasetForTextClassification,
DatasetForTokenClassification,
)
from rubrix.client.metrics.models import MetricResults
from rubrix.client.models import (
BulkResponse,
Record,
Text2TextRecord,
TextClassificationRecord,
TokenClassificationRecord,
)
from rubrix.client.sdk.client import AuthenticatedClient
from rubrix.client.sdk.commons.api import async_bulk
from rubrix.client.sdk.commons.errors import RubrixClientError
from rubrix.client.sdk.datasets import api as datasets_api
from rubrix.client.sdk.datasets.models import CopyDatasetRequest, TaskType
from rubrix.client.sdk.metrics import api as metrics_api
from rubrix.client.sdk.metrics.models import MetricInfo
from rubrix.client.sdk.text2text import api as text2text_api
from rubrix.client.sdk.text2text.models import (
CreationText2TextRecord,
Text2TextBulkData,
Text2TextQuery,
)
from rubrix.client.sdk.text_classification import api as text_classification_api
from rubrix.client.sdk.text_classification.models import (
CreationTextClassificationRecord,
LabelingRule,
LabelingRuleMetricsSummary,
TextClassificationBulkData,
TextClassificationQuery,
)
from rubrix.client.sdk.token_classification import api as token_classification_api
from rubrix.client.sdk.token_classification.models import (
CreationTokenClassificationRecord,
TokenClassificationBulkData,
TokenClassificationQuery,
)
from rubrix.client.sdk.users import api as users_api
from rubrix.client.sdk.users.models import User
from rubrix.utils import setup_loop_in_thread
_LOGGER = logging.getLogger(__name__)
class _RubrixLogAgent:
def __init__(self, api: "Api"):
self.__api__ = api
self.__loop__, self.__thread__ = setup_loop_in_thread()
@staticmethod
async def __log_internal__(api: "Api", *args, **kwargs):
try:
return await api.log_async(*args, **kwargs)
except Exception as ex:
_LOGGER.error(
f"Cannot log data {args, kwargs}\n"
f"Error of type {type(ex)}\n: {ex}. ({ex.args})"
)
raise ex
def log(self, *args, **kwargs) -> Future:
return asyncio.run_coroutine_threadsafe(
self.__log_internal__(self.__api__, *args, **kwargs), self.__loop__
)
class Api:
# Larger sizes will trigger a warning
_MAX_CHUNK_SIZE = 5000
def __init__(
self,
api_url: Optional[str] = None,
api_key: Optional[str] = None,
workspace: Optional[str] = None,
timeout: int = 60,
):
"""Init the Python client.
We will automatically init a default client for you when calling other client methods.
The arguments provided here will overwrite your corresponding environment variables.
Args:
api_url: Address of the REST API. If `None` (default) and the env variable ``RUBRIX_API_URL`` is not set,
it will default to `http://localhost:6900`.
api_key: Authentification key for the REST API. If `None` (default) and the env variable ``RUBRIX_API_KEY``
is not set, it will default to `rubrix.apikey`.
workspace: The workspace to which records will be logged/loaded. If `None` (default) and the
env variable ``RUBRIX_WORKSPACE`` is not set, it will default to the private user workspace.
timeout: Wait `timeout` seconds for the connection to timeout. Default: 60.
Examples:
>>> import rubrix as rb
>>> rb.init(api_url="http://localhost:9090", api_key="4AkeAPIk3Y")
"""
api_url = api_url or os.getenv("RUBRIX_API_URL", "http://localhost:6900")
# Checking that the api_url does not end in '/'
api_url = re.sub(r"\/$", "", api_url)
api_key = api_key or os.getenv("RUBRIX_API_KEY", DEFAULT_API_KEY)
workspace = workspace or os.getenv("RUBRIX_WORKSPACE")
self._client: AuthenticatedClient = AuthenticatedClient(
base_url=api_url, token=api_key, timeout=timeout
)
self._user: User = users_api.whoami(client=self._client)
if workspace is not None:
self.set_workspace(workspace)
self._agent = _RubrixLogAgent(self)
def __del__(self):
if hasattr(self, "_client"):
del self._client
if hasattr(self, "_agent"):
del self._agent
@property
def client(self):
"""The underlying authenticated client"""
return self._client
@property
def datasets(self) -> Datasets:
return Datasets(client=self._client)
@property
def searches(self):
return Searches(client=self._client)
@property
def metrics(self):
return MetricsAPI(client=self.client)
def set_workspace(self, workspace: str):
"""Sets the active workspace.
Args:
workspace: The new workspace
"""
if workspace is None:
raise Exception("Must provide a workspace")
if workspace != self.get_workspace():
if workspace == self._user.username:
self._client.headers.pop(RUBRIX_WORKSPACE_HEADER_NAME, workspace)
elif (
self._user.workspaces is not None
and workspace not in self._user.workspaces
):
raise Exception(f"Wrong provided workspace {workspace}")
self._client.headers[RUBRIX_WORKSPACE_HEADER_NAME] = workspace
def get_workspace(self) -> str:
"""Returns the name of the active workspace.
Returns:
The name of the active workspace as a string.
"""
return self._client.headers.get(
RUBRIX_WORKSPACE_HEADER_NAME, self._user.username
)
def copy(self, dataset: str, name_of_copy: str, workspace: str = None):
"""Creates a copy of a dataset including its tags and metadata
Args:
dataset: Name of the source dataset
name_of_copy: Name of the copied dataset
workspace: If provided, dataset will be copied to that workspace
Examples:
>>> import rubrix as rb
>>> rb.copy("my_dataset", name_of_copy="new_dataset")
>>> rb.load("new_dataset")
"""
datasets_api.copy_dataset(
client=self._client,
name=dataset,
json_body=CopyDatasetRequest(name=name_of_copy, target_workspace=workspace),
)
def delete(self, name: str) -> None:
"""Deletes a dataset.
Args:
name: The dataset name.
Examples:
>>> import rubrix as rb
>>> rb.delete(name="example-dataset")
"""
datasets_api.delete_dataset(client=self._client, name=name)
def log(
self,
records: Union[Record, Iterable[Record], Dataset],
name: str,
tags: Optional[Dict[str, str]] = None,
metadata: Optional[Dict[str, Any]] = None,
chunk_size: int = 500,
verbose: bool = True,
background: bool = False,
) -> Union[BulkResponse, Future]:
"""Logs Records to Rubrix.
The logging happens asynchronously in a background thread.
Args:
records: The record, an iterable of records, or a dataset to log.
name: The dataset name.
tags: A dictionary of tags related to the dataset.
metadata: A dictionary of extra info for the dataset.
chunk_size: The chunk size for a data bulk.
verbose: If True, shows a progress bar and prints out a quick summary at the end.
background: If True, we will NOT wait for the logging process to finish and return an ``asyncio.Future``
object. You probably want to set ``verbose`` to False in that case.
Returns:
Summary of the response from the REST API.
If the ``background`` argument is set to True, an ``asyncio.Future`` will be returned instead.
Examples:
>>> import rubrix as rb
>>> record = rb.TextClassificationRecord(
... text="my first rubrix example",
... prediction=[('spam', 0.8), ('ham', 0.2)]
... )
>>> rb.log(record, name="example-dataset")
1 records logged to http://localhost:6900/datasets/rubrix/example-dataset
BulkResponse(dataset='example-dataset', processed=1, failed=0)
>>>
>>> # Logging records in the background
>>> rb.log(record, name="example-dataset", background=True, verbose=False)
<Future at 0x7f675a1fffa0 state=pending>
"""
future = self._agent.log(
records=records,
name=name,
tags=tags,
metadata=metadata,
chunk_size=chunk_size,
verbose=verbose,
)
if background:
return future
try:
return future.result()
finally:
future.cancel()
async def log_async(
self,
records: Union[Record, Iterable[Record], Dataset],
name: str,
tags: Optional[Dict[str, str]] = None,
metadata: Optional[Dict[str, Any]] = None,
chunk_size: int = 500,
verbose: bool = True,
) -> BulkResponse:
"""Logs Records to Rubrix with asyncio.
Args:
records: The record, an iterable of records, or a dataset to log.
name: The dataset name.
tags: A dictionary of tags related to the dataset.
metadata: A dictionary of extra info for the dataset.
chunk_size: The chunk size for a data bulk.
verbose: If True, shows a progress bar and prints out a quick summary at the end.
Returns:
Summary of the response from the REST API
Examples:
>>> # Log asynchronously from your notebook
>>> import asyncio
>>> import rubrix as rb
>>> from rubrix.utils import setup_loop_in_thread
>>> loop, _ = setup_loop_in_thread()
>>> future_response = asyncio.run_coroutine_threadsafe(
... rb.log_async(my_records, dataset_name), loop
... )
"""
tags = tags or {}
metadata = metadata or {}
if not name:
raise InputValueError("Empty dataset name has been passed as argument.")
if not re.match(DATASET_NAME_REGEX_PATTERN, name):
raise InputValueError(
f"Provided dataset name {name} does not match the pattern {DATASET_NAME_REGEX_PATTERN}. "
"Please, use a valid name for your dataset"
)
if chunk_size > self._MAX_CHUNK_SIZE:
_LOGGER.warning(
"""The introduced chunk size is noticeably large, timeout errors may occur.
Consider a chunk size smaller than %s""",
self._MAX_CHUNK_SIZE,
)
if isinstance(records, Record.__args__):
records = [records]
records = list(records)
try:
record_type = type(records[0])
except IndexError:
raise InputValueError("Empty record list has been passed as argument.")
if record_type is TextClassificationRecord:
bulk_class = TextClassificationBulkData
creation_class = CreationTextClassificationRecord
elif record_type is TokenClassificationRecord:
bulk_class = TokenClassificationBulkData
creation_class = CreationTokenClassificationRecord
elif record_type is Text2TextRecord:
bulk_class = Text2TextBulkData
creation_class = CreationText2TextRecord
else:
raise InputValueError(
f"Unknown record type {record_type}. Available values are {Record.__args__}"
)
processed, failed = 0, 0
progress_bar = tqdm(total=len(records), disable=not verbose)
for i in range(0, len(records), chunk_size):
chunk = records[i : i + chunk_size]
response = await async_bulk(
client=self._client,
name=name,
json_body=bulk_class(
tags=tags,
metadata=metadata,
records=[creation_class.from_client(r) for r in chunk],
),
)
processed += response.parsed.processed
failed += response.parsed.failed
progress_bar.update(len(chunk))
progress_bar.close()
# TODO: improve logging policy in library
if verbose:
_LOGGER.info(
f"Processed {processed} records in dataset {name}. Failed: {failed}"
)
workspace = self.get_workspace()
if (
not workspace
): # Just for backward comp. with datasets with no workspaces
workspace = "-"
print(
f"{processed} records logged to {self._client.base_url}/datasets/{workspace}/{name}"
)
# Creating a composite BulkResponse with the total processed and failed
return BulkResponse(dataset=name, processed=processed, failed=failed)
def delete_records(
self,
name: str,
query: Optional[str] = None,
ids: Optional[List[Union[str, int]]] = None,
discard_only: bool = False,
discard_when_forbidden: bool = True,
) -> Tuple[int, int]:
"""Delete records from a Rubrix dataset.
Args:
name: The dataset name.
query: An ElasticSearch query with the `query string syntax
<https://rubrix.readthedocs.io/en/stable/guides/queries.html>`_
ids: If provided, deletes dataset records with given ids.
discard_only: If `True`, matched records won't be deleted. Instead, they will be marked as `Discarded`
discard_when_forbidden: Only super-user or dataset creator can delete records from a dataset.
So, running "hard" deletion for other users will raise an `ForbiddenApiError` error.
If this parameter is `True`, the client API will automatically try to mark as ``Discarded``
records instead. Default, `True`
Returns:
The total of matched records and real number of processed errors. These numbers could not
be the same if some data conflicts are found during operations (some matched records change during
deletion).
Examples:
>>> ## Delete by id
>>> import rubrix as rb
>>> rb.delete_records(name="example-dataset", ids=[1,3,5])
>>> ## Discard records by query
>>> import rubrix as rb
>>> rb.delete_records(name="example-dataset", query="metadata.code=33", discard_only=True)
"""
return self.datasets.delete_records(
name=name,
query=query,
ids=ids,
mark_as_discarded=discard_only,
discard_when_forbidden=discard_when_forbidden,
)
def load(
self,
name: str,
query: Optional[str] = None,
ids: Optional[List[Union[str, int]]] = None,
limit: Optional[int] = None,
id_from: Optional[str] = None,
as_pandas=None,
) -> Dataset:
"""Loads a Rubrix dataset.
Parameters:
-----------
name:
The dataset name.
query:
An ElasticSearch query with the
`query string syntax <https://rubrix.readthedocs.io/en/stable/guides/queries.html>`_
ids:
If provided, load dataset records with given ids.
limit:
The number of records to retrieve.
id_from:
If provided, starts gathering the records starting from that Record. As the Records returned with the
load method are sorted by ID, ´id_from´ can be used to load using batches.
as_pandas:
DEPRECATED! To get a pandas DataFrame do ``rb.load('my_dataset').to_pandas()``.
Returns:
--------
A Rubrix dataset.
Examples:
**Basic Loading**: load the samples sorted by their ID
>>> import rubrix as rb
>>> dataset = rb.load(name="example-dataset")
**Iterate over a large dataset:**
When dealing with a large dataset you might want to load it in batches to optimize memory consumption
and avoid network timeouts. To that end, a simple batch-iteration over the whole database can be done
employing the `from_id` parameter. This parameter will act as a delimiter, retrieving the N items after
the given id, where N is determined by the `limit` parameter. **NOTE** If
no `limit` is given the whole dataset after that ID will be retrieved.
>>> import rubrix as rb
>>> dataset_batch_1 = rb.load(name="example-dataset", limit=1000)
>>> dataset_batch_2 = rb.load(name="example-dataset", limit=1000, id_from=dataset_batch_1[-1].id)
"""
if as_pandas is False:
warnings.warn(
"The argument `as_pandas` is deprecated and will be removed in a future version. "
"Please adapt your code accordingly. ",
FutureWarning,
)
elif as_pandas is True:
raise ValueError(
"The argument `as_pandas` is deprecated and will be removed in a future version. "
"Please adapt your code accordingly. ",
"If you want a pandas DataFrame do `rb.load('my_dataset').to_pandas()`.",
)
response = datasets_api.get_dataset(client=self._client, name=name)
task = response.parsed.task
task_config = {
TaskType.text_classification: (
text_classification_api.data,
TextClassificationQuery,
DatasetForTextClassification,
),
TaskType.token_classification: (
token_classification_api.data,
TokenClassificationQuery,
DatasetForTokenClassification,
),
TaskType.text2text: (
text2text_api.data,
Text2TextQuery,
DatasetForText2Text,
),
}
try:
get_dataset_data, request_class, dataset_class = task_config[task]
except KeyError:
raise ValueError(
f"Load method not supported for the '{task}' task. Supported tasks: "
f"{[TaskType.text_classification, TaskType.token_classification, TaskType.text2text]}"
)
response = get_dataset_data(
client=self._client,
name=name,
request=request_class(ids=ids, query_text=query),
limit=limit,
id_from=id_from,
)
records = [sdk_record.to_client() for sdk_record in response.parsed]
try:
records_sorted_by_id = sorted(records, key=lambda x: x.id)
# record ids can be a mix of int/str -> sort all as str type
except TypeError:
records_sorted_by_id = sorted(records, key=lambda x: str(x.id))
return dataset_class(records_sorted_by_id)
def dataset_metrics(self, name: str) -> List[MetricInfo]:
response = datasets_api.get_dataset(self._client, name)
response = metrics_api.get_dataset_metrics(
self._client, name=name, task=response.parsed.task
)
return response.parsed
def get_metric(self, name: str, metric: str) -> Optional[MetricInfo]:
metrics = self.dataset_metrics(name)
for metric_ in metrics:
if metric_.id == metric:
return metric_
def compute_metric(
self,
name: str,
metric: str,
query: Optional[str] = None,
interval: Optional[float] = None,
size: Optional[int] = None,
) -> MetricResults:
response = datasets_api.get_dataset(self._client, name)
metric_ = self.get_metric(name, metric=metric)
assert metric_ is not None, f"Metric {metric} not found !!!"
response = metrics_api.compute_metric(
self._client,
name=name,
task=response.parsed.task,
metric=metric,
query=query,
interval=interval,
size=size,
)
return MetricResults(**metric_.dict(), results=response.parsed)
def fetch_dataset_labeling_rules(self, dataset: str) -> List[LabelingRule]:
response = text_classification_api.fetch_dataset_labeling_rules(
self._client, name=dataset
)
return [LabelingRule.parse_obj(data) for data in response.parsed]
def rule_metrics_for_dataset(
self, dataset: str, rule: LabelingRule
) -> LabelingRuleMetricsSummary:
response = text_classification_api.dataset_rule_metrics(
self._client, name=dataset, query=rule.query, label=rule.label
)
return LabelingRuleMetricsSummary.parse_obj(response.parsed)
__ACTIVE_API__: Optional[Api] = None
def active_api() -> Api:
"""Returns the active API.
If Active API is None, initialize a default one.
"""
global __ACTIVE_API__
if __ACTIVE_API__ is None:
__ACTIVE_API__ = Api()
return __ACTIVE_API__
def api_wrapper(api_method: Callable):
"""Decorator to wrap the API methods in module functions.
Propagates the docstrings and adapts the signature of the methods.
"""
def decorator(func):
if asyncio.iscoroutinefunction(api_method):
@wraps(api_method)
async def wrapped_func(*args, **kwargs):
return await func(*args, **kwargs)
else:
@wraps(api_method)
def wrapped_func(*args, **kwargs):
return func(*args, **kwargs)
sign = signature(api_method)
wrapped_func.__signature__ = sign.replace(
parameters=[val for key, val in sign.parameters.items() if key != "self"]
)
return wrapped_func
return decorator
@api_wrapper(Api.__init__)
def init(*args, **kwargs):
global __ACTIVE_API__
__ACTIVE_API__ = Api(*args, **kwargs)
@api_wrapper(Api.set_workspace)
def set_workspace(*args, **kwargs):
return active_api().set_workspace(*args, **kwargs)
@api_wrapper(Api.get_workspace)
def get_workspace(*args, **kwargs):
return active_api().get_workspace(*args, **kwargs)
@api_wrapper(Api.copy)
def copy(*args, **kwargs):
return active_api().copy(*args, **kwargs)
@api_wrapper(Api.delete)
def delete(*args, **kwargs):
return active_api().delete(*args, **kwargs)
@api_wrapper(Api.log)
def log(*args, **kwargs):
return active_api().log(*args, **kwargs)
@api_wrapper(Api.log_async)
def log_async(*args, **kwargs):
return active_api().log_async(*args, **kwargs)
@api_wrapper(Api.load)
def load(*args, **kwargs):
return active_api().load(*args, **kwargs)
@api_wrapper(Api.delete_records)
def delete_records(*args, **kwargs):
return active_api().delete_records(*args, **kwargs)
class InputValueError(RubrixClientError):
pass