fsmoreir commited on
Commit
abc1e01
·
verified ·
1 Parent(s): 0fd312d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -133
app.py CHANGED
@@ -3,112 +3,61 @@ 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;
@@ -117,42 +66,44 @@ css = """
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)
131
- pdf_link = gr.Textbox(placeholder='Enter an Arxiv link here', label='PDF link🔗🌐', scale=1)
132
-
133
- with gr.Row():
134
- btn = gr.Button('Run NOUGAT🍫')
135
- clr = gr.Button('Clear🚿')
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)
 
3
  import uuid
4
  import os
5
  import requests
 
 
6
 
7
  def get_pdf(pdf_link):
8
+ # Generate a unique filename
9
+ unique_filename = f"input/downloaded_paper_{uuid.uuid4().hex}.pdf"
 
 
 
10
 
11
+ # Send a GET request to the PDF link
12
+ response = requests.get(pdf_link)
 
 
 
 
 
 
13
 
14
+ if response.status_code == 200:
15
+ # Save the PDF content to a local file
16
+ with open(unique_filename, 'wb') as pdf_file:
17
+ pdf_file.write(response.content)
18
+ print("PDF downloaded successfully.")
19
+ else:
20
+ print("Failed to download the PDF.")
21
+ return unique_filename
22
 
23
  def nougat_ocr(file_name):
24
+ # Command to run
25
+ cli_command = [
26
+ 'nougat',
27
+ '--out', 'output',
28
+ 'pdf', f'{file_name}',
29
+ '--checkpoint', 'nougat',
30
+ '--markdown'
31
+ ]
32
+ subprocess.run(cli_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
 
 
 
 
 
 
 
 
 
33
 
34
  def predict(pdf_file, pdf_link):
35
+ if pdf_file is None:
36
+ if pdf_link == '':
37
+ print("No file is uploaded and No link is provided")
38
+ return "No data provided. Upload a pdf file or provide a pdf link and try again!", "", ""
39
+ else:
40
+ print(f'pdf_link is - {pdf_link}')
41
+ file_name = get_pdf(pdf_link)
42
+ print(f'file_name is - {file_name}')
43
  else:
44
+ file_name = pdf_file.name
45
+ print(file_name)
46
+ pdf_name = pdf_file.name.split('/')[-1].split('.')[0]
47
+ print(pdf_name)
48
+
49
+ # Call nougat
50
+ nougat_ocr(file_name)
51
+
52
+ # Open the file for reading
53
+ file_name = file_name.split('/')[-1][:-4]
54
+ output_path = f'output/{file_name}.mmd'
55
+ with open(output_path, 'r') as file:
56
+ content = file.read()
57
+ # Switch math delimiters
58
+ content = content.replace(r'\(', '$').replace(r'\)', '$').replace(r'\[', '$$').replace(r'\]', '$$')
59
+ return content, content, output_path
60
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  css = """
62
  #mkd {
63
  height: 500px;
 
66
  }
67
  """
68
 
69
+ # JavaScript function to copy content to clipboard automatically
70
+ js_auto_copy = """
71
+ function autoCopyToClipboard() {
72
+ var content = document.getElementById('markdown-content').value;
73
+ navigator.clipboard.writeText(content).then(function() {
74
+ console.log('Content copied to clipboard');
75
+ }).catch(function(error) {
76
+ console.error('Error copying content to clipboard: ', error);
77
+ });
78
+ }
79
+ """
80
+
81
  with gr.Blocks(css=css) as demo:
82
+ gr.HTML("<h1><center>Nougat: Neural Optical Understanding for Academic Documents<center><h1>")
83
+ 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>")
84
+
85
+ with gr.Row():
86
+ mkd = gr.Markdown('<h4><center>Upload a PDF</center></h4>', scale=1)
87
+ mkd = gr.Markdown('<h4><center><i>OR</i></center></h4>', scale=1)
88
+ mkd = gr.Markdown('<h4><center>Provide a PDF link</center></h4>', scale=1)
89
+
90
+ with gr.Row(equal_height=True):
91
+ pdf_file = gr.File(label='PDF📃', file_count='single', scale=1)
92
+ pdf_link = gr.Textbox(placeholder='Enter an Arxiv link here', label='PDF link🔗🌐', scale=1)
93
+
94
+ with gr.Row():
95
+ btn = gr.Button('Run NOUGAT🍫')
96
+ clr = gr.Button('Clear🚿')
97
+ dwn = gr.Button('Download📥')
98
+
99
+ output_headline = gr.Markdown("<h3>PDF converted to markup language through Nougat-OCR👇:</h3>")
100
+ parsed_output = gr.Markdown(elem_id='mkd', value='📃🔤OCR Output')
101
+ markdown_hidden = gr.Textbox(elem_id='markdown-content', visible=False)
102
+ download_link = gr.File(elem_id='download-link', visible=False)
103
+
104
+ btn.click(predict, [pdf_file, pdf_link], [parsed_output, markdown_hidden, download_link], _js=js_auto_copy)
105
+ clr.click(lambda: (gr.update(value=None), gr.update(value=None), gr.update(value=None)), [], [pdf_file, pdf_link, parsed_output, markdown_hidden, download_link])
106
+ dwn.click(None, [download_link], None)
107
+
 
 
 
 
 
 
 
 
 
 
108
  demo.queue()
109
  demo.launch(debug=True)