-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_dataset.py
212 lines (190 loc) · 5.44 KB
/
prepare_dataset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import os
import pandas as pd
from nbformat import reads, NO_CONVERT
from tqdm import tqdm
from datasets import Dataset
from typing import Dict
from huggingface_hub import HfApi, create_repo
import tempfile
import subprocess
MIRROR_DIRECTORY = "fltk-1.4.x"
DATASET_ID = "fltk-1.4.x"
#SERIALIZE_IN_CHUNKS = 100
SERIALIZE_IN_CHUNKS = False
FEATHER_FORMAT = "ftr"
# TODO: export HF_TOKEN="hf_"
# Block the following formats.
IMAGE = ["png", "jpg", "jpeg", "gif", "ico", "xpm"]
VIDEO = ["mp4", "jfif"]
DOC = [
"dox",
"log",
"list",
"status",
"in",
"ac",
"sub",
"guess",
"spec",
"html",
"ANNOUNCEMENT",
"README",
"COPYING",
"VERSION",
"ChangeLog",
"requests",
"configure",
"Makefile",
"makedepend",
"makeinclude",
"htmlfooter",
"html_footer",
"update_config_scripts",
"fltk-config",
".clang-format",
"PkgInfo",
"mm",
"xbm",
"fl",
"0",
"man",
"key",
"PDF",
"pdf",
"docx",
"xlsx",
"pptx",
"cmake",
"cbp",
"exclude",
"ecrc",
"xml",
"desktop",
"json",
"md",
"txt",
"plist",
"rc",
"sh",
"yml",
"winmake",
"workspace",
"editorconfig",
"changelog",
"copyright",
"SConstruct",
"patch",
"diff",
]
AUDIO = ["flac", "ogg", "mid", "webm", "wav", "mp3"]
ARCHIVE = ["jar", "aar", "gz", "zip", "bz2"]
MODEL = ["onnx", "pickle", "model", "neuron"]
OTHERS = [
"npy",
"index",
"inv",
"index",
"DS_Store",
"rdb",
"pack",
"idx",
"glb",
"gltf",
"len",
"otf",
"unitypackage",
"ttf",
"xz",
"pcm",
"opus",
"ps1",
]
ANTI_FOMATS = tuple(IMAGE + VIDEO + DOC + AUDIO + ARCHIVE + OTHERS)
def upload_to_hub(file_format: str, repo_id: str):
"""Moves all the files matching `file_format` to a folder and
uploads the folder to the Hugging Face Hub."""
api = HfApi()
repo_id = create_repo(repo_id=repo_id, exist_ok=True, repo_type="dataset").repo_id
with tempfile.TemporaryDirectory() as tmpdirname:
os.makedirs(tmpdirname, 0o777, True)
command = f"mv *.{file_format} {tmpdirname}"
os.system(command)
#_ = subprocess.run(command.split())
api.upload_folder(repo_id=repo_id, folder_path=tmpdirname, repo_type="dataset")
def filter_code_cell(cell) -> bool:
"""Filters a code cell w.r.t shell commands, etc."""
only_shell = cell["source"].startswith("!")
only_magic = "%%capture" in cell["source"]
if only_shell or only_magic:
return False
else:
return True
def process_file(directory_name: str, file_path: str) -> Dict[str, str]:
"""Processes a single file."""
try:
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
if file_path.endswith("ipynb"):
# Code courtesy: Chansung Park and Sayak Paul.
code_cell_str = ""
notebook = reads(content, NO_CONVERT)
code_cells = [
c
for c in notebook["cells"]
if c["cell_type"] == "code"
if filter_code_cell(c)
]
for cell in code_cells:
code_cell_str += cell["source"]
content = code_cell_str
except Exception:
content = ""
return {
"repo_id": directory_name,
"file_path": file_path,
"content": content,
}
def read_repository_files(directory) -> pd.DataFrame:
"""Reads the files from the locally cloned repositories."""
file_paths = []
df = pd.DataFrame(columns=["repo_id", "file_path", "content"])
chunk_flag = 0
# Recursively find all files within the directory
for root, _, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
if not file_path.endswith(ANTI_FOMATS) and all(
k not in file_path for k in [".git", "__pycache__", "xcodeproj"]
):
file_paths.append((os.path.dirname(root), file_path))
# Process files sequentially.
print(f"Total file paths: {len(file_paths)}.")
print("Reading file contents...")
for i, (directory_name, file_path) in enumerate(tqdm(file_paths)):
file_content = process_file(directory_name, file_path)
if file_content["content"] != "":
temp_df = pd.DataFrame.from_dict([file_content])
df = pd.concat([df, temp_df])
if (
SERIALIZE_IN_CHUNKS
and len(df) != 0
and (len(df) % SERIALIZE_IN_CHUNKS == 0)
):
df_path = f"df_chunk_{chunk_flag}_{len(df)}.{FEATHER_FORMAT}"
print(f"Serializing dataframe to {df_path}...")
df.reset_index().to_feather(df_path)
del df
df = pd.DataFrame(columns=["repo_id", "file_path", "content"])
chunk_flag += 1
else:
print("content is empty")
return df
if __name__ == "__main__":
df = read_repository_files(MIRROR_DIRECTORY)
print("DataFrame created, creating dataset...")
upload_to_hub(file_format=FEATHER_FORMAT, repo_id=DATASET_ID)
print(f"{FEATHER_FORMAT} files uploaded to the Hub.")
if not SERIALIZE_IN_CHUNKS:
print(f"{FEATHER_FORMAT} files uploaded to the Hub 2.")
dataset = Dataset.from_pandas(df)
dataset.push_to_hub(DATASET_ID, private=True)