refactor: improve PDF extraction, error handling, and proxy configuration
This commit is contained in:
@@ -23,13 +23,5 @@ class ValidationError(AppError):
|
||||
"""请求参数校验失败(400)。"""
|
||||
|
||||
|
||||
class ExternalAPIError(AppError):
|
||||
"""外部 API 调用失败(502)。"""
|
||||
|
||||
|
||||
class PdfProcessError(AppError):
|
||||
"""PDF 处理错误(500)。"""
|
||||
|
||||
|
||||
class ConflictError(AppError):
|
||||
"""资源冲突(409)— 如锁冲突、并发任务冲突。"""
|
||||
|
||||
-10
@@ -13,9 +13,7 @@ from app.config import settings
|
||||
from app.exceptions import (
|
||||
AppError,
|
||||
ConflictError,
|
||||
ExternalAPIError,
|
||||
NotFoundError,
|
||||
PdfProcessError,
|
||||
ValidationError,
|
||||
)
|
||||
from app.database import engine, init_db
|
||||
@@ -87,14 +85,6 @@ def create_app() -> FastAPI:
|
||||
async def _validation_handler(request, exc):
|
||||
return JSONResponse(status_code=400, content={"error": exc.message})
|
||||
|
||||
@app.exception_handler(ExternalAPIError)
|
||||
async def _external_api_handler(request, exc):
|
||||
return JSONResponse(status_code=502, content={"error": exc.message})
|
||||
|
||||
@app.exception_handler(PdfProcessError)
|
||||
async def _pdf_process_handler(request, exc):
|
||||
return JSONResponse(status_code=500, content={"error": exc.message})
|
||||
|
||||
@app.exception_handler(ConflictError)
|
||||
async def _conflict_handler(request, exc):
|
||||
return JSONResponse(status_code=409, content={"error": exc.message})
|
||||
|
||||
@@ -41,7 +41,8 @@ async def call_claude(
|
||||
fix_errors: 上一轮验证错误列表(用于重试)
|
||||
"""
|
||||
if session_id is None:
|
||||
session_id = f"claude-summary-{uuid.uuid4().hex[:8]}"
|
||||
# claude CLI 的 --session-id 要求合法 UUID;非 UUID 会被拒绝(Invalid session ID)
|
||||
session_id = str(uuid.uuid4())
|
||||
|
||||
cmd = [settings.CLAUDE_BIN, "-p", "--output-format", "text"]
|
||||
|
||||
@@ -51,22 +52,22 @@ async def call_claude(
|
||||
else:
|
||||
cmd += ["--session-id", session_id]
|
||||
|
||||
cmd.append(prompt)
|
||||
|
||||
logger.info(
|
||||
"Calling claude (session=%s, fix=%s)",
|
||||
session_id,
|
||||
bool(fix_errors),
|
||||
)
|
||||
|
||||
# prompt 走 stdin,避免长文本(论文 PDF 动辄数十 KB)超出系统 ARG_MAX
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdin=asyncio.subprocess.PIPE,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
try:
|
||||
stdout, stderr = await asyncio.wait_for(
|
||||
proc.communicate(),
|
||||
proc.communicate(input=prompt.encode("utf-8")),
|
||||
timeout=settings.SUMMARY_TIMEOUT_SECONDS,
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
@@ -77,8 +78,10 @@ async def call_claude(
|
||||
)
|
||||
|
||||
if proc.returncode != 0:
|
||||
raise ClaudeProcessError(
|
||||
proc.returncode, stderr.decode("utf-8", errors="replace")
|
||||
)
|
||||
# claude CLI 把 API 错误输出到 stdout(stderr 常为空),优先用有内容的
|
||||
detail = stderr.decode("utf-8", errors="replace")
|
||||
if not detail.strip():
|
||||
detail = stdout.decode("utf-8", errors="replace")
|
||||
raise ClaudeProcessError(proc.returncode, detail)
|
||||
|
||||
return stdout.decode("utf-8", errors="replace"), session_id
|
||||
|
||||
@@ -140,7 +140,7 @@ def _get_embedding(text: str) -> list[float] | None:
|
||||
}
|
||||
|
||||
try:
|
||||
with make_http_client(sync=True) as client:
|
||||
with make_http_client(sync=True, use_proxy=False) as client:
|
||||
resp = client.post(url, json=payload, headers=headers)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
@@ -36,12 +36,18 @@ _CLUSTER_GAP = 15
|
||||
_MIN_BOX_AREA = 2000
|
||||
# caption 文本块与 figure/table 内容块的最大垂直距离(单位: pt)
|
||||
_CAPTION_MATCH_DISTANCE = 120
|
||||
# 方向不符(figure 标题在上 / table 标题在下)的配对惩罚分(仍允许,兜底异常排版)
|
||||
_CAPTION_WRONG_SIDE_PENALTY = 300
|
||||
# 游离碎片(配不到 caption)并入紧邻已配 cluster 的最大垂直间距(单位: pt)
|
||||
# 容下多面板图的 "(a)/(b)" 子图标占位(实测 Figure 4 两面板间距 36pt)
|
||||
_ABSORB_GAP = 60
|
||||
# caption 开头标记:Figure 3 / Fig. 3 / Table C1 / Figure 3.5 等(大小写均可)
|
||||
# 编号 = 数字开头 或 字母+数字(附录 C1);行首匹配,规避正文 "see Table 3" 引用
|
||||
# 编号 = 数字开头 或 字母+数字(附录 C1);行首匹配,规避正文 "see Table 3" 引用。
|
||||
# 否定前瞻再排除多图引用型正文 —— "Figure 14 and 15 show..." / "Figure 1 to 3" /
|
||||
# "Table 2, 3" 这类引用多张图表的句子不是独立标题,真标题从不引用多个编号。
|
||||
_CAPTION_HEAD_RE = re.compile(
|
||||
r"^\s*(Figure|Fig\.?|Table)\b\.?\s+([0-9][0-9A-Za-z.]*|[A-Z]\d[0-9A-Za-z.]*)",
|
||||
r"^\s*(Figure|Fig\.?|Table)\b\.?\s+([0-9][0-9A-Za-z.]*|[A-Z]\d[0-9A-Za-z.]*)"
|
||||
r"(?![0-9A-Za-z.])" # 编号须是完整 token,防止 "Figure 14" 回溯成 "Figure 1" 逃逸
|
||||
r"(?!\s*(?:and|to|through|vs\.?|&)\s+\d)" # "Figure 3 and 4" / "Figure 1 to 3"
|
||||
r"(?!\s*,\s*\d)", # "Figure 3, 4"
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
@@ -142,8 +148,8 @@ def _find_caption_blocks(page) -> list[_CaptionBlock]:
|
||||
"".join(span.get("text", "") for span in line.get("spans", []))
|
||||
for line in lines
|
||||
]
|
||||
first_line = next((t for t in line_texts if t.strip()), "")
|
||||
m = _CAPTION_HEAD_RE.match(first_line)
|
||||
joined = " ".join(t.strip() for t in line_texts if t.strip())
|
||||
m = _CAPTION_HEAD_RE.match(joined)
|
||||
if not m:
|
||||
continue
|
||||
kind_word, num = m.group(1), m.group(2)
|
||||
@@ -151,7 +157,7 @@ def _find_caption_blocks(page) -> list[_CaptionBlock]:
|
||||
bbox = block.get("bbox")
|
||||
if not bbox or len(bbox) != 4:
|
||||
continue
|
||||
full_text = " ".join(t.strip() for t in line_texts if t.strip())
|
||||
full_text = joined
|
||||
results.append(
|
||||
_CaptionBlock(
|
||||
id=f"{'Table' if is_table else 'Figure'} {num}",
|
||||
@@ -166,18 +172,25 @@ def _find_caption_blocks(page) -> list[_CaptionBlock]:
|
||||
def _pair_caption_blocks(
|
||||
content_clusters: list[_BoxCluster],
|
||||
caption_blocks: list[_CaptionBlock],
|
||||
) -> dict[int, _CaptionBlock]:
|
||||
"""每个内容块配方向上最近的同类型标题块。
|
||||
) -> dict[int, list[int]]:
|
||||
"""每个 caption 配对其垂直 span 内的所有同类型 cluster(支持复合图/子表)。
|
||||
|
||||
figure 标题惯例在下方、table 标题在上方;方向相符优先,不符加惩罚兜底
|
||||
(跨页 / 异常排版)。按 (距离+惩罚) 升序贪心匹配,每个内容块与标题块唯一配对。
|
||||
不预设标题在内容上方还是下方 —— figure 惯例标题在下、table 惯例标题在上,
|
||||
但不少论文反向排版(table 标题在表下方);用方向作硬约束或加错向惩罚会把
|
||||
"同页相邻两张表" 错并(标题居中的那张吞掉邻居)。改为上下两侧平等地按垂直
|
||||
距离打分,每个 cluster 唯一归属最近的同类 caption,但一个 caption 可被多个
|
||||
cluster 共享 —— 这样一张被 DocLayout 切成多个稀疏子框的复合图/复合表,能
|
||||
整体配到它的主标题(而非只截其中一个子图/子表)。
|
||||
|
||||
Returns:
|
||||
caption_idx → [cluster_idx, ...],按 cluster 在页面上的位置排序,
|
||||
保证合并/渲染顺序稳定。
|
||||
"""
|
||||
candidates: list[tuple[float, int, int]] = []
|
||||
for c_idx, content in enumerate(content_clusters):
|
||||
want_below = content.boxclass == "picture" # figure 标题在下
|
||||
want_kind = "figure" if want_below else "table"
|
||||
cluster_kind = "figure" if content.boxclass == "picture" else "table"
|
||||
for b_idx, cap in enumerate(caption_blocks):
|
||||
if cap.kind != want_kind:
|
||||
if cap.kind != cluster_kind: # 类型过滤:防 figure↔table 串台
|
||||
continue
|
||||
cx0, cy0, cx1, cy1 = cap.bbox
|
||||
h_overlap = min(content.x1, cx1) - max(content.x0, cx0)
|
||||
@@ -185,24 +198,87 @@ def _pair_caption_blocks(
|
||||
if min_width <= 0 or h_overlap < min_width * 0.25:
|
||||
continue
|
||||
if cy1 <= content.y0: # 标题在内容上方
|
||||
side_below, v_gap = False, content.y0 - cy1
|
||||
v_gap = content.y0 - cy1
|
||||
elif cy0 >= content.y1: # 标题在内容下方
|
||||
side_below, v_gap = True, cy0 - content.y1
|
||||
v_gap = cy0 - content.y1
|
||||
else:
|
||||
continue # 重叠,跳过
|
||||
if v_gap > _CAPTION_MATCH_DISTANCE:
|
||||
continue
|
||||
penalty = 0.0 if side_below == want_below else _CAPTION_WRONG_SIDE_PENALTY
|
||||
candidates.append((v_gap + penalty, c_idx, b_idx))
|
||||
candidates.append((v_gap, c_idx, b_idx))
|
||||
|
||||
matches: dict[int, _CaptionBlock] = {}
|
||||
used: set[int] = set()
|
||||
# cluster 唯一归属最近的 caption;caption 可被多个 cluster 共享(复合图/子图)
|
||||
cluster_to_caption: dict[int, int] = {}
|
||||
for _score, c_idx, b_idx in sorted(candidates):
|
||||
if c_idx in matches or b_idx in used:
|
||||
if c_idx in cluster_to_caption:
|
||||
continue
|
||||
matches[c_idx] = caption_blocks[b_idx]
|
||||
used.add(b_idx)
|
||||
return matches
|
||||
cluster_to_caption[c_idx] = b_idx
|
||||
|
||||
# 聚合 caption → clusters,按页面位置排序保证稳定的合并/渲染顺序
|
||||
caption_to_clusters: dict[int, list[int]] = {}
|
||||
for c_idx, b_idx in cluster_to_caption.items():
|
||||
caption_to_clusters.setdefault(b_idx, []).append(c_idx)
|
||||
for b_idx in caption_to_clusters:
|
||||
caption_to_clusters[b_idx].sort(
|
||||
key=lambda i: (content_clusters[i].y0, content_clusters[i].x0)
|
||||
)
|
||||
return caption_to_clusters
|
||||
|
||||
|
||||
def _absorb_stragglers(
|
||||
clusters: list[_BoxCluster],
|
||||
caption_blocks: list[_CaptionBlock],
|
||||
caption_matches: dict[int, list[int]],
|
||||
) -> None:
|
||||
"""把配不到 caption、却紧邻某已配 cluster 的同类型游离碎片并入该 cluster 所属 caption。
|
||||
|
||||
多面板图常把主 caption 只放在最下方,离 caption 过远(> _CAPTION_MATCH_DISTANCE)
|
||||
的上方面板会被正常配对漏掉(如 Figure 4 的 (a) 子图距主标题 270pt)。这里把它们
|
||||
并入紧邻的、已归属某 caption 的同类型 cluster,两道护栏避免误并:
|
||||
|
||||
1. 垂直紧邻(≤ _ABSORB_GAP)且水平重叠(同一列)——排除不同列的无关图;
|
||||
2. 两者之间不得夹其他 caption ——有 caption 即另一张图/表的边界(如 Table 7/8
|
||||
之间夹 Table 7 标题),不并,从而不破坏表格的独立配对。
|
||||
"""
|
||||
# cluster_idx → caption_idx(已配 cluster 的反向索引)
|
||||
cluster_to_cap: dict[int, int] = {}
|
||||
for cap_idx, idxs in caption_matches.items():
|
||||
for c_idx in idxs:
|
||||
cluster_to_cap[c_idx] = cap_idx
|
||||
|
||||
# 多趟扫描直到稳定:游离面板可能链式排列(上图→中图→下图→caption),
|
||||
# 单趟只能吸收紧邻已配 cluster 的那一层;中图被并入后才轮到上图,故需重复。
|
||||
changed = True
|
||||
while changed:
|
||||
changed = False
|
||||
for u_idx, u in enumerate(clusters):
|
||||
if u_idx in cluster_to_cap:
|
||||
continue # 已配,无需吸收
|
||||
best_cap: int | None = None
|
||||
best_gap: float | None = None
|
||||
for p_idx, cap_idx in cluster_to_cap.items():
|
||||
p = clusters[p_idx]
|
||||
if p.boxclass != u.boxclass:
|
||||
continue # 类型不同(figure vs table)不并
|
||||
gap = max(0.0, max(u.y0, p.y0) - min(u.y1, p.y1))
|
||||
if gap > _ABSORB_GAP:
|
||||
continue # 护栏 1:垂直不紧邻
|
||||
if min(u.x1, p.x1) - max(u.x0, p.x0) <= 0:
|
||||
continue # 护栏 1:水平不重叠,非同一列
|
||||
# 护栏 2:u 与 p 之间的垂直区间不得夹任何 caption
|
||||
between_lo = min(u.y1, p.y1)
|
||||
between_hi = max(u.y0, p.y0)
|
||||
if any(
|
||||
not (cb.bbox[3] < between_lo or cb.bbox[1] > between_hi)
|
||||
for cb in caption_blocks
|
||||
):
|
||||
continue
|
||||
if best_gap is None or gap < best_gap:
|
||||
best_gap, best_cap = gap, cap_idx
|
||||
if best_cap is not None:
|
||||
caption_matches[best_cap].append(u_idx)
|
||||
cluster_to_cap[u_idx] = best_cap # 标记已并入,供后续趟链式吸收
|
||||
changed = True
|
||||
|
||||
|
||||
# ── Phase 1: 检测 + 渲染 ──────────────────────────────────────────────
|
||||
@@ -259,6 +335,7 @@ def _process_page(
|
||||
"""处理单页:检测内容 box → 文本定位 caption → 只渲染配到标题的。
|
||||
|
||||
配到 Figure/Table caption 的 box 用 caption 自带 ID 命名(figure_3.jpg);
|
||||
同一 caption 的多个 cluster(复合图/子图被 DocLayout 切散)合并 bbox 整张截取;
|
||||
没配到标题的(Algorithm 伪代码、无编号附录表、误检碎片)一律过滤,不输出。
|
||||
"""
|
||||
page = doc[page_idx]
|
||||
@@ -280,38 +357,46 @@ def _process_page(
|
||||
# 聚类:将同一 figure/table 的碎片 box 合并;用 PDF 文本定位 caption
|
||||
clusters = _cluster_boxes(raw_boxes)
|
||||
caption_blocks = _find_caption_blocks(page)
|
||||
# caption_idx → [cluster_idx, ...];一个 caption 可含多个子图 cluster(复合图)
|
||||
caption_matches = _pair_caption_blocks(clusters, caption_blocks)
|
||||
# 把距主 caption 过远的游离面板(如多面板图上方的 (a) 子图)并入紧邻的已配 cluster
|
||||
_absorb_stragglers(clusters, caption_blocks, caption_matches)
|
||||
|
||||
extracted = 0
|
||||
for cluster_idx, cluster in enumerate(clusters):
|
||||
cap_match = caption_matches.get(cluster_idx)
|
||||
if cap_match is None:
|
||||
continue # 无 Figure/Table 标题 → 过滤(Algorithm、无编号表、误检碎片)
|
||||
if cap_match.id in seen_labels:
|
||||
for cap_idx, cluster_indices in caption_matches.items():
|
||||
cap = caption_blocks[cap_idx]
|
||||
if cap.id in seen_labels:
|
||||
continue # 同一图表被 DocLayout 切成多块重复检测,跳过后续
|
||||
seen_labels.add(cap_match.id)
|
||||
seen_labels.add(cap.id)
|
||||
|
||||
filename = f"{cap_match.id.replace(' ', '_').lower()}.jpg"
|
||||
# 同一 caption 的所有 cluster 合并 bbox,复合图/子图整张截取
|
||||
members = [clusters[i] for i in cluster_indices]
|
||||
merged = _BoxCluster(members)
|
||||
|
||||
filename = f"{cap.id.replace(' ', '_').lower()}.jpg"
|
||||
if not _render_box(
|
||||
page,
|
||||
cluster,
|
||||
merged,
|
||||
images_dest,
|
||||
filename,
|
||||
cap_match.kind,
|
||||
cap.kind,
|
||||
page_num,
|
||||
caption_bbox=cap_match.bbox,
|
||||
caption_bbox=cap.bbox,
|
||||
):
|
||||
continue
|
||||
|
||||
manifest[filename] = {
|
||||
info = {
|
||||
"page": page_num,
|
||||
"type": cap_match.kind,
|
||||
"label": cap_match.id,
|
||||
"box": _cluster_to_box(cluster),
|
||||
"caption_text": cap_match.text[:500],
|
||||
"caption_box": cap_match.bbox,
|
||||
"type": cap.kind,
|
||||
"label": cap.id,
|
||||
"box": _cluster_to_box(merged),
|
||||
"caption_text": cap.text[:500],
|
||||
"caption_box": cap.bbox,
|
||||
"caption_source": "text",
|
||||
}
|
||||
if len(members) > 1:
|
||||
info["subfigure_count"] = len(members)
|
||||
manifest[filename] = info
|
||||
extracted += 1
|
||||
|
||||
return extracted
|
||||
@@ -458,19 +543,31 @@ def link_figures_with_images(
|
||||
if not unmatched:
|
||||
return figures
|
||||
|
||||
# 已被策略 1(精确匹配)占用的图片不参与兜底,否则会把已正确归属的图复用给
|
||||
# 别的条目(如缺失的 Table 4 误链到 Table 1 的截图)。
|
||||
assigned_urls = {f["image_url"] for f in figures if f.get("image_url")}
|
||||
|
||||
# 按类型分流:Figure vs Table
|
||||
fig_type_unmatched = [f for f in unmatched if _is_figure_type(f.get("id", ""))]
|
||||
table_type_unmatched = [
|
||||
f for f in unmatched if not _is_figure_type(f.get("id", ""))
|
||||
]
|
||||
|
||||
# 提取的图片按类型分流,按文件名中的编号排序
|
||||
# 剩余未占用图片按类型分流,按文件名中的编号排序
|
||||
fig_images = sorted(
|
||||
[img for img in images if "table" not in img["name"].lower()],
|
||||
[
|
||||
img
|
||||
for img in images
|
||||
if "table" not in img["name"].lower() and img["url"] not in assigned_urls
|
||||
],
|
||||
key=lambda img: _image_sort_key(img["name"]),
|
||||
)
|
||||
table_images = sorted(
|
||||
[img for img in images if "table" in img["name"].lower()],
|
||||
[
|
||||
img
|
||||
for img in images
|
||||
if "table" in img["name"].lower() and img["url"] not in assigned_urls
|
||||
],
|
||||
key=lambda img: _image_sort_key(img["name"]),
|
||||
)
|
||||
|
||||
|
||||
+3
-2
@@ -93,13 +93,14 @@ def release_lock(db, lock) -> None:
|
||||
|
||||
|
||||
def make_http_client(
|
||||
*, sync: bool = False, follow_redirects: bool = False, **kwargs
|
||||
*, sync: bool = False, follow_redirects: bool = False, use_proxy: bool = True, **kwargs
|
||||
) -> httpx.AsyncClient | httpx.Client:
|
||||
"""创建带 proxy 和默认配置的 httpx 客户端。
|
||||
|
||||
Args:
|
||||
sync: True 返回同步 Client,False 返回 AsyncClient
|
||||
follow_redirects: 是否跟随重定向
|
||||
use_proxy: 是否使用 HF_PROXY 代理(默认 True;国内 API 如 embedder 应传 False)
|
||||
**kwargs: 覆盖默认参数
|
||||
"""
|
||||
defaults: dict = {
|
||||
@@ -107,7 +108,7 @@ def make_http_client(
|
||||
"headers": {"User-Agent": settings.HTTP_USER_AGENT},
|
||||
"follow_redirects": follow_redirects,
|
||||
}
|
||||
if settings.http_proxy:
|
||||
if use_proxy and settings.http_proxy:
|
||||
defaults["transport"] = (
|
||||
httpx.HTTPTransport(proxy=settings.http_proxy)
|
||||
if sync
|
||||
|
||||
Reference in New Issue
Block a user