nouamanetazi HF staff commited on
Commit
0fd18e2
1 Parent(s): 4108347

Upload prepare_data.py

Browse files
Files changed (1) hide show
  1. prepare_data.py +22 -0
prepare_data.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datasets import load_dataset
2
+ import json
3
+ import os
4
+ from huggingface_hub import create_repo, upload_file
5
+
6
+ dataset = load_dataset("banking77")
7
+ id2label = dataset['train'].features['label'].names
8
+
9
+ repo_name = "banking77"
10
+ create_repo(repo_name, organization="mteb", repo_type="dataset")
11
+ for split in ['train', 'test']:
12
+ savepath = f'{os.path.dirname(__file__)}/{split}.jsonl'
13
+ with open(savepath, 'w') as fOut:
14
+ rows = list(dataset[split])
15
+ if split == 'test' and 'validation' in dataset:
16
+ rows += list(dataset['validation'])
17
+
18
+ for id, row in rows:
19
+ fOut.write(json.dumps({'text': row['text'], 'label': row['label'], 'label_text': id2label[row['label']]})+"\n")
20
+
21
+ upload_file(savepath, path_in_repo=f"{split}.jsonl", repo_id="mteb/" + repo_name, repo_type="dataset")
22
+ os.system(f"rm {savepath}")