Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
183 lines (152 loc) · 7.45 KB
/
Copy pathapp.py
File metadata and controls
183 lines (152 loc) · 7.45 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See LICENSE in the project root for license information.
# ---------------------------------------------------------------------------
# 本文件基于微软开源项目 MarkItDown (https://github.com/microsoft/markitdown) 深度二次开发,
# 直接调用了 MarkItDown 的转换 API(MarkItDown / StreamInfo),故保留上方原版版权声明。
# 以下内容由 Nexcore 团队独立实现:
# - 自研 PyMuPDF PDF 增强解析引擎(公式/图片/链接,见 pdfx.py)
# - 自研 RapidOCR 离线识别集成(见 ocr.py)
# - 自研 Office 内嵌图片提取(见 officex.py)
# - 自研 Web 服务编排与前端(index.html)
# ---------------------------------------------------------------------------
"""NexcoreFileRead —— NexcoreWork 的本地文档读取服务
本服务将 PDF / Office / 图片 / 网页 / 数据文件等转换为结构完整的 Markdown,
所有转换均在本机完成,文件不会离开用户设备。
启动: uvicorn app:app --host 127.0.0.1 --port 8765
"""
import asyncio
import base64
import io
import mimetypes
import os
import sys
from pathlib import Path
from fastapi import FastAPI, File, HTTPException, Request, UploadFile
from fastapi.responses import HTMLResponse, JSONResponse
from PIL import Image
from markitdown import MarkItDown
from markitdown._stream_info import StreamInfo
from ocr import ocr_image_bytes, ocr_pdf_bytes
from officex import append_images_section
from pdfx import convert_pdf
# ---- 常量定义 ----
IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".bmp", ".webp", ".gif", ".tif", ".tiff"} # 图片类型
OFFICE_EXTS = {".docx", ".doc", ".pptx", ".ppt", ".xlsx", ".xls", ".odt", ".odp", ".ods"} # Office 文档
EMBED_MAX_BYTES = 3 * 1024 * 1024 # 内嵌图片体积上限,超过则压缩缩略图
def resource_path(rel: str) -> Path:
"""定位资源文件:兼容源码运行与 PyInstaller 打包运行(onefile 解压到 _MEIPASS)"""
base = Path(getattr(sys, "_MEIPASS", Path(__file__).resolve().parent))
return base / rel
app = FastAPI(title="NexcoreFileRead", version="4.0.0")
MAX_UPLOAD_BYTES = 100 * 1024 * 1024
@app.middleware("http")
async def loopback_only(request: Request, call_next):
"""No authentication secret is carried by this service, so refuse non-loopback clients."""
host = request.client.host if request.client else ""
if host not in {"127.0.0.1", "::1", "localhost"}:
return JSONResponse(status_code=403, content={"detail": "NexcoreFileRead only accepts loopback clients"})
return await call_next(request)
# 核心转换引擎(markitdown),负责 Office / 网页 / 数据文件等格式
engine = MarkItDown()
def _inline_image(raw: bytes, filename: str) -> str:
"""将图片以 base64 data URI 内嵌进 Markdown;超大图自动压缩缩略图"""
try:
img = Image.open(io.BytesIO(raw))
img.load()
except Exception:
# 无法解析时原样内嵌
b64 = base64.b64encode(raw).decode("ascii")
mime = mimetypes.guess_type(filename)[0] or "image/png"
return f""
# 压缩策略:超过体积上限或超过 1600px 边长时生成缩略图
if len(raw) > EMBED_MAX_BYTES or max(img.size) > 1600:
img.thumbnail((1600, 1600), Image.LANCZOS)
buf = io.BytesIO()
if img.mode in ("RGBA", "LA", "P"):
img.convert("RGBA").save(buf, "PNG")
mime = "image/png"
else:
img.convert("RGB").save(buf, "JPEG", quality=82)
mime = "image/jpeg"
raw = buf.getvalue()
else:
mime = mimetypes.guess_type(filename)[0] or "image/png"
b64 = base64.b64encode(raw).decode("ascii")
return f""
@app.get("/", response_class=HTMLResponse)
async def index() -> str:
"""返回前端页面"""
return (resource_path("index.html")).read_text(encoding="utf-8")
@app.get("/api/health")
async def health() -> JSONResponse:
return JSONResponse({"ok": True, "service": "NexcoreFileRead", "version": "4.0.0", "loopbackOnly": True})
@app.post("/api/convert")
async def convert_document(file: UploadFile = File(...)) -> JSONResponse:
"""统一转换入口:按文件类型分派到对应引擎"""
if not file.filename:
raise HTTPException(status_code=400, detail="未选择文件")
raw = await file.read()
if not raw:
raise HTTPException(status_code=400, detail="文件内容为空")
if len(raw) > MAX_UPLOAD_BYTES:
raise HTTPException(status_code=413, detail="单个文件不得超过 100 MB")
ext = os.path.splitext(file.filename)[1].lower() or None
loop = asyncio.get_running_loop()
warnings = []
engine_name = "markitdown"
# ---- PDF:PyMuPDF 增强引擎(公式/图片/链接完整保留,扫描件自动 OCR)----
if ext == ".pdf":
try:
# convert_pdf 内部:文本 < 50 字符判定为扫描件并回调 ocr_pdf_bytes
pdf_res = await loop.run_in_executor(None, convert_pdf, raw, ocr_pdf_bytes)
md_text = pdf_res.markdown or ""
ocr_fallback = pdf_res.is_scanned
engine_name = "pymupdf+rapidocr" if ocr_fallback else "pymupdf"
except Exception: # 增强引擎异常时回退 markitdown
try:
result = engine.convert_stream(
io.BytesIO(raw),
stream_info=StreamInfo(filename=file.filename, extension=ext),
)
md_text = result.text_content or ""
ocr_fallback = False
warnings.append("PyMuPDF 增强解析失败,已回退到 MarkItDown")
except Exception as exc:
raise HTTPException(status_code=500, detail=f"转换失败: {exc}") from exc
# ---- 独立图片:OCR 识别文字 + 内嵌原图 ----
elif ext in IMAGE_EXTS:
embed = _inline_image(raw, file.filename)
try:
# OCR 在独立线程执行,避免阻塞事件循环
ocr_text = (await loop.run_in_executor(None, ocr_image_bytes, raw)).strip()
except Exception:
ocr_text = ""
warnings.append("图片 OCR 失败,已保留原图并继续返回")
md_text = (ocr_text + "\n\n" + embed).strip() if ocr_text else embed
ocr_fallback = bool(ocr_text)
engine_name = "rapidocr" if ocr_fallback else "image-embed"
# ---- 其他格式(Office / HTML / 数据文件):markitdown + 提取内嵌图片 ----
else:
try:
result = engine.convert_stream(
io.BytesIO(raw),
stream_info=StreamInfo(filename=file.filename, extension=ext),
)
md_text = result.text_content or ""
except Exception as exc:
raise HTTPException(status_code=500, detail=f"转换失败: {exc}") from exc
ocr_fallback = False
# Office 文档(zip 结构)提取内嵌图片,图片不丢失
if ext in OFFICE_EXTS:
md_text = append_images_section(md_text, raw)
engine_name = "markitdown+office-images"
return JSONResponse(
{
"filename": file.filename,
"markdown": md_text,
"ocr": ocr_fallback,
"engine": engine_name,
"warnings": warnings,
"ocr_note": "此文件通过 OCR 识别,可能存在个别识别误差。" if ocr_fallback else None,
}
)