voorhs commited on
Commit
de8f30c
·
verified ·
1 Parent(s): 5296c8d

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +52 -43
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
- # define utils
74
- import requests
75
  import json
76
- from autointent import Dataset
 
 
77
  from datasets import load_dataset
78
 
79
- def load_json_from_url(github_file: str):
80
- raw_text = requests.get(github_file).text
81
- return json.loads(raw_text)
82
-
83
- def convert_banking77(banking77_train, shots_per_intent, intent_names):
84
- all_labels = sorted(banking77_train.unique("label"))
85
- n_classes = len(intent_names)
86
- assert all_labels == list(range(n_classes))
87
-
88
- classwise_utterance_records = [[] for _ in range(n_classes)]
89
- intents = [
90
- {
91
- "id": i,
92
- "name": name,
93
- }
94
- for i, name in enumerate(intent_names)
95
- ]
96
-
97
- for b77_batch in banking77_train.iter(batch_size=16, drop_last_batch=False):
98
- for txt, intent_id in zip(b77_batch["text"], b77_batch["label"], strict=False):
99
- target_list = classwise_utterance_records[intent_id]
100
- if shots_per_intent is not None and len(target_list) >= shots_per_intent:
101
- continue
102
- target_list.append({"utterance": txt, "label": intent_id})
103
-
104
- utterances = [rec for lst in classwise_utterance_records for rec in lst]
105
- return Dataset.from_dict({"intents": intents, "train": utterances})
106
-
107
- # load
108
- file_url = "https://huggingface.co/datasets/PolyAI/banking77/resolve/main/dataset_infos.json"
109
- dataset_description = load_json_from_url(file_url)
110
- intent_names = dataset_description["default"]["features"]["label"]["names"]
111
- banking77 = load_dataset("PolyAI/banking77", trust_remote_code=True)
112
-
113
- # convert
114
- banking77_converted = convert_banking77(
115
- banking77["train"],
116
- shots_per_intent=None,
117
- intent_names=intent_names
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
  ```