OleehyO commited on
Commit
9cae0ba
1 Parent(s): e8f9c45

Upload latex-formulas.py

Browse files
Files changed (1) hide show
  1. latex-formulas.py +86 -0
latex-formulas.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import datasets
2
+ import json
3
+
4
+
5
+ RAW_METADATA_URL = r'https://huggingface.co/datasets/OleehyO/latex-formulas/resolve/main/raw_formulas.jsonl'
6
+
7
+ FILTERED_METADATA_URL = r'https://huggingface.co/datasets/OleehyO/latex-formulas/resolve/main/formulas_finally.jsonl'
8
+ FILTERED_IMG_URL = r'https://huggingface.co/datasets/OleehyO/latex-formulas/resolve/main/formulas_pic.tar.gz'
9
+
10
+
11
+ class LatexFormulasConfig(datasets.BuilderConfig):
12
+ def __init__(self, img_archive_url, metadata_url, **kwargs):
13
+ super().__init__(**kwargs)
14
+ self.img_archive_url = img_archive_url
15
+ self.metadata_url = metadata_url
16
+
17
+
18
+ class LatexFormulas(datasets.GeneratorBasedBuilder):
19
+ BUILDER_CONFIGS = [
20
+ LatexFormulasConfig(
21
+ name="raw_formulas",
22
+ img_archive_url=None,
23
+ metadata_url=RAW_METADATA_URL
24
+ ),
25
+ LatexFormulasConfig(
26
+ name="filtered_formulas",
27
+ img_archive_url=FILTERED_IMG_URL,
28
+ metadata_url=FILTERED_METADATA_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 == "filtered_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
+ metadata_path = dl_manager.download(self.config.metadata_url)
49
+
50
+ if self.config.name == "filtered_formulas":
51
+ image_archive_path = self.config.img_archive_url
52
+ images = dl_manager.iter_archive(image_archive_path)
53
+ elif self.config.name == "raw_formulas":
54
+ images = None
55
+
56
+ return [
57
+ datasets.SplitGenerator(
58
+ name=datasets.Split.TRAIN,
59
+ gen_kwargs={
60
+ "images": images,
61
+ "metadata_path": metadata_path
62
+ }
63
+ )
64
+ ]
65
+
66
+ def _generate_examples(self, images, metadata_path):
67
+ if images is not None:
68
+ img_formula_pair = {}
69
+ with open(metadata_path, 'r', encoding="utf-8") as f:
70
+ for line in f:
71
+ single_json = json.loads(line)
72
+ img_formula_pair[single_json["id"]] = single_json["formula"]
73
+
74
+ for img_path, img_obj in images:
75
+ img_name = img_path.split('/')[-1]
76
+ if img_name in img_formula_pair:
77
+ yield img_path, {
78
+ "image": {"path": img_path, "bytes": img_obj.read()},
79
+ "latex_formula": img_formula_pair[img_name]
80
+ }
81
+ else:
82
+ with open(metadata_path, 'r', encoding="utf-8") as f:
83
+ for idx, line in enumerate(f):
84
+ yield idx, {
85
+ "latex_formula": json.loads(line)["formula"]
86
+ }