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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import time
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import contextlib
|
| 5 |
+
|
| 6 |
+
logging.basicConfig(
|
| 7 |
+
level=logging.INFO,
|
| 8 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import gradio as gr
|
| 13 |
+
import nltk
|
| 14 |
+
import torch
|
| 15 |
+
|
| 16 |
+
from pdf2text import *
|
| 17 |
+
|
| 18 |
+
_here = Path(__file__).parent
|
| 19 |
+
|
| 20 |
+
nltk.download("stopwords") # TODO=find where this requirement originates from
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def load_uploaded_file(file_obj, temp_dir: Path = None):
|
| 24 |
+
"""
|
| 25 |
+
load_uploaded_file - process an uploaded file
|
| 26 |
+
Args:
|
| 27 |
+
file_obj (POTENTIALLY list): Gradio file object inside a list
|
| 28 |
+
Returns:
|
| 29 |
+
str, the uploaded file contents
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
# check if mysterious file object is a list
|
| 33 |
+
if isinstance(file_obj, list):
|
| 34 |
+
file_obj = file_obj[0]
|
| 35 |
+
file_path = Path(file_obj.name)
|
| 36 |
+
|
| 37 |
+
if temp_dir is None:
|
| 38 |
+
_temp_dir = _here / "temp"
|
| 39 |
+
_temp_dir.mkdir(exist_ok=True)
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
pdf_bytes_obj = open(file_path, "rb").read()
|
| 43 |
+
temp_path = temp_dir / file_path.name if temp_dir else file_path
|
| 44 |
+
# save to PDF file
|
| 45 |
+
with open(temp_path, "wb") as f:
|
| 46 |
+
f.write(pdf_bytes_obj)
|
| 47 |
+
logging.info(f"Saved uploaded file to {temp_path}")
|
| 48 |
+
return str(temp_path.resolve())
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
logging.error(f"Trying to load file with path {file_path}, error: {e}")
|
| 52 |
+
print(f"Trying to load file with path {file_path}, error: {e}")
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def convert_PDF(
|
| 57 |
+
pdf_obj,
|
| 58 |
+
language: str = "en",
|
| 59 |
+
max_pages=20,
|
| 60 |
+
):
|
| 61 |
+
"""
|
| 62 |
+
convert_PDF - convert a PDF file to text
|
| 63 |
+
Args:
|
| 64 |
+
pdf_bytes_obj (bytes): PDF file contents
|
| 65 |
+
language (str, optional): Language to use for OCR. Defaults to "en".
|
| 66 |
+
Returns:
|
| 67 |
+
str, the PDF file contents as text
|
| 68 |
+
"""
|
| 69 |
+
# clear local text cache
|
| 70 |
+
rm_local_text_files()
|
| 71 |
+
global ocr_model
|
| 72 |
+
st = time.perf_counter()
|
| 73 |
+
if isinstance(pdf_obj, list):
|
| 74 |
+
pdf_obj = pdf_obj[0]
|
| 75 |
+
file_path = Path(pdf_obj.name)
|
| 76 |
+
if not file_path.suffix == ".pdf":
|
| 77 |
+
logging.error(f"File {file_path} is not a PDF file")
|
| 78 |
+
|
| 79 |
+
html_error = f"""
|
| 80 |
+
<div style="color: red; font-size: 20px; font-weight: bold;">
|
| 81 |
+
File {file_path} is not a PDF file. Please upload a PDF file.
|
| 82 |
+
</div>
|
| 83 |
+
"""
|
| 84 |
+
return "File is not a PDF file", html_error, None
|
| 85 |
+
|
| 86 |
+
conversion_stats = convert_PDF_to_Text(
|
| 87 |
+
file_path,
|
| 88 |
+
ocr_model=ocr_model,
|
| 89 |
+
max_pages=max_pages,
|
| 90 |
+
)
|
| 91 |
+
converted_txt = conversion_stats["converted_text"]
|
| 92 |
+
num_pages = conversion_stats["num_pages"]
|
| 93 |
+
was_truncated = conversion_stats["truncated"]
|
| 94 |
+
# if alt_lang: # TODO: fix this
|
| 95 |
+
|
| 96 |
+
rt = round((time.perf_counter() - st) / 60, 2)
|
| 97 |
+
print(f"Runtime: {rt} minutes")
|
| 98 |
+
html = ""
|
| 99 |
+
if was_truncated:
|
| 100 |
+
html += f"<p>WARNING - PDF was truncated to {max_pages} pages</p>"
|
| 101 |
+
html += f"<p>Runtime: {rt} minutes on CPU for {num_pages} pages</p>"
|
| 102 |
+
|
| 103 |
+
_output_name = f"RESULT_{file_path.stem}_OCR.txt"
|
| 104 |
+
with open(_output_name, "w", encoding="utf-8", errors="ignore") as f:
|
| 105 |
+
f.write(converted_txt)
|
| 106 |
+
|
| 107 |
+
return converted_txt, html, _output_name
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
if __name__ == "__main__":
|
| 111 |
+
logging.info("Starting app")
|
| 112 |
+
|
| 113 |
+
use_GPU = torch.cuda.is_available()
|
| 114 |
+
logging.info(f"Using GPU status: {use_GPU}")
|
| 115 |
+
logging.info("Loading OCR model")
|
| 116 |
+
with contextlib.redirect_stdout(None):
|
| 117 |
+
ocr_model = ocr_predictor(
|
| 118 |
+
"db_resnet50",
|
| 119 |
+
"crnn_mobilenet_v3_large",
|
| 120 |
+
pretrained=True,
|
| 121 |
+
assume_straight_pages=True,
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
# define pdf bytes as None
|
| 125 |
+
pdf_obj = _here / "example_file.pdf"
|
| 126 |
+
pdf_obj = str(pdf_obj.resolve())
|
| 127 |
+
_temp_dir = _here / "temp"
|
| 128 |
+
_temp_dir.mkdir(exist_ok=True)
|
| 129 |
+
|
| 130 |
+
logging.info("starting demo")
|
| 131 |
+
demo = gr.Blocks()
|
| 132 |
+
|
| 133 |
+
with demo:
|
| 134 |
+
|
| 135 |
+
gr.Markdown("# PDF to Text")
|
| 136 |
+
gr.Markdown(
|
| 137 |
+
"A basic demo of pdf-to-text conversion using OCR from the [doctr](https://mindee.github.io/doctr/index.html) package"
|
| 138 |
+
)
|
| 139 |
+
gr.Markdown("---")
|
| 140 |
+
|
| 141 |
+
with gr.Column():
|
| 142 |
+
|
| 143 |
+
gr.Markdown("## Load Inputs")
|
| 144 |
+
gr.Markdown("Upload your own file & replace the default. Files should be < 10MB to avoid upload issues - search for a PDF compressor online as needed.")
|
| 145 |
+
gr.Markdown(
|
| 146 |
+
"_If no file is uploaded, a sample PDF will be used. PDFs are truncated to 20 pages._"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
uploaded_file = gr.File(
|
| 150 |
+
label="Upload a PDF file",
|
| 151 |
+
file_count="single",
|
| 152 |
+
type="file",
|
| 153 |
+
value=_here / "example_file.pdf",
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
gr.Markdown("---")
|
| 157 |
+
|
| 158 |
+
with gr.Column():
|
| 159 |
+
gr.Markdown("## Convert PDF to Text")
|
| 160 |
+
convert_button = gr.Button("Convert PDF!", variant="primary")
|
| 161 |
+
out_placeholder = gr.HTML("<p><em>Output will appear below:</em></p>")
|
| 162 |
+
gr.Markdown("### Output")
|
| 163 |
+
OCR_text = gr.Textbox(
|
| 164 |
+
label="OCR Result", placeholder="The OCR text will appear here"
|
| 165 |
+
)
|
| 166 |
+
text_file = gr.File(
|
| 167 |
+
label="Download Text File",
|
| 168 |
+
file_count="single",
|
| 169 |
+
type="file",
|
| 170 |
+
interactive=False,
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
convert_button.click(
|
| 174 |
+
fn=convert_PDF,
|
| 175 |
+
inputs=[uploaded_file],
|
| 176 |
+
outputs=[OCR_text, out_placeholder, text_file],
|
| 177 |
+
)
|
| 178 |
+
demo.launch(enable_queue=True)
|