Add Hugging Face dataset loading script
Browse files
chasm.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
import datasets
|
| 6 |
+
from datasets.tasks import TextClassification, ImageClassification
|
| 7 |
+
|
| 8 |
+
_CITATION = """\
|
| 9 |
+
@misc{CHASM2023,
|
| 10 |
+
author = {Jingyi Zhen},
|
| 11 |
+
title = {CHASM: Covert Advertisement on RedNote},
|
| 12 |
+
year = {2023},
|
| 13 |
+
publisher = {Hugging Face},
|
| 14 |
+
howpublished = {https://huggingface.co/datasets/Jingyi77/CHASM-Covert_Advertisement_on_RedNote},
|
| 15 |
+
}
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
_DESCRIPTION = """\
|
| 19 |
+
A dataset containing posts from Xiaohongshu (RedNote) for text classification tasks,
|
| 20 |
+
specifically focused on identifying covert advertisements.
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
_HOMEPAGE = "https://huggingface.co/datasets/Jingyi77/CHASM-Covert_Advertisement_on_RedNote"
|
| 24 |
+
|
| 25 |
+
_LICENSE = "MIT"
|
| 26 |
+
|
| 27 |
+
_URLS = {
|
| 28 |
+
"train": "hf_format/train.json",
|
| 29 |
+
"validation": "hf_format/validation.json",
|
| 30 |
+
"test": "hf_format/test.json",
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
_FEATURES = datasets.Features({
|
| 34 |
+
"id": datasets.Value("string"),
|
| 35 |
+
"title": datasets.Value("string"),
|
| 36 |
+
"description": datasets.Value("string"),
|
| 37 |
+
"date": datasets.Value("string"),
|
| 38 |
+
"comments": datasets.Sequence(datasets.Value("string")),
|
| 39 |
+
"images": datasets.Sequence(datasets.Value("string")),
|
| 40 |
+
"label": datasets.ClassLabel(names=["non_advertisement", "advertisement"]),
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
class CHASMConfig(datasets.BuilderConfig):
|
| 44 |
+
"""BuilderConfig for CHASM dataset."""
|
| 45 |
+
|
| 46 |
+
def __init__(self, **kwargs):
|
| 47 |
+
"""BuilderConfig for CHASM.
|
| 48 |
+
Args:
|
| 49 |
+
**kwargs: keyword arguments forwarded to super.
|
| 50 |
+
"""
|
| 51 |
+
super(CHASMConfig, self).__init__(**kwargs)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class CHASM(datasets.GeneratorBasedBuilder):
|
| 55 |
+
"""CHASM: Covert Advertisement on RedNote Dataset."""
|
| 56 |
+
|
| 57 |
+
VERSION = datasets.Version("1.0.0")
|
| 58 |
+
BUILDER_CONFIGS = [
|
| 59 |
+
CHASMConfig(
|
| 60 |
+
name="chasm",
|
| 61 |
+
version=VERSION,
|
| 62 |
+
description="CHASM dataset for covert advertisement detection",
|
| 63 |
+
),
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
def _info(self):
|
| 67 |
+
return datasets.DatasetInfo(
|
| 68 |
+
description=_DESCRIPTION,
|
| 69 |
+
features=_FEATURES,
|
| 70 |
+
supervised_keys=None,
|
| 71 |
+
homepage=_HOMEPAGE,
|
| 72 |
+
license=_LICENSE,
|
| 73 |
+
citation=_CITATION,
|
| 74 |
+
task_templates=[
|
| 75 |
+
TextClassification(
|
| 76 |
+
text_column="description",
|
| 77 |
+
label_column="label",
|
| 78 |
+
),
|
| 79 |
+
ImageClassification(
|
| 80 |
+
image_column="images",
|
| 81 |
+
label_column="label",
|
| 82 |
+
),
|
| 83 |
+
],
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def _split_generators(self, dl_manager):
|
| 87 |
+
data_dir = os.path.dirname(os.path.abspath(__file__))
|
| 88 |
+
|
| 89 |
+
return [
|
| 90 |
+
datasets.SplitGenerator(
|
| 91 |
+
name=datasets.Split.TRAIN,
|
| 92 |
+
gen_kwargs={
|
| 93 |
+
"filepath": os.path.join(data_dir, _URLS["train"]),
|
| 94 |
+
"split": "train",
|
| 95 |
+
},
|
| 96 |
+
),
|
| 97 |
+
datasets.SplitGenerator(
|
| 98 |
+
name=datasets.Split.VALIDATION,
|
| 99 |
+
gen_kwargs={
|
| 100 |
+
"filepath": os.path.join(data_dir, _URLS["validation"]),
|
| 101 |
+
"split": "validation",
|
| 102 |
+
},
|
| 103 |
+
),
|
| 104 |
+
datasets.SplitGenerator(
|
| 105 |
+
name=datasets.Split.TEST,
|
| 106 |
+
gen_kwargs={
|
| 107 |
+
"filepath": os.path.join(data_dir, _URLS["test"]),
|
| 108 |
+
"split": "test",
|
| 109 |
+
},
|
| 110 |
+
),
|
| 111 |
+
]
|
| 112 |
+
|
| 113 |
+
def _generate_examples(self, filepath, split):
|
| 114 |
+
"""Generate CHASM examples."""
|
| 115 |
+
with open(filepath, encoding="utf-8") as f:
|
| 116 |
+
data = json.load(f)
|
| 117 |
+
for i, example in enumerate(data):
|
| 118 |
+
yield i, {
|
| 119 |
+
"id": example["id"],
|
| 120 |
+
"title": example["title"],
|
| 121 |
+
"description": example["description"],
|
| 122 |
+
"date": example["date"],
|
| 123 |
+
"comments": example["comments"],
|
| 124 |
+
"images": example["images"],
|
| 125 |
+
"label": example["label"],
|
| 126 |
+
}
|