holylovenia
commited on
Commit
•
f3b1af6
1
Parent(s):
808459b
Upload thai_databricks_dolly.py with huggingface_hub
Browse files- thai_databricks_dolly.py +114 -0
thai_databricks_dolly.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
from typing import Dict, List, Tuple
|
3 |
+
|
4 |
+
import datasets
|
5 |
+
import pandas as pd
|
6 |
+
from datasets.download.download_manager import DownloadManager
|
7 |
+
|
8 |
+
from seacrowd.utils import schemas
|
9 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
10 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
11 |
+
|
12 |
+
# No paper citation found.
|
13 |
+
_CITATION = ""
|
14 |
+
|
15 |
+
_LOCAL = False
|
16 |
+
_LANGUAGES = ["tha"]
|
17 |
+
_DATASETNAME = "thai_databricks_dolly"
|
18 |
+
_DESCRIPTION = """\
|
19 |
+
This is a Thai-instructed dataset translated from databricks-dolly-15k using
|
20 |
+
Google Cloud Translation. databricks-dolly-15k is an open-source dataset of
|
21 |
+
instruction-following records generated by thousands of Databricks employees in
|
22 |
+
several behavioral categories outlined in the InstructGPT paper, including
|
23 |
+
brainstorming, classification, closed QA, generation, information extraction,
|
24 |
+
open QA, and summarization.
|
25 |
+
"""
|
26 |
+
|
27 |
+
_HOMEPAGE = "https://huggingface.co/datasets/Thaweewat/databricks-dolly-15k-th"
|
28 |
+
_LICENSE = Licenses.CC_BY_SA_3_0.value
|
29 |
+
_URL = "https://huggingface.co/datasets/Thaweewat/databricks-dolly-15k-th/resolve/main/databricks-dolly-15k-th.parquet"
|
30 |
+
_SUPPORTED_TASKS = [Tasks.INSTRUCTION_TUNING]
|
31 |
+
_SOURCE_VERSION = "1.0.0"
|
32 |
+
_SEACROWD_VERSION = "2024.06.20"
|
33 |
+
|
34 |
+
|
35 |
+
class ThaiDatabricksDollyDataset(datasets.GeneratorBasedBuilder):
|
36 |
+
"""Thai Databricks Dolly Dataset"""
|
37 |
+
|
38 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
39 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
40 |
+
|
41 |
+
SEACROWD_SCHEMA_NAME = "t2t"
|
42 |
+
|
43 |
+
BUILDER_CONFIGS = [
|
44 |
+
SEACrowdConfig(
|
45 |
+
name=f"{_DATASETNAME}_source",
|
46 |
+
version=SOURCE_VERSION,
|
47 |
+
description=f"{_DATASETNAME} source schema",
|
48 |
+
schema="source",
|
49 |
+
subset_id=_DATASETNAME,
|
50 |
+
),
|
51 |
+
SEACrowdConfig(
|
52 |
+
name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
53 |
+
version=SEACROWD_VERSION,
|
54 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
55 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
56 |
+
subset_id=_DATASETNAME,
|
57 |
+
),
|
58 |
+
]
|
59 |
+
|
60 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
61 |
+
|
62 |
+
def _info(self) -> datasets.DatasetInfo:
|
63 |
+
if self.config.schema == "source":
|
64 |
+
features = datasets.Features(
|
65 |
+
{
|
66 |
+
"instruction": datasets.Value("string"),
|
67 |
+
"context": datasets.Value("string"),
|
68 |
+
"response": datasets.Value("string"),
|
69 |
+
"category": datasets.Value("string"),
|
70 |
+
}
|
71 |
+
)
|
72 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
73 |
+
features = schemas.text2text_features
|
74 |
+
return datasets.DatasetInfo(
|
75 |
+
description=_DESCRIPTION,
|
76 |
+
features=features,
|
77 |
+
homepage=_HOMEPAGE,
|
78 |
+
license=_LICENSE,
|
79 |
+
citation=_CITATION,
|
80 |
+
)
|
81 |
+
|
82 |
+
def _split_generators(self, dl_manager: DownloadManager) -> List[datasets.SplitGenerator]:
|
83 |
+
"""Returns SplitGenerators."""
|
84 |
+
data_file = Path(dl_manager.download_and_extract(_URL))
|
85 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_file})]
|
86 |
+
|
87 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
88 |
+
"""Yield examples as (key, example) tuples"""
|
89 |
+
# pyarrow is an implicit dependency to load the parquet files
|
90 |
+
df = pd.read_parquet(filepath, engine="pyarrow")
|
91 |
+
for idx, row in df.iterrows():
|
92 |
+
instruction = row.get("instruction").strip()
|
93 |
+
context = row.get("context").strip()
|
94 |
+
response = row.get("response").strip()
|
95 |
+
category = row.get("category").strip()
|
96 |
+
if self.config.schema == "source":
|
97 |
+
example = {
|
98 |
+
"instruction": instruction,
|
99 |
+
"context": context,
|
100 |
+
"response": response,
|
101 |
+
"category": category,
|
102 |
+
}
|
103 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
104 |
+
text_1 = f"Context: {context}\n\n{instruction}" if context else instruction
|
105 |
+
text_2 = response
|
106 |
+
example = {
|
107 |
+
"id": str(idx),
|
108 |
+
"text_1": text_1,
|
109 |
+
"text_2": text_2,
|
110 |
+
"text_1_name": "context_and_instruction",
|
111 |
+
"text_2_name": "response",
|
112 |
+
}
|
113 |
+
|
114 |
+
yield idx, example
|