Spaces:
Running on Zero
Running on Zero
Remove PDF upload and keep image parsing only
Browse files- README.md +1 -3
- __pycache__/app.cpython-312.pyc +0 -0
- app.py +13 -40
- requirements.txt +0 -1
README.md
CHANGED
|
@@ -17,9 +17,7 @@ models:
|
|
| 17 |
|
| 18 |
# MiniCPM-OCR on ZeroGPU
|
| 19 |
|
| 20 |
-
Upload one page image
|
| 21 |
`PaddlePaddle/PP-LCNet_x1_0_doc_ori_safetensors`, layout detection uses
|
| 22 |
`PaddlePaddle/PP-DocLayoutV3_safetensors`, and each region is read by
|
| 23 |
`openbmb/MiniCPM-OCR-1.0`. Seal and chart regions can be skipped.
|
| 24 |
-
|
| 25 |
-
A PDF is limited to the first two pages so one visit fits the ZeroGPU quota.
|
|
|
|
| 17 |
|
| 18 |
# MiniCPM-OCR on ZeroGPU
|
| 19 |
|
| 20 |
+
Upload one page image. Optional upright correction uses
|
| 21 |
`PaddlePaddle/PP-LCNet_x1_0_doc_ori_safetensors`, layout detection uses
|
| 22 |
`PaddlePaddle/PP-DocLayoutV3_safetensors`, and each region is read by
|
| 23 |
`openbmb/MiniCPM-OCR-1.0`. Seal and chart regions can be skipped.
|
|
|
|
|
|
__pycache__/app.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
|
|
|
app.py
CHANGED
|
@@ -39,7 +39,6 @@ from transformers import (
|
|
| 39 |
OCR_ID = os.environ.get("OCR_MODEL_ID", "openbmb/MiniCPM-OCR-1.0")
|
| 40 |
LAYOUT_ID = os.environ.get("LAYOUT_MODEL_ID", "PaddlePaddle/PP-DocLayoutV3_safetensors")
|
| 41 |
ORI_ID = os.environ.get("ORI_MODEL_ID", "PaddlePaddle/PP-LCNet_x1_0_doc_ori_safetensors")
|
| 42 |
-
MAX_PDF_PAGES = 2
|
| 43 |
|
| 44 |
PROMPTS = {
|
| 45 |
"text": ("Text Recognition:", 2048),
|
|
@@ -79,9 +78,9 @@ ocr_processor = AutoProcessor.from_pretrained(OCR_ID)
|
|
| 79 |
ocr_model = MiniCPMOcrForConditionalGeneration.from_pretrained(OCR_ID, dtype=torch.bfloat16).to("cuda").eval()
|
| 80 |
|
| 81 |
|
| 82 |
-
def _duration(image,
|
| 83 |
del image, upright, recognize_seal, recognize_chart
|
| 84 |
-
return
|
| 85 |
|
| 86 |
|
| 87 |
def _numbers(value):
|
|
@@ -367,43 +366,18 @@ def _parse_page(image: Image.Image, upright: bool, recognize_seal: bool, recogni
|
|
| 367 |
return "\n\n".join(pieces) or "这一页没有识别出内容。"
|
| 368 |
|
| 369 |
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
pages = []
|
| 376 |
-
try:
|
| 377 |
-
for index in range(min(len(document), MAX_PDF_PAGES)):
|
| 378 |
-
pixmap = document[index].get_pixmap(matrix=fitz.Matrix(2, 2), alpha=False)
|
| 379 |
-
pages.append(Image.frombytes("RGB", (pixmap.width, pixmap.height), pixmap.samples))
|
| 380 |
-
finally:
|
| 381 |
-
document.close()
|
| 382 |
-
return pages
|
| 383 |
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
return _parse_page(page, bool(upright), bool(recognize_seal), bool(recognize_chart))
|
| 389 |
-
|
| 390 |
-
if pdf is not None:
|
| 391 |
-
pages = _pdf_pages(pdf)
|
| 392 |
-
if len(pages) > 1:
|
| 393 |
-
parts = [f"## 第 {index + 1} 页\n\n{one(page)}" for index, page in enumerate(pages)]
|
| 394 |
-
return "\n\n".join(parts)
|
| 395 |
-
if pages:
|
| 396 |
-
return one(pages[0])
|
| 397 |
-
if image is not None:
|
| 398 |
-
return one(image)
|
| 399 |
-
raise gr.Error("请上传一张图片,或一份 PDF。")
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
def run(image, pdf, upright, recognize_seal, recognize_chart):
|
| 403 |
-
if image is None and pdf is None:
|
| 404 |
-
raise gr.Error("请上传一张图片,或一份 PDF。")
|
| 405 |
yield "正在解析…"
|
| 406 |
-
yield parse(image,
|
| 407 |
|
| 408 |
|
| 409 |
if os.environ.get("OCR_SKIP_LAUNCH") != "1":
|
|
@@ -413,13 +387,12 @@ if os.environ.get("OCR_SKIP_LAUNCH") != "1":
|
|
| 413 |
gr.Markdown(
|
| 414 |
"""
|
| 415 |
# 文档解析
|
| 416 |
-
上传一
|
| 417 |
"""
|
| 418 |
)
|
| 419 |
with gr.Row():
|
| 420 |
with gr.Column():
|
| 421 |
image = gr.Image(type="pil", label="图片")
|
| 422 |
-
pdf = gr.File(label="PDF", file_types=[".pdf"])
|
| 423 |
upright = gr.Checkbox(label="自适应旋转", value=True)
|
| 424 |
recognize_seal = gr.Checkbox(label="印章识别", value=True)
|
| 425 |
recognize_chart = gr.Checkbox(label="图表识别", value=True)
|
|
@@ -435,7 +408,7 @@ if os.environ.get("OCR_SKIP_LAUNCH") != "1":
|
|
| 435 |
)
|
| 436 |
button.click(
|
| 437 |
run,
|
| 438 |
-
inputs=[image,
|
| 439 |
outputs=markdown,
|
| 440 |
show_progress="full",
|
| 441 |
)
|
|
|
|
| 39 |
OCR_ID = os.environ.get("OCR_MODEL_ID", "openbmb/MiniCPM-OCR-1.0")
|
| 40 |
LAYOUT_ID = os.environ.get("LAYOUT_MODEL_ID", "PaddlePaddle/PP-DocLayoutV3_safetensors")
|
| 41 |
ORI_ID = os.environ.get("ORI_MODEL_ID", "PaddlePaddle/PP-LCNet_x1_0_doc_ori_safetensors")
|
|
|
|
| 42 |
|
| 43 |
PROMPTS = {
|
| 44 |
"text": ("Text Recognition:", 2048),
|
|
|
|
| 78 |
ocr_model = MiniCPMOcrForConditionalGeneration.from_pretrained(OCR_ID, dtype=torch.bfloat16).to("cuda").eval()
|
| 79 |
|
| 80 |
|
| 81 |
+
def _duration(image, upright, recognize_seal, recognize_chart):
|
| 82 |
del image, upright, recognize_seal, recognize_chart
|
| 83 |
+
return 90
|
| 84 |
|
| 85 |
|
| 86 |
def _numbers(value):
|
|
|
|
| 366 |
return "\n\n".join(pieces) or "这一页没有识别出内容。"
|
| 367 |
|
| 368 |
|
| 369 |
+
@spaces.GPU(duration=_duration)
|
| 370 |
+
def parse(image, upright, recognize_seal, recognize_chart):
|
| 371 |
+
if image is None:
|
| 372 |
+
raise gr.Error("请上传一张图片。")
|
| 373 |
+
return _parse_page(image, bool(upright), bool(recognize_seal), bool(recognize_chart))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 374 |
|
| 375 |
|
| 376 |
+
def run(image, upright, recognize_seal, recognize_chart):
|
| 377 |
+
if image is None:
|
| 378 |
+
raise gr.Error("请上传一张图片。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
yield "正在解析…"
|
| 380 |
+
yield parse(image, upright, recognize_seal, recognize_chart)
|
| 381 |
|
| 382 |
|
| 383 |
if os.environ.get("OCR_SKIP_LAUNCH") != "1":
|
|
|
|
| 387 |
gr.Markdown(
|
| 388 |
"""
|
| 389 |
# 文档解析
|
| 390 |
+
上传一张图片。开启自适应旋转时,先做小角度摆正和 0°/90°/180°/270° 转向,再做版面检测和识别。
|
| 391 |
"""
|
| 392 |
)
|
| 393 |
with gr.Row():
|
| 394 |
with gr.Column():
|
| 395 |
image = gr.Image(type="pil", label="图片")
|
|
|
|
| 396 |
upright = gr.Checkbox(label="自适应旋转", value=True)
|
| 397 |
recognize_seal = gr.Checkbox(label="印章识别", value=True)
|
| 398 |
recognize_chart = gr.Checkbox(label="图表识别", value=True)
|
|
|
|
| 408 |
)
|
| 409 |
button.click(
|
| 410 |
run,
|
| 411 |
+
inputs=[image, upright, recognize_seal, recognize_chart],
|
| 412 |
outputs=markdown,
|
| 413 |
show_progress="full",
|
| 414 |
)
|
requirements.txt
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
gradio==6.31.0
|
| 2 |
transformers==5.21.0
|
| 3 |
torchvision==0.28.0
|
| 4 |
-
pymupdf
|
| 5 |
opencv-python-headless
|
| 6 |
pillow
|
|
|
|
| 1 |
gradio==6.31.0
|
| 2 |
transformers==5.21.0
|
| 3 |
torchvision==0.28.0
|
|
|
|
| 4 |
opencv-python-headless
|
| 5 |
pillow
|