Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,22 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
from paddleocr import PaddleOCR
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
import os
|
| 8 |
-
import time
|
| 9 |
|
| 10 |
# --- 配置 ---
|
| 11 |
-
OUTPUT_DIR = "output_results"
|
| 12 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
# --- 模型加载器 ---
|
|
|
|
| 15 |
def load_gpu_model():
|
| 16 |
-
print("
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
print("GPU模型加载完成。")
|
| 21 |
return ocr_model
|
| 22 |
|
| 23 |
# --- Gradio调用的核心处理函数 ---
|
|
@@ -30,12 +29,12 @@ def process_pdf_max_speed(pdf_file, progress=gr.Progress(track_tqdm=True)):
|
|
| 30 |
return "请先上传一个PDF文件。", None
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
# 在GPU
|
| 34 |
ocr = load_gpu_model()
|
| 35 |
|
| 36 |
doc = fitz.open(pdf_file.name)
|
| 37 |
total_pages = len(doc)
|
| 38 |
-
batch_size = 4 #
|
| 39 |
full_text_result = []
|
| 40 |
|
| 41 |
for i in progress.tqdm(range(0, total_pages, batch_size), desc="🚀 批处理中..."):
|
|
@@ -48,7 +47,8 @@ def process_pdf_max_speed(pdf_file, progress=gr.Progress(track_tqdm=True)):
|
|
| 48 |
batch_images.append(np.array(img))
|
| 49 |
|
| 50 |
if batch_images:
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
for page_index, page_result in enumerate(results):
|
| 54 |
page_texts = []
|
|
@@ -78,8 +78,8 @@ def process_pdf_max_speed(pdf_file, progress=gr.Progress(track_tqdm=True)):
|
|
| 78 |
with gr.Blocks(title="极速PDF识别器", theme=gr.themes.Soft()) as demo:
|
| 79 |
gr.Markdown(
|
| 80 |
"""
|
| 81 |
-
# 🔥 极速PDF识别器 (GPU
|
| 82 |
-
|
| 83 |
"""
|
| 84 |
)
|
| 85 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
from paddleocr import PaddleOCR
|
| 4 |
import fitz # PyMuPDF
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
import os
|
|
|
|
| 8 |
|
| 9 |
# --- 配置 ---
|
| 10 |
+
OUTPUT_DIR = "output_results"
|
| 11 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 12 |
|
| 13 |
# --- 模型加载器 ---
|
| 14 |
+
# 这个函数会在GPU会话中被调用
|
| 15 |
def load_gpu_model():
|
| 16 |
+
print("正在加载经过版本锁定的PaddleOCR GPU模型...")
|
| 17 |
+
# 使用与锁定版本兼容的参数:use_gpu=True是必需的
|
| 18 |
+
ocr_model = PaddleOCR(use_angle_cls=True, lang='ch', use_gpu=True)
|
| 19 |
+
print("GPU模型加载成功。")
|
|
|
|
| 20 |
return ocr_model
|
| 21 |
|
| 22 |
# --- Gradio调用的核心处理函数 ---
|
|
|
|
| 29 |
return "请先上传一个PDF文件。", None
|
| 30 |
|
| 31 |
try:
|
| 32 |
+
# 在GPU会札中加载模型
|
| 33 |
ocr = load_gpu_model()
|
| 34 |
|
| 35 |
doc = fitz.open(pdf_file.name)
|
| 36 |
total_pages = len(doc)
|
| 37 |
+
batch_size = 4 # 批处理大小
|
| 38 |
full_text_result = []
|
| 39 |
|
| 40 |
for i in progress.tqdm(range(0, total_pages, batch_size), desc="🚀 批处理中..."):
|
|
|
|
| 47 |
batch_images.append(np.array(img))
|
| 48 |
|
| 49 |
if batch_images:
|
| 50 |
+
# 使用与锁定版本兼容的调用方式:需要 cls=True
|
| 51 |
+
results = ocr.ocr(batch_images, cls=True)
|
| 52 |
|
| 53 |
for page_index, page_result in enumerate(results):
|
| 54 |
page_texts = []
|
|
|
|
| 78 |
with gr.Blocks(title="极速PDF识别器", theme=gr.themes.Soft()) as demo:
|
| 79 |
gr.Markdown(
|
| 80 |
"""
|
| 81 |
+
# 🔥 极速PDF识别器 (GPU加速稳定版) 🔥
|
| 82 |
+
**速度拉满!实时进度显示,处理期间请勿关闭页面。**
|
| 83 |
"""
|
| 84 |
)
|
| 85 |
|