Duy1332002 commited on
Commit
4260285
·
verified ·
1 Parent(s): 9ae54dc

Upload instruck500k_vi.py

Browse files

scrip load data instruct500k vi

Files changed (1) hide show
  1. instruck500k_vi.py +152 -0
instruck500k_vi.py ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import datasets
3
+ from huggingface_hub import HfFileSystem
4
+
5
+
6
+ logger = datasets.logging.get_logger(__name__)
7
+ fs = HfFileSystem()
8
+
9
+
10
+ _CITATION = """
11
+
12
+ """
13
+ _DESCRIPTION = """
14
+
15
+ """
16
+ _HOMEPAGE = "https://github.com/FPT-VVU/ViVidBot"
17
+ _REPO_URL = "https://huggingface.co/{}/resolve/main/datasets/Vividbot/instruct500k_vi"
18
+ _URLS = {
19
+ "meta": f"{_REPO_URL}/instruct500k_vi.json",
20
+ "image": f"{_REPO_URL}/images" + "{shard}.zip",
21
+ }
22
+
23
+ _CONFIGS = [
24
+ os.path.basename(file_name).split(".")[0]
25
+ for file_name in fs.listdir(_REPO_URL + "/images", detail=False)
26
+ if file_name.endswith(".zip")
27
+ ]
28
+
29
+ class Instruct500k_ViConfig(datasets.BuilderConfig):
30
+ """BuilderConfig for Vast2M_Vi."""
31
+
32
+ def __init__(self, **kwargs):
33
+ """
34
+ :param kwargs: Arguments.
35
+ """
36
+ super().__init__(
37
+ version=datasets.Version("1.0.0"),
38
+ description=_DESCRIPTION,
39
+ **kwargs,
40
+ )
41
+
42
+
43
+ class Instruck500k_Vi(datasets.GeneratorBasedBuilder):
44
+ """Vast2M Vi dataset."""
45
+
46
+ BUILDER_CONFIGS = Instruct500k_ViConfig()
47
+
48
+ def _info(self) -> datasets.DatasetInfo:
49
+ features = datasets.Features(
50
+ {
51
+ "id": datasets.Value("string"),
52
+ "image": datasets.Value("binary"),
53
+ "conversations": datasets.Value(
54
+ "dict",
55
+ ),
56
+ }
57
+ )
58
+
59
+ return datasets.DatasetInfo(
60
+ description=_DESCRIPTION,
61
+ features=features,
62
+ homepage=_HOMEPAGE,
63
+ citation=_CITATION,
64
+ )
65
+
66
+ def _split_generators(
67
+ self, dl_manager: datasets.DownloadManager
68
+ ) -> list[datasets.SplitGenerator]:
69
+ """
70
+ Get splits.
71
+ :param dl_manager: Download manager.
72
+ :return: Splits.
73
+ """
74
+
75
+ metadata_paths = dl_manager.download(_URLS["meta"])
76
+ dataset = datasets.load_dataset(
77
+ "json",
78
+ data_files=metadata_paths,
79
+ split="train",
80
+ )
81
+ dataset = dataset.train_test_split(test_size=0.1, shuffle=True, seed=42)
82
+ train_set = dataset["train"]
83
+ val_test_set = dataset["test"].train_test_split(test_size=0.5)
84
+ val_set = val_test_set["train"]
85
+ test_set = val_test_set["test"]
86
+
87
+ split_dict = {
88
+ datasets.Split.TRAIN: train_set,
89
+ datasets.Split.VALIDATION: val_set,
90
+ datasets.Split.TEST: test_set,
91
+ }
92
+
93
+ image_dirs = dl_manager.download_and_extract(
94
+ [_URLS["image"].format(shard=shard) for shard in _CONFIGS]
95
+ )
96
+ image_dict = {
97
+ shard: visual_dir
98
+ for shard, visual_dir in zip(_CONFIGS, image_dirs)
99
+ }
100
+
101
+ return [
102
+ datasets.SplitGenerator(
103
+ gen_kwargs={
104
+ "split": split,
105
+ "image_dict": image_dict,
106
+ },
107
+ )
108
+ for split in split_dict.items()
109
+ ]
110
+
111
+ def _generate_examples(
112
+ self,
113
+ split: datasets.Dataset,
114
+ image_dict: dict,
115
+ ) -> tuple[int, dict]:
116
+ """
117
+ Generate examples.
118
+ :param split: Split.
119
+ :param visual_dict: Paths to directory containing visual files.
120
+ :param audio_dict: Paths to directory containing audio files.
121
+ :param transcript_dict: Paths to directory containing transcripts.
122
+ :return: Example.
123
+ """
124
+ for i, sample in enumerate(split):
125
+ shard = sample["image"].split("/")[-1].split(".")[0]
126
+ image_path = os.path.join(
127
+ image_dict[shard], sample["image"]
128
+ )
129
+
130
+ yield i, {
131
+ "id": sample["id"],
132
+ "video": self.__get_binary_data(image_path),
133
+ "conversations": sample["conversations"],
134
+ }
135
+
136
+ def __get_binary_data(self, path: str) -> bytes:
137
+ """
138
+ Get binary data from path.
139
+ :param path: Path to file.
140
+ :return: Binary data.
141
+ """
142
+ with open(path, "rb") as f:
143
+ return f.read()
144
+
145
+ def __get_text_data(self, path: str) -> str:
146
+ """
147
+ Get transcript from path.
148
+ :param path: Path to transcript.
149
+ :return: Transcript.
150
+ """
151
+ with open(path, "r", encoding="utf-8") as f:
152
+ return f.read().strip()