{ "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 }