Spaces:
Sleeping
Sleeping
fix: process pdf once
Browse files
app.py
CHANGED
@@ -3,11 +3,10 @@ import os
|
|
3 |
import uuid
|
4 |
|
5 |
import gradio as gr
|
|
|
6 |
import torch
|
7 |
from transformers import AutoConfig, AutoModel, AutoTokenizer
|
8 |
|
9 |
-
from got_ocr import got_ocr
|
10 |
-
|
11 |
# 初始化模型和分词器
|
12 |
model_name = "ucaslcl/GOT-OCR2_0"
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -24,6 +23,42 @@ UPLOAD_FOLDER = "./uploads"
|
|
24 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
25 |
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
def perform_ocr(image):
|
28 |
if image is None:
|
29 |
return "请上传图片"
|
|
|
3 |
import uuid
|
4 |
|
5 |
import gradio as gr
|
6 |
+
import spaces
|
7 |
import torch
|
8 |
from transformers import AutoConfig, AutoModel, AutoTokenizer
|
9 |
|
|
|
|
|
10 |
# 初始化模型和分词器
|
11 |
model_name = "ucaslcl/GOT-OCR2_0"
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
23 |
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
24 |
|
25 |
|
26 |
+
@spaces.GPU()
|
27 |
+
def got_ocr(model, tokenizer, image_path, got_mode="format texts OCR", fine_grained_mode="", ocr_color="", ocr_box=""):
|
28 |
+
# 执行OCR
|
29 |
+
try:
|
30 |
+
if got_mode == "plain texts OCR":
|
31 |
+
res = model.chat(tokenizer, image_path, ocr_type="ocr")
|
32 |
+
return res, None
|
33 |
+
elif got_mode == "format texts OCR":
|
34 |
+
result_path = f"{os.path.splitext(image_path)[0]}_result.html"
|
35 |
+
res = model.chat(tokenizer, image_path, ocr_type="format", render=True, save_render_file=result_path)
|
36 |
+
elif got_mode == "plain multi-crop OCR":
|
37 |
+
res = model.chat_crop(tokenizer, image_path, ocr_type="ocr")
|
38 |
+
return res, None
|
39 |
+
elif got_mode == "format multi-crop OCR":
|
40 |
+
result_path = f"{os.path.splitext(image_path)[0]}_result.html"
|
41 |
+
res = model.chat_crop(tokenizer, image_path, ocr_type="format", render=True, save_render_file=result_path)
|
42 |
+
elif got_mode == "plain fine-grained OCR":
|
43 |
+
res = model.chat(tokenizer, image_path, ocr_type="ocr", ocr_box=ocr_box, ocr_color=ocr_color)
|
44 |
+
return res, None
|
45 |
+
elif got_mode == "format fine-grained OCR":
|
46 |
+
result_path = f"{os.path.splitext(image_path)[0]}_result.html"
|
47 |
+
res = model.chat(tokenizer, image_path, ocr_type="format", ocr_box=ocr_box, ocr_color=ocr_color, render=True, save_render_file=result_path)
|
48 |
+
|
49 |
+
# 处理格式化结果
|
50 |
+
if "format" in got_mode and os.path.exists(result_path):
|
51 |
+
with open(result_path, "r") as f:
|
52 |
+
html_content = f.read()
|
53 |
+
encoded_html = base64.b64encode(html_content.encode("utf-8")).decode("utf-8")
|
54 |
+
return res, encoded_html
|
55 |
+
else:
|
56 |
+
return res, None
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
return f"错误: {str(e)}", None
|
60 |
+
|
61 |
+
|
62 |
def perform_ocr(image):
|
63 |
if image is None:
|
64 |
return "请上传图片"
|