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"}