Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,11 @@ import re
|
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
from nougat.dataset.rasterize import rasterize_paper
|
6 |
-
|
7 |
from transformers import NougatProcessor, VisionEncoderDecoderModel
|
8 |
import torch
|
|
|
|
|
|
|
9 |
|
10 |
processor = NougatProcessor.from_pretrained("facebook/nougat-small")
|
11 |
model = VisionEncoderDecoderModel.from_pretrained("facebook/nougat-small")
|
@@ -48,7 +50,7 @@ def predict(image):
|
|
48 |
|
49 |
|
50 |
|
51 |
-
def inference(pdf_file, pdf_link):
|
52 |
if pdf_file is None:
|
53 |
if pdf_link == '':
|
54 |
print("No file is uploaded and No link is provided")
|
@@ -61,19 +63,20 @@ def inference(pdf_file, pdf_link):
|
|
61 |
|
62 |
images = rasterize_paper(file_name, return_pil=True)
|
63 |
sequence = ""
|
64 |
-
#
|
65 |
for image in images:
|
66 |
sequence += predict(image)
|
67 |
|
68 |
|
69 |
content = sequence.replace(r'\(', '$').replace(r'\)', '$').replace(r'\[', '$$').replace(r'\]', '$$')
|
70 |
-
return content
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
css = """
|
79 |
#mkd {
|
@@ -98,25 +101,30 @@ with gr.Blocks(css=css) as demo:
|
|
98 |
with gr.Row(equal_height=True):
|
99 |
pdf_file = gr.File(label='PDF ๐', file_count='single', scale=1)
|
100 |
pdf_link = gr.Textbox(placeholder='Enter an arxiv link here', label='Link to Paper๐', scale=1)
|
101 |
-
|
|
|
102 |
with gr.Row():
|
103 |
btn = gr.Button('Run Nougat ๐ซ')
|
104 |
-
|
|
|
105 |
|
106 |
-
output_headline = gr.Markdown("PDF converted to markup language through Nougat-OCR๐")
|
107 |
-
|
|
|
|
|
108 |
|
109 |
-
btn.click(inference, [pdf_file, pdf_link], parsed_output )
|
110 |
clr.click(lambda : (gr.update(value=None),
|
111 |
gr.update(value=None),
|
|
|
112 |
gr.update(value=None)),
|
113 |
[],
|
114 |
-
[pdf_file, pdf_link, parsed_output]
|
115 |
)
|
116 |
gr.Examples(
|
117 |
-
[["nougat.pdf", ""], [None, "https://arxiv.org/pdf/2308.08316.pdf"]],
|
118 |
-
inputs = [pdf_file, pdf_link],
|
119 |
-
outputs = parsed_output,
|
120 |
fn=inference,
|
121 |
cache_examples=True,
|
122 |
label='Click on any Examples below to get Nougat OCR results quickly:'
|
|
|
3 |
from PIL import Image
|
4 |
import requests
|
5 |
from nougat.dataset.rasterize import rasterize_paper
|
|
|
6 |
from transformers import NougatProcessor, VisionEncoderDecoderModel
|
7 |
import torch
|
8 |
+
import gradio as gr
|
9 |
+
import uuid
|
10 |
+
import os
|
11 |
|
12 |
processor = NougatProcessor.from_pretrained("facebook/nougat-small")
|
13 |
model = VisionEncoderDecoderModel.from_pretrained("facebook/nougat-small")
|
|
|
50 |
|
51 |
|
52 |
|
53 |
+
def inference(pdf_file, pdf_link, file_btn):
|
54 |
if pdf_file is None:
|
55 |
if pdf_link == '':
|
56 |
print("No file is uploaded and No link is provided")
|
|
|
63 |
|
64 |
images = rasterize_paper(file_name, return_pil=True)
|
65 |
sequence = ""
|
66 |
+
# infer for every page and concat
|
67 |
for image in images:
|
68 |
sequence += predict(image)
|
69 |
|
70 |
|
71 |
content = sequence.replace(r'\(', '$').replace(r'\)', '$').replace(r'\[', '$$').replace(r'\]', '$$')
|
|
|
72 |
|
73 |
+
if file_btn:
|
74 |
+
with open(f"{os.getcwd()}/output.txt","w+") as f:
|
75 |
+
f.write(content)
|
76 |
+
f.close()
|
77 |
+
|
78 |
+
return content, f"{os.getcwd()}/output.txt"
|
79 |
+
|
80 |
|
81 |
css = """
|
82 |
#mkd {
|
|
|
101 |
with gr.Row(equal_height=True):
|
102 |
pdf_file = gr.File(label='PDF ๐', file_count='single', scale=1)
|
103 |
pdf_link = gr.Textbox(placeholder='Enter an arxiv link here', label='Link to Paper๐', scale=1)
|
104 |
+
with gr.Row():
|
105 |
+
file_btn = gr.Checkbox(label='Download output as file ๐')
|
106 |
with gr.Row():
|
107 |
btn = gr.Button('Run Nougat ๐ซ')
|
108 |
+
with gr.Row():
|
109 |
+
clr = gr.Button('Clear Inputs & Outputs ๐งผ')
|
110 |
|
111 |
+
output_headline = gr.Markdown("## PDF converted to markup language through Nougat-OCR๐")
|
112 |
+
with gr.Row():
|
113 |
+
parsed_output = gr.Markdown(elem_id='mkd', value='Output Text ๐')
|
114 |
+
output_file = gr.File(file_types = ["txt"], label="Output File ๐")
|
115 |
|
116 |
+
btn.click(inference, [pdf_file, pdf_link, file_btn], [parsed_output, output_file])
|
117 |
clr.click(lambda : (gr.update(value=None),
|
118 |
gr.update(value=None),
|
119 |
+
gr.update(value=None),
|
120 |
gr.update(value=None)),
|
121 |
[],
|
122 |
+
[pdf_file, pdf_link, file_btn, parsed_output, output_file]
|
123 |
)
|
124 |
gr.Examples(
|
125 |
+
[["nougat.pdf", "", True], [None, "https://arxiv.org/pdf/2308.08316.pdf", True]],
|
126 |
+
inputs = [pdf_file, pdf_link, file_btn],
|
127 |
+
outputs = [parsed_output, output_file],
|
128 |
fn=inference,
|
129 |
cache_examples=True,
|
130 |
label='Click on any Examples below to get Nougat OCR results quickly:'
|