Datasets:
Delete latex-formulas.py with huggingface_hub
Browse files- latex-formulas.py +0 -110
latex-formulas.py
DELETED
@@ -1,110 +0,0 @@
|
|
1 |
-
from io import BytesIO
|
2 |
-
from PIL import Image
|
3 |
-
from pathlib import Path
|
4 |
-
import datasets
|
5 |
-
import json
|
6 |
-
|
7 |
-
|
8 |
-
RAW_METADATA_URL = r'https://huggingface.co/datasets/OleehyO/latex-formulas/resolve/main/raw_formulas.jsonl'
|
9 |
-
|
10 |
-
# DIR_URL = r'https://huggingface.co/datasets/OleehyO/latex-formulas/resolve/main/data.tar.gz'
|
11 |
-
DIR_URL = r'https://huggingface.co/datasets/OleehyO/latex-formulas/resolve/main/data1.tar.gz'
|
12 |
-
|
13 |
-
|
14 |
-
class LatexFormulasConfig(datasets.BuilderConfig):
|
15 |
-
def __init__(self, data_url, **kwargs):
|
16 |
-
super().__init__(**kwargs)
|
17 |
-
self.data_url = data_url
|
18 |
-
|
19 |
-
|
20 |
-
class LatexFormulas(datasets.GeneratorBasedBuilder):
|
21 |
-
BUILDER_CONFIGS = [
|
22 |
-
LatexFormulasConfig(
|
23 |
-
name="raw_formulas",
|
24 |
-
data_url=RAW_METADATA_URL
|
25 |
-
),
|
26 |
-
LatexFormulasConfig(
|
27 |
-
name="cleaned_formulas",
|
28 |
-
data_url=DIR_URL
|
29 |
-
)
|
30 |
-
]
|
31 |
-
|
32 |
-
def _info(self):
|
33 |
-
if self.config.name == "raw_formulas":
|
34 |
-
return datasets.DatasetInfo(
|
35 |
-
features=datasets.Features({
|
36 |
-
"latex_formula": datasets.Value("string")
|
37 |
-
})
|
38 |
-
)
|
39 |
-
if self.config.name == "cleaned_formulas":
|
40 |
-
return datasets.DatasetInfo(
|
41 |
-
features=datasets.Features({
|
42 |
-
"image": datasets.Image(),
|
43 |
-
"latex_formula": datasets.Value("string")
|
44 |
-
})
|
45 |
-
)
|
46 |
-
|
47 |
-
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
48 |
-
if self.config.name == 'raw_formulas':
|
49 |
-
data_path = dl_manager.download(self.config.data_url)
|
50 |
-
return [
|
51 |
-
datasets.SplitGenerator(
|
52 |
-
name=datasets.Split.TRAIN,
|
53 |
-
gen_kwargs={
|
54 |
-
"data_path": data_path
|
55 |
-
}
|
56 |
-
)
|
57 |
-
]
|
58 |
-
|
59 |
-
if self.config.name == "cleaned_formulas":
|
60 |
-
# dir_path = Path(data_path)
|
61 |
-
# dir_path = Path(dl_manager.download_and_extract(data_path))
|
62 |
-
dir_path = Path(dl_manager.download_and_extract(self.config.data_url)) / 'common_formulas'
|
63 |
-
assert dir_path.is_dir()
|
64 |
-
|
65 |
-
return [
|
66 |
-
datasets.SplitGenerator(
|
67 |
-
name=datasets.Split.TRAIN,
|
68 |
-
gen_kwargs={
|
69 |
-
'dir_path': dir_path,
|
70 |
-
'dl_manager': dl_manager
|
71 |
-
}
|
72 |
-
)
|
73 |
-
]
|
74 |
-
|
75 |
-
def _generate_examples(self, data_path=None, dir_path: Path=None, dl_manager=None):
|
76 |
-
if self.config.name == 'cleaned_formulas':
|
77 |
-
idx = 0
|
78 |
-
for directory in dir_path.iterdir():
|
79 |
-
if not directory.is_dir():
|
80 |
-
continue
|
81 |
-
if not directory.name.startswith('process'):
|
82 |
-
continue
|
83 |
-
image_path = str(directory / "compressed_img.tar.gz")
|
84 |
-
metadata_path = str(directory / "tokenized_finally.jsonl")
|
85 |
-
images = dl_manager.iter_archive(image_path)
|
86 |
-
|
87 |
-
img_formula_pair = {}
|
88 |
-
with open(metadata_path, 'r', encoding='utf-8') as f:
|
89 |
-
for line in f:
|
90 |
-
single_json = json.loads(line)
|
91 |
-
img_formula_pair[single_json['id']] = single_json['formula']
|
92 |
-
|
93 |
-
for img_path, img_obj in images:
|
94 |
-
img_name = img_path.split('/')[-1]
|
95 |
-
|
96 |
-
if img_name in img_formula_pair:
|
97 |
-
idx += 1
|
98 |
-
# yield idx, {
|
99 |
-
yield str(directory) + img_path, {
|
100 |
-
"image": {"path": img_path, "bytes": img_obj.read()},
|
101 |
-
"latex_formula": img_formula_pair[img_name]
|
102 |
-
}
|
103 |
-
|
104 |
-
if self.config.name == 'raw_formulas':
|
105 |
-
assert data_path is not None
|
106 |
-
with open(data_path, 'r', encoding="utf-8") as f:
|
107 |
-
for idx, line in enumerate(f):
|
108 |
-
yield idx, {
|
109 |
-
"latex_formula": json.loads(line)["formula"]
|
110 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|