Dominic28 commited on
Commit
90befd3
Β·
verified Β·
1 Parent(s): 832c219

Upload ThG-pages.ipynb

Browse files
Files changed (1) hide show
  1. ThG-pages.ipynb +235 -0
ThG-pages.ipynb ADDED
@@ -0,0 +1,235 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 3,
6
+ "id": "b8c2eeb2",
7
+ "metadata": {},
8
+ "outputs": [
9
+ {
10
+ "name": "stderr",
11
+ "output_type": "stream",
12
+ "text": [
13
+ "Setting num_proc from 1 back to 1 for the train split to disable multiprocessing as it only contains one shard.\n",
14
+ "Map: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 100/100 [00:00<00:00, 1466.36 examples/s]shards/s]\n",
15
+ "Creating parquet from Arrow format: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:00<00:00, 8.30ba/s]\n",
16
+ "Processing Files (1 / 1): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 39.3MB / 39.3MB, 3.02MB/s \n",
17
+ "\u001b[A\n",
18
+ "\u001b[A\n",
19
+ "Processing Files (1 / 1): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 39.3MB / 39.3MB, 2.99MB/s \n",
20
+ "New Data Upload: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 39.3MB / 39.3MB, 2.99MB/s \n",
21
+ "Validating: β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 100% \n",
22
+ "Uploading the dataset shards: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:04<00:00, 4.54s/ shards]\n"
23
+ ]
24
+ },
25
+ {
26
+ "data": {
27
+ "text/plain": [
28
+ "CommitInfo(commit_url='https://huggingface.co/datasets/divinity-library/theologie-der-gegenwart-pages/commit/e3d1dbb3b96611a0837db1b8e829c6785bbaef27', commit_message='Upload dataset', commit_description='', oid='e3d1dbb3b96611a0837db1b8e829c6785bbaef27', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/divinity-library/theologie-der-gegenwart-pages', endpoint='https://huggingface.co', repo_type='dataset', repo_id='divinity-library/theologie-der-gegenwart-pages'), pr_revision=None, pr_num=None)"
29
+ ]
30
+ },
31
+ "execution_count": 3,
32
+ "metadata": {},
33
+ "output_type": "execute_result"
34
+ }
35
+ ],
36
+ "source": [
37
+ "import tempfile\n",
38
+ "from pathlib import Path\n",
39
+ "\n",
40
+ "import fitz # pymupdf\n",
41
+ "from datasets import Dataset, Image\n",
42
+ "from huggingface_hub import HfApi, hf_hub_download\n",
43
+ "\n",
44
+ "REPO = \"divinity-library/theologie-der-gegenwart\"\n",
45
+ "TARGET = \"divinity-library/theologie-der-gegenwart-pages\"\n",
46
+ "MAX_PDFS = None # how many PDFs to process\n",
47
+ "MAX_PAGES = None # pages per PDF, None for all\n",
48
+ "\n",
49
+ "api = HfApi()\n",
50
+ "pdf_files = sorted(\n",
51
+ " f for f in api.list_repo_files(REPO, repo_type=\"dataset\")\n",
52
+ " if f.lower().endswith(\".pdf\") and f.startswith(\"train/\")\n",
53
+ ")[:MAX_PDFS]\n",
54
+ "\n",
55
+ "\n",
56
+ "out_dir = Path(tempfile.mkdtemp())\n",
57
+ "rows = []\n",
58
+ "for name in pdf_files:\n",
59
+ " pdf_path = Path(hf_hub_download(REPO, name, repo_type=\"dataset\"))\n",
60
+ " doc = fitz.open(pdf_path)\n",
61
+ " for i, page in enumerate(doc):\n",
62
+ " if MAX_PAGES and i >= MAX_PAGES:\n",
63
+ " break\n",
64
+ " img_path = out_dir / f\"{Path(name).stem}_{i:04d}.png\"\n",
65
+ " page.get_pixmap(dpi=200).save(img_path)\n",
66
+ " rows.append({\"image\": str(img_path), \"source\": name, \"page\": i + 1})\n",
67
+ "\n",
68
+ "ds = Dataset.from_list(rows).cast_column(\"image\", Image())\n",
69
+ "ds.push_to_hub(TARGET, private=True)"
70
+ ]
71
+ },
72
+ {
73
+ "cell_type": "code",
74
+ "execution_count": 4,
75
+ "id": "f07dd453",
76
+ "metadata": {},
77
+ "outputs": [
78
+ {
79
+ "name": "stderr",
80
+ "output_type": "stream",
81
+ "text": [
82
+ "Setting num_proc from 1 back to 1 for the train split to disable multiprocessing as it only contains one shard.\n",
83
+ "Map: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 6/6 [00:00<00:00, 330.13 examples/s]?, ? shards/s]\n",
84
+ "Creating parquet from Arrow format: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:00<00:00, 56.33ba/s]\n",
85
+ "Processing Files (1 / 1): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2.41MB / 2.41MB, ???B/s \n",
86
+ "\u001b[A\n",
87
+ "\u001b[A\n",
88
+ "\u001b[A\n",
89
+ "\u001b[A\n",
90
+ "Processing Files (1 / 1): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 2.41MB / 2.41MB, 233kB/s \n",
91
+ "New Data Upload: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 188kB / 188kB, 18.1kB/s \n",
92
+ "Validating: β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 100% \n",
93
+ "Uploading the dataset shards: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:00<00:00, 1.67 shards/s]\n"
94
+ ]
95
+ },
96
+ {
97
+ "data": {
98
+ "text/plain": [
99
+ "CommitInfo(commit_url='https://huggingface.co/datasets/divinity-library/theologie-der-gegenwart-test/commit/b7cbed9e2954a5098d01d94f4f4eb59e7a08e290', commit_message='Upload dataset', commit_description='', oid='b7cbed9e2954a5098d01d94f4f4eb59e7a08e290', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/divinity-library/theologie-der-gegenwart-test', endpoint='https://huggingface.co', repo_type='dataset', repo_id='divinity-library/theologie-der-gegenwart-test'), pr_revision=None, pr_num=None)"
100
+ ]
101
+ },
102
+ "execution_count": 4,
103
+ "metadata": {},
104
+ "output_type": "execute_result"
105
+ }
106
+ ],
107
+ "source": [
108
+ "import tempfile\n",
109
+ "from pathlib import Path\n",
110
+ "\n",
111
+ "import fitz # pymupdf\n",
112
+ "from datasets import Dataset, Image\n",
113
+ "from huggingface_hub import HfApi, hf_hub_download\n",
114
+ "\n",
115
+ "REPO = \"divinity-library/theologie-der-gegenwart\"\n",
116
+ "TARGET = \"divinity-library/theologie-der-gegenwart-test\"\n",
117
+ "MAX_PDFS = 2 # how many PDFs to process\n",
118
+ "MAX_PAGES = 5 # pages per PDF, None for all\n",
119
+ "\n",
120
+ "api = HfApi()\n",
121
+ "pdf_files = sorted(\n",
122
+ " f for f in api.list_repo_files(REPO, repo_type=\"dataset\")\n",
123
+ " if f.lower().endswith(\".pdf\") and f.startswith(\"train/\")\n",
124
+ ")[:MAX_PDFS]\n",
125
+ "\n",
126
+ "out_dir = Path(tempfile.mkdtemp())\n",
127
+ "rows = []\n",
128
+ "for name in pdf_files:\n",
129
+ " pdf_path = Path(hf_hub_download(REPO, name, repo_type=\"dataset\"))\n",
130
+ " doc = fitz.open(pdf_path)\n",
131
+ " for i, page in enumerate(doc):\n",
132
+ " if MAX_PAGES and i >= MAX_PAGES:\n",
133
+ " break\n",
134
+ " img_path = out_dir / f\"{Path(name).stem}_{i:04d}.png\"\n",
135
+ " page.get_pixmap(dpi=200).save(img_path)\n",
136
+ " rows.append({\"image\": str(img_path), \"source\": name, \"page\": i + 1})\n",
137
+ "\n",
138
+ "ds = Dataset.from_list(rows).cast_column(\"image\", Image())\n",
139
+ "ds.push_to_hub(TARGET, private=True)"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": 6,
145
+ "id": "8c806fcf",
146
+ "metadata": {},
147
+ "outputs": [
148
+ {
149
+ "name": "stderr",
150
+ "output_type": "stream",
151
+ "text": [
152
+ "c:\\Users\\domin\\AppData\\Local\\Python\\pythoncore-3.14-64\\Lib\\site-packages\\huggingface_hub\\file_download.py:149: UserWarning: `huggingface_hub` cache-system uses symlinks by default to efficiently store duplicated files but your machine does not support them in C:\\Users\\domin\\.cache\\huggingface\\hub\\datasets--divinity-library--theologie-der-gegenwart-pages. Caching files will still work but in a degraded version that might require more space on your disk. This warning can be disabled by setting the `HF_HUB_DISABLE_SYMLINKS_WARNING` environment variable. For more details, see https://huggingface.co/docs/huggingface_hub/how-to-cache#limitations.\n",
153
+ "To support symlinks on Windows, you either need to activate Developer Mode or to run Python as an administrator. In order to activate developer mode, see this article: https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development\n",
154
+ " warnings.warn(message)\n",
155
+ "Generating train split: 100 examples [00:00, 1039.31 examples/s]\n",
156
+ "Setting num_proc from 1 back to 1 for the train split to disable multiprocessing as it only contains one shard.\n",
157
+ "Creating parquet from Arrow format: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:00<00:00, 464.90ba/s]\n",
158
+ "Processing Files (1 / 1): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 160kB / 160kB, ???B/s \n",
159
+ "\u001b[A\n",
160
+ "\u001b[A\n",
161
+ "Processing Files (1 / 1): 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 160kB / 160kB, 15.7kB/s \n",
162
+ "New Data Upload: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 160kB / 160kB, 15.7kB/s \n",
163
+ "Validating: β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 100% \n",
164
+ "Uploading the dataset shards: 100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 1/1 [00:00<00:00, 2.26 shards/s]\n"
165
+ ]
166
+ },
167
+ {
168
+ "data": {
169
+ "text/plain": [
170
+ "CommitInfo(commit_url='https://huggingface.co/datasets/divinity-library/theologie-der-gegenwart/commit/55e2b5451b799cb47c8460820ebc690191f6b19b', commit_message='Upload dataset', commit_description='', oid='55e2b5451b799cb47c8460820ebc690191f6b19b', pr_url=None, repo_url=RepoUrl('https://huggingface.co/datasets/divinity-library/theologie-der-gegenwart', endpoint='https://huggingface.co', repo_type='dataset', repo_id='divinity-library/theologie-der-gegenwart'), pr_revision=None, pr_num=None)"
171
+ ]
172
+ },
173
+ "execution_count": 6,
174
+ "metadata": {},
175
+ "output_type": "execute_result"
176
+ }
177
+ ],
178
+ "source": [
179
+ "from collections import defaultdict\n",
180
+ "from datasets import Dataset, load_dataset\n",
181
+ "\n",
182
+ "SOURCE = \"divinity-library/theologie-der-gegenwart-pages\" # page-level dataset with OCR column\n",
183
+ "TARGET = \"divinity-library/theologie-der-gegenwart\"\n",
184
+ "OCR_COLUMN = \"markdown_glm_ocr\"\n",
185
+ "PAGE_MARKERS = True # insert <!-- page N --> between pages\n",
186
+ "\n",
187
+ "# Drop the image column so no images are decoded\n",
188
+ "ds = load_dataset(SOURCE, split=\"train\").select_columns([\"source\", \"page\", OCR_COLUMN])\n",
189
+ "\n",
190
+ "pages = defaultdict(list)\n",
191
+ "for row in ds:\n",
192
+ " pages[row[\"source\"]].append((row[\"page\"], row[OCR_COLUMN]))\n",
193
+ "\n",
194
+ "docs = []\n",
195
+ "for source, items in sorted(pages.items()):\n",
196
+ " items.sort(key=lambda x: x[0])\n",
197
+ " failed = [p for p, text in items if text == \"[OCR ERROR]\"]\n",
198
+ " if failed:\n",
199
+ " print(f\"WARNING {source}: OCR failed on pages {failed}\")\n",
200
+ " parts = [\n",
201
+ " (f\"<!-- page {p} -->\\n\" if PAGE_MARKERS else \"\") + text\n",
202
+ " for p, text in items\n",
203
+ " ]\n",
204
+ " docs.append({\n",
205
+ " \"source\": source,\n",
206
+ " \"num_pages\": len(items),\n",
207
+ " \"markdown\": \"\\n\\n\".join(parts),\n",
208
+ " })\n",
209
+ "\n",
210
+ "Dataset.from_list(docs).push_to_hub(TARGET, private=True)"
211
+ ]
212
+ }
213
+ ],
214
+ "metadata": {
215
+ "kernelspec": {
216
+ "display_name": "Python 3",
217
+ "language": "python",
218
+ "name": "python3"
219
+ },
220
+ "language_info": {
221
+ "codemirror_mode": {
222
+ "name": "ipython",
223
+ "version": 3
224
+ },
225
+ "file_extension": ".py",
226
+ "mimetype": "text/x-python",
227
+ "name": "python",
228
+ "nbconvert_exporter": "python",
229
+ "pygments_lexer": "ipython3",
230
+ "version": "3.14.7"
231
+ }
232
+ },
233
+ "nbformat": 4,
234
+ "nbformat_minor": 5
235
+ }