Update README.md
Browse files
README.md
CHANGED
|
@@ -70,50 +70,59 @@ banking77 = Dataset.from_datasets("AutoIntent/banking77")
|
|
| 70 |
This dataset is taken from `PolyAI/banking77` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
| 71 |
|
| 72 |
```python
|
| 73 |
-
|
| 74 |
-
|
| 75 |
import json
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
from datasets import load_dataset
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
def
|
| 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 |
```
|
|
|
|
| 70 |
This dataset is taken from `PolyAI/banking77` and formatted with our [AutoIntent Library](https://deeppavlov.github.io/AutoIntent/index.html):
|
| 71 |
|
| 72 |
```python
|
| 73 |
+
"""Convert events dataset to autointent internal format and scheme."""
|
| 74 |
+
|
| 75 |
import json
|
| 76 |
+
|
| 77 |
+
import requests
|
| 78 |
+
from datasets import Dataset as HFDataset
|
| 79 |
from datasets import load_dataset
|
| 80 |
|
| 81 |
+
from autointent import Dataset
|
| 82 |
+
from autointent.schemas import Intent, Sample
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def get_intents_data(github_file: str | None = None) -> list[Intent]:
|
| 86 |
+
"""Load specific json from HF repo."""
|
| 87 |
+
github_file = github_file or "https://huggingface.co/datasets/PolyAI/banking77/resolve/main/dataset_infos.json"
|
| 88 |
+
raw_text = requests.get(github_file, timeout=5).text
|
| 89 |
+
dataset_description = json.loads(raw_text)
|
| 90 |
+
intent_names = dataset_description["default"]["features"]["label"]["names"]
|
| 91 |
+
return [Intent(id=i, name=name) for i, name in enumerate(intent_names)]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def convert_banking77(
|
| 95 |
+
banking77_split: HFDataset, intents_data: list[Intent], shots_per_intent: int | None = None
|
| 96 |
+
) -> list[Sample]:
|
| 97 |
+
"""Convert one split into desired format."""
|
| 98 |
+
all_labels = sorted(banking77_split.unique("label"))
|
| 99 |
+
|
| 100 |
+
n_classes = len(intents_data)
|
| 101 |
+
if all_labels != list(range(n_classes)):
|
| 102 |
+
msg = "Something's wrong"
|
| 103 |
+
raise ValueError(msg)
|
| 104 |
+
|
| 105 |
+
classwise_samples = [[] for _ in range(n_classes)]
|
| 106 |
+
|
| 107 |
+
for sample in banking77_split:
|
| 108 |
+
target_list = classwise_samples[sample["label"]]
|
| 109 |
+
if shots_per_intent is not None and len(target_list) >= shots_per_intent:
|
| 110 |
+
continue
|
| 111 |
+
target_list.append(Sample(utterance=sample["text"], label=sample["label"]))
|
| 112 |
+
|
| 113 |
+
samples = [sample for samples_from_one_class in classwise_samples for sample in samples_from_one_class]
|
| 114 |
+
print(f"{len(samples)=}")
|
| 115 |
+
return samples
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
intents_data = get_intents_data()
|
| 120 |
+
banking77 = load_dataset("PolyAI/banking77", trust_remote_code=True)
|
| 121 |
+
|
| 122 |
+
train_samples = convert_banking77(banking77["train"], intents_data=intents_data)
|
| 123 |
+
test_samples = convert_banking77(banking77["test"], intents_data=intents_data)
|
| 124 |
+
|
| 125 |
+
banking77_converted = Dataset.from_dict(
|
| 126 |
+
{"train": train_samples, "test": test_samples, "intents": intents_data}
|
| 127 |
+
)
|
| 128 |
```
|