File size: 1,339 Bytes
84c3ae8 ae1592a 84c3ae8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from fastapi import FastAPI, File, UploadFile
from magic_pdf.user_api import MagicPDF
import tempfile
import os
import json
import shutil
app = FastAPI(title="MinerU PDF Processing API",
description="API for processing PDF documents using MinerU",
version="1.0.0")
# 初始化 MagicPDF
config_file = os.path.join(os.path.expanduser('~'), 'magic-pdf.json')
magic_pdf = MagicPDF(config_file)
@app.post("/process_pdf")
async def process_pdf(file: UploadFile = File(...)):
"""
处理上传的PDF文件并返回分析结果
"""
if not file.filename.lower().endswith('.pdf'):
return {"error": "Only PDF files are supported"}
try:
# 创建临时目录
with tempfile.TemporaryDirectory() as temp_dir:
# 保存上传的文件
temp_pdf = os.path.join(temp_dir, file.filename)
with open(temp_pdf, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
# 处理 PDF
result = magic_pdf.process(temp_pdf)
return result
except Exception as e:
return {"error": str(e)}
@app.get("/health")
async def health_check():
"""
健康检查接口
"""
return {"status": "healthy", "version": "1.0.0"} |