feat: 新增AI换脸与AI海报生成两个授权应用——换脸(insightface buffalo_l+inswapper_128本机CPU推理, POST /ai/face-swap, consent必填, 多脸取最大); 海报(25套模板9分类, 改文字/换图片/AI生成配图与背景/图层增删排序/背景预设, 按宽高比匹配上游尺寸档位, 导出1080x1440 PNG); license.ts与relay PROTECTED_TOOLS双端注册使pricing/buy自动+2; relay副本同步(Dockerfile装insightface+COPY face-models不入库)

This commit is contained in:
jinghui committed 2026-11-13 19:35:03 +08:00
1 parent afe73922cc
commit 67c320a2de
14 files changed
+1552 -8

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
.env
data/
__pycache__/
*.pyc
hf-cache/
piper-models/
ai-models/
waifu2x/
face-models/
*.log
.venv-hf/
+3
View File
@@ -7,7 +7,10 @@ RUN apt-get update \
# models are pre-downloaded on the host (build env has no internet)
RUN pip install --no-cache-dir piper-tts faster-whisper
RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple "rembg[cpu]"
# AI 换脸: insightface(纯Python wheel 免编译) + buffalo_l人脸检测 + inswapper_128换脸模型(宿主已预置)
RUN pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple insightface
COPY hf-cache /root/.cache/huggingface
COPY face-models /app/face-models
COPY piper-models /app/piper-models
# AI 图片: u2net 抠图模型(预下载) + waifu2x-ncnn-vulkan 无损放大(CPU)
COPY ai-models/u2net.onnx /root/.u2net/u2net.onnx
+204 -7
View File
@@ -68,7 +68,7 @@ CF_DAILY_LIMIT = int(os.environ.get("CF_DAILY_LIMIT", "173"))
CF_DAILY_FILE = os.environ.get("CF_DAILY_FILE", "/app/data/daily_gen_cf.json")
DAY_MS = 24 * 60 * 60 * 1000
TZ_OFFSET_MS = 8 * 60 * 60 * 1000 # 北京时间 UTC+8 —— 重置对齐自然日 0 点
PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "text-to-image", "ai-image-tools", "copywriting", "screenshot-qa", "video-subtitle", "ai-gen", "ai-text-edit"}
PROTECTED_TOOLS = {"translator", "image-ocr", "pdf-ocr", "ai-retouch", "doc-convert", "image-tools", "pdf-tools", "video-tools", "audio-tools", "text-to-image", "ai-image-tools", "copywriting", "screenshot-qa", "video-subtitle", "ai-gen", "ai-text-edit", "face-swap", "poster-maker"}
MACHINE_RE = re.compile(r"^\d{10,16}$")
# verify() messages that unconditionally block activation
@@ -711,6 +711,51 @@ def check(machine: str, tool: str) -> int:
return 200 if trial_allowed(machine, tool) else 403
# ---- AI 换脸引擎:insightface buffalo_l 人脸检测 + inswapper_128 换脸(本机 CPU 推理) ----
_FACE_LOCK = threading.Lock()
_FACE_APP = None
_FACE_SWAP = None
_FACE_MODELS_DIR = os.environ.get("FACE_MODELS_DIR", "/app/face-models")
def _face_engine():
"""懒加载换脸引擎(线程安全;首次调用约 1~2s,之后常驻内存复用)。"""
global _FACE_APP, _FACE_SWAP
if _FACE_APP is None:
from insightface.app import FaceAnalysis
from insightface import model_zoo
app = FaceAnalysis(name="buffalo_l", root=_FACE_MODELS_DIR, providers=["CPUExecutionProvider"])
app.prepare(ctx_id=0, det_size=(640, 640))
swap = model_zoo.get_model(os.path.join(_FACE_MODELS_DIR, "inswapper_128.onnx"),
providers=["CPUExecutionProvider"])
_FACE_APP, _FACE_SWAP = app, swap
return _FACE_APP, _FACE_SWAP
def _largest_face(faces):
"""多张脸时取面积最大的一张(换脸默认作用于最显眼的人脸)。"""
return max(faces, key=lambda f: float((f.bbox[2] - f.bbox[0]) * (f.bbox[3] - f.bbox[1])))
def _face_swap_run(src_img, dst_img):
"""源人脸 -> 目标照片。返回 (jpg字节|None, 错误信息, 耗时ms)。加锁串行避免并发吃满 CPU。"""
import cv2 as _cv2
t0 = time.time()
with _FACE_LOCK:
app, swap = _face_engine()
src_faces = app.get(src_img)
dst_faces = app.get(dst_img)
if not src_faces:
return None, "源图中未检测到人脸,请换一张清晰的正脸照片", 0
if not dst_faces:
return None, "目标图中未检测到人脸,请换一张含人物的照片", 0
out = swap.get(dst_img, _largest_face(dst_faces), _largest_face(src_faces), paste_back=True)
ok, buf = _cv2.imencode(".jpg", out, [int(_cv2.IMWRITE_JPEG_QUALITY), 92])
if not ok:
return None, "结果编码失败", 0
return buf.tobytes(), None, int((time.time() - t0) * 1000)
def _account_license_ok(machine: str) -> bool:
"""True when this device logged into an authorized account.
@@ -3124,6 +3169,9 @@ class Handler(BaseHTTPRequestHandler):
if parsed.path == "/ai/edit-image":
self._handle_edit_image()
return
if parsed.path == "/ai/face-swap":
self._handle_face_swap()
return
if parsed.path == "/ai/seamless-edit":
self._handle_seamless_edit()
return
@@ -3204,6 +3252,51 @@ class Handler(BaseHTTPRequestHandler):
self._reply(404, b"not found")
# ---------- 中文提示词自动转英文(Flux/SDXL 系对英文理解最好) ----------
_ZH_RE = re.compile(r"[\u4e00-\u9fff]")
_trans_cache: dict = {}
def _translate_prompt(self, prompt: str):
"""含中文才翻;成功 (英文, True);失败/未配置回退 (原文, False),绝不阻断生图。"""
if not prompt or not self._ZH_RE.search(prompt):
return prompt, False
cached = self._trans_cache.get(prompt)
if cached:
return cached, True
key = os.environ.get("NEWAPI_KEY", "")
if not key:
return prompt, False
try:
body = json.dumps({
"model": os.environ.get("IMG_PROMPT_MODEL", "mimo-v2.6-flash"),
"messages": [
{"role": "system", "content": (
"You are an expert prompt engineer for text-to-image models (Flux / SDXL / DALL-E). "
"Translate the user's Chinese image prompt into ONE high-quality English image-generation prompt. "
"Preserve every style, composition, aspect-ratio, lighting and detail keyword; you may expand "
"slightly for natural flow. Output ONLY the English prompt itself - no quotes, no explanations, "
"no Chinese characters, no markdown.")},
{"role": "user", "content": prompt},
],
"temperature": 0.3, "max_tokens": 500,
}).encode()
req = request.Request(
os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions"),
data=body, headers={"Content-Type": "application/json", "Authorization": "Bearer " + key})
data = json.loads(request.urlopen(req, timeout=30).read())
en = str(data.get("choices", [{}])[0].get("message", {}).get("content") or "").strip()
en = en.strip("`\"'\u201c\u201d ").strip()
if not en or self._ZH_RE.search(en[:120]):
return prompt, False
if len(self._trans_cache) >= 300:
self._trans_cache.clear()
self._trans_cache[prompt] = en
print(f"[img] prompt translated: {prompt[:40]} -> {en[:70]}", flush=True)
return en, True
except Exception as e: # noqa: BLE001
print(f"[img] translate failed, use original: {e}", flush=True)
return prompt, False
# ---------- AI 生图 (NEWAPI) ----------
def _nm_base(self):
return os.environ.get("NEWAPI_URL", "http://192.168.68.19:3099/v1/chat/completions").replace("/chat/completions", "")
@@ -3256,7 +3349,7 @@ class Handler(BaseHTTPRequestHandler):
b64 = item.get("b64_json") or ""
if not url and not b64:
raise RuntimeError("上游未返回图片")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"url": url, "b64": b64, "img_url": (d.get("img_url") or "") if isinstance(d, dict) else ""}}, ensure_ascii=False).encode("utf-8"), "application/json")
self._reply(200, json.dumps({"code": 200, "message": "ok", "data": {"url": url, "b64": b64, "img_url": (d.get("img_url") or "") if isinstance(d, dict) else "", "prompt_en": (d.get("prompt_en") or "") if isinstance(d, dict) else ""}}, ensure_ascii=False).encode("utf-8"), "application/json")
def _handle_image_models(self):
try:
@@ -3302,7 +3395,7 @@ class Handler(BaseHTTPRequestHandler):
msg = str(err or d.get("message") or "")
raise RuntimeError(msg[:300] or "上游未返回图片")
def _persist_gen_image(self, machine, d, model, prompt, w, h):
def _persist_gen_image(self, machine, d, model, prompt, w, h, kind="gen"):
"""把生成结果落盘并写入该机器的历史索引;返回站内图片 URL(任何失败返回空串)。"""
try:
if not self._gen_machine_ok(machine):
@@ -3321,6 +3414,14 @@ class Handler(BaseHTTPRequestHandler):
raw = rr.read()
if not raw or len(raw) < 200:
return ""
if not (w and h):
try:
from PIL import Image as _PIL
import io as _io
_im = _PIL.open(_io.BytesIO(raw))
w, h = _im.size
except Exception:
w, h = 0, 0
if raw[:8] == b"\x89PNG\r\n\x1a\n":
ext = ".png"
elif raw[:2] == b"\xff\xd8":
@@ -3349,6 +3450,7 @@ class Handler(BaseHTTPRequestHandler):
idx = []
idx.insert(0, {"id": iid, "file": fname, "at": int(time.time()),
"model": str(model)[:60], "prompt": str(prompt)[:500],
"kind": str(kind or "gen")[:20],
"w": w, "h": h})
while len(idx) > 100:
old = idx.pop() or {}
@@ -3525,20 +3627,24 @@ class Handler(BaseHTTPRequestHandler):
w, h = 1024, 1024
w = max(16, min(2048, w))
h = max(16, min(2048, h))
# 中文提示词自动转英文再发上游(Flux/SDXL 英文效果最好;翻译失败回退原文)
gen_prompt, translated = self._translate_prompt(prompt)
try:
if own_provider:
d = self._provider_images_gen(provider, model, prompt, f"{w}x{h}")
d = self._provider_images_gen(provider, model, gen_prompt, f"{w}x{h}")
else:
d = self._nm_images_gen(model, prompt, f"{w}x{h}")
d = self._nm_images_gen(model, gen_prompt, f"{w}x{h}")
daily_consume(machine, model) # 内置渠道成功才扣次(机器计数 + CF 共享池)
except Exception as e: # noqa: BLE001
print(f"[img] gen failed: {e}", flush=True)
self._reply(502, json.dumps({"code": 502, "message": f"生图失败:{e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
# 落盘保存到该机器的历史(失败不阻断响应)
img_url = self._persist_gen_image(machine, d, model, prompt, w, h)
img_url = self._persist_gen_image(machine, d, model, prompt, w, h) # 历史存用户原文
if isinstance(d, dict) and img_url:
d["img_url"] = img_url
if translated and isinstance(d, dict):
d["prompt_en"] = gen_prompt # 实际发给上游的英文提示词(供前端提示)
self._img_reply_ok(d)
def _handle_edit_image(self):
@@ -3565,8 +3671,11 @@ class Handler(BaseHTTPRequestHandler):
return
try:
img = up.file.read()
d = self._nm_images_edit(model, prompt, img, up.filename)
gen_prompt, translated = self._translate_prompt(prompt)
d = self._nm_images_edit(model, gen_prompt, img, up.filename)
daily_consume(machine, model) # 内置渠道成功才扣次(机器计数 + CF 共享池)
if translated and isinstance(d, dict):
d["prompt_en"] = gen_prompt
self._img_reply_ok(d)
except Exception as e: # noqa: BLE001
print(f"[img] edit failed: {e}", flush=True)
@@ -3820,6 +3929,94 @@ class Handler(BaseHTTPRequestHandler):
canvas.paste(im2, ((tw - im2.width) // 2, (th - im2.height) // 2))
return canvas
def _handle_face_swap(self):
"""AI换脸(授权功能):源人脸照片 -> 目标照片,本机 CPU 推理,结果走 /ai/img 站内图链路。"""
machine = self.headers.get("X-Machine-Id", "")
code = check(machine, "face-swap")
if code != 200:
self._reply(403, json.dumps({"code": 403, "message": "AI换脸是授权功能:试用期内可免费体验,正式使用请先激活卡密"},
ensure_ascii=False).encode("utf-8"), "application/json")
return
log_usage(machine, "face-swap")
ctype = self.headers.get("Content-Type") or ""
if "multipart/form-data" not in ctype:
self._reply(400, json.dumps({"code": 400, "message": "需要 multipart/form-data"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,
environ={"REQUEST_METHOD": "POST", "CONTENT_TYPE": ctype})
except Exception as e: # noqa: BLE001
self._reply(400, json.dumps({"code": 400, "message": f"表单解析失败: {e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if str(form.getvalue("consent") or "") != "1":
self._reply(400, json.dumps({"code": 400, "message": "请先勾选「我承诺仅使用已获本人授权的照片进行换脸」"},
ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
src_raw = form["source"].file.read() if "source" in form else b""
dst_raw = form["target"].file.read() if "target" in form else b""
except Exception:
src_raw, dst_raw = b"", b""
if not src_raw or not dst_raw:
self._reply(400, json.dumps({"code": 400, "message": "请同时上传源人脸照片与目标照片"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if len(src_raw) > 15 * 1024 * 1024 or len(dst_raw) > 15 * 1024 * 1024:
self._reply(400, json.dumps({"code": 400, "message": "单张图片不能超过 15MB"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
import cv2 as _cv2
import numpy as _np
src_img = _cv2.imdecode(_np.frombuffer(src_raw, _np.uint8), _cv2.IMREAD_COLOR)
dst_img = _cv2.imdecode(_np.frombuffer(dst_raw, _np.uint8), _cv2.IMREAD_COLOR)
if src_img is None or dst_img is None:
self._reply(400, json.dumps({"code": 400, "message": "图片解码失败,请上传 JPG/PNG 等常见格式"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
out_jpg, msg, ms = _face_swap_run(src_img, dst_img)
except Exception as e: # noqa: BLE001
print(f"[face-swap] failed: {e}", flush=True)
self._reply(500, json.dumps({"code": 500, "message": f"换脸失败: {e}"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
if out_jpg is None:
self._reply(400, json.dumps({"code": 400, "message": msg}, ensure_ascii=False).encode("utf-8"), "application/json")
return
try:
gen_dir = os.path.join(DATA_DIR, "gen_images", machine)
os.makedirs(gen_dir, exist_ok=True)
iid = f"{int(time.time())}_{uuid.uuid4().hex[:8]}"
with open(os.path.join(gen_dir, iid + ".jpg"), "wb") as f:
f.write(out_jpg)
idx_path = os.path.join(gen_dir, "index.json")
with GEN_IMG_LOCK:
idx = []
if os.path.exists(idx_path):
try:
idx = json.load(open(idx_path, encoding="utf-8"))
if not isinstance(idx, list):
idx = []
except Exception:
idx = []
idx.insert(0, {"id": iid, "file": iid + ".jpg", "at": int(time.time()),
"model": "AI换脸", "prompt": "face-swap",
"w": int(dst_img.shape[1]), "h": int(dst_img.shape[0])})
while len(idx) > 100:
old = idx.pop() or {}
fn = str(old.get("file") or "")
if fn and "/" not in fn and "\\" not in fn:
try:
os.remove(os.path.join(gen_dir, fn))
except Exception:
pass
with open(idx_path, "w", encoding="utf-8") as f:
json.dump(idx, f, ensure_ascii=False)
except Exception as e: # noqa: BLE001
print(f"[face-swap] persist failed: {e}", flush=True)
self._reply(500, json.dumps({"code": 500, "message": "结果保存失败"}, ensure_ascii=False).encode("utf-8"), "application/json")
return
body = json.dumps({"code": 200, "message": "ok",
"data": {"url": f"/relay/ai/img/{machine}/{iid}", "elapsed_ms": ms}},
ensure_ascii=False).encode("utf-8")
self._reply(200, body, "application/json")
def _handle_seamless_edit(self):
machine = self.headers.get("X-Machine-Id", "")
if check(machine, "ai-text-edit") != 200: