Upload 4 files
Browse files- Dockerfile +34 -0
- app.py +44 -0
- download_models.py +69 -0
- requirements.txt +13 -0
Dockerfile
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9-slim
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
# 安装系统依赖
|
6 |
+
RUN apt-get update && apt-get install -y \
|
7 |
+
build-essential \
|
8 |
+
libpq-dev \
|
9 |
+
tesseract-ocr \
|
10 |
+
poppler-utils \
|
11 |
+
git \
|
12 |
+
&& rm -rf /var/lib/apt/lists/*
|
13 |
+
|
14 |
+
# 克隆 MinerU 项目
|
15 |
+
RUN git clone https://github.com/myhloli/MinerU.git /tmp/MinerU \
|
16 |
+
&& cp -r /tmp/MinerU/magic_pdf /app/magic_pdf \
|
17 |
+
&& rm -rf /tmp/MinerU
|
18 |
+
|
19 |
+
# 复制必要文件
|
20 |
+
COPY requirements.txt .
|
21 |
+
COPY app.py .
|
22 |
+
COPY download_models.py .
|
23 |
+
|
24 |
+
# 安装 Python 依赖
|
25 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
26 |
+
|
27 |
+
# 下载模型
|
28 |
+
RUN python download_models.py
|
29 |
+
|
30 |
+
# 设置环境变量
|
31 |
+
ENV PORT=7860
|
32 |
+
|
33 |
+
# 启动应用
|
34 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1"]
|
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from magic_pdf.magic_pdf import MagicPDF
|
3 |
+
import tempfile
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import shutil
|
7 |
+
|
8 |
+
app = FastAPI(title="MinerU PDF Processing API",
|
9 |
+
description="API for processing PDF documents using MinerU",
|
10 |
+
version="1.0.0")
|
11 |
+
|
12 |
+
# 初始化 MagicPDF
|
13 |
+
config_file = os.path.join(os.path.expanduser('~'), 'magic-pdf.json')
|
14 |
+
magic_pdf = MagicPDF(config_file)
|
15 |
+
|
16 |
+
@app.post("/process_pdf")
|
17 |
+
async def process_pdf(file: UploadFile = File(...)):
|
18 |
+
"""
|
19 |
+
处理上传的PDF文件并返回分析结果
|
20 |
+
"""
|
21 |
+
if not file.filename.lower().endswith('.pdf'):
|
22 |
+
return {"error": "Only PDF files are supported"}
|
23 |
+
|
24 |
+
try:
|
25 |
+
# 创建临时目录
|
26 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
27 |
+
# 保存上传的文件
|
28 |
+
temp_pdf = os.path.join(temp_dir, file.filename)
|
29 |
+
with open(temp_pdf, "wb") as buffer:
|
30 |
+
shutil.copyfileobj(file.file, buffer)
|
31 |
+
|
32 |
+
# 处理 PDF
|
33 |
+
result = magic_pdf.process(temp_pdf)
|
34 |
+
|
35 |
+
return result
|
36 |
+
except Exception as e:
|
37 |
+
return {"error": str(e)}
|
38 |
+
|
39 |
+
@app.get("/health")
|
40 |
+
async def health_check():
|
41 |
+
"""
|
42 |
+
健康检查接口
|
43 |
+
"""
|
44 |
+
return {"status": "healthy", "version": "1.0.0"}
|
download_models.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
|
4 |
+
import requests
|
5 |
+
from modelscope import snapshot_download
|
6 |
+
|
7 |
+
|
8 |
+
def download_json(url):
|
9 |
+
response = requests.get(url)
|
10 |
+
response.raise_for_status()
|
11 |
+
return response.json()
|
12 |
+
|
13 |
+
|
14 |
+
def download_and_modify_json(url, local_filename, modifications):
|
15 |
+
if os.path.exists(local_filename):
|
16 |
+
data = json.load(open(local_filename))
|
17 |
+
config_version = data.get('config_version', '0.0.0')
|
18 |
+
if config_version < '1.0.0':
|
19 |
+
data = download_json(url)
|
20 |
+
else:
|
21 |
+
data = download_json(url)
|
22 |
+
|
23 |
+
# 修改内容
|
24 |
+
for key, value in modifications.items():
|
25 |
+
data[key] = value
|
26 |
+
|
27 |
+
# 保存修改后的内容
|
28 |
+
with open(local_filename, 'w', encoding='utf-8') as f:
|
29 |
+
json.dump(data, f, ensure_ascii=False, indent=4)
|
30 |
+
|
31 |
+
|
32 |
+
if __name__ == '__main__':
|
33 |
+
# 在 Docker 环境中使用 /app 目录
|
34 |
+
base_dir = "/app"
|
35 |
+
models_dir = os.path.join(base_dir, "models")
|
36 |
+
os.makedirs(models_dir, exist_ok=True)
|
37 |
+
|
38 |
+
mineru_patterns = [
|
39 |
+
"models/Layout/LayoutLMv3/*",
|
40 |
+
"models/Layout/YOLO/*",
|
41 |
+
"models/MFD/YOLO/*",
|
42 |
+
"models/MFR/unimernet_small/*",
|
43 |
+
"models/TabRec/TableMaster/*",
|
44 |
+
"models/TabRec/StructEqTable/*",
|
45 |
+
]
|
46 |
+
|
47 |
+
# 下载模型到 Docker 容器中的固定路径
|
48 |
+
model_dir = snapshot_download('opendatalab/PDF-Extract-Kit-1.0',
|
49 |
+
cache_dir=models_dir,
|
50 |
+
allow_patterns=mineru_patterns)
|
51 |
+
layoutreader_model_dir = snapshot_download('ppaanngggg/layoutreader',
|
52 |
+
cache_dir=models_dir)
|
53 |
+
|
54 |
+
model_dir = os.path.join(model_dir, 'models')
|
55 |
+
print(f'model_dir is: {model_dir}')
|
56 |
+
print(f'layoutreader_model_dir is: {layoutreader_model_dir}')
|
57 |
+
|
58 |
+
# 配置文件保存到 Docker 容器的固定路径
|
59 |
+
json_url = 'https://gitee.com/myhloli/MinerU/raw/master/magic-pdf.template.json'
|
60 |
+
config_file_name = 'magic-pdf.json'
|
61 |
+
config_file = os.path.join(base_dir, config_file_name)
|
62 |
+
|
63 |
+
json_mods = {
|
64 |
+
'models-dir': model_dir,
|
65 |
+
'layoutreader-model-dir': layoutreader_model_dir,
|
66 |
+
}
|
67 |
+
|
68 |
+
download_and_modify_json(json_url, config_file, json_mods)
|
69 |
+
print(f'配置文件已成功配置,路径为: {config_file}')
|
requirements.txt
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.104.1
|
2 |
+
python-multipart==0.0.6
|
3 |
+
uvicorn==0.24.0
|
4 |
+
modelscope==1.9.5
|
5 |
+
torch==2.1.0
|
6 |
+
transformers==4.35.2
|
7 |
+
opencv-python-headless==4.8.1.78
|
8 |
+
Pillow==10.1.0
|
9 |
+
numpy==1.26.2
|
10 |
+
pydantic==2.5.1
|
11 |
+
python-magic==0.4.27
|
12 |
+
pdf2image==1.16.3
|
13 |
+
pytesseract==0.3.10
|