merve HF staff commited on
Commit
4788a5c
โ€ข
1 Parent(s): 52ca21c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
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
- #ย infer for every page and concat
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
- import gradio as gr
73
- import uuid
74
- import os
75
- import requests
76
- import re
 
 
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
- clr = gr.Button('Clear ๐Ÿงผ')
 
105
 
106
- output_headline = gr.Markdown("PDF converted to markup language through Nougat-OCR๐Ÿ‘‡")
107
- parsed_output = gr.Markdown(elem_id='mkd', value='OCR Output ๐Ÿ“')
 
 
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:'