|
| 1 | +--- |
| 2 | +description: In this section, we will provide a step-by-step guide to show how to import and export datasets to Python, local disk, or the Hugging Face Hub |
| 3 | +--- |
| 4 | + |
| 5 | +# Importing and exporting datasets and records |
| 6 | + |
| 7 | +This guide provides an overview of how to import and export your dataset or its records to Python, your local disk, or the Hugging Face Hub. |
| 8 | + |
| 9 | +In Argilla, you can import/export two main components of a dataset: |
| 10 | +- The dataset's complete configuration defined in `rg.Settings`. This is useful if your want to share your feedback task or restore it later in Argilla. |
| 11 | +- The records stored in the dataset, including `Metadata`, `Vectors`, `Suggestions`, and `Responses`. This is useful if you want to use your dataset's records outside of Argilla. |
| 12 | + |
| 13 | +Check the [Dataset - Python Reference](../reference/argilla/datasets/dataset.md) to see the attributes, arguments, and methods of the export `Dataset` class in detail. |
| 14 | + |
| 15 | +To import records to a dataset, used the `rg.Datasets.records.log` method. Their is a guide on how to do this in the [Record - Python Reference](record.md). |
| 16 | + |
| 17 | +## Import and Export an `rg.Dataset` from Argilla |
| 18 | + |
| 19 | +First, we will go through exporting a complete dataset from Argilla. This includes the dataset's setting and records. All of these methods use the `rg.Dataset.from_*` and `rg.Dataset.to_*` methods. |
| 20 | + |
| 21 | +### Push an Argilla dataset to the Hugging Face Hub |
| 22 | + |
| 23 | +You can push a dataset from Argilla to the Hugging Face Hub. This is useful if you want to share your dataset with the community or version control it. You can push the dataset to the Hugging Face Hub using the `rg.Dataset.to_hub` method. |
| 24 | + |
| 25 | +```python |
| 26 | +import argilla as rg |
| 27 | + |
| 28 | +client = rg.Argilla(api_url="<api_url>", api_key="<api_key>") |
| 29 | +dataset = client.datasets(name="my_dataset") |
| 30 | +dataset.to_hub(repo_id="<repo_id>") |
| 31 | +``` |
| 32 | + |
| 33 | +!!! note "With or without records" |
| 34 | + The example above will push the dataset's `Settings` and records to the hub. If you only want to push the dataset's configuration, you can set the `with_records` parameter to `False`. This is useful if you're just interested in a specific dataset template or you want to make changes in the dataset settings and/or records. |
| 35 | + |
| 36 | + ```python |
| 37 | + dataset.to_hub(repo_id="<repo_id>", with_records=False) |
| 38 | + ``` |
| 39 | + |
| 40 | + |
| 41 | +### Pull an Argilla dataset from the Hugging Face Hub |
| 42 | + |
| 43 | +You can pull a dataset from the Hugging Face Hub to Argilla. This is useful if you want to restore a dataset and its configuration. You can pull the dataset from the Hugging Face Hub using the `rg.Dataset.from_hub` method. |
| 44 | + |
| 45 | +```python |
| 46 | + |
| 47 | +import argilla as rg |
| 48 | + |
| 49 | +client = rg.Argilla(api_url="<api_url>", api_key="<api_key>") |
| 50 | +dataset = rg.Dataset.from_hub(repo_id="<repo_id>") |
| 51 | +``` |
| 52 | + |
| 53 | +The `rg.Dataset.from_hub` method loads the configuration and records from the dataset repo. If you only want to load records, you can pass a `datasets.Dataset` object to the `rg.Dataset.log` method. This enables you to configure your own dataset and reuse existing Hub datasets. See the [guide on records](record.md) for more information. |
| 54 | + |
| 55 | +!!! note "With or without records" |
| 56 | + |
| 57 | + The example above will pull the dataset's `Settings` and records from the hub. If you only want to pull the dataset's configuration, you can set the `with_records` parameter to `False`. This is useful if you're just interested in a specific dataset template or you want to make changes in the dataset settings and/or records. |
| 58 | + |
| 59 | + ```python |
| 60 | + dataset = rg.Dataset.from_hub(repo_id="<repo_id>", with_records=False) |
| 61 | + ``` |
| 62 | + |
| 63 | + With the dataset's configuration you could then make changes to the dataset. For example, you could adapt the dataset's settings for a different task: |
| 64 | + |
| 65 | + ```python |
| 66 | + dataset.settings.questions = [rg.TextQuestion(name="answer")] |
| 67 | + ``` |
| 68 | + |
| 69 | + You could then log the dataset's records using the `load_dataset` method of the `datasets` package and pass the dataset to the `rg.Dataset.log` method. |
| 70 | + |
| 71 | + ```python |
| 72 | + hf_dataset = load_dataset("<repo_id>") |
| 73 | + dataset.log(hf_dataset) |
| 74 | + ``` |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | +### Saving an Argilla dataset to local disk |
| 79 | + |
| 80 | +You can save a dataset from Argilla to your local disk. This is useful if you want to back up your dataset. You can use the `rg.Dataset.to_disk` method. |
| 81 | + |
| 82 | +```python |
| 83 | +import argilla as rg |
| 84 | + |
| 85 | +client = rg.Argilla(api_url="<api_url>", api_key="<api_key>") |
| 86 | +dataset = client.datasets(name="my_dataset", workspace=workspace) |
| 87 | + |
| 88 | +dataset.to_disk(path="path/to/dataset") |
| 89 | +``` |
| 90 | + |
| 91 | +This will save the dataset's configuration and records to the specified path. If you only want to save the dataset's configuration, you can set the `with_records` parameter to `False`. |
| 92 | + |
| 93 | +```python |
| 94 | +dataset.to_disk(path="path/to/dataset", with_records=False) |
| 95 | +``` |
| 96 | + |
| 97 | +### Loading an Argilla dataset from local disk |
| 98 | + |
| 99 | +You can load a dataset from your local disk to Argilla. This is useful if you want to restore a dataset's configuration. You can use the `rg.Dataset.from_disk` method. |
| 100 | + |
| 101 | +```python |
| 102 | +import argilla as rg |
| 103 | + |
| 104 | +dataset = rg.Dataset.from_disk(path="path/to/dataset") |
| 105 | +``` |
| 106 | + |
| 107 | +!!! note "Directing the dataset to a workspace and name" |
| 108 | + You can also specify the workspace and name of the dataset when loading it from the disk. |
| 109 | + |
| 110 | + ```python |
| 111 | + dataset = rg.Dataset.from_disk(path="path/to/dataset", target_workspace=workspace, target_name="my_dataset") |
| 112 | + ``` |
| 113 | + |
| 114 | +## Export only records from Argilla Datasets |
| 115 | + |
| 116 | +The records alone can be exported from a dataset in Argilla. This is useful if you want to process the records in Python, export them to a different platform, or use them in model training. All of these methods use the `rg.Dataset.records` attribute. |
| 117 | + |
| 118 | +The records can be exported as a dictionary, a list of dictionaries, or to a `Dataset` of the `datasets` package. |
| 119 | + |
| 120 | + |
| 121 | +=== "To the `datasets` package" |
| 122 | + |
| 123 | + |
| 124 | + Records can be exported from `Dataset.records` to the `datasets` package. The `to_dataset` method can be used to export records to the `datasets` package. You can specify the name of the dataset and the split to export the records. |
| 125 | + |
| 126 | + ```python |
| 127 | + import argilla as rg |
| 128 | + |
| 129 | + client = rg.Argilla(api_url="<api_url>", api_key="<api_key>") |
| 130 | + dataset = client.datasets(name="my_dataset") |
| 131 | + |
| 132 | + # Export records as a dictionary |
| 133 | + exported_ds = dataset.records.to_datasets() |
| 134 | + ``` |
| 135 | + |
| 136 | +=== "To a Python dictionary" |
| 137 | + |
| 138 | + Records can be exported from `Dataset.records` as a dictionary. The `to_dict` method can be used to export records as a dictionary. You can specify the orientation of the dictionary output. You can also decide if to flatten or not the dictionary. |
| 139 | + |
| 140 | + ```python |
| 141 | + import argilla as rg |
| 142 | + |
| 143 | + client = rg.Argilla(api_url="<api_url>", api_key="<api_key>") |
| 144 | + dataset = client.datasets(name="my_dataset") |
| 145 | + |
| 146 | + # Export records as a dictionary |
| 147 | + exported_records = dataset.records.to_dict() |
| 148 | + # {'fields': [{'text': 'Hello'},{'text': 'World'}], suggestions': [{'label': {'value': 'positive'}}, {'label': {'value': 'negative'}}] |
| 149 | + |
| 150 | + # Export records as a dictionary with orient=index |
| 151 | + exported_records = dataset.records.to_dict(orient="index") |
| 152 | + # {"uuid": {'fields': {'text': 'Hello'}, 'suggestions': {'label': {'value': 'positive'}}}, {"uuid": {'fields': {'text': 'World'}, 'suggestions': {'label': {'value': 'negative'}}}, |
| 153 | + |
| 154 | + # Export records as a dictionary with flatten=false |
| 155 | + exported_records = dataset.records.to_dict(flatten=True) |
| 156 | + # {"text": ["Hello", "World"], "label.suggestion": ["greeting", "greeting"]} |
| 157 | + ``` |
| 158 | + |
| 159 | +=== "To a python list" |
| 160 | + |
| 161 | + Records can be exported from `Dataset.records` as a list of dictionaries. The `to_list` method can be used to export records as a list of dictionaries. You can decide if to flatten it or not. |
| 162 | + |
| 163 | + ```python |
| 164 | + import argilla as rg |
| 165 | + |
| 166 | + client = rg.Argilla(api_url="<api_url>", api_key="<api_key>") |
| 167 | + |
| 168 | + workspace = client.workspaces("my_workspace") |
| 169 | + |
| 170 | + dataset = client.datasets(name="my_dataset", workspace=workspace) |
| 171 | + |
| 172 | + # Export records as a list of dictionaries |
| 173 | + exported_records = dataset.records.to_list() |
| 174 | + # [{'fields': {'text': 'Hello'}, 'suggestion': {'label': {value: 'greeting'}}}, {'fields': {'text': 'World'}, 'suggestion': {'label': {value: 'greeting'}}}] |
| 175 | + |
| 176 | + # Export records as a list of dictionaries with flatten=False |
| 177 | + exported_records = dataset.records.to_list(flatten=True) |
| 178 | + # [{"text": "Hello", "label": "greeting"}, {"text": "World", "label": "greeting"}] |
| 179 | + ``` |
0 commit comments