Spaces:
Running
Running
sync from GitHub 943c198: feat(tex): 公式改用固定槽位模板(去掉 list-item 内的 for),ensure 失败带 HTTP 状态码
Browse files- Dockerfile +7 -3
- _chem_check.py +62 -0
- _ref_test.py +58 -0
- _tex_e2e_test.py +251 -0
- app/api/pseudo_stream.py +0 -7
- app/api/tex.py +37 -2
- app/api/xtc.py +2 -8
- app/services/formula.py +230 -93
Dockerfile
CHANGED
|
@@ -24,10 +24,14 @@ ENV MPLCONFIGDIR=/tmp/matplotlib
|
|
| 24 |
# 预热 mathtext 字体缓存 + 在构建期就验证公式渲染可用:
|
| 25 |
# 否则第一个含公式的回复要等字体缓存建立(数秒)。断言失败会让镜像构建失败,
|
| 26 |
# 避免部署出一个「公式静默不渲染」的后端。
|
| 27 |
-
# 注意:
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
RUN mkdir -p /tmp/matplotlib \
|
| 30 |
-
&& python -c 'from app.services.formula import
|
| 31 |
|
| 32 |
ENV PORT=7860
|
| 33 |
ENV PYTHONUNBUFFERED=1
|
|
|
|
| 24 |
# 预热 mathtext 字体缓存 + 在构建期就验证公式渲染可用:
|
| 25 |
# 否则第一个含公式的回复要等字体缓存建立(数秒)。断言失败会让镜像构建失败,
|
| 26 |
# 避免部署出一个「公式静默不渲染」的后端。
|
| 27 |
+
# 注意:ensure_batch 吃的是「不带 $ 的 LaTeX 正文」(与端上 /v1/xtc/tex/ensure
|
| 28 |
+
# 的入参一致);这里还顺带断言两件事——
|
| 29 |
+
# 1) 单批 12 条必须全部渲染回来(_MAX_FORMULAS 若被改小,端上一次请求里的
|
| 30 |
+
# 多余公式会被截掉并永久降级成纯文本);
|
| 31 |
+
# 2) 下发的 id 必须能在 LRU 里直接命中(降级渲染时 id 用实际 scale 计算,
|
| 32 |
+
# 键不一致会让每次回源取图都重渲染一遍)。
|
| 33 |
RUN mkdir -p /tmp/matplotlib \
|
| 34 |
+
&& python -c 'from app.services.formula import ensure_batch, get_png, is_available; assert is_available(), "matplotlib unavailable"; items = [{"latex": "x_%d^2 + y_%d^2 = r_%d^2" % (i, i, i), "key": "k%d" % i} for i in range(12)]; r = ensure_batch(items); assert len(r) == 12, "formula batch truncated: %d/12" % len(r); assert get_png(r[0]["id"]), "formula cache id mismatch"; print("formula render ok:", r[0]["id"], r[0]["w"], "x", r[0]["h"], "batch", len(r))'
|
| 35 |
|
| 36 |
ENV PORT=7860
|
| 37 |
ENV PYTHONUNBUFFERED=1
|
_chem_check.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
r"""化学公式渲染覆盖自检(保留作回归用)。
|
| 2 |
+
|
| 3 |
+
背景:\ce{...}(mhchem 宏包语法)mathtext 不认,formula.py 里有一层手写转换
|
| 4 |
+
(_convert_mhchem / _replace_ce)。这里把常见化学写法一条条过一遍,看哪些真能
|
| 5 |
+
渲出来、转换后的 mathtext 长什么样、哪些按设计「不渲染」(端上退回 LaTeX 原文)。
|
| 6 |
+
|
| 7 |
+
注意:端上发过来的是**去掉分隔符的公式正文**($$ / \[ \] / $ 在前端就被剥掉了),
|
| 8 |
+
所以这里不要拿带 \[ \] 的整段正文去测——那条路径根本不会走到后端。
|
| 9 |
+
"""
|
| 10 |
+
from __future__ import annotations
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import sys
|
| 14 |
+
|
| 15 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 16 |
+
|
| 17 |
+
from app.services import formula as F # noqa: E402
|
| 18 |
+
|
| 19 |
+
# (名称, 公式正文, 期望结果) expect: 'ok' 渲染成功 / 'none' 按设计不渲染
|
| 20 |
+
CASES = [
|
| 21 |
+
("氢气燃烧(参考公式 10)", r"\ce{2H2 + O2 -> 2H2O}", "ok"),
|
| 22 |
+
("合成氨(参考公式 11)", r"\ce{N2 + 3H2 <=> 2NH3}", "ok"),
|
| 23 |
+
("可逆号 <->", r"\ce{A <-> B}", "ok"),
|
| 24 |
+
("括号化合物 + 上标电荷", r"\ce{Ca(OH)2 -> Ca^2+ + 2OH-}", "ok"),
|
| 25 |
+
("醋酸电离", r"\ce{CH3COOH <=> CH3COO- + H+}", "ok"),
|
| 26 |
+
("络合平衡", r"\ce{Fe^3+ + 3SCN- <=> Fe(SCN)3}", "ok"),
|
| 27 |
+
("高锰酸钾氧化", r"\ce{MnO4- + 8H+ + 5e- -> Mn^2+ + 4H2O}", "ok"),
|
| 28 |
+
("呼吸作用(多系数)", r"\ce{C6H12O6 + 6O2 -> 6CO2 + 6H2O}", "ok"),
|
| 29 |
+
("纯分子式", r"\ce{H2SO4}", "ok"),
|
| 30 |
+
("带状态符号", r"\ce{H2O(l) -> H2O(g)}", "ok"),
|
| 31 |
+
("同位素(嵌套花括号)", r"\ce{^{14}C -> ^{14}N + e-}", "ok"),
|
| 32 |
+
("气体上升号 ^", r"\ce{2Na + 2H2O -> 2NaOH + H2 ^}", "ok"),
|
| 33 |
+
("沉淀号 v", r"\ce{Ag+ + Cl- -> AgCl v}", "ok"),
|
| 34 |
+
("两个 \\ce 连着写", r"\ce{H2} + \ce{O2}", "ok"),
|
| 35 |
+
("平衡常数(普通公式 12)", r"K_c = \frac{[C]^c [D]^d}{[A]^a [B]^b}", "ok"),
|
| 36 |
+
("化学式 + 单位文字混排", r"\ce{2H2(g) + O2(g) -> 2H2O(l)} \quad \Delta H = -571.6\ \text{kJ/mol}", "ok"),
|
| 37 |
+
("中文混进公式 → 不渲染(否则画成空心方块)", r"燃烧的方程式是 \ce{2H2 + O2 -> 2H2O},放热。", "none"),
|
| 38 |
+
("中文注解块级公式 → 不渲染", "热力学第一定律:\\Delta U = Q + W", "none"),
|
| 39 |
+
("矩阵 → 不渲染(mathtext 硬限制)", r"\begin{pmatrix} a & b \\ c & d \end{pmatrix}", "none"),
|
| 40 |
+
("\\ce 花括号没闭合 → 不渲染", r"\ce{2H2 + O2", "none"),
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
ok = 0
|
| 44 |
+
bad = 0
|
| 45 |
+
for name, tex, expect in CASES:
|
| 46 |
+
lines = F.normalize_latex(tex)
|
| 47 |
+
info = F.render(tex, fs=12, scale=3)
|
| 48 |
+
got = "ok" if (info and lines) else "none"
|
| 49 |
+
hit = (got == expect)
|
| 50 |
+
if hit:
|
| 51 |
+
ok += 1
|
| 52 |
+
else:
|
| 53 |
+
bad += 1
|
| 54 |
+
print("[%s] %-34s %s" % ("OK " if hit else "BAD ", name, "渲染成功" if got == "ok" else "不渲染 → 退回原文"))
|
| 55 |
+
if lines:
|
| 56 |
+
print(" 转换后:%s" % " | ".join(lines))
|
| 57 |
+
if got == "ok":
|
| 58 |
+
print(" 体积 %5dB design=%sx%s" % (info["bytes"], info["w"], info["h"]))
|
| 59 |
+
else:
|
| 60 |
+
print(" 预处理:%s" % ("判定不可渲染" if not lines else "mathtext 解析失败"))
|
| 61 |
+
|
| 62 |
+
print("\n合计 %d 条:符合预期 %d / 不符合 %d" % (len(CASES), ok, bad))
|
_ref_test.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""用用户给的 13 条参考公式验证识别/预处理/渲染覆盖率。"""
|
| 2 |
+
import io
|
| 3 |
+
import os
|
| 4 |
+
import sys
|
| 5 |
+
|
| 6 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 7 |
+
|
| 8 |
+
from PIL import Image # noqa: E402
|
| 9 |
+
|
| 10 |
+
from app.services import formula # noqa: E402
|
| 11 |
+
|
| 12 |
+
SAMPLES = [
|
| 13 |
+
("1 欧拉恒等式", r"\[e^{i\pi} + 1 = 0\]"),
|
| 14 |
+
("2 高斯积分", r"\[\int_{-\infty}^{\infty} e^{-x^2}\,dx = \sqrt{\pi}\]"),
|
| 15 |
+
("3 巴塞尔问题", r"\[\sum_{n=1}^{\infty} \frac{1}{n^2} = \frac{\pi^2}{6}\]"),
|
| 16 |
+
("4 傅里叶变换", r"\[\hat{f}(\xi) = \int_{-\infty}^{\infty} f(x) e^{-2\pi i x \xi}\,dx\]"),
|
| 17 |
+
("5 质能方程", r"\[E = mc^2\]"),
|
| 18 |
+
("6 万有引力", r"\[F = G \frac{m_1 m_2}{r^2}\]"),
|
| 19 |
+
("7 薛定谔方程", "\\[i\\hbar \\frac{\\partial}{\\partial t}\\Psi(\\mathbf{r},t)\n"
|
| 20 |
+
"= -\\frac{\\hbar^2}{2m}\\nabla^2 \\Psi(\\mathbf{r},t)\n"
|
| 21 |
+
"+ V(\\mathbf{r},t)\\Psi(\\mathbf{r},t)\\]"),
|
| 22 |
+
("8 麦克斯韦方程组", "\\[\\begin{aligned}\n\\nabla \\cdot \\mathbf{E} &= \\frac{\\rho}{\\varepsilon_0}, &\n"
|
| 23 |
+
"\\nabla \\cdot \\mathbf{B} &= 0, \\\\\n\\nabla \\times \\mathbf{E} &= -\\frac{\\partial \\mathbf{B}}{\\partial t}, &\n"
|
| 24 |
+
"\\nabla \\times \\mathbf{B} &= \\mu_0 \\mathbf{J}\n+ \\mu_0 \\varepsilon_0 \\frac{\\partial \\mathbf{E}}{\\partial t}.\n\\end{aligned}\\]"),
|
| 25 |
+
("9 热力学第一定律", r"\[\Delta U = Q - W\]"),
|
| 26 |
+
("10 氢气燃烧", r"\[\ce{2H2 + O2 -> 2H2O}\]"),
|
| 27 |
+
("11 合成氨", r"\[\ce{N2(g) + 3H2(g) <=> 2NH3(g)} \qquad \Delta H < 0\]"),
|
| 28 |
+
("12 平衡常数", r"\[K_c = \frac{[\ce{NH3}]^2}{[\ce{N2}][\ce{H2}]^3}\]"),
|
| 29 |
+
("13 理想气体", r"\[PV = nRT\]"),
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
# 端上识别(JS)会先把分隔符剥掉,这里模拟同样的输入
|
| 33 |
+
def strip_delim(s):
|
| 34 |
+
t = s.strip()
|
| 35 |
+
for a, b in (("\\[", "\\]"), ("$$", "$$"), ("\\(", "\\)")):
|
| 36 |
+
if t.startswith(a) and t.endswith(b):
|
| 37 |
+
return t[len(a):len(t) - len(b)].strip()
|
| 38 |
+
return t
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
ok = bad = 0
|
| 42 |
+
for name, raw in SAMPLES:
|
| 43 |
+
body = strip_delim(raw)
|
| 44 |
+
lines = formula.normalize_latex(body)
|
| 45 |
+
info = formula.render(body, fs=16)
|
| 46 |
+
if info:
|
| 47 |
+
ok += 1
|
| 48 |
+
img = Image.open(io.BytesIO(info["png"]))
|
| 49 |
+
print("[OK ] %-16s %d 行 png=%dx%d design=%sx%s %5dB %s" % (
|
| 50 |
+
name, len(lines or []), img.size[0], img.size[1],
|
| 51 |
+
info["w"], info["h"], info["bytes"], img.mode))
|
| 52 |
+
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "_ref_%s.png" % name.split()[0]), "wb") as fp:
|
| 53 |
+
fp.write(info["png"])
|
| 54 |
+
else:
|
| 55 |
+
bad += 1
|
| 56 |
+
print("[FAIL] %-16s %s" % (name, "normalize 返回 None" if not lines else "渲染失败"))
|
| 57 |
+
|
| 58 |
+
print("\n合计 %d 条:成功 %d / 失败 %d" % (len(SAMPLES), ok, bad))
|
_tex_e2e_test.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""公式渲染后端:真实 HTTP 端到端自检(临时脚本,验证完可删)。
|
| 2 |
+
|
| 3 |
+
与 _ref_test.py 的区别:那个只调服务层函数,这个起 uvicorn(完整 app.main:app),
|
| 4 |
+
真发 HTTP 请求(stdlib urllib,本机 Python 3.14 上 httpx 0.28.1 解析 URL 就崩),
|
| 5 |
+
覆盖端上实际会走的两条路径:
|
| 6 |
+
|
| 7 |
+
POST /v1/xtc/tex/ensure (带 x-xtc-access-key,批量渲染)
|
| 8 |
+
GET /v1/xtc/tex/{id}.png (capability URL,端上 request.download 直接取)
|
| 9 |
+
|
| 10 |
+
检查项:
|
| 11 |
+
1. 鉴权:没带 access key 的 /ensure → 401
|
| 12 |
+
2. 批大小:12 条一次全部渲染回来(旧上限 4 会把后 8 条静默截断)
|
| 13 |
+
3. 降级:矩阵这类 mathtext 排不了的公式不出现在响应里(端上据此保留原文)
|
| 14 |
+
4. 图片本身:PNG 签名、image/png、<=50KB、RGBA 且真有透明像素、
|
| 15 |
+
像素尺寸 = design 尺寸 * scale
|
| 16 |
+
5. 内容寻址:同一批两次请求 id 完全一致;重复取图字节一致
|
| 17 |
+
6. 体积兜底:超 50KB 的公式自动降 scale,且下发 id 能直接命中缓存(不重复渲染)
|
| 18 |
+
7. 防滥用:id 与 f/fs/scale 不一致 → 403;未知 id 无 f → 404;限流 → 429
|
| 19 |
+
8. 参数校验:fs 越界 / 缺 items → 400
|
| 20 |
+
|
| 21 |
+
跑法:cd d:\\AI\\xtc-backend-hf && python _tex_e2e_test.py
|
| 22 |
+
"""
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import io
|
| 26 |
+
import json
|
| 27 |
+
import os
|
| 28 |
+
import sys
|
| 29 |
+
import threading
|
| 30 |
+
import time
|
| 31 |
+
import urllib.error
|
| 32 |
+
import urllib.request
|
| 33 |
+
|
| 34 |
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 35 |
+
os.environ.setdefault("XTC_DB_PATH", os.path.join(os.path.dirname(HERE), "_tex_e2e.db"))
|
| 36 |
+
# 端上就是带 access key 调的;这里设一个 key 顺便验证鉴权链路
|
| 37 |
+
os.environ["XTC_ACCESS_KEY"] = "e2e-access-key-0123456789"
|
| 38 |
+
os.environ["XTC_JWT_SECRET"] = "e2e-jwt-secret-0123456789"
|
| 39 |
+
os.environ.setdefault("XTC_ADMIN_KEY", "e2e-admin-key-0123456789")
|
| 40 |
+
|
| 41 |
+
sys.path.insert(0, HERE)
|
| 42 |
+
|
| 43 |
+
import uvicorn # noqa: E402
|
| 44 |
+
|
| 45 |
+
from app.main import app # noqa: E402
|
| 46 |
+
from app.services import formula as F # noqa: E402
|
| 47 |
+
from app.api import tex as TEX_API # noqa: E402
|
| 48 |
+
|
| 49 |
+
PORT = 8813
|
| 50 |
+
BASE = "http://127.0.0.1:%d" % PORT
|
| 51 |
+
KEY = "e2e-access-key-0123456789"
|
| 52 |
+
|
| 53 |
+
PASS = 0
|
| 54 |
+
FAIL = 0
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def check(name: str, ok: bool, detail: str = "") -> None:
|
| 58 |
+
global PASS, FAIL
|
| 59 |
+
if ok:
|
| 60 |
+
PASS += 1
|
| 61 |
+
print(" [PASS] %s %s" % (name, detail))
|
| 62 |
+
else:
|
| 63 |
+
FAIL += 1
|
| 64 |
+
print(" [FAIL] %s %s" % (name, detail))
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def http(method: str, path: str, *, body=None, headers=None):
|
| 68 |
+
"""返回 (status, headers_dict, content_bytes)。"""
|
| 69 |
+
url = path if path.startswith("http") else BASE + path
|
| 70 |
+
data = None
|
| 71 |
+
hdrs = {k: v for k, v in (headers or {}).items()}
|
| 72 |
+
if body is not None:
|
| 73 |
+
data = json.dumps(body).encode("utf-8")
|
| 74 |
+
hdrs["Content-Type"] = "application/json"
|
| 75 |
+
req = urllib.request.Request(url, data=data, headers=hdrs, method=method)
|
| 76 |
+
try:
|
| 77 |
+
with urllib.request.urlopen(req, timeout=120) as resp:
|
| 78 |
+
return resp.status, {k.lower(): v for k, v in resp.headers.items()}, resp.read()
|
| 79 |
+
except urllib.error.HTTPError as e:
|
| 80 |
+
return e.code, {k.lower(): v for k, v in (e.headers or {}).items()}, e.read()
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def post_json(path, body, headers=None):
|
| 84 |
+
st, _h, raw = http("POST", path, body=body, headers=headers)
|
| 85 |
+
try:
|
| 86 |
+
return st, json.loads(raw.decode("utf-8"))
|
| 87 |
+
except Exception:
|
| 88 |
+
return st, {}
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def start_server() -> uvicorn.Server:
|
| 92 |
+
cfg = uvicorn.Config(app, host="127.0.0.1", port=PORT, log_level="warning", access_log=False)
|
| 93 |
+
srv = uvicorn.Server(cfg)
|
| 94 |
+
threading.Thread(target=srv.run, daemon=True).start()
|
| 95 |
+
for _ in range(300):
|
| 96 |
+
if srv.started:
|
| 97 |
+
return srv
|
| 98 |
+
time.sleep(0.05)
|
| 99 |
+
raise RuntimeError("uvicorn 没起来")
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def png_info(raw: bytes):
|
| 103 |
+
from PIL import Image
|
| 104 |
+
|
| 105 |
+
img = Image.open(io.BytesIO(raw))
|
| 106 |
+
return img.mode, img.size, (img.getchannel("A").getextrema() if img.mode == "RGBA" else None)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def qs(url: str) -> dict:
|
| 110 |
+
return dict(p.split("=", 1) for p in url.split("?", 1)[1].split("&"))
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def main() -> int:
|
| 114 |
+
srv = start_server()
|
| 115 |
+
print("uvicorn 已启动:%s\n" % BASE)
|
| 116 |
+
try:
|
| 117 |
+
# ---------- 1. 鉴权 ----------
|
| 118 |
+
print("1) 鉴权")
|
| 119 |
+
st, _ = post_json("/v1/xtc/tex/ensure", {"items": [{"latex": "a^2", "key": "k"}]})
|
| 120 |
+
check("无 access key → 401", st == 401, "got %d" % st)
|
| 121 |
+
st, _ = post_json("/v1/xtc/tex/ensure", {"items": [{"latex": "a^2", "key": "k"}]},
|
| 122 |
+
{"x-xtc-access-key": KEY})
|
| 123 |
+
check("带 access key → 200", st == 200, "got %d" % st)
|
| 124 |
+
|
| 125 |
+
# ---------- 2. 批大小:12 条 ----------
|
| 126 |
+
print("2) 批大小(端上单条消息上限 12)")
|
| 127 |
+
items = [{"latex": "x_{%d}^{2} + y_{%d}^{2} = r_{%d}^{2}" % (i, i, i), "key": "key%d" % i}
|
| 128 |
+
for i in range(F._MAX_FORMULAS)]
|
| 129 |
+
st, body = post_json("/v1/xtc/tex/ensure", {"fs": 12, "scale": 3, "items": items},
|
| 130 |
+
{"x-xtc-access-key": KEY})
|
| 131 |
+
got = body.get("items") or []
|
| 132 |
+
check("12 条全部返回", len(got) == 12, "返回 %d 条" % len(got))
|
| 133 |
+
check("key 原样回传", len(got) == 12 and all(got[i].get("key") == "key%d" % i for i in range(12)))
|
| 134 |
+
check("有 id/url/w/h", all(g.get("id") and g.get("url") and g.get("w") and g.get("h") for g in got))
|
| 135 |
+
if got:
|
| 136 |
+
print(" 示例:%s -> %sx%s %s" % (got[0]["id"], got[0]["w"], got[0]["h"], got[0]["url"][:70]))
|
| 137 |
+
|
| 138 |
+
# ---------- 3. 降级 ----------
|
| 139 |
+
print("3) 渲染不了的公式必须不出现在响应里(端上据此保留 LaTeX 原文)")
|
| 140 |
+
mix = [
|
| 141 |
+
{"latex": "a^2 + b^2 = c^2", "key": "ok1"},
|
| 142 |
+
{"latex": "\\begin{pmatrix} a & b \\\\ c & d \\end{pmatrix}", "key": "matrix"},
|
| 143 |
+
{"latex": "\\int_0^1 x\\,dx = \\frac{1}{2}", "key": "ok2"},
|
| 144 |
+
{"latex": "\\begin{array}{cc} 1 & 2 \\end{array}", "key": "array"},
|
| 145 |
+
{"latex": "x" * 600, "key": "toolong"},
|
| 146 |
+
]
|
| 147 |
+
_st, body = post_json("/v1/xtc/tex/ensure", {"items": mix}, {"x-xtc-access-key": KEY})
|
| 148 |
+
keys = [it.get("key") for it in (body.get("items") or [])]
|
| 149 |
+
check("矩阵/array/超长都不下发", keys == ["ok1", "ok2"], "返回 %s" % keys)
|
| 150 |
+
|
| 151 |
+
# ---------- 4. 取图 ----------
|
| 152 |
+
print("4) GET 取图")
|
| 153 |
+
url = got[0]["url"]
|
| 154 |
+
scale = int(qs(url)["scale"])
|
| 155 |
+
st, hdrs, raw = http("GET", url)
|
| 156 |
+
check("200 + image/png", st == 200 and hdrs.get("content-type") == "image/png",
|
| 157 |
+
"%d %s" % (st, hdrs.get("content-type")))
|
| 158 |
+
check("PNG 签名", raw[:8] == b"\x89PNG\r\n\x1a\n")
|
| 159 |
+
check("单张 <=50KB", len(raw) <= 50 * 1024, "%d 字节" % len(raw))
|
| 160 |
+
check("Cache-Control immutable", "immutable" in (hdrs.get("cache-control") or ""))
|
| 161 |
+
mode, size, alpha = png_info(raw)
|
| 162 |
+
check("RGBA 真彩色", mode == "RGBA", mode)
|
| 163 |
+
check("真有透明像素", alpha is not None and alpha[0] == 0 and alpha[1] == 255, str(alpha))
|
| 164 |
+
want_w = round(got[0]["w"] * scale)
|
| 165 |
+
want_h = round(got[0]["h"] * scale)
|
| 166 |
+
check("像素尺寸 = design*scale", abs(size[0] - want_w) <= 1 and abs(size[1] - want_h) <= 1,
|
| 167 |
+
"png %s vs 期望 %sx%s" % (size, want_w, want_h))
|
| 168 |
+
|
| 169 |
+
# ---------- 5. 内容寻址 ----------
|
| 170 |
+
print("5) 内容寻址 / 幂等")
|
| 171 |
+
_st, body2 = post_json("/v1/xtc/tex/ensure", {"fs": 12, "scale": 3, "items": items},
|
| 172 |
+
{"x-xtc-access-key": KEY})
|
| 173 |
+
ids2 = [it["id"] for it in (body2.get("items") or [])]
|
| 174 |
+
check("同内容同 id", ids2 == [g["id"] for g in got])
|
| 175 |
+
_st, _h, raw2 = http("GET", url)
|
| 176 |
+
check("重复取图字节一致", raw2 == raw)
|
| 177 |
+
|
| 178 |
+
# ---------- 6. 体积兜底 + 缓存键对齐 ----------
|
| 179 |
+
# 真造一条 >50KB 的公式得 1000+ 字符,会先被 _MAX_LATEX_LEN(512) 拦掉;
|
| 180 |
+
# 这里临时把体积上限压到 4KB(该公式 scale=3 约 7.1KB、scale=2 约 3.1KB),
|
| 181 |
+
# 等价地逼出「降 scale 重渲染」这条路。
|
| 182 |
+
print("6) 体积兜底(超过体积上限自动降 scale,下发 id 必须直接命中缓存)")
|
| 183 |
+
old_max_bytes = F._MAX_BYTES
|
| 184 |
+
F._MAX_BYTES = 4 * 1024
|
| 185 |
+
try:
|
| 186 |
+
big = "\\sum_{i=0}^{n} \\frac{x^{i}}{i!} + \\int_{0}^{\\infty} e^{-t} t^{x-1} dt"
|
| 187 |
+
_st, bbody = post_json("/v1/xtc/tex/ensure",
|
| 188 |
+
{"fs": 12, "scale": 3, "items": [{"latex": big, "key": "big"}]},
|
| 189 |
+
{"x-xtc-access-key": KEY})
|
| 190 |
+
finally:
|
| 191 |
+
F._MAX_BYTES = old_max_bytes
|
| 192 |
+
big_items = bbody.get("items") or []
|
| 193 |
+
check("被判定超体积的公式仍渲染成功", len(big_items) == 1)
|
| 194 |
+
if big_items:
|
| 195 |
+
big_scale = int(qs(big_items[0]["url"])["scale"])
|
| 196 |
+
check("确实降了 scale", big_scale < 3, "scale=%d" % big_scale)
|
| 197 |
+
used_before = TEX_API._window_used
|
| 198 |
+
st, _h, rb = http("GET", big_items[0]["url"])
|
| 199 |
+
check("降级图取图 200 且 <=50KB", st == 200 and len(rb) <= 50 * 1024, "%d 字节" % len(rb))
|
| 200 |
+
check("命中缓存、没再消耗限流额度", TEX_API._window_used == used_before,
|
| 201 |
+
"%d -> %d" % (used_before, TEX_API._window_used))
|
| 202 |
+
|
| 203 |
+
# ---------- 7. 防滥用 ----------
|
| 204 |
+
# 注意:LRU 命中时不校验 f(id 本身就是 capability,端上取图就是这个语义),
|
| 205 |
+
# 所以这里必须拿一个「从没渲染过」的 id 去撞校验分支。
|
| 206 |
+
print("7) 防滥用")
|
| 207 |
+
from urllib.parse import quote
|
| 208 |
+
never = "\\gamma_{7} = \\delta_{7} \\cdot \\theta_{7}"
|
| 209 |
+
never_id = F.formula_id(never, 12, 3, F.DEFAULT_FG)
|
| 210 |
+
st, _h, _b = http("GET", "/v1/xtc/tex/%s.png?f=%s&fs=12&scale=3&fg=%s" % (
|
| 211 |
+
never_id, quote("\\rho_{9} = \\sigma_{9}", safe=""), quote(F.DEFAULT_FG, safe="")))
|
| 212 |
+
check("id 与内容不一致 → 403", st == 403, "got %d" % st)
|
| 213 |
+
st, _h, _b = http("GET", "/v1/xtc/tex/0123456789abcdef0123.png")
|
| 214 |
+
check("未知 id 且无 f → 404", st == 404, "got %d" % st)
|
| 215 |
+
|
| 216 |
+
old_budget, old_used, old_start = TEX_API._RENDER_BUDGET, TEX_API._window_used, TEX_API._window_start
|
| 217 |
+
TEX_API._RENDER_BUDGET = 1
|
| 218 |
+
TEX_API._window_used = 0
|
| 219 |
+
TEX_API._window_start = time.time()
|
| 220 |
+
try:
|
| 221 |
+
def fresh_url(i):
|
| 222 |
+
from urllib.parse import quote
|
| 223 |
+
tex = "\\alpha_{%d} = \\beta_{%d}" % (900 + i, 900 + i)
|
| 224 |
+
tid = F.formula_id(tex, 12, 3, F.DEFAULT_FG)
|
| 225 |
+
return "/v1/xtc/tex/%s.png?f=%s&fs=12&scale=3&fg=%s" % (
|
| 226 |
+
tid, quote(tex, safe=""), quote(F.DEFAULT_FG, safe=""))
|
| 227 |
+
|
| 228 |
+
a = http("GET", fresh_url(1))[0]
|
| 229 |
+
b = http("GET", fresh_url(2))[0]
|
| 230 |
+
check("限流:第 1 个放行 / 第 2 个 429", a == 200 and b == 429, "%d / %d" % (a, b))
|
| 231 |
+
finally:
|
| 232 |
+
TEX_API._RENDER_BUDGET, TEX_API._window_used, TEX_API._window_start = old_budget, 0, old_start
|
| 233 |
+
|
| 234 |
+
# ---------- 8. 参数校验 ----------
|
| 235 |
+
print("8) 参数校验")
|
| 236 |
+
st, _ = post_json("/v1/xtc/tex/ensure", {"fs": 99, "items": []}, {"x-xtc-access-key": KEY})
|
| 237 |
+
check("fs 越界 → 400", st == 400, "got %d" % st)
|
| 238 |
+
st, _ = post_json("/v1/xtc/tex/ensure", {"nope": 1}, {"x-xtc-access-key": KEY})
|
| 239 |
+
check("缺 items → 400", st == 400, "got %d" % st)
|
| 240 |
+
st, _h, _b = http("GET", "/v1/xtc/tex/%s.png" % got[0]["id"])
|
| 241 |
+
check("有 id 时不需要 f(LRU 命中直接返回)", st == 200, "got %d" % st)
|
| 242 |
+
finally:
|
| 243 |
+
srv.should_exit = True
|
| 244 |
+
time.sleep(0.3)
|
| 245 |
+
|
| 246 |
+
print("\n合计:pass %d / fail %d" % (PASS, FAIL))
|
| 247 |
+
return 1 if FAIL else 0
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
if __name__ == "__main__":
|
| 251 |
+
raise SystemExit(main())
|
app/api/pseudo_stream.py
CHANGED
|
@@ -23,7 +23,6 @@ from ..media.imagefix import (
|
|
| 23 |
from ..providers.policy import is_image_model
|
| 24 |
from ..services import pseudo_store
|
| 25 |
from ..services.config_store import load_config
|
| 26 |
-
from ..services.formula import build_texs
|
| 27 |
from ..utils.thought import extract_thought_and_answer
|
| 28 |
from ._common import (
|
| 29 |
CORS_HEADERS,
|
|
@@ -256,11 +255,6 @@ async def _run_pseudo_session(
|
|
| 256 |
streamed_thought = "".join(thought_parts)
|
| 257 |
tag_thought, text = extract_thought_and_answer(full_text)
|
| 258 |
thought = streamed_thought or tag_thought
|
| 259 |
-
# 公式渲染:把回复里的 $...$ 公式在服务端渲染成小尺寸 PNG,只回「公式清单」
|
| 260 |
-
# (正文 text 保持不变,端上按出现顺序切分正文即可还原图文混排)。
|
| 261 |
-
# 放线程池:mathtext 解析是 CPU 密集,直接在事件循环里跑会阻塞其他请求。
|
| 262 |
-
# 失败/依赖缺失返回空清单,端上按纯文本渲染,不影响主链路。
|
| 263 |
-
texs = await asyncio.to_thread(build_texs, text)
|
| 264 |
# 一次性写入完整回复 + emit done。前端轮询到 done 直接读 body.text。
|
| 265 |
pseudo_store.update_session_state(
|
| 266 |
session_id,
|
|
@@ -270,7 +264,6 @@ async def _run_pseudo_session(
|
|
| 270 |
)
|
| 271 |
pseudo_store.append_event(
|
| 272 |
session_id, type="done", delta="", finish_reason=last_finish or "stop",
|
| 273 |
-
extra={"texs": texs} if texs else None,
|
| 274 |
)
|
| 275 |
record_usage(
|
| 276 |
access_key=access_key, provider=provider.id, model=clean_model,
|
|
|
|
| 23 |
from ..providers.policy import is_image_model
|
| 24 |
from ..services import pseudo_store
|
| 25 |
from ..services.config_store import load_config
|
|
|
|
| 26 |
from ..utils.thought import extract_thought_and_answer
|
| 27 |
from ._common import (
|
| 28 |
CORS_HEADERS,
|
|
|
|
| 255 |
streamed_thought = "".join(thought_parts)
|
| 256 |
tag_thought, text = extract_thought_and_answer(full_text)
|
| 257 |
thought = streamed_thought or tag_thought
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
# 一次性写入完整回复 + emit done。前端轮询到 done 直接读 body.text。
|
| 259 |
pseudo_store.update_session_state(
|
| 260 |
session_id,
|
|
|
|
| 264 |
)
|
| 265 |
pseudo_store.append_event(
|
| 266 |
session_id, type="done", delta="", finish_reason=last_finish or "stop",
|
|
|
|
| 267 |
)
|
| 268 |
record_usage(
|
| 269 |
access_key=access_key, provider=provider.id, model=clean_model,
|
app/api/tex.py
CHANGED
|
@@ -14,11 +14,12 @@ from __future__ import annotations
|
|
| 14 |
import logging
|
| 15 |
import time
|
| 16 |
|
| 17 |
-
from fastapi import APIRouter, Query, Response
|
| 18 |
|
|
|
|
| 19 |
from ..errors import HttpError
|
| 20 |
from ..services import formula
|
| 21 |
-
from ._common import CORS_HEADERS
|
| 22 |
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
|
@@ -42,6 +43,40 @@ def _allow_render() -> bool:
|
|
| 42 |
return True
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
@router.get("/{tex_id}.png")
|
| 46 |
async def get_tex_png(
|
| 47 |
tex_id: str,
|
|
|
|
| 14 |
import logging
|
| 15 |
import time
|
| 16 |
|
| 17 |
+
from fastapi import APIRouter, Depends, Query, Request, Response
|
| 18 |
|
| 19 |
+
from ..auth import require_access_key
|
| 20 |
from ..errors import HttpError
|
| 21 |
from ..services import formula
|
| 22 |
+
from ._common import CORS_HEADERS, ok_with_cors, read_request_body
|
| 23 |
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
|
|
|
| 43 |
return True
|
| 44 |
|
| 45 |
|
| 46 |
+
@router.post("/ensure")
|
| 47 |
+
async def ensure_texs(
|
| 48 |
+
request: Request,
|
| 49 |
+
_key: str = Depends(require_access_key),
|
| 50 |
+
) -> dict:
|
| 51 |
+
"""端上「前端识别公式 → 请求生成图片」的批量入口。
|
| 52 |
+
|
| 53 |
+
请求:{"items": [{"latex": "...", "key": "端上自算的标识"}]}
|
| 54 |
+
返回:{"items": [{"key", "id", "url", "w", "h"}]}(渲染失败的条目不下发,
|
| 55 |
+
端上按「该处保留 LaTeX 原文」降级处理)。
|
| 56 |
+
|
| 57 |
+
端上先查本地缓存(文件名 t_<id>.png):命中的公式不会进这个请求,
|
| 58 |
+
只有确实缺图的才来渲染,渲染结果按内容寻址进进程内 LRU。
|
| 59 |
+
"""
|
| 60 |
+
body = await read_request_body(request)
|
| 61 |
+
items = body.get("items") if isinstance(body, dict) else None
|
| 62 |
+
if not isinstance(items, list):
|
| 63 |
+
raise HttpError("items is required", status=400, code="bad_request")
|
| 64 |
+
try:
|
| 65 |
+
fs = int(body.get("fs") or formula.DEFAULT_FS)
|
| 66 |
+
scale = int(body.get("scale") or formula.DEFAULT_SCALE)
|
| 67 |
+
except (TypeError, ValueError):
|
| 68 |
+
raise HttpError("invalid fs/scale", status=400, code="bad_request")
|
| 69 |
+
if fs < 8 or fs > 48 or scale < 1 or scale > 4:
|
| 70 |
+
raise HttpError("fs/scale out of range", status=400, code="bad_request")
|
| 71 |
+
fg = str(body.get("fg") or formula.DEFAULT_FG)[:16]
|
| 72 |
+
|
| 73 |
+
# 渲染是 CPU 密集:放线程池,避免阻塞事件循环(mathtext 解析 + PIL 编码)
|
| 74 |
+
import asyncio
|
| 75 |
+
|
| 76 |
+
rendered = await asyncio.to_thread(formula.ensure_batch, items, fs, scale, fg)
|
| 77 |
+
return ok_with_cors({"items": rendered})
|
| 78 |
+
|
| 79 |
+
|
| 80 |
@router.get("/{tex_id}.png")
|
| 81 |
async def get_tex_png(
|
| 82 |
tex_id: str,
|
app/api/xtc.py
CHANGED
|
@@ -27,7 +27,6 @@ from ..media.imagefix import (
|
|
| 27 |
)
|
| 28 |
from ..providers.policy import is_image_model, is_model_allowed
|
| 29 |
from ..services.config_store import load_config
|
| 30 |
-
from ..services.formula import build_texs
|
| 31 |
from ..utils.sse import sse_data, sse_done
|
| 32 |
from ..utils.thought import (
|
| 33 |
extract_thought_and_answer,
|
|
@@ -319,7 +318,7 @@ async def xtc_chat(
|
|
| 319 |
usage=(openai_resp or {}).get("usage"),
|
| 320 |
ok=True,
|
| 321 |
)
|
| 322 |
-
return
|
| 323 |
openai_resp,
|
| 324 |
provider=provider.id,
|
| 325 |
model=clean_model,
|
|
@@ -358,7 +357,7 @@ def _build_upstream_body(body: dict, clean_model: str, messages: list) -> dict:
|
|
| 358 |
return out
|
| 359 |
|
| 360 |
|
| 361 |
-
|
| 362 |
openai_resp: dict,
|
| 363 |
*,
|
| 364 |
provider: str,
|
|
@@ -397,11 +396,6 @@ async def _build_xtc_response(
|
|
| 397 |
"usage": openai_resp.get("usage") or {},
|
| 398 |
"finish_reason": choice.get("finish_reason"),
|
| 399 |
}
|
| 400 |
-
# 公式渲染:只回公式清单(正文 text 不变),端上按出现顺序切分正文还原图文混排。
|
| 401 |
-
# 放线程池避免 mathtext 阻塞事件循环;失败/依赖缺失返回空清单。
|
| 402 |
-
texs = await asyncio.to_thread(build_texs, text)
|
| 403 |
-
if texs:
|
| 404 |
-
payload["texs"] = texs
|
| 405 |
if image_fix_mode:
|
| 406 |
payload["image_fix_mode"] = image_fix_mode
|
| 407 |
payload["image_fixed"] = image_fixed
|
|
|
|
| 27 |
)
|
| 28 |
from ..providers.policy import is_image_model, is_model_allowed
|
| 29 |
from ..services.config_store import load_config
|
|
|
|
| 30 |
from ..utils.sse import sse_data, sse_done
|
| 31 |
from ..utils.thought import (
|
| 32 |
extract_thought_and_answer,
|
|
|
|
| 318 |
usage=(openai_resp or {}).get("usage"),
|
| 319 |
ok=True,
|
| 320 |
)
|
| 321 |
+
return _build_xtc_response(
|
| 322 |
openai_resp,
|
| 323 |
provider=provider.id,
|
| 324 |
model=clean_model,
|
|
|
|
| 357 |
return out
|
| 358 |
|
| 359 |
|
| 360 |
+
def _build_xtc_response(
|
| 361 |
openai_resp: dict,
|
| 362 |
*,
|
| 363 |
provider: str,
|
|
|
|
| 396 |
"usage": openai_resp.get("usage") or {},
|
| 397 |
"finish_reason": choice.get("finish_reason"),
|
| 398 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
if image_fix_mode:
|
| 400 |
payload["image_fix_mode"] = image_fix_mode
|
| 401 |
payload["image_fixed"] = image_fixed
|
app/services/formula.py
CHANGED
|
@@ -36,33 +36,17 @@ DEFAULT_BG = "#1f2937"
|
|
| 36 |
|
| 37 |
_MAX_BYTES = 50 * 1024
|
| 38 |
_MAX_LATEX_LEN = 512 # 单条公式长度上限(防超长输入拖垮解析)
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
_MAX_PIXELS = 4096 * 4096 # 输出像素总量上限
|
| 41 |
|
| 42 |
-
# 公式抽取:$$...$$ 优先于 $...$;\(...\) 与 \[...\] 一并支持
|
| 43 |
-
_MATH_PATTERNS = (
|
| 44 |
-
re.compile(r"\$\$(.+?)\$\$", re.S),
|
| 45 |
-
re.compile(r"\\\[(.+?)\\\]", re.S),
|
| 46 |
-
re.compile(r"\\\((.+?)\\\)", re.S),
|
| 47 |
-
re.compile(r"(?<![\\$])\$([^$\n]+?)\$(?!\$)"),
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
_rendered: "LRUCache" = LRUCache(maxsize=512)
|
| 51 |
_render_lock = threading.Lock()
|
| 52 |
_matplotlib_ok: Optional[bool] = None
|
| 53 |
|
| 54 |
-
# 行内 $...$ 的合理性过滤:货币/价格写法("$5 和 $10")会配对成假公式。
|
| 55 |
-
# 显式分隔符($$、\[、\()不做过滤——那是作者明确标注的公式。
|
| 56 |
-
_CJK_RE = re.compile(r"[\u2e80-\u9fff\uff00-\uffef]")
|
| 57 |
-
_MATH_HINT_RE = re.compile(r"[\\^_=+\-*/<>()\[\]{}|]")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def _plausible_inline(body: str) -> bool:
|
| 61 |
-
"""行内 $...$ 是否像公式:含数学符号且不含中文,避免把价格写法当公式。"""
|
| 62 |
-
if _CJK_RE.search(body):
|
| 63 |
-
return False
|
| 64 |
-
return bool(_MATH_HINT_RE.search(body))
|
| 65 |
-
|
| 66 |
|
| 67 |
def is_available() -> bool:
|
| 68 |
"""matplotlib 是否可用(导入失败则整个公式功能关闭)。"""
|
|
@@ -87,68 +71,200 @@ def formula_id(latex: str, fs: int = DEFAULT_FS, scale: int = DEFAULT_SCALE, fg:
|
|
| 87 |
return hashlib.sha1(raw.encode("utf-8")).hexdigest()[:20]
|
| 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 |
-
continue
|
| 121 |
-
taken.append((a, b))
|
| 122 |
-
hits.append((a, b, body))
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
|
| 128 |
# ===== 渲染 =====
|
| 129 |
|
| 130 |
-
def
|
| 131 |
-
"""渲染一张
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
from matplotlib.figure import Figure
|
| 133 |
from PIL import Image
|
| 134 |
|
| 135 |
dpi = 72.0 * scale
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
if bg:
|
| 148 |
-
out = Image.new("RGB",
|
| 149 |
-
out.paste(
|
| 150 |
else:
|
| 151 |
-
out =
|
| 152 |
out.info = {} # 丢掉元数据
|
| 153 |
png = io.BytesIO()
|
| 154 |
out.save(png, format="PNG", compress_level=9)
|
|
@@ -175,7 +291,10 @@ def render(latex: str, fs: int = DEFAULT_FS, scale: int = DEFAULT_SCALE, fg: str
|
|
| 175 |
if s in tried or s < 1:
|
| 176 |
continue
|
| 177 |
tried.append(s)
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
| 179 |
if pw * ph > _MAX_PIXELS:
|
| 180 |
return None
|
| 181 |
if len(png) <= _MAX_BYTES or s == 1:
|
|
@@ -188,6 +307,13 @@ def render(latex: str, fs: int = DEFAULT_FS, scale: int = DEFAULT_SCALE, fg: str
|
|
| 188 |
}
|
| 189 |
with _render_lock:
|
| 190 |
_rendered[key] = info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
return info
|
| 192 |
except Exception:
|
| 193 |
logger.info("[formula] render failed: %s", body[:80], exc_info=False)
|
|
@@ -195,41 +321,52 @@ def render(latex: str, fs: int = DEFAULT_FS, scale: int = DEFAULT_SCALE, fg: str
|
|
| 195 |
return None
|
| 196 |
|
| 197 |
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
-
def build_texs(text: str, fs: int = DEFAULT_FS) -> list[dict]:
|
| 201 |
-
"""把正文里的公式渲染成清单:[{id, latex, url, w, h, tex}](最多 _MAX_FORMULAS 条)。
|
| 202 |
|
| 203 |
-
|
| 204 |
-
渲染
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
"""
|
| 206 |
if not is_available():
|
| 207 |
return []
|
| 208 |
out: list[dict] = []
|
| 209 |
-
for
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
if not info:
|
| 212 |
continue
|
|
|
|
| 213 |
out.append({
|
| 214 |
-
"
|
| 215 |
-
"
|
| 216 |
-
|
|
|
|
|
|
|
| 217 |
"w": info["w"],
|
| 218 |
"h": info["h"],
|
| 219 |
-
"tex": True,
|
| 220 |
})
|
| 221 |
return out
|
| 222 |
|
| 223 |
|
| 224 |
-
def
|
| 225 |
-
|
| 226 |
-
entry = _rendered.get(str(tex_id or ""))
|
| 227 |
-
if not entry:
|
| 228 |
-
return None
|
| 229 |
-
return entry.get("png")
|
| 230 |
-
|
| 231 |
|
| 232 |
-
|
| 233 |
-
"""按公式内容渲染取 PNG(端点调用:内容寻址,可安全地按需渲染)。"""
|
| 234 |
-
info = render(latex, fs=fs, scale=scale, fg=fg)
|
| 235 |
-
return info.get("png") if info else None
|
|
|
|
| 36 |
|
| 37 |
_MAX_BYTES = 50 * 1024
|
| 38 |
_MAX_LATEX_LEN = 512 # 单条公式长度上限(防超长输入拖垮解析)
|
| 39 |
+
# 单次批量请求最多渲染几条。必须 >= 端上单条消息的公式上限(tex.js MAX_PER_MSG=12),
|
| 40 |
+
# 否则端上一次请求里的多余条目会被这里截掉,端上又拿不到结果 → 那些公式被
|
| 41 |
+
# 永久降级成 LaTeX 原文(旧值 4 就是这个 bug 的根源)。
|
| 42 |
+
# 端上另外做了分批(每批 <= _ENSURE_BATCH),两端上限不一致时也不会静默丢。
|
| 43 |
+
_MAX_FORMULAS = 12
|
| 44 |
_MAX_PIXELS = 4096 * 4096 # 输出像素总量上限
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
_rendered: "LRUCache" = LRUCache(maxsize=512)
|
| 47 |
_render_lock = threading.Lock()
|
| 48 |
_matplotlib_ok: Optional[bool] = None
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
def is_available() -> bool:
|
| 52 |
"""matplotlib 是否可用(导入失败则整个公式功能关闭)。"""
|
|
|
|
| 71 |
return hashlib.sha1(raw.encode("utf-8")).hexdigest()[:20]
|
| 72 |
|
| 73 |
|
| 74 |
+
# ===== LaTeX 预处理 =====
|
| 75 |
+
# 模型/文档里的公式五花八门,mathtext 只认最基础的数学语法,这里先把常见写法
|
| 76 |
+
# 归一化成 mathtext 可解析的形式(否则整条公式渲染失败、只能降级成文本)。
|
| 77 |
|
| 78 |
+
# 文档级命令:不是公式,出现在裸公式块里应当忽略
|
| 79 |
+
_PREAMBLE_CMDS = (
|
| 80 |
+
"usepackage", "documentclass", "begin{document}", "end{document}",
|
| 81 |
+
"title", "author", "date", "maketitle", "tableofcontents", "newpage",
|
| 82 |
+
"clearpage", "centering", "setlength", "geometry", "pagestyle",
|
| 83 |
+
"section", "subsection", "label", "ref", "cite", "includegraphics",
|
| 84 |
+
"hline", "toprule", "midrule", "bottomrule", "today",
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
# 多行环境(\\ 分行、& 分列):去掉环境包裹与对齐符后按行渲染
|
| 88 |
+
_MULTILINE_ENVS = (
|
| 89 |
+
"aligned", "align", "align*", "gathered", "gather", "gather*",
|
| 90 |
+
"split", "cases", "dcases", "eqnarray", "eqnarray*",
|
| 91 |
+
)
|
| 92 |
+
# 不支持的环境(矩阵/表格需要列对齐,mathtext 排不了)→ 直接判定渲染失败
|
| 93 |
+
_UNSUPPORTED_ENVS = ("matrix", "pmatrix", "bmatrix", "vmatrix", "array", "tabular", "table")
|
| 94 |
+
|
| 95 |
+
_re_begin = re.compile(r"\\begin\{([^}]*)\}")
|
| 96 |
+
_re_comment = re.compile(r"(?<!\\)%[^\n]*")
|
| 97 |
+
# 公式正文里的中日韩字符(含全角标点):mathtext 没有中文字形,画出来是一排空心
|
| 98 |
+
# 方块,比退回 LaTeX 原文更难读。端上对行内 $..$ 已经排除中文,但 $$..$$ / \[..\]
|
| 99 |
+
# 没有排除(模型很爱在块级公式里写中文注解),这里兜底判为「不渲染」。
|
| 100 |
+
_re_cjk = re.compile(r"[\u2e80-\u9fff\u3000-\u303f\uff00-\uffef]")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def is_preamble_latex(body: str) -> bool:
|
| 104 |
+
"""整块是导言区/文档级命令(不是公式),用于裸公式识别时排除。"""
|
| 105 |
+
s = str(body or "").strip()
|
| 106 |
+
if not s:
|
| 107 |
+
return True
|
| 108 |
+
for cmd in _PREAMBLE_CMDS:
|
| 109 |
+
if ("\\" + cmd) in s or ("\\begin{document}") in s:
|
| 110 |
+
return True
|
| 111 |
+
return False
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
|
| 114 |
+
def _convert_mhchem(body: str) -> str:
|
| 115 |
+
"""\\ce{2H2 + O2 -> 2H2O} 这类 mhchem 写法 → mathtext 能排的等价写法。
|
| 116 |
+
|
| 117 |
+
mhchem 是宏包语法,mathtext 不支持,这里只覆盖化学方程式的常见写法:
|
| 118 |
+
箭头、可逆号、元素后的数字下标、电荷上标、气体/沉淀号。
|
| 119 |
+
"""
|
| 120 |
+
b = str(body or "")
|
| 121 |
+
b = b.replace("<=>", " \\rightleftharpoons ").replace("<->", " \\rightleftharpoons ")
|
| 122 |
+
b = b.replace("->", " \\rightarrow ").replace("<-", " \\leftarrow ")
|
| 123 |
+
# 元素符号/括号后的数字 → 下标(H2 → H_{2},NH3 → NH_{3},(g) 不动)
|
| 124 |
+
b = re.sub(r"(?<=[A-Za-z\)\]])([0-9]+)(?![0-9])", r"_{\1}", b)
|
| 125 |
+
# 电荷上标:^2- / ^+ / ^3+ → ^{2-}
|
| 126 |
+
b = re.sub(r"\^([0-9]*[+\-])", r"^{\1}", b)
|
| 127 |
+
# 气体上升号 ^ / 沉淀号 v(mhchem 写法,mathtext 没有这两个记号)→ 箭头。
|
| 128 |
+
# 只认「独立成词」的那个字符:^{14} 的 ^、hv(光量子)里的 v 都不能动。
|
| 129 |
+
b = re.sub(r"(?:^|(?<=\s))\^(?=\s|$)", r" \\uparrow ", b)
|
| 130 |
+
b = re.sub(r"(?:^|(?<=\s))v(?=\s|$)", r" \\downarrow ", b)
|
| 131 |
+
return b
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def _replace_ce(s: str) -> str:
|
| 135 |
+
"""把 \\ce{...} 换掉。手写扫描而不是正则:\\ce 里会出现嵌套花括号
|
| 136 |
+
(同位素 ^{14}C、\\ce{Fe(SCN)3} 之类),正则 [^{}]* 匹配不到就会原样漏给
|
| 137 |
+
mathtext → 整条公式渲染失败。
|
| 138 |
+
"""
|
| 139 |
+
out = []
|
| 140 |
+
i = 0
|
| 141 |
+
while i < len(s):
|
| 142 |
+
j = s.find("\\ce{", i)
|
| 143 |
+
if j < 0:
|
| 144 |
+
out.append(s[i:])
|
| 145 |
+
break
|
| 146 |
+
out.append(s[i:j])
|
| 147 |
+
k = j + 4
|
| 148 |
+
depth = 1
|
| 149 |
+
buf = []
|
| 150 |
+
while k < len(s) and depth > 0:
|
| 151 |
+
ch = s[k]
|
| 152 |
+
if ch == "{":
|
| 153 |
+
depth += 1
|
| 154 |
+
elif ch == "}":
|
| 155 |
+
depth -= 1
|
| 156 |
+
if depth > 0:
|
| 157 |
+
buf.append(ch)
|
| 158 |
+
k += 1
|
| 159 |
+
if depth != 0:
|
| 160 |
+
# 花括号没闭合:原样保留,交给 mathtext 报错(端上退回原文)
|
| 161 |
+
out.append(s[j:])
|
| 162 |
+
break
|
| 163 |
+
out.append(_convert_mhchem("".join(buf)))
|
| 164 |
+
i = k
|
| 165 |
+
return "".join(out)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def normalize_latex(body: str) -> Optional[list]:
|
| 169 |
+
"""把一条公式归一化成「待渲染的行列表」;无法渲染返回 None。
|
| 170 |
+
|
| 171 |
+
返回的每行都是不带 $ 的 mathtext 表达式,多行由调用方按行堆叠渲染。
|
| 172 |
+
"""
|
| 173 |
+
s = str(body or "").strip()
|
| 174 |
+
if not s:
|
| 175 |
+
return None
|
| 176 |
+
# 去掉 LaTeX 注释(\% 转义的不算注释)
|
| 177 |
+
s = _re_comment.sub(" ", s)
|
| 178 |
+
# mhchem 化学式
|
| 179 |
+
s = _replace_ce(s)
|
| 180 |
+
|
| 181 |
+
# 环境名处理:多行环境去壳,不支持的环境直接失败
|
| 182 |
+
envs = _re_begin.findall(s)
|
| 183 |
+
for env in envs:
|
| 184 |
+
base = env.strip().rstrip("*")
|
| 185 |
+
if base in _UNSUPPORTED_ENVS:
|
| 186 |
+
return None
|
| 187 |
+
if base not in [e.rstrip("*") for e in _MULTILINE_ENVS]:
|
| 188 |
+
# 未知环境(如 \text、自定义环境)交给 mathtext 自然报错
|
| 189 |
+
pass
|
| 190 |
+
s = re.sub(r"\\begin\{[^}]*\}|\\end\{[^}]*\}", " ", s)
|
| 191 |
+
s = s.replace("&", " ")
|
| 192 |
+
|
| 193 |
+
# 换行:\\ 是 LaTeX 显式换行;源码里的裸换行也按换行处理——手表屏宽只有
|
| 194 |
+
# 240px,长公式横向铺满后必须整体缩小才放得下,按源码本就断行的地方拆开
|
| 195 |
+
# 反而更清楚(每行各自居中堆叠)。
|
| 196 |
+
s = s.replace("\\\\", "\n")
|
| 197 |
+
lines = []
|
| 198 |
+
for raw in s.split("\n"):
|
| 199 |
+
ln = " ".join(raw.split())
|
| 200 |
+
if ln:
|
| 201 |
+
lines.append(ln)
|
| 202 |
+
if not lines:
|
| 203 |
+
return None
|
| 204 |
+
# 含中文 → 不渲染(mathtext 会画成空心方块),端上退回 LaTeX 原文
|
| 205 |
+
for ln in lines:
|
| 206 |
+
if _re_cjk.search(ln):
|
| 207 |
+
return None
|
| 208 |
+
# 全部行都只是导言区命令 → 不是公式
|
| 209 |
+
if all(is_preamble_latex(ln) for ln in lines):
|
| 210 |
+
return None
|
| 211 |
+
return lines
|
| 212 |
|
| 213 |
|
| 214 |
# ===== 渲染 =====
|
| 215 |
|
| 216 |
+
def _render_lines_rgba(lines: list, fs: int, scale: int, fg: str):
|
| 217 |
+
"""按行渲染并垂直堆叠为一张 RGBA 图(多行公式:换行符/aligned/cases)。
|
| 218 |
+
|
| 219 |
+
逐行单独出图再拼接,避免在 figure 里做坐标换算(bbox_inches='tight'
|
| 220 |
+
会重新裁剪,figure 的分数坐标无法预知最终尺寸)。行内居中。
|
| 221 |
+
"""
|
| 222 |
from matplotlib.figure import Figure
|
| 223 |
from PIL import Image
|
| 224 |
|
| 225 |
dpi = 72.0 * scale
|
| 226 |
+
parts = []
|
| 227 |
+
for ln in lines:
|
| 228 |
+
fig = Figure(figsize=(2, 2), dpi=dpi)
|
| 229 |
+
fig.patch.set_alpha(0)
|
| 230 |
+
fig.text(0, 0, "$" + ln + "$", fontsize=fs, color=fg)
|
| 231 |
+
buf = io.BytesIO()
|
| 232 |
+
fig.savefig(buf, format="png", dpi=dpi, transparent=True,
|
| 233 |
+
bbox_inches="tight", pad_inches=0.0)
|
| 234 |
+
fig.clear()
|
| 235 |
+
buf.seek(0)
|
| 236 |
+
parts.append(Image.open(buf).convert("RGBA"))
|
| 237 |
+
|
| 238 |
+
gap = max(1, int(round(fs * scale * 0.35)))
|
| 239 |
+
width = max(p.size[0] for p in parts)
|
| 240 |
+
height = sum(p.size[1] for p in parts) + gap * (len(parts) - 1)
|
| 241 |
+
out = Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
| 242 |
+
y = 0
|
| 243 |
+
for p in parts:
|
| 244 |
+
out.paste(p, ((width - p.size[0]) // 2, y), p)
|
| 245 |
+
y += p.size[1] + gap
|
| 246 |
+
return out
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
def _render_png(body: str, fs: int, scale: int, fg: str, bg: str = "") -> Optional[tuple]:
|
| 250 |
+
"""渲染一条公式(可多行),返回 (bytes, w_px, h_px);无法渲染返回 None。"""
|
| 251 |
+
from PIL import Image
|
| 252 |
+
|
| 253 |
+
lines = normalize_latex(body)
|
| 254 |
+
if not lines:
|
| 255 |
+
return None
|
| 256 |
+
|
| 257 |
+
img = _render_lines_rgba(lines, fs, scale, fg)
|
| 258 |
+
# 四周补 1 design px 边距(避免端上圆角/描边切到字形或根号上划线)
|
| 259 |
+
pad = max(1, int(round(scale)))
|
| 260 |
+
canvas = Image.new("RGBA", (img.size[0] + pad * 2, img.size[1] + pad * 2), (0, 0, 0, 0))
|
| 261 |
+
canvas.paste(img, (pad, pad), img)
|
| 262 |
+
|
| 263 |
if bg:
|
| 264 |
+
out = Image.new("RGB", canvas.size, bg)
|
| 265 |
+
out.paste(canvas, (0, 0), canvas)
|
| 266 |
else:
|
| 267 |
+
out = canvas
|
| 268 |
out.info = {} # 丢掉元数据
|
| 269 |
png = io.BytesIO()
|
| 270 |
out.save(png, format="PNG", compress_level=9)
|
|
|
|
| 291 |
if s in tried or s < 1:
|
| 292 |
continue
|
| 293 |
tried.append(s)
|
| 294 |
+
got = _render_png(body, fs, s, fg)
|
| 295 |
+
if not got:
|
| 296 |
+
return None
|
| 297 |
+
png, pw, ph = got
|
| 298 |
if pw * ph > _MAX_PIXELS:
|
| 299 |
return None
|
| 300 |
if len(png) <= _MAX_BYTES or s == 1:
|
|
|
|
| 307 |
}
|
| 308 |
with _render_lock:
|
| 309 |
_rendered[key] = info
|
| 310 |
+
# 体积兜底降级后,下发给端上的 id 用的是「实际 scale」算出来的
|
| 311 |
+
# (见 ensure_batch),端上之后就拿这个 id 回源取图。这里同时按
|
| 312 |
+
# 实际 scale 的 id 入缓存,否则 /v1/xtc/tex/{id}.png 每次都 LRU
|
| 313 |
+
# 未命中,白白重渲染一遍(限流额度也被重复消耗)。
|
| 314 |
+
actual = formula_id(body, fs, s, fg)
|
| 315 |
+
if actual != key:
|
| 316 |
+
_rendered[actual] = info
|
| 317 |
return info
|
| 318 |
except Exception:
|
| 319 |
logger.info("[formula] render failed: %s", body[:80], exc_info=False)
|
|
|
|
| 321 |
return None
|
| 322 |
|
| 323 |
|
| 324 |
+
def get_png(tex_id: str) -> Optional[bytes]:
|
| 325 |
+
"""按 id 取已渲染的 PNG(LRU 命中才返回),未命中返回 None。"""
|
| 326 |
+
entry = _rendered.get(str(tex_id or ""))
|
| 327 |
+
if not entry:
|
| 328 |
+
return None
|
| 329 |
+
return entry.get("png")
|
| 330 |
|
|
|
|
|
|
|
| 331 |
|
| 332 |
+
def render_png(latex: str, fs: int = DEFAULT_FS, scale: int = DEFAULT_SCALE, fg: str = DEFAULT_FG) -> Optional[bytes]:
|
| 333 |
+
"""按公式内容渲染取 PNG(端点调用:内容寻址,可安全地按需渲染)。"""
|
| 334 |
+
info = render(latex, fs=fs, scale=scale, fg=fg)
|
| 335 |
+
return info.get("png") if info else None
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
def ensure_batch(items: list, fs: int = DEFAULT_FS, scale: int = DEFAULT_SCALE, fg: str = DEFAULT_FG) -> list:
|
| 339 |
+
"""端上「前端识别 → 请求生成」入口:批量渲染公式,返回可下载清单。
|
| 340 |
+
|
| 341 |
+
items: [{latex, key}](key 由端上自算,用于把结果对回原文里的公式)
|
| 342 |
+
返回: [{key, id, url, w, h}],渲染失败的条目被剔除并由端上降级为纯文本。
|
| 343 |
"""
|
| 344 |
if not is_available():
|
| 345 |
return []
|
| 346 |
out: list[dict] = []
|
| 347 |
+
for it in (items or [])[:_MAX_FORMULAS]:
|
| 348 |
+
if not isinstance(it, dict):
|
| 349 |
+
continue
|
| 350 |
+
latex = str(it.get("latex") or "").strip()
|
| 351 |
+
if not latex or len(latex) > _MAX_LATEX_LEN:
|
| 352 |
+
continue
|
| 353 |
+
info = render(latex, fs=fs, scale=scale, fg=fg)
|
| 354 |
if not info:
|
| 355 |
continue
|
| 356 |
+
tid = formula_id(latex, fs, info["scale"], fg)
|
| 357 |
out.append({
|
| 358 |
+
"key": str(it.get("key") or ""),
|
| 359 |
+
"id": tid,
|
| 360 |
+
# 带 f=:Space 重启后进程内 LRU 丢失,端上仍能按需取到同一张图
|
| 361 |
+
"url": "/v1/xtc/tex/%s.png?f=%s&fs=%d&scale=%d&fg=%s" % (
|
| 362 |
+
tid, _urlquote(latex), int(fs), int(info["scale"]), _urlquote(fg)),
|
| 363 |
"w": info["w"],
|
| 364 |
"h": info["h"],
|
|
|
|
| 365 |
})
|
| 366 |
return out
|
| 367 |
|
| 368 |
|
| 369 |
+
def _urlquote(s: str) -> str:
|
| 370 |
+
from urllib.parse import quote
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
+
return quote(str(s or ""), safe="")
|
|
|
|
|
|
|
|
|