Spaces:
Running
on
L40S
Running
on
L40S
Update app.py
Browse files
app.py
CHANGED
@@ -8,11 +8,11 @@ import time
|
|
8 |
import zipfile
|
9 |
from pathlib import Path
|
10 |
|
11 |
-
os.system('pip uninstall -y magic-pdf')
|
12 |
os.system('pip uninstall -y mineru')
|
13 |
-
os.system('pip install git+https://github.com/
|
|
|
14 |
|
15 |
-
os.system('mineru-models-download -s huggingface -m
|
16 |
|
17 |
with open('/home/user/mineru.json', 'r') as file:
|
18 |
config = json.load(file)
|
@@ -35,24 +35,27 @@ from gradio_pdf import PDF
|
|
35 |
import gradio as gr
|
36 |
from loguru import logger
|
37 |
|
38 |
-
from mineru.cli.common import prepare_env,
|
39 |
from mineru.utils.hash_utils import str_sha256
|
40 |
|
41 |
os.environ['MINERU_MODEL_SOURCE'] = 'local'
|
42 |
|
43 |
|
44 |
-
def parse_pdf(doc_path, output_dir, end_page_id, is_ocr, formula_enable, table_enable, language):
|
45 |
os.makedirs(output_dir, exist_ok=True)
|
46 |
|
47 |
try:
|
48 |
-
file_name = f'{
|
49 |
pdf_data = read_fn(doc_path)
|
50 |
if is_ocr:
|
51 |
parse_method = 'ocr'
|
52 |
else:
|
53 |
parse_method = 'auto'
|
|
|
|
|
|
|
54 |
local_image_dir, local_md_dir = prepare_env(output_dir, file_name, parse_method)
|
55 |
-
|
56 |
output_dir=output_dir,
|
57 |
pdf_file_names=[file_name],
|
58 |
pdf_bytes_list=[pdf_data],
|
@@ -61,6 +64,8 @@ def parse_pdf(doc_path, output_dir, end_page_id, is_ocr, formula_enable, table_e
|
|
61 |
end_page_id=end_page_id,
|
62 |
p_formula_enable=formula_enable,
|
63 |
p_table_enable=table_enable,
|
|
|
|
|
64 |
)
|
65 |
return local_md_dir, file_name
|
66 |
except Exception as e:
|
@@ -112,12 +117,10 @@ def replace_image_with_base64(markdown_text, image_dir_path):
|
|
112 |
return re.sub(pattern, replace, markdown_text)
|
113 |
|
114 |
|
115 |
-
def to_markdown(file_path, end_pages, is_ocr, formula_enable, table_enable, language):
|
116 |
file_path = to_pdf(file_path)
|
117 |
-
if end_pages > 20:
|
118 |
-
end_pages = 20
|
119 |
# 获取识别的md文件以及压缩包文件路径
|
120 |
-
local_md_dir, file_name = parse_pdf(file_path, './output', end_pages - 1, is_ocr, formula_enable, table_enable, language)
|
121 |
archive_zip_path = os.path.join('./output', str_sha256(local_md_dir) + '.zip')
|
122 |
zip_archive_success = compress_directory_to_zip(local_md_dir, archive_zip_path)
|
123 |
if zip_archive_success == 0:
|
@@ -134,8 +137,12 @@ def to_markdown(file_path, end_pages, is_ocr, formula_enable, table_enable, lang
|
|
134 |
return md_content, txt_content, archive_zip_path, new_pdf_path
|
135 |
|
136 |
|
137 |
-
latex_delimiters = [
|
138 |
-
|
|
|
|
|
|
|
|
|
139 |
|
140 |
|
141 |
with open("header.html", "r") as file:
|
@@ -192,19 +199,39 @@ def to_pdf(file_path):
|
|
192 |
return tmp_file_path
|
193 |
|
194 |
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
with gr.Blocks() as demo:
|
197 |
gr.HTML(header)
|
198 |
with gr.Row():
|
199 |
with gr.Column(variant='panel', scale=5):
|
200 |
with gr.Row():
|
201 |
file = gr.File(label='Please upload a PDF or image', file_types=['.pdf', '.png', '.jpeg', '.jpg'])
|
202 |
-
with gr.Row(equal_height=True):
|
203 |
-
with gr.Column(scale=4):
|
204 |
-
max_pages = gr.Slider(1, 20, 10, step=1, label='Max convert pages')
|
205 |
-
with gr.Column(scale=1):
|
206 |
-
language = gr.Dropdown(all_lang, label='Language', value='ch')
|
207 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
is_ocr = gr.Checkbox(label='Force enable OCR', value=False)
|
209 |
formula_enable = gr.Checkbox(label='Enable formula recognition', value=True)
|
210 |
table_enable = gr.Checkbox(label='Enable table recognition(test)', value=True)
|
@@ -212,13 +239,15 @@ if __name__ == '__main__':
|
|
212 |
change_bu = gr.Button('Convert')
|
213 |
clear_bu = gr.ClearButton(value='Clear')
|
214 |
pdf_show = PDF(label='PDF preview', interactive=False, visible=True, height=800)
|
215 |
-
|
216 |
example_root = os.path.join(os.path.dirname(__file__), 'examples')
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
222 |
|
223 |
with gr.Column(variant='panel', scale=5):
|
224 |
output_file = gr.File(label='convert result', interactive=False)
|
@@ -229,9 +258,35 @@ if __name__ == '__main__':
|
|
229 |
line_breaks=True)
|
230 |
with gr.Tab('Markdown text'):
|
231 |
md_text = gr.TextArea(lines=45, show_copy_button=True)
|
232 |
-
|
233 |
-
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
clear_bu.add([file, md, pdf_show, md_text, output_file, is_ocr])
|
236 |
|
237 |
-
demo.launch(ssr_mode=True)
|
|
|
|
|
|
|
|
|
|
8 |
import zipfile
|
9 |
from pathlib import Path
|
10 |
|
|
|
11 |
os.system('pip uninstall -y mineru')
|
12 |
+
os.system('pip install git+https://github.com/myhloli/Magic-PDF.git@dev')
|
13 |
+
os.system('pip install sglang[all]==0.4.8')
|
14 |
|
15 |
+
os.system('mineru-models-download -s huggingface -m all')
|
16 |
|
17 |
with open('/home/user/mineru.json', 'r') as file:
|
18 |
config = json.load(file)
|
|
|
35 |
import gradio as gr
|
36 |
from loguru import logger
|
37 |
|
38 |
+
from mineru.cli.common import prepare_env, read_fn, aio_do_parse
|
39 |
from mineru.utils.hash_utils import str_sha256
|
40 |
|
41 |
os.environ['MINERU_MODEL_SOURCE'] = 'local'
|
42 |
|
43 |
|
44 |
+
async def parse_pdf(doc_path, output_dir, end_page_id, is_ocr, formula_enable, table_enable, language, backend, url):
|
45 |
os.makedirs(output_dir, exist_ok=True)
|
46 |
|
47 |
try:
|
48 |
+
file_name = f'{safe_stem(Path(doc_path).stem)}_{time.strftime("%y%m%d_%H%M%S")}'
|
49 |
pdf_data = read_fn(doc_path)
|
50 |
if is_ocr:
|
51 |
parse_method = 'ocr'
|
52 |
else:
|
53 |
parse_method = 'auto'
|
54 |
+
|
55 |
+
if backend.startswith("vlm"):
|
56 |
+
parse_method = "vlm"
|
57 |
local_image_dir, local_md_dir = prepare_env(output_dir, file_name, parse_method)
|
58 |
+
await aio_do_parse(
|
59 |
output_dir=output_dir,
|
60 |
pdf_file_names=[file_name],
|
61 |
pdf_bytes_list=[pdf_data],
|
|
|
64 |
end_page_id=end_page_id,
|
65 |
p_formula_enable=formula_enable,
|
66 |
p_table_enable=table_enable,
|
67 |
+
backend=backend,
|
68 |
+
server_url=url,
|
69 |
)
|
70 |
return local_md_dir, file_name
|
71 |
except Exception as e:
|
|
|
117 |
return re.sub(pattern, replace, markdown_text)
|
118 |
|
119 |
|
120 |
+
async def to_markdown(file_path, end_pages=10, is_ocr=False, formula_enable=True, table_enable=True, language="ch", backend="pipeline", url=None):
|
121 |
file_path = to_pdf(file_path)
|
|
|
|
|
122 |
# 获取识别的md文件以及压缩包文件路径
|
123 |
+
local_md_dir, file_name = await parse_pdf(file_path, './output', end_pages - 1, is_ocr, formula_enable, table_enable, language, backend, url)
|
124 |
archive_zip_path = os.path.join('./output', str_sha256(local_md_dir) + '.zip')
|
125 |
zip_archive_success = compress_directory_to_zip(local_md_dir, archive_zip_path)
|
126 |
if zip_archive_success == 0:
|
|
|
137 |
return md_content, txt_content, archive_zip_path, new_pdf_path
|
138 |
|
139 |
|
140 |
+
latex_delimiters = [
|
141 |
+
{'left': '$$', 'right': '$$', 'display': True},
|
142 |
+
{'left': '$', 'right': '$', 'display': False},
|
143 |
+
{'left': '\\(', 'right': '\\)', 'display': False},
|
144 |
+
{'left': '\\[', 'right': '\\]', 'display': True},
|
145 |
+
]
|
146 |
|
147 |
|
148 |
with open("header.html", "r") as file:
|
|
|
199 |
return tmp_file_path
|
200 |
|
201 |
|
202 |
+
def main():
|
203 |
+
example_enable = True
|
204 |
+
|
205 |
+
try:
|
206 |
+
print("预初始化SgLang引擎...")
|
207 |
+
from mineru.backend.vlm.vlm_analyze import ModelSingleton
|
208 |
+
modelsingleton = ModelSingleton()
|
209 |
+
predictor = modelsingleton.get_model(
|
210 |
+
"sglang-engine",
|
211 |
+
None,
|
212 |
+
'http://localhost:30000',
|
213 |
+
mem_fraction_static=0.5,
|
214 |
+
enable_torch_compile=True,
|
215 |
+
)
|
216 |
+
print("SgLang引擎初始化完成")
|
217 |
+
except Exception as e:
|
218 |
+
logger.exception(e)
|
219 |
+
|
220 |
with gr.Blocks() as demo:
|
221 |
gr.HTML(header)
|
222 |
with gr.Row():
|
223 |
with gr.Column(variant='panel', scale=5):
|
224 |
with gr.Row():
|
225 |
file = gr.File(label='Please upload a PDF or image', file_types=['.pdf', '.png', '.jpeg', '.jpg'])
|
|
|
|
|
|
|
|
|
|
|
226 |
with gr.Row():
|
227 |
+
max_pages = gr.Slider(1, 20, 10, step=1, label='Max convert pages')
|
228 |
+
with gr.Row():
|
229 |
+
backend = gr.Dropdown(["pipeline", "vlm-sglang-engine"], label="Backend", value="pipeline")
|
230 |
+
with gr.Row(visible=True) as ocr_options:
|
231 |
+
language = gr.Dropdown(all_lang, label='Language', value='ch')
|
232 |
+
with gr.Row(visible=False) as client_options:
|
233 |
+
url = gr.Textbox(label='Server URL', value='http://localhost:30000', placeholder='http://localhost:30000')
|
234 |
+
with gr.Row(visible=True) as pipeline_options:
|
235 |
is_ocr = gr.Checkbox(label='Force enable OCR', value=False)
|
236 |
formula_enable = gr.Checkbox(label='Enable formula recognition', value=True)
|
237 |
table_enable = gr.Checkbox(label='Enable table recognition(test)', value=True)
|
|
|
239 |
change_bu = gr.Button('Convert')
|
240 |
clear_bu = gr.ClearButton(value='Clear')
|
241 |
pdf_show = PDF(label='PDF preview', interactive=False, visible=True, height=800)
|
242 |
+
if example_enable:
|
243 |
example_root = os.path.join(os.path.dirname(__file__), 'examples')
|
244 |
+
if os.path.exists(example_root):
|
245 |
+
with gr.Accordion('Examples:'):
|
246 |
+
gr.Examples(
|
247 |
+
examples=[os.path.join(example_root, _) for _ in os.listdir(example_root) if
|
248 |
+
_.endswith('pdf')],
|
249 |
+
inputs=file
|
250 |
+
)
|
251 |
|
252 |
with gr.Column(variant='panel', scale=5):
|
253 |
output_file = gr.File(label='convert result', interactive=False)
|
|
|
258 |
line_breaks=True)
|
259 |
with gr.Tab('Markdown text'):
|
260 |
md_text = gr.TextArea(lines=45, show_copy_button=True)
|
261 |
+
|
262 |
+
|
263 |
+
# 更新界面函数
|
264 |
+
def update_interface(backend_choice):
|
265 |
+
if backend_choice in ["vlm-transformers", "vlm-sglang-engine"]:
|
266 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
267 |
+
elif backend_choice in ["vlm-sglang-client"]: # pipeline
|
268 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
269 |
+
elif backend_choice in ["pipeline"]:
|
270 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=True)
|
271 |
+
else:
|
272 |
+
pass
|
273 |
+
|
274 |
+
|
275 |
+
# 添加事件处理
|
276 |
+
backend.change(
|
277 |
+
fn=update_interface,
|
278 |
+
inputs=[backend],
|
279 |
+
outputs=[client_options, ocr_options, pipeline_options],
|
280 |
+
api_name=False
|
281 |
+
)
|
282 |
+
|
283 |
+
file.change(fn=to_pdf, inputs=file, outputs=pdf_show, api_name=False)
|
284 |
+
change_bu.click(fn=to_markdown, inputs=[file, max_pages, is_ocr, formula_enable, table_enable, language, backend, url],
|
285 |
+
outputs=[md, md_text, output_file, pdf_show], api_name=False)
|
286 |
clear_bu.add([file, md, pdf_show, md_text, output_file, is_ocr])
|
287 |
|
288 |
+
demo.launch(server_name='localhost', ssr_mode=True)
|
289 |
+
|
290 |
+
|
291 |
+
if __name__ == '__main__':
|
292 |
+
main()
|