Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
gr.HTML("<h1><center>Nougat: Neural Optical Understanding for Academic Documents<center><h1>")
|
2 |
gr.HTML("<h3><center>Lukas Blecher et al. <a href='https://arxiv.org/pdf/2308.13418.pdf' target='_blank'>Paper</a>, <a href='https://facebookresearch.github.io/nougat/'>Project</a><center></h3>")
|
3 |
|
4 |
with gr.Row():
|
5 |
-
mkd = gr.Markdown('<h4><center>Upload a PDF</center></h4>',
|
6 |
-
mkd = gr.Markdown('<h4><center><i>OR</i></center></h4>',
|
7 |
-
mkd = gr.Markdown('<h4><center>Provide a PDF link</center></h4>',
|
8 |
|
9 |
with gr.Row(equal_height=True):
|
10 |
pdf_file = gr.File(label='PDF๐', file_count='single', scale=1)
|
@@ -16,27 +136,23 @@
|
|
16 |
|
17 |
output_headline = gr.Markdown("<h3>PDF converted to markup language through Nougat-OCR๐:</h3>")
|
18 |
parsed_output = gr.Markdown(elem_id='mkd', value='๐๐คOCR Output')
|
19 |
-
mmd_file_download = gr.File(label='Download .mmd file', interactive=False)
|
20 |
-
|
21 |
-
def handle_predict(pdf_file, pdf_link):
|
22 |
-
content, mmd_file_path = predict(pdf_file, pdf_link)
|
23 |
-
return gr.update(value=content), mmd_file_path
|
24 |
|
25 |
-
btn.click(
|
26 |
-
clr.click(lambda: (gr.update(value=None),
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
gr.Examples(
|
33 |
[["input/nougat.pdf", ""], [None, "https://arxiv.org/pdf/2308.08316.pdf"]],
|
34 |
-
inputs=[pdf_file, pdf_link],
|
35 |
-
outputs=parsed_output,
|
36 |
fn=process_example,
|
37 |
cache_examples=True,
|
38 |
label='Click on any Examples below to get Nougat OCR results quickly:'
|
39 |
)
|
40 |
|
41 |
demo.queue()
|
42 |
-
demo.launch(debug=True)
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import uuid
|
4 |
+
import os
|
5 |
+
import requests
|
6 |
+
import re
|
7 |
+
|
8 |
+
|
9 |
+
def get_pdf(pdf_link):
|
10 |
+
# Generate a unique filename
|
11 |
+
unique_filename = f"input/downloaded_paper_{uuid.uuid4().hex}.pdf"
|
12 |
+
|
13 |
+
# Send a GET request to the PDF link
|
14 |
+
response = requests.get(pdf_link)
|
15 |
+
|
16 |
+
if response.status_code == 200:
|
17 |
+
# Save the PDF content to a local file
|
18 |
+
with open(unique_filename, 'wb') as pdf_file:
|
19 |
+
pdf_file.write(response.content)
|
20 |
+
print("PDF downloaded successfully.")
|
21 |
+
else:
|
22 |
+
print("Failed to download the PDF.")
|
23 |
+
return unique_filename #.split('/')[-1][:-4]
|
24 |
+
|
25 |
+
|
26 |
+
def nougat_ocr(file_name):
|
27 |
+
|
28 |
+
#unique_filename = f"/content/output/downloaded_paper_{uuid.uuid4().hex}.pdf"
|
29 |
+
# Command to run
|
30 |
+
cli_command = [
|
31 |
+
'nougat',
|
32 |
+
#'--out', unique_filename,
|
33 |
+
'--out', 'output',
|
34 |
+
'pdf', f'{file_name}',
|
35 |
+
'--checkpoint', 'nougat',
|
36 |
+
'--markdown'
|
37 |
+
]
|
38 |
+
|
39 |
+
# Run the command and capture its output
|
40 |
+
#completed_process =
|
41 |
+
subprocess.run(cli_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
42 |
+
|
43 |
+
return #unique_filename
|
44 |
+
|
45 |
+
|
46 |
+
def predict(pdf_file, pdf_link):
|
47 |
+
if pdf_file is None:
|
48 |
+
if pdf_link == '':
|
49 |
+
print("No file is uploaded and No link is provided")
|
50 |
+
return "No data provided. Upload a pdf file or provide a pdf link and try again!"
|
51 |
+
else:
|
52 |
+
print(f'pdf_link is - {pdf_link}')
|
53 |
+
file_name = get_pdf(pdf_link)
|
54 |
+
print(f'file_name is - {file_name}')
|
55 |
+
else:
|
56 |
+
file_name = pdf_file.name
|
57 |
+
print(file_name)
|
58 |
+
pdf_name = pdf_file.name.split('/')[-1].split('.')[0]
|
59 |
+
print(pdf_name)
|
60 |
+
|
61 |
+
# Call nougat
|
62 |
+
nougat_ocr(file_name)
|
63 |
+
#print("BACKKKK")
|
64 |
+
|
65 |
+
# Open the file for reading
|
66 |
+
file_name = file_name.split('/')[-1][:-4]
|
67 |
+
with open(f'output/{file_name}.mmd', 'r') as file:
|
68 |
+
content = file.read()
|
69 |
+
# switch math delimiters
|
70 |
+
content = content.replace(r'\(', '$').replace(r'\)', '$').replace(r'\[', '$$').replace(r'\]', '$$')
|
71 |
+
return content
|
72 |
+
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
def nougat_ocr1(file_name):
|
77 |
+
print('******* inside nougat_ocr *******')
|
78 |
+
# CLI Command to run
|
79 |
+
cli_command = [
|
80 |
+
'nougat',
|
81 |
+
'--out', 'output',
|
82 |
+
'pdf', f'{file_name}',
|
83 |
+
'--checkpoint', 'nougat',
|
84 |
+
'--markdown'
|
85 |
+
]
|
86 |
+
|
87 |
+
# Run the command and get .mmd file in an output folder
|
88 |
+
subprocess.run(cli_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
89 |
+
return
|
90 |
+
|
91 |
+
|
92 |
+
def predict1(pdf_file):
|
93 |
+
print('******* inside predict *******')
|
94 |
+
print(f"temporary file - {pdf_file.name}")
|
95 |
+
pdf_name = pdf_file.name.split('/')[-1].split('.')[0]
|
96 |
+
print(f"pdf file name - {pdf_name}")
|
97 |
+
|
98 |
+
#! Get prediction for a PDF using nougat
|
99 |
+
nougat_ocr(pdf_file.name)
|
100 |
+
print("BAACCKKK")
|
101 |
+
|
102 |
+
# Open the multimarkdown (.mmd) file for reading
|
103 |
+
with open(f'output/{pdf_name}.mmd', 'r') as file:
|
104 |
+
content = file.read()
|
105 |
+
|
106 |
+
return content
|
107 |
+
|
108 |
+
def process_example(pdf_file,pdf_link):
|
109 |
+
ocr_content = predict(pdf_file,pdf_link)
|
110 |
+
return gr.update(value=ocr_content)
|
111 |
+
|
112 |
+
css = """
|
113 |
+
#mkd {
|
114 |
+
height: 500px;
|
115 |
+
overflow: auto;
|
116 |
+
border: 1px solid #ccc;
|
117 |
+
}
|
118 |
+
"""
|
119 |
+
|
120 |
+
with gr.Blocks(css=css) as demo:
|
121 |
gr.HTML("<h1><center>Nougat: Neural Optical Understanding for Academic Documents<center><h1>")
|
122 |
gr.HTML("<h3><center>Lukas Blecher et al. <a href='https://arxiv.org/pdf/2308.13418.pdf' target='_blank'>Paper</a>, <a href='https://facebookresearch.github.io/nougat/'>Project</a><center></h3>")
|
123 |
|
124 |
with gr.Row():
|
125 |
+
mkd = gr.Markdown('<h4><center>Upload a PDF</center></h4>',scale=1)
|
126 |
+
mkd = gr.Markdown('<h4><center><i>OR</i></center></h4>',scale=1)
|
127 |
+
mkd = gr.Markdown('<h4><center>Provide a PDF link</center></h4>',scale=1)
|
128 |
|
129 |
with gr.Row(equal_height=True):
|
130 |
pdf_file = gr.File(label='PDF๐', file_count='single', scale=1)
|
|
|
136 |
|
137 |
output_headline = gr.Markdown("<h3>PDF converted to markup language through Nougat-OCR๐:</h3>")
|
138 |
parsed_output = gr.Markdown(elem_id='mkd', value='๐๐คOCR Output')
|
|
|
|
|
|
|
|
|
|
|
139 |
|
140 |
+
btn.click(predict, [pdf_file, pdf_link], parsed_output )
|
141 |
+
clr.click(lambda : (gr.update(value=None),
|
142 |
+
gr.update(value=None),
|
143 |
+
gr.update(value=None)),
|
144 |
+
[],
|
145 |
+
[pdf_file, pdf_link, parsed_output]
|
146 |
+
)
|
147 |
|
148 |
gr.Examples(
|
149 |
[["input/nougat.pdf", ""], [None, "https://arxiv.org/pdf/2308.08316.pdf"]],
|
150 |
+
inputs = [pdf_file, pdf_link],
|
151 |
+
outputs = parsed_output,
|
152 |
fn=process_example,
|
153 |
cache_examples=True,
|
154 |
label='Click on any Examples below to get Nougat OCR results quickly:'
|
155 |
)
|
156 |
|
157 |
demo.queue()
|
158 |
+
demo.launch(debug=True)
|