Spaces:
Running
Running
fix: process pdf once
Browse files- .gitignore +1 -0
- app-pdf.py +164 -0
- app.py +35 -34
.gitignore
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
results/*
|
2 |
uploads/*
|
|
|
|
1 |
results/*
|
2 |
uploads/*
|
3 |
+
__pycache__
|
app-pdf.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
import uuid
|
4 |
+
|
5 |
+
import fitz # PyMuPDF
|
6 |
+
import gradio as gr
|
7 |
+
import torch
|
8 |
+
from PIL import Image, ImageEnhance
|
9 |
+
from transformers import AutoConfig, AutoModel, AutoTokenizer
|
10 |
+
|
11 |
+
from got_ocr import got_ocr
|
12 |
+
|
13 |
+
# 初始化模型和分词器
|
14 |
+
# model_name = "stepfun-ai/GOT-OCR2_0"
|
15 |
+
model_name = "ucaslcl/GOT-OCR2_0"
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
19 |
+
config = AutoConfig.from_pretrained(model_name, trust_remote_code=True)
|
20 |
+
model = AutoModel.from_pretrained(model_name, trust_remote_code=True, low_cpu_mem_usage=True, device_map="cuda", use_safetensors=True)
|
21 |
+
model = model.eval().to(device)
|
22 |
+
model.config.pad_token_id = tokenizer.eos_token_id
|
23 |
+
|
24 |
+
UPLOAD_FOLDER = "./uploads"
|
25 |
+
RESULTS_FOLDER = "./results"
|
26 |
+
|
27 |
+
# 确保必要的文件夹存在
|
28 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
29 |
+
os.makedirs(RESULTS_FOLDER, exist_ok=True)
|
30 |
+
|
31 |
+
|
32 |
+
def pdf_to_images(pdf_path):
|
33 |
+
images = []
|
34 |
+
pdf_document = fitz.open(pdf_path)
|
35 |
+
for page_num in range(len(pdf_document)):
|
36 |
+
page = pdf_document.load_page(page_num)
|
37 |
+
# 进一步增加分辨率和缩放比例
|
38 |
+
zoom = 10 # 增加缩放比例到4
|
39 |
+
mat = fitz.Matrix(zoom, zoom)
|
40 |
+
pix = page.get_pixmap(matrix=mat, alpha=False)
|
41 |
+
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
42 |
+
|
43 |
+
# 增对比度
|
44 |
+
enhancer = ImageEnhance.Contrast(img)
|
45 |
+
img = enhancer.enhance(1.5) # 增加50%的对比度
|
46 |
+
|
47 |
+
images.append(img)
|
48 |
+
pdf_document.close()
|
49 |
+
return images
|
50 |
+
|
51 |
+
|
52 |
+
def process_pdf(pdf_file):
|
53 |
+
if pdf_file is None:
|
54 |
+
return None
|
55 |
+
|
56 |
+
temp_pdf_path = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}.pdf")
|
57 |
+
|
58 |
+
# 使用 shutil 复制上传的件到临时位置
|
59 |
+
shutil.copy(pdf_file.name, temp_pdf_path)
|
60 |
+
|
61 |
+
images = pdf_to_images(temp_pdf_path)
|
62 |
+
os.remove(temp_pdf_path)
|
63 |
+
|
64 |
+
# 将图像保存为临时文件并返回文件路径列表
|
65 |
+
image_paths = []
|
66 |
+
for i, img in enumerate(images):
|
67 |
+
img_path = os.path.join(RESULTS_FOLDER, f"page_{i+1}.png")
|
68 |
+
img.save(img_path, "PNG")
|
69 |
+
image_paths.append(img_path)
|
70 |
+
|
71 |
+
return image_paths
|
72 |
+
|
73 |
+
|
74 |
+
def on_image_select(evt: gr.SelectData):
|
75 |
+
if evt.index is not None:
|
76 |
+
return evt.index
|
77 |
+
return None
|
78 |
+
|
79 |
+
|
80 |
+
# 更新perform_ocr函数的输入参数
|
81 |
+
def perform_ocr(selected_index, image_gallery, got_mode, fine_grained_type, color, box):
|
82 |
+
if selected_index is None or len(image_gallery) == 0:
|
83 |
+
return "请先选择一张图片"
|
84 |
+
|
85 |
+
selected_image = image_gallery[selected_index][0]
|
86 |
+
|
87 |
+
# 根据选择的任务和参数调用GOT OCR
|
88 |
+
ocr_color = color if fine_grained_type == "color" else ""
|
89 |
+
ocr_box = box if fine_grained_type == "box" else ""
|
90 |
+
|
91 |
+
result, html_content = got_ocr(
|
92 |
+
model,
|
93 |
+
tokenizer,
|
94 |
+
selected_image,
|
95 |
+
got_mode=got_mode,
|
96 |
+
fine_grained_mode=fine_grained_type,
|
97 |
+
ocr_color=ocr_color,
|
98 |
+
ocr_box=ocr_box,
|
99 |
+
)
|
100 |
+
|
101 |
+
if html_content:
|
102 |
+
iframe_src = f"data:text/html;base64,{html_content}"
|
103 |
+
iframe = f'<iframe src="{iframe_src}" width="100%" height="600px"></iframe>'
|
104 |
+
download_link = f'<a href="data:text/html;base64,{html_content}" download="result.html">下载完整结果</a>'
|
105 |
+
return gr.Markdown(f"{download_link}\n\n{iframe}")
|
106 |
+
else:
|
107 |
+
return gr.Markdown(result)
|
108 |
+
|
109 |
+
|
110 |
+
def task_update(task):
|
111 |
+
if "fine-grained" in task:
|
112 |
+
return [gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)]
|
113 |
+
else:
|
114 |
+
return [gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)]
|
115 |
+
|
116 |
+
|
117 |
+
def fine_grained_update(fine_grained_type):
|
118 |
+
if fine_grained_type == "color":
|
119 |
+
return [gr.update(visible=True), gr.update(visible=False)]
|
120 |
+
elif fine_grained_type == "box":
|
121 |
+
return [gr.update(visible=False), gr.update(visible=True)]
|
122 |
+
else:
|
123 |
+
return [gr.update(visible=False), gr.update(visible=False)]
|
124 |
+
|
125 |
+
|
126 |
+
with gr.Blocks() as demo:
|
127 |
+
pdf_input = gr.File(label="上传PDF文件")
|
128 |
+
image_gallery = gr.Gallery(
|
129 |
+
label="PDF页面预览",
|
130 |
+
columns=3,
|
131 |
+
height=600,
|
132 |
+
object_fit="contain",
|
133 |
+
preview=True,
|
134 |
+
)
|
135 |
+
selected_index = gr.State(None)
|
136 |
+
pdf_input.upload(fn=process_pdf, inputs=pdf_input, outputs=image_gallery)
|
137 |
+
image_gallery.select(fn=on_image_select, inputs=[], outputs=selected_index)
|
138 |
+
|
139 |
+
####################
|
140 |
+
task_dropdown = gr.Dropdown(
|
141 |
+
choices=["plain texts OCR", "format texts OCR", "plain multi-crop OCR", "format multi-crop OCR", "plain fine-grained OCR", "format fine-grained OCR"],
|
142 |
+
label="选择GOT模式",
|
143 |
+
value="format texts OCR",
|
144 |
+
)
|
145 |
+
fine_grained_dropdown = gr.Dropdown(choices=["box", "color"], label="fine-grained类型", visible=False)
|
146 |
+
color_dropdown = gr.Dropdown(choices=["red", "green", "blue"], label="颜色列表", visible=False)
|
147 |
+
box_input = gr.Textbox(label="输入框: [x1,y1,x2,y2]", placeholder="例如: [0,0,100,100]", visible=False)
|
148 |
+
|
149 |
+
ocr_button = gr.Button("开始OCR识别")
|
150 |
+
ocr_result = gr.HTML(label="OCR结果") # 将Textbox更改为HTML组件
|
151 |
+
|
152 |
+
task_dropdown.change(task_update, inputs=[task_dropdown], outputs=[fine_grained_dropdown, color_dropdown, box_input])
|
153 |
+
fine_grained_dropdown.change(fine_grained_update, inputs=[fine_grained_dropdown], outputs=[color_dropdown, box_input])
|
154 |
+
|
155 |
+
# 更新ocr_button的click事件,传递所有必要的参数
|
156 |
+
ocr_button.click(
|
157 |
+
fn=perform_ocr,
|
158 |
+
inputs=[selected_index, image_gallery, task_dropdown, fine_grained_dropdown, color_dropdown, box_input],
|
159 |
+
outputs=ocr_result,
|
160 |
+
)
|
161 |
+
|
162 |
+
|
163 |
+
if __name__ == "__main__":
|
164 |
+
demo.launch()
|
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
import shutil
|
3 |
import uuid
|
@@ -78,33 +79,37 @@ def on_image_select(evt: gr.SelectData):
|
|
78 |
|
79 |
|
80 |
# 更新perform_ocr函数的输入参数
|
81 |
-
def perform_ocr(
|
82 |
-
if
|
83 |
-
return "
|
84 |
-
|
85 |
-
selected_image = image_gallery[selected_index][0]
|
86 |
-
|
87 |
-
# 根据选择的任务和参数调用GOT OCR
|
88 |
-
ocr_color = color if fine_grained_type == "color" else ""
|
89 |
-
ocr_box = box if fine_grained_type == "box" else ""
|
90 |
-
|
91 |
-
result, html_content = got_ocr(
|
92 |
-
model,
|
93 |
-
tokenizer,
|
94 |
-
selected_image,
|
95 |
-
got_mode=got_mode,
|
96 |
-
fine_grained_mode=fine_grained_type,
|
97 |
-
ocr_color=ocr_color,
|
98 |
-
ocr_box=ocr_box,
|
99 |
-
)
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
109 |
|
110 |
def task_update(task):
|
@@ -132,11 +137,8 @@ with gr.Blocks() as demo:
|
|
132 |
object_fit="contain",
|
133 |
preview=True,
|
134 |
)
|
135 |
-
selected_index = gr.State(None)
|
136 |
pdf_input.upload(fn=process_pdf, inputs=pdf_input, outputs=image_gallery)
|
137 |
-
image_gallery.select(fn=on_image_select, inputs=[], outputs=selected_index)
|
138 |
|
139 |
-
####################
|
140 |
task_dropdown = gr.Dropdown(
|
141 |
choices=["plain texts OCR", "format texts OCR", "plain multi-crop OCR", "format multi-crop OCR", "plain fine-grained OCR", "format fine-grained OCR"],
|
142 |
label="选择GOT模式",
|
@@ -147,18 +149,17 @@ with gr.Blocks() as demo:
|
|
147 |
box_input = gr.Textbox(label="输入框: [x1,y1,x2,y2]", placeholder="例如: [0,0,100,100]", visible=False)
|
148 |
|
149 |
ocr_button = gr.Button("开始OCR识别")
|
150 |
-
ocr_result = gr.
|
|
|
151 |
|
152 |
task_dropdown.change(task_update, inputs=[task_dropdown], outputs=[fine_grained_dropdown, color_dropdown, box_input])
|
153 |
fine_grained_dropdown.change(fine_grained_update, inputs=[fine_grained_dropdown], outputs=[color_dropdown, box_input])
|
154 |
|
155 |
-
# 更新ocr_button的click事件,传递所有必要的参数
|
156 |
ocr_button.click(
|
157 |
fn=perform_ocr,
|
158 |
-
inputs=[
|
159 |
-
outputs=ocr_result,
|
160 |
)
|
161 |
|
162 |
-
|
163 |
if __name__ == "__main__":
|
164 |
demo.launch()
|
|
|
1 |
+
import base64
|
2 |
import os
|
3 |
import shutil
|
4 |
import uuid
|
|
|
79 |
|
80 |
|
81 |
# 更新perform_ocr函数的输入参数
|
82 |
+
def perform_ocr(image_gallery, got_mode, fine_grained_type, color, box):
|
83 |
+
if len(image_gallery) == 0:
|
84 |
+
return "请先上传PDF文件", None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
results = []
|
87 |
+
progress = gr.Progress()
|
88 |
+
|
89 |
+
for i, image_info in enumerate(progress.tqdm(image_gallery)):
|
90 |
+
selected_image = image_info[0]
|
91 |
+
|
92 |
+
ocr_color = color if fine_grained_type == "color" else ""
|
93 |
+
ocr_box = box if fine_grained_type == "box" else ""
|
94 |
+
|
95 |
+
result, _ = got_ocr(
|
96 |
+
model,
|
97 |
+
tokenizer,
|
98 |
+
selected_image,
|
99 |
+
got_mode=got_mode,
|
100 |
+
fine_grained_mode=fine_grained_type,
|
101 |
+
ocr_color=ocr_color,
|
102 |
+
ocr_box=ocr_box,
|
103 |
+
)
|
104 |
+
results.append(f"第 {i+1} 页结果:\n{result}\n\n")
|
105 |
+
|
106 |
+
combined_result = "".join(results)
|
107 |
+
|
108 |
+
# 生成下载链接
|
109 |
+
encoded_result = base64.b64encode(combined_result.encode("utf-8")).decode("utf-8")
|
110 |
+
download_link = f'<a href="data:text/plain;base64,{encoded_result}" download="ocr_result.txt">下载完整OCR结果</a>'
|
111 |
+
|
112 |
+
return gr.Markdown(f"{download_link}\n\n{combined_result[:1000]}..."), combined_result
|
113 |
|
114 |
|
115 |
def task_update(task):
|
|
|
137 |
object_fit="contain",
|
138 |
preview=True,
|
139 |
)
|
|
|
140 |
pdf_input.upload(fn=process_pdf, inputs=pdf_input, outputs=image_gallery)
|
|
|
141 |
|
|
|
142 |
task_dropdown = gr.Dropdown(
|
143 |
choices=["plain texts OCR", "format texts OCR", "plain multi-crop OCR", "format multi-crop OCR", "plain fine-grained OCR", "format fine-grained OCR"],
|
144 |
label="选择GOT模式",
|
|
|
149 |
box_input = gr.Textbox(label="输入框: [x1,y1,x2,y2]", placeholder="例如: [0,0,100,100]", visible=False)
|
150 |
|
151 |
ocr_button = gr.Button("开始OCR识别")
|
152 |
+
ocr_result = gr.Markdown(label="OCR结果预览")
|
153 |
+
full_result = gr.State()
|
154 |
|
155 |
task_dropdown.change(task_update, inputs=[task_dropdown], outputs=[fine_grained_dropdown, color_dropdown, box_input])
|
156 |
fine_grained_dropdown.change(fine_grained_update, inputs=[fine_grained_dropdown], outputs=[color_dropdown, box_input])
|
157 |
|
|
|
158 |
ocr_button.click(
|
159 |
fn=perform_ocr,
|
160 |
+
inputs=[image_gallery, task_dropdown, fine_grained_dropdown, color_dropdown, box_input],
|
161 |
+
outputs=[ocr_result, full_result],
|
162 |
)
|
163 |
|
|
|
164 |
if __name__ == "__main__":
|
165 |
demo.launch()
|