pszemraj commited on
Commit
b050418
·
verified ·
1 Parent(s): 69c2feb

Upload marker_pdf_conversion.ipynb

Browse files
Files changed (1) hide show
  1. marker_pdf_conversion.ipynb +315 -0
marker_pdf_conversion.ipynb ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "source": [
6
+ "# marker pdf conversion\n",
7
+ "\n",
8
+ "\n",
9
+ "- https://github.com/VikParuchuri/marker/tree/master\n",
10
+ "\n",
11
+ "\n",
12
+ "---"
13
+ ],
14
+ "metadata": {
15
+ "id": "PzogeOQ6rbvL"
16
+ }
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": null,
21
+ "metadata": {
22
+ "id": "lIYdn1woOS1n"
23
+ },
24
+ "outputs": [],
25
+ "source": [
26
+ "!git clone https://github.com/VikParuchuri/marker.git\n",
27
+ "%cd marker"
28
+ ]
29
+ },
30
+ {
31
+ "cell_type": "code",
32
+ "source": [
33
+ "!pip install -U -q pip ninja poetry"
34
+ ],
35
+ "metadata": {
36
+ "id": "YkbBuXMudX5g"
37
+ },
38
+ "execution_count": null,
39
+ "outputs": []
40
+ },
41
+ {
42
+ "cell_type": "code",
43
+ "source": [
44
+ "%%bash\n",
45
+ "set -e\n",
46
+ "sudo apt-get update\n",
47
+ "sudo apt-get install -y software-properties-common\n",
48
+ "sudo apt-get install -y build-essential python3 python3-pip git\n",
49
+ "sudo apt-get install -y python3-dev libssl-dev libc6-dev && sudo apt install zip unzip tmux nano p7zip-full git -y\n",
50
+ "clear\n",
51
+ "echo \"done\""
52
+ ],
53
+ "metadata": {
54
+ "id": "JzW38yGYhH3W"
55
+ },
56
+ "execution_count": null,
57
+ "outputs": []
58
+ },
59
+ {
60
+ "cell_type": "code",
61
+ "source": [
62
+ "# %%bash\n",
63
+ "!sudo apt update && sudo apt upgrade -y > /dev/null\n",
64
+ "!bash scripts/install/tesseract_5_install.sh\n",
65
+ "# !bash scripts/install/ghostscript_install.sh && clear\n",
66
+ "!sudo apt install ghostscript && clear\n",
67
+ "!cat scripts/install/apt-requirements.txt | xargs sudo apt install -y --allow-unauthenticated > LOG_installs_apt.log"
68
+ ],
69
+ "metadata": {
70
+ "id": "cDcjkP3hd_Kh"
71
+ },
72
+ "execution_count": null,
73
+ "outputs": []
74
+ },
75
+ {
76
+ "cell_type": "code",
77
+ "source": [
78
+ "%%writefile convert_pdf_dir.sh\n",
79
+ "#@title convert_pdf_dir.sh\n",
80
+ "#!/bin/bash\n",
81
+ "\n",
82
+ "# Default source directory is the current directory if not specified\n",
83
+ "SOURCE_DIR=${1:-$(pwd)}\n",
84
+ "# Default target directory is parent of source directory + marker-pdf2text\n",
85
+ "TARGET_DIR=${2:-$(dirname \"$(realpath $SOURCE_DIR)\")/marker-pdf2text}\n",
86
+ "\n",
87
+ "# Function to convert a single PDF file\n",
88
+ "convert_pdf() {\n",
89
+ " local source_pdf=$1\n",
90
+ " local target_md=$2\n",
91
+ " mkdir -p \"$(dirname \"$target_md\")\" # Ensure the target directory exists\n",
92
+ " poetry run python convert_single.py \"$source_pdf\" \"$target_md\"\n",
93
+ "}\n",
94
+ "\n",
95
+ "export -f convert_pdf\n",
96
+ "\n",
97
+ "# Find all PDF files and convert them\n",
98
+ "find \"$SOURCE_DIR\" -type f -name '*.pdf' -exec bash -c 'convert_pdf \"$0\" \"${1/pdf/md}\"' {} \"$TARGET_DIR/$(realpath --relative-to=\"$SOURCE_DIR\" \"{}\")\" \\;\n",
99
+ "\n",
100
+ "echo \"Conversion complete. All markdown files have been saved to $TARGET_DIR.\""
101
+ ],
102
+ "metadata": {
103
+ "cellView": "form",
104
+ "id": "aRi6CBR_ekIw"
105
+ },
106
+ "execution_count": null,
107
+ "outputs": []
108
+ },
109
+ {
110
+ "cell_type": "code",
111
+ "source": [
112
+ "!find / -name tessdata\n",
113
+ "!echo \"TESSDATA_PREFIX=/usr/share/tesseract-ocr/5/tessdata\" > local.env\n",
114
+ "!sudo rm *.lock\n",
115
+ "!poetry install -q"
116
+ ],
117
+ "metadata": {
118
+ "id": "TlQ6wV_Lfawh",
119
+ "colab": {
120
+ "base_uri": "https://localhost:8080/"
121
+ },
122
+ "outputId": "299eca48-79ba-41a3-95c1-7b5ffc657b14"
123
+ },
124
+ "execution_count": null,
125
+ "outputs": [
126
+ {
127
+ "output_type": "stream",
128
+ "name": "stdout",
129
+ "text": [
130
+ "\u001b[1A\u001b[0J \u001b[34;1m-\u001b[39;22m \u001b[39mInstalling \u001b[39m\u001b[36mtorch\u001b[39m\u001b[39m (\u001b[39m\u001b[39;1m2.3.0\u001b[39;22m\u001b[39m)\u001b[39m: \u001b[34mInstalling...\u001b[39m\n",
131
+ "\u001b[1A\u001b[0J \u001b[32;1m-\u001b[39;22m \u001b[39mInstalling \u001b[39m\u001b[36mtorch\u001b[39m\u001b[39m (\u001b[39m\u001b[32m2.3.0\u001b[39m\u001b[39m)\u001b[39m\n",
132
+ "\n",
133
+ "\u001b[34mWriting lock file\u001b[39m\n",
134
+ "\n",
135
+ "\u001b[39;1mInstalling\u001b[39;22m the current project: \u001b[36mmarker-pdf\u001b[39m (\u001b[39;1m0.1.3\u001b[39;22m)\u001b[1G\u001b[2K\u001b[39;1mInstalling\u001b[39;22m the current project: \u001b[36mmarker-pdf\u001b[39m (\u001b[32m0.1.3\u001b[39m)\n"
136
+ ]
137
+ }
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "markdown",
142
+ "source": [
143
+ "---"
144
+ ],
145
+ "metadata": {
146
+ "id": "jNOFjXVJSz2L"
147
+ }
148
+ },
149
+ {
150
+ "cell_type": "markdown",
151
+ "source": [
152
+ "## from dropbox\n"
153
+ ],
154
+ "metadata": {
155
+ "id": "yn0qVN93V0v6"
156
+ }
157
+ },
158
+ {
159
+ "cell_type": "code",
160
+ "source": [
161
+ "URL = \"https://www.dropbox.com/scl/fo/kbbliz7or5ptwc1c7vgb9/AF0b76tmbY3BuxHDnMzNO1E?rlkey=x5q3r7o9wdob85xjazjanloe3&dl=1\" # @param {type:\"string\"}\n",
162
+ "!wget -q -O data.zip $URL\n",
163
+ "!unzip data.zip -d /content/pdf-textbooks\n",
164
+ "!rm data.zip && clear"
165
+ ],
166
+ "metadata": {
167
+ "id": "YwnblF4_iZuu"
168
+ },
169
+ "execution_count": null,
170
+ "outputs": []
171
+ },
172
+ {
173
+ "cell_type": "code",
174
+ "source": [
175
+ "# !bash convert_pdf_dir.sh /content/pdf-textbooks"
176
+ ],
177
+ "metadata": {
178
+ "id": "MmkB5ix-imgH"
179
+ },
180
+ "execution_count": null,
181
+ "outputs": []
182
+ },
183
+ {
184
+ "cell_type": "code",
185
+ "source": [
186
+ "!DEFAULT_LANG=\"en\" TORCH_DEVICE=cuda poetry run python convert.py\\\n",
187
+ " /content/pdf-textbooks/python-book \\\n",
188
+ " /content/marker-md2text/python-book \\\n",
189
+ " --workers 6 --min_length 100"
190
+ ],
191
+ "metadata": {
192
+ "id": "GWZURJALmDtV"
193
+ },
194
+ "execution_count": null,
195
+ "outputs": []
196
+ },
197
+ {
198
+ "cell_type": "code",
199
+ "source": [
200
+ "out_file=\"/content/archived-all-marker.7z\"\n",
201
+ "!find /content/marker-md2text -type f -name \"*.json\" -print -delete # fuck off\n",
202
+ "!7z a $out_file /content/marker-md2text"
203
+ ],
204
+ "metadata": {
205
+ "id": "c5xndco3mJdq"
206
+ },
207
+ "execution_count": null,
208
+ "outputs": []
209
+ },
210
+ {
211
+ "cell_type": "markdown",
212
+ "source": [
213
+ "## napierone"
214
+ ],
215
+ "metadata": {
216
+ "id": "qybBz9Oqp2pH"
217
+ }
218
+ },
219
+ {
220
+ "cell_type": "code",
221
+ "source": [
222
+ "target_dir = '/content/napierone-epub'\n",
223
+ "!wget -q http://napierone.com.s3-eu-north-1.amazonaws.com/NapierOne/Data/EPUB/EPUB-total.zip\n",
224
+ "!unzip -o -q EPUB-total.zip -d $target_dir\n",
225
+ "!ls $target_dir | wc -l\n",
226
+ "!rm *.zip"
227
+ ],
228
+ "metadata": {
229
+ "id": "jhB5lOA6n8f5"
230
+ },
231
+ "execution_count": null,
232
+ "outputs": []
233
+ },
234
+ {
235
+ "cell_type": "code",
236
+ "source": [
237
+ "!DEFAULT_LANG=\"en\" TORCH_DEVICE=cuda poetry run python convert.py\\\n",
238
+ " $target_dir \\\n",
239
+ " /content/marker-napierone-file2text/EPUB \\\n",
240
+ " --workers 6 --min_length 300"
241
+ ],
242
+ "metadata": {
243
+ "colab": {
244
+ "base_uri": "https://localhost:8080/"
245
+ },
246
+ "id": "Poi65wYSpryu",
247
+ "outputId": "8a20c15e-ae37-4be3-85e8-be3dea88afb8"
248
+ },
249
+ "execution_count": null,
250
+ "outputs": [
251
+ {
252
+ "metadata": {
253
+ "tags": null
254
+ },
255
+ "name": "stdout",
256
+ "output_type": "stream",
257
+ "text": [
258
+ "2024-05-08 22:37:05,427\tINFO worker.py:1749 -- Started a local Ray instance.\n",
259
+ "Loaded texify model to cuda with torch.float16 dtype\n",
260
+ "Converting 5000 pdfs in chunk 1/1 with 6 processes, and storing in /content/marker-napierone-file2text/EPUB\n",
261
+ "100% 4999/5000 [14:01:52<00:08, 8.66s/it]"
262
+ ]
263
+ }
264
+ ]
265
+ },
266
+ {
267
+ "cell_type": "code",
268
+ "source": [
269
+ "out_file=\"/content/archived-marker-napierone-epub.7z\"\n",
270
+ "!find /content/marker-napierone-file2text -type f -name \"*.json\" -print -delete # fuck off\n",
271
+ "!7z a $out_file /content/marker-napierone-file2text"
272
+ ],
273
+ "metadata": {
274
+ "id": "sU8QpnNStjFn"
275
+ },
276
+ "execution_count": null,
277
+ "outputs": []
278
+ },
279
+ {
280
+ "cell_type": "code",
281
+ "source": [
282
+ "from google.colab import files\n",
283
+ "files.download(out_file)"
284
+ ],
285
+ "metadata": {
286
+ "id": "iyzMPMmtqAzI"
287
+ },
288
+ "execution_count": null,
289
+ "outputs": []
290
+ },
291
+ {
292
+ "cell_type": "code",
293
+ "source": [],
294
+ "metadata": {
295
+ "id": "CiYoo9ZEtrEz"
296
+ },
297
+ "execution_count": null,
298
+ "outputs": []
299
+ }
300
+ ],
301
+ "metadata": {
302
+ "colab": {
303
+ "provenance": [],
304
+ "gpuType": "L4",
305
+ "machine_shape": "hm"
306
+ },
307
+ "kernelspec": {
308
+ "display_name": "Python 3",
309
+ "name": "python3"
310
+ },
311
+ "accelerator": "GPU"
312
+ },
313
+ "nbformat": 4,
314
+ "nbformat_minor": 0
315
+ }