Upload armeme_loader.py with huggingface_hub
Browse files- armeme_loader.py +69 -0
armeme_loader.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import datasets
|
| 4 |
+
from datasets import Dataset, DatasetDict, load_dataset, Features, Value, Image, ClassLabel
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# Define the paths to your dataset
|
| 8 |
+
image_root_dir = "./"
|
| 9 |
+
train_jsonl_file_path = "arabic_memes_categorization_train.jsonl"
|
| 10 |
+
dev_jsonl_file_path = "arabic_memes_categorization_dev.jsonl"
|
| 11 |
+
test_jsonl_file_path = "arabic_memes_categorization_test.jsonl"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Function to load each dataset split
|
| 16 |
+
def load_armeme_split(jsonl_file_path, image_root_dir):
|
| 17 |
+
texts = []
|
| 18 |
+
images = []
|
| 19 |
+
ids=[]
|
| 20 |
+
class_labels=[]
|
| 21 |
+
image_file_paths = []
|
| 22 |
+
|
| 23 |
+
# Load JSONL file
|
| 24 |
+
with open(jsonl_file_path, 'r') as f:
|
| 25 |
+
for line in f:
|
| 26 |
+
item = json.loads(line)
|
| 27 |
+
ids.append(item['id'])
|
| 28 |
+
texts.append(item['text'])
|
| 29 |
+
image_file_path = os.path.join(image_root_dir, item['img_path'])
|
| 30 |
+
images.append(image_file_path)
|
| 31 |
+
image_file_paths.append(image_file_path)
|
| 32 |
+
class_labels.append(item['class_label'])
|
| 33 |
+
|
| 34 |
+
# Create a dictionary to match the dataset structure
|
| 35 |
+
data_dict = {
|
| 36 |
+
'id':ids,
|
| 37 |
+
'text': texts,
|
| 38 |
+
'image': images,
|
| 39 |
+
'img_path': image_file_paths,
|
| 40 |
+
'class_label': class_labels
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Define the features
|
| 44 |
+
features = Features({
|
| 45 |
+
'id': Value('string'),
|
| 46 |
+
'text': Value('string'),
|
| 47 |
+
'image': Image(),
|
| 48 |
+
'img_path': Value('string'),
|
| 49 |
+
'class_label': ClassLabel(names=['not_propaganda','propaganda','not-meme','other'])
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
# Create a Hugging Face dataset from the dictionary
|
| 53 |
+
dataset = Dataset.from_dict(data_dict, features=features)
|
| 54 |
+
return dataset
|
| 55 |
+
|
| 56 |
+
# Load each split
|
| 57 |
+
train_dataset = load_armeme_split(train_jsonl_file_path, image_root_dir)
|
| 58 |
+
dev_dataset = load_armeme_split(dev_jsonl_file_path, image_root_dir)
|
| 59 |
+
test_dataset = load_armeme_split(test_jsonl_file_path, image_root_dir)
|
| 60 |
+
|
| 61 |
+
# Create a DatasetDict
|
| 62 |
+
dataset_dict = DatasetDict({
|
| 63 |
+
'train': train_dataset,
|
| 64 |
+
'dev': dev_dataset,
|
| 65 |
+
'test': test_dataset
|
| 66 |
+
})
|
| 67 |
+
|
| 68 |
+
# Push the dataset to Hugging Face Hub
|
| 69 |
+
dataset_dict.push_to_hub("QCRI/ArMeme")
|