2
2
import sys
3
3
4
4
if "APP_PATH" in os .environ :
5
- os .chdir (os .environ ["APP_PATH" ])
6
- # fix sys.path for import
7
- sys .path .append (os .getcwd ())
5
+ app_path = os .path .abspath (os .environ ["APP_PATH" ])
6
+ if os .getcwd () != app_path :
7
+ # fix sys.path for import
8
+ os .chdir (app_path )
9
+ if app_path not in sys .path :
10
+ sys .path .append (app_path )
8
11
9
12
import gradio as gr
10
13
27
30
def load_models ():
28
31
return create_model_dict ()
29
32
30
- def convert_pdf (fname : str , ** kwargs ) -> (str , Dict [str , Any ], dict ):
31
- config_parser = ConfigParser (kwargs )
33
+ def convert_pdf (fname : str , config_parser : ConfigParser ) -> (str , Dict [str , Any ], dict ):
32
34
config_dict = config_parser .generate_config_dict ()
33
35
config_dict ["pdftext_workers" ] = 1
34
36
converter = PdfConverter (
@@ -57,9 +59,6 @@ def get_page_image(pdf_file, page_num, dpi=96):
57
59
png_image = png .convert ("RGB" )
58
60
return png_image
59
61
60
- def get_uploaded_image (in_file ):
61
- return Image .open (in_file ).convert ("RGB" )
62
-
63
62
64
63
def img_to_html (img , img_alt ):
65
64
img_bytes = io .BytesIO ()
@@ -80,8 +79,9 @@ def markdown_insert_images(markdown, images):
80
79
markdown = markdown .replace (image_markdown , img_to_html (images [image_path ], image_alt ))
81
80
return markdown
82
81
83
-
84
- model_dict = load_models ()
82
+ # Load models if not already loaded in reload mode
83
+ if 'model_dict' not in globals ():
84
+ model_dict = load_models ()
85
85
86
86
with gr .Blocks (title = "Marker" ) as demo :
87
87
gr .Markdown ("""
@@ -94,54 +94,63 @@ def markdown_insert_images(markdown, images):
94
94
95
95
with gr .Row ():
96
96
with gr .Column ():
97
- in_file = gr .File (label = "PDF file or image :" , file_types = [".pdf" , ".png" , ".jpg" , ".jpeg" , ".gif" , ".webp " ])
98
- in_num = gr .Slider (label = "Page number" , minimum = 1 , maximum = 100 , value = 1 , step = 1 )
99
- in_img = gr .Image (label = "Select page of Image " , type = "pil" , sources = None )
97
+ in_file = gr .File (label = "PDF file:" , file_types = [".pdf" ])
98
+ in_num = gr .Slider (label = "PDF file page number" , minimum = 1 , maximum = 1 , value = 1 , step = 1 , visible = False )
99
+ in_img = gr .Image (label = "PDF file (preview) " , type = "pil" , sources = None , visible = False )
100
100
101
101
page_range_txt = gr .Textbox (label = "Page range to parse, comma separated like 0,5-10,20" , value = f"0-0" )
102
102
output_format_dd = gr .Dropdown (label = "Output format" , choices = ["markdown" , "json" , "html" ], value = "markdown" )
103
103
104
104
force_ocr_ckb = gr .Checkbox (label = "Force OCR" , value = True , info = "Force OCR on all pages" )
105
105
debug_ckb = gr .Checkbox (label = "Debug" , value = False , info = "Show debug information" )
106
- trun_marker_btn = gr .Button ("Run Marker" )
106
+ trun_marker_btn = gr .Button ("Run Marker" , interactive = False )
107
107
with gr .Column ():
108
- result_md = gr .Markdown (label = "Result markdown" )
109
- result_json = gr .JSON (label = "Result json" )
110
- result_html = gr .Markdown (label = "Result html" )
108
+ result_md = gr .Markdown (label = "Result markdown" , visible = False )
109
+ result_json = gr .JSON (label = "Result json" , visible = False )
110
+ result_html = gr .Markdown (label = "Result html" , visible = False )
111
111
debug_img_pdf = gr .Image (label = "PDF debug image" , visible = False )
112
112
debug_img_layout = gr .Image (label = "Layout debug image" , visible = False )
113
113
114
114
def show_image (file , num = 1 ):
115
- if file .endswith ('.pdf' ):
116
- count = count_pdf (file )
117
- img = get_page_image (file , num )
118
- return [
119
- gr .update (visible = True , maximum = count ),
120
- gr .update (value = img )]
121
- else :
122
- img = get_uploaded_image (file )
115
+ if file is None :
123
116
return [
117
+ gr .update (visible = False , maximum = 1 , value = num ),
124
118
gr .update (visible = False ),
125
- gr .update (value = img )]
126
-
119
+ "0-0" ]
120
+ count = count_pdf (file )
121
+ img = get_page_image (file , num )
122
+ return [
123
+ gr .update (visible = True , maximum = count ),
124
+ gr .update (visible = True , value = img ),
125
+ f"0-{ num - 1 } " ]
126
+
127
+ in_file .clear (
128
+ fn = show_image ,
129
+ inputs = [in_file ],
130
+ outputs = [in_num , in_img , page_range_txt ]
131
+ )
127
132
in_file .upload (
128
133
fn = show_image ,
129
134
inputs = [in_file ],
130
- outputs = [in_num , in_img ],
135
+ outputs = [in_num , in_img , page_range_txt ]
131
136
)
132
137
in_num .change (
133
138
fn = show_image ,
134
139
inputs = [in_file , in_num ],
135
- outputs = [in_num , in_img ],
140
+ outputs = [in_num , in_img , page_range_txt ]
136
141
)
137
142
138
143
def check_page_range (page_range , file ):
139
144
count = count_pdf (file ) if file is not None else 1
140
145
if not re .match (r"^(\d+(-\d+)?)?$" , page_range ):
141
146
gr .Warning (f"Invalid format. Please use 0-{ count - 1 } " , duration = 0 )
142
- return gr .update (info = f"format 0-{ count - 1 } " ), gr .update (interactive = False )
147
+ return [
148
+ gr .update (info = f"format 0-{ count - 1 } " ),
149
+ gr .update (interactive = False )]
143
150
else :
144
- return gr .update (info = f"format 0-{ count - 1 } " ), gr .update (interactive = True )
151
+ return [
152
+ gr .update (info = f"format 0-{ count - 1 } " ),
153
+ gr .update (interactive = True )]
145
154
page_range_txt .change (
146
155
fn = check_page_range ,
147
156
inputs = [page_range_txt , in_file ],
@@ -150,28 +159,34 @@ def check_page_range(page_range, file):
150
159
151
160
# Run Marker
152
161
def run_marker_img (filename , page_range , force_ocr , output_format , debug ):
162
+ cli_options = {
163
+ "output_format" : output_format ,
164
+ "page_range" : page_range ,
165
+ "force_ocr" : force_ocr ,
166
+ "debug" : debug ,
167
+ "output_dir" : settings .DEBUG_DATA_FOLDER if debug else None ,
168
+ }
169
+ config_parser = ConfigParser (cli_options )
153
170
rendered = convert_pdf (
154
171
filename ,
155
- page_range = page_range ,
156
- force_ocr = force_ocr ,
157
- output_format = output_format ,
158
- output_dir = settings .DEBUG_DATA_FOLDER if debug else None ,
159
- debug = debug
172
+ config_parser
160
173
)
161
- text , ext , images = text_from_rendered (rendered )
162
-
163
174
gr_debug_pdf = gr .update (visible = False )
164
175
gr_debug_lay = gr .update (visible = False )
165
176
if debug :
166
177
debug_data_path = rendered .metadata .get ("debug_data_path" )
167
178
if debug_data_path :
168
- pdf_image_path = os .path .join (debug_data_path , f"pdf_page_0.png" )
179
+ page_range = config_parser .generate_config_dict ()["page_range" ]
180
+ first_page = page_range [0 ] if page_range else 0
181
+
182
+ pdf_image_path = os .path .join (debug_data_path , f"pdf_page_{ first_page } .png" )
169
183
img = Image .open (pdf_image_path )
170
184
gr_debug_pdf = gr .update (visible = True , value = img )
171
- layout_image_path = os .path .join (debug_data_path , f"layout_page_0 .png" )
185
+ layout_image_path = os .path .join (debug_data_path , f"layout_page_ { first_page } .png" )
172
186
img = Image .open (layout_image_path )
173
187
gr_debug_lay = gr .update (visible = True , value = img )
174
188
189
+ text , ext , images = text_from_rendered (rendered )
175
190
if output_format == "markdown" :
176
191
text = markdown_insert_images (text , images )
177
192
return [
@@ -197,11 +212,12 @@ def run_marker_img(filename, page_range, force_ocr, output_format, debug):
197
212
gr_debug_pdf ,
198
213
gr_debug_lay
199
214
]
200
-
215
+
201
216
trun_marker_btn .click (
202
217
fn = run_marker_img ,
203
218
inputs = [in_file , page_range_txt , force_ocr_ckb , output_format_dd , debug_ckb ],
204
- outputs = [result_md , result_json , result_html , debug_img_pdf , debug_img_layout ],
219
+ outputs = [result_md , result_json , result_html , debug_img_pdf , debug_img_layout ]
205
220
)
206
221
207
- demo .launch ()
222
+ if __name__ == "__main__" :
223
+ demo .launch ()
0 commit comments