File size: 9,268 Bytes
b050418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# marker pdf conversion\n",
        "\n",
        "\n",
        "- https://github.com/VikParuchuri/marker/tree/master\n",
        "\n",
        "\n",
        "---"
      ],
      "metadata": {
        "id": "PzogeOQ6rbvL"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "lIYdn1woOS1n"
      },
      "outputs": [],
      "source": [
        "!git clone https://github.com/VikParuchuri/marker.git\n",
        "%cd marker"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -U -q pip ninja poetry"
      ],
      "metadata": {
        "id": "YkbBuXMudX5g"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "%%bash\n",
        "set -e\n",
        "sudo apt-get update\n",
        "sudo apt-get install -y software-properties-common\n",
        "sudo apt-get install -y build-essential python3 python3-pip git\n",
        "sudo apt-get install -y python3-dev libssl-dev libc6-dev && sudo apt install zip unzip tmux nano p7zip-full git -y\n",
        "clear\n",
        "echo \"done\""
      ],
      "metadata": {
        "id": "JzW38yGYhH3W"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# %%bash\n",
        "!sudo apt update && sudo apt upgrade -y > /dev/null\n",
        "!bash scripts/install/tesseract_5_install.sh\n",
        "# !bash scripts/install/ghostscript_install.sh && clear\n",
        "!sudo apt install ghostscript && clear\n",
        "!cat scripts/install/apt-requirements.txt | xargs sudo apt install -y --allow-unauthenticated > LOG_installs_apt.log"
      ],
      "metadata": {
        "id": "cDcjkP3hd_Kh"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "%%writefile convert_pdf_dir.sh\n",
        "#@title convert_pdf_dir.sh\n",
        "#!/bin/bash\n",
        "\n",
        "# Default source directory is the current directory if not specified\n",
        "SOURCE_DIR=${1:-$(pwd)}\n",
        "# Default target directory is parent of source directory + marker-pdf2text\n",
        "TARGET_DIR=${2:-$(dirname \"$(realpath $SOURCE_DIR)\")/marker-pdf2text}\n",
        "\n",
        "# Function to convert a single PDF file\n",
        "convert_pdf() {\n",
        "    local source_pdf=$1\n",
        "    local target_md=$2\n",
        "    mkdir -p \"$(dirname \"$target_md\")\" # Ensure the target directory exists\n",
        "    poetry run python convert_single.py \"$source_pdf\" \"$target_md\"\n",
        "}\n",
        "\n",
        "export -f convert_pdf\n",
        "\n",
        "# Find all PDF files and convert them\n",
        "find \"$SOURCE_DIR\" -type f -name '*.pdf' -exec bash -c 'convert_pdf \"$0\" \"${1/pdf/md}\"' {} \"$TARGET_DIR/$(realpath --relative-to=\"$SOURCE_DIR\" \"{}\")\" \\;\n",
        "\n",
        "echo \"Conversion complete. All markdown files have been saved to $TARGET_DIR.\""
      ],
      "metadata": {
        "cellView": "form",
        "id": "aRi6CBR_ekIw"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!find / -name tessdata\n",
        "!echo \"TESSDATA_PREFIX=/usr/share/tesseract-ocr/5/tessdata\" > local.env\n",
        "!sudo rm *.lock\n",
        "!poetry install -q"
      ],
      "metadata": {
        "id": "TlQ6wV_Lfawh",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "299eca48-79ba-41a3-95c1-7b5ffc657b14"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\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",
            "\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",
            "\n",
            "\u001b[34mWriting lock file\u001b[39m\n",
            "\n",
            "\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"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "---"
      ],
      "metadata": {
        "id": "jNOFjXVJSz2L"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## from dropbox\n"
      ],
      "metadata": {
        "id": "yn0qVN93V0v6"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "URL = \"https://www.dropbox.com/scl/fo/kbbliz7or5ptwc1c7vgb9/AF0b76tmbY3BuxHDnMzNO1E?rlkey=x5q3r7o9wdob85xjazjanloe3&dl=1\" # @param {type:\"string\"}\n",
        "!wget -q -O data.zip $URL\n",
        "!unzip data.zip -d /content/pdf-textbooks\n",
        "!rm data.zip && clear"
      ],
      "metadata": {
        "id": "YwnblF4_iZuu"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# !bash convert_pdf_dir.sh /content/pdf-textbooks"
      ],
      "metadata": {
        "id": "MmkB5ix-imgH"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!DEFAULT_LANG=\"en\" TORCH_DEVICE=cuda poetry run python convert.py\\\n",
        "    /content/pdf-textbooks/python-book \\\n",
        "    /content/marker-md2text/python-book \\\n",
        "    --workers 6 --min_length 100"
      ],
      "metadata": {
        "id": "GWZURJALmDtV"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "out_file=\"/content/archived-all-marker.7z\"\n",
        "!find /content/marker-md2text -type f -name \"*.json\" -print -delete # fuck off\n",
        "!7z a $out_file /content/marker-md2text"
      ],
      "metadata": {
        "id": "c5xndco3mJdq"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "## napierone"
      ],
      "metadata": {
        "id": "qybBz9Oqp2pH"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "target_dir = '/content/napierone-epub'\n",
        "!wget -q http://napierone.com.s3-eu-north-1.amazonaws.com/NapierOne/Data/EPUB/EPUB-total.zip\n",
        "!unzip -o -q EPUB-total.zip -d $target_dir\n",
        "!ls $target_dir | wc -l\n",
        "!rm *.zip"
      ],
      "metadata": {
        "id": "jhB5lOA6n8f5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!DEFAULT_LANG=\"en\" TORCH_DEVICE=cuda poetry run python convert.py\\\n",
        "    $target_dir \\\n",
        "    /content/marker-napierone-file2text/EPUB \\\n",
        "    --workers 6 --min_length 300"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Poi65wYSpryu",
        "outputId": "8a20c15e-ae37-4be3-85e8-be3dea88afb8"
      },
      "execution_count": null,
      "outputs": [
        {
          "metadata": {
            "tags": null
          },
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "2024-05-08 22:37:05,427\tINFO worker.py:1749 -- Started a local Ray instance.\n",
            "Loaded texify model to cuda with torch.float16 dtype\n",
            "Converting 5000 pdfs in chunk 1/1 with 6 processes, and storing in /content/marker-napierone-file2text/EPUB\n",
            "100% 4999/5000 [14:01:52<00:08,  8.66s/it]"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "out_file=\"/content/archived-marker-napierone-epub.7z\"\n",
        "!find /content/marker-napierone-file2text -type f -name \"*.json\" -print -delete # fuck off\n",
        "!7z a $out_file /content/marker-napierone-file2text"
      ],
      "metadata": {
        "id": "sU8QpnNStjFn"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import files\n",
        "files.download(out_file)"
      ],
      "metadata": {
        "id": "iyzMPMmtqAzI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "CiYoo9ZEtrEz"
      },
      "execution_count": null,
      "outputs": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "gpuType": "L4",
      "machine_shape": "hm"
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "accelerator": "GPU"
  },
  "nbformat": 4,
  "nbformat_minor": 0
}