feat(multimodal): 实现本地模型管线与请求用量配置

This commit is contained in:
2026-09-04 12:39:43 +08:00
parent e52e909c41
commit 8d092533f6
42 changed files with 2234 additions and 98 deletions
+1
View File
@@ -0,0 +1 @@
"""Optional local inference; importing this package does not load model libraries."""
+31
View File
@@ -0,0 +1,31 @@
"""Reviewed model identities. Runtime never resolves a moving model revision."""
from dataclasses import asdict, dataclass
@dataclass(frozen=True)
class ModelSpec:
key: str
name: str
capability: str
repository: str
revision: str
license: str
source: str = "huggingface"
dimensions: int | None = None
def public(self):
return asdict(self)
CATALOG = {
spec.key: spec for spec in [
ModelSpec("bekko", "Bekko Embedding v1 A8M", "embedding", "hotchpotch/bekko-embedding-v1-a8m",
"c721113d59a1d91b447450324f51c4b3332c924a", "MIT", dimensions=384),
ModelSpec("granite", "Granite Embedding 97M Multilingual r2", "embedding", "ibm-granite/granite-embedding-97m-multilingual-r2",
"835ad14087e140460703cf0fae09f97d469d65c2", "Apache-2.0", dimensions=384),
ModelSpec("qwen3-asr", "Qwen3 ASR 0.6B", "transcription", "Qwen/Qwen3-ASR-0.6B",
"5eb144179a02acc5e5ba31e748d22b0cf3e303b0", "Apache-2.0"),
ModelSpec("eres2netv2", "ERes2NetV2 中文声纹", "speaker_matching", "iic/speech_eres2netv2_sv_zh-cn_16k-common",
"3317286545c587ae682dbc166831d9448780eebb", "Apache-2.0", source="modelscope", dimensions=192),
]
}
+178
View File
@@ -0,0 +1,178 @@
"""Explicit resumable downloads; inference itself never fetches weights."""
from __future__ import annotations
import asyncio
import hashlib
import json
import shutil
from pathlib import Path
from urllib.parse import quote
import httpx
from app.config import get_settings
from app.errors import ApiError
from app.local_models.catalog import CATALOG
_downloads: dict[tuple[str, str], asyncio.Task] = {}
def model_path(key: str) -> Path:
if key not in CATALOG:
raise ApiError(404, "MODEL_NOT_FOUND", "Unknown local model.")
return get_settings().data_dir / "models" / key / CATALOG[key].revision
def state_path(key):
return model_path(key) / "install-state.json"
def read_state(key):
try:
state = json.loads(state_path(key).read_text(encoding="utf-8"))
except (OSError, ValueError):
state = {"status": "not_installed", "downloaded_bytes": 0, "total_bytes": None}
if state["status"] == "downloading" and task_key(key) not in _downloads:
state.update(status="interrupted", error_code="DOWNLOAD_INTERRUPTED")
return state
def write_state(key, state):
path = state_path(key)
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_suffix(".tmp")
temporary.write_text(json.dumps(state), encoding="utf-8")
temporary.replace(path)
def task_key(key):
return str(model_path(key)), key
def describe():
return {"items": [{**spec.public(), **read_state(key)} for key, spec in CATALOG.items()]}
async def download(key):
model_path(key)
if task_key(key) not in _downloads and read_state(key)["status"] != "installed":
write_state(key, {"status": "downloading", "downloaded_bytes": 0, "total_bytes": None})
task = asyncio.create_task(_download(key))
_downloads[task_key(key)] = task
task.add_done_callback(lambda done: _downloads.pop(task_key(key), None))
return read_state(key)
async def cancel_download(key):
task = _downloads.get(task_key(key))
if task:
task.cancel()
await asyncio.gather(task, return_exceptions=True)
state = read_state(key)
if state["status"] == "downloading":
state["status"] = "interrupted"
write_state(key, state)
return state
async def delete(key):
from app.local_models.runtime import runtime
if runtime.in_use(key):
raise ApiError(409, "MODEL_IN_USE", "Model is serving an active request.")
await cancel_download(key)
path = model_path(key).resolve()
root = (get_settings().data_dir / "models").resolve()
if not path.is_relative_to(root) or path == root:
raise ApiError(400, "INVALID_MODEL_PATH", "Model path escapes storage.")
if path.exists():
shutil.rmtree(path)
return read_state(key)
async def _manifest(client, spec):
if spec.source == "huggingface":
response = await client.get(f"https://huggingface.co/api/models/{spec.repository}/revision/{spec.revision}?blobs=true")
response.raise_for_status()
files = []
for item in response.json()["siblings"]:
name = item["rfilename"]
if name.startswith(("onnx/", "openvino/", ".")) or not name.endswith((".json", ".txt", ".safetensors", ".md")):
continue
lfs = item.get("lfs") or {}
files.append({"path": name, "size": item["size"], "hash": lfs.get("sha256") or item["blobId"],
"algorithm": "sha256" if lfs else "git-blob",
"url": f"https://huggingface.co/{spec.repository}/resolve/{spec.revision}/{quote(name)}"})
return files
response = await client.get(f"https://modelscope.cn/api/v1/models/{spec.repository}/repo/files",
params={"Revision": spec.revision, "Recursive": "true"})
response.raise_for_status()
return [{"path": f["Path"], "size": f["Size"], "hash": f["Sha256"], "algorithm": "sha256",
"url": f"https://modelscope.cn/api/v1/models/{spec.repository}/repo?Revision={spec.revision}&FilePath={quote(f['Path'])}"}
for f in response.json()["Data"]["Files"]
if f["Path"] in {"configuration.json", "pretrained_eres2netv2.ckpt", "README.md"}]
def valid_file(path, entry):
if not path.is_file() or path.stat().st_size != entry["size"]:
return False
digest = hashlib.sha256() if entry["algorithm"] == "sha256" else hashlib.sha1()
if entry["algorithm"] == "git-blob":
digest.update(f"blob {entry['size']}\0".encode())
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest() == entry["hash"]
async def _download(key):
spec, root = CATALOG[key], model_path(key).resolve()
state = {"status": "downloading", "downloaded_bytes": 0, "total_bytes": None}
try:
async with httpx.AsyncClient(timeout=60, follow_redirects=True) as client:
manifest = await _manifest(client, spec)
if not manifest or not any(f["path"].endswith((".safetensors", ".ckpt")) for f in manifest):
raise ValueError("Missing weights in model manifest")
state["total_bytes"] = sum(f["size"] for f in manifest)
root.mkdir(parents=True, exist_ok=True)
if shutil.disk_usage(root).free < state["total_bytes"] + 100 * 1024 * 1024:
raise ApiError(507, "MODEL_DISK_FULL", "Insufficient free disk space.")
complete = 0
for entry in manifest:
path = (root / entry["path"]).resolve()
if not path.is_relative_to(root):
raise ValueError("Invalid model manifest path")
path.parent.mkdir(parents=True, exist_ok=True)
if await asyncio.to_thread(valid_file, path, entry):
complete += entry["size"]
continue
partial = path.with_suffix(path.suffix + ".partial")
offset = partial.stat().st_size if partial.exists() else 0
if offset >= entry["size"]:
partial.unlink()
offset = 0
async with client.stream("GET", entry["url"], headers={"Range": f"bytes={offset}-"} if offset else {}) as response:
response.raise_for_status()
if offset and response.status_code != 206:
offset = 0
if response.status_code == 206 and not response.headers.get("content-range", "").startswith(f"bytes {offset}-"):
raise ValueError("Invalid download range")
with partial.open("ab" if offset else "wb") as stream:
async for chunk in response.aiter_bytes(1024 * 1024):
offset += len(chunk)
if offset > entry["size"]:
raise ValueError("Download exceeds manifest size")
stream.write(chunk)
state["downloaded_bytes"] = complete + offset
write_state(key, state)
if not await asyncio.to_thread(valid_file, partial, entry):
partial.unlink(missing_ok=True)
raise ApiError(422, "MODEL_CHECKSUM_FAILED", "Model file checksum did not match.")
partial.replace(path)
complete += entry["size"]
(root / "verified-manifest.json").write_text(json.dumps(manifest), encoding="utf-8")
state.update(status="installed", downloaded_bytes=complete)
except asyncio.CancelledError:
state.update(status="interrupted", error_code="DOWNLOAD_CANCELLED")
except Exception as exc:
state.update(status="failed", error_code=exc.code if isinstance(exc, ApiError) else "MODEL_DOWNLOAD_FAILED")
write_state(key, state)
+198
View File
@@ -0,0 +1,198 @@
"""Bounded, cancellable model subprocesses with CPU as the default device."""
from __future__ import annotations
import asyncio
import json
import os
from contextlib import closing
from contextvars import ContextVar
from pathlib import Path
from typing import Literal
from pydantic import BaseModel, Field
from app.config import BACKEND_DIR
from app.database.db import connect
from app.errors import ApiError
from app.local_models.catalog import CATALOG
from app.local_models.manager import model_path, read_state
from app.providers.base import ProviderError
class RuntimeConfig(BaseModel):
device: Literal["cpu", "cuda"] = "cpu"
cpu_threads: int = Field(default=2, ge=1, le=32)
memory_limit_mb: int = Field(default=8192, ge=1024, le=131072)
gpu_memory_limit_mb: int = Field(default=4096, ge=512, le=65536)
timeout_seconds: int = Field(default=1800, ge=30, le=14400)
embedding_model: Literal["bekko", "granite"] = "bekko"
version: int = Field(default=1, ge=1)
runtime_context = ContextVar("runtime_config", default=None)
runtime_progress = ContextVar("runtime_progress", default=None)
def configuration():
if runtime_context.get() is not None:
return runtime_context.get()
with closing(connect()) as conn:
conn.execute("CREATE TABLE IF NOT EXISTS local_runtime_config (id INTEGER PRIMARY KEY CHECK(id=1), config_json TEXT NOT NULL)")
row = conn.execute("SELECT config_json FROM local_runtime_config WHERE id=1").fetchone()
return RuntimeConfig.model_validate_json(row[0]) if row else RuntimeConfig()
def configure(request):
from app.database.db import transaction
configuration()
with closing(connect()) as conn, transaction(conn):
row = conn.execute("SELECT config_json FROM local_runtime_config WHERE id=1").fetchone()
previous = RuntimeConfig.model_validate_json(row[0]) if row else RuntimeConfig()
if request.version != previous.version:
raise ApiError(409, "VERSION_CONFLICT", "Local runtime settings changed; reload first.")
request = request.model_copy(update={"version": request.version + 1})
conn.execute("INSERT OR REPLACE INTO local_runtime_config VALUES (1,?)", (request.model_dump_json(),))
return request
def interpreter():
return Path(os.getenv("APP_MODEL_PYTHON", str(BACKEND_DIR / ".venv-models" / ("Scripts/python.exe" if os.name == "nt" else "bin/python"))))
class Runtime:
def __init__(self):
self.active = {}
self.active_files = {}
self.waiters = []
self.counter = 0
self.diagnostics = []
def in_use(self, key):
return key in self.active.values()
def media_in_use(self, path):
target = str(Path(path).resolve())
return any(target in paths for paths in self.active_files.values())
async def infer(self, key, operation, payload, *, priority=10):
if read_state(key)["status"] != "installed":
raise ProviderError("LOCAL_MODEL_NOT_INSTALLED", "请先在模型配置中下载本地模型。")
if not interpreter().is_file():
raise ProviderError("LOCAL_RUNTIME_NOT_INSTALLED", "请先运行本地模型 CPU/CUDA 安装脚本。")
config = configuration()
self.counter += 1
ticket = (priority, self.counter)
self.waiters.append(ticket)
process = None
attempt = None
try:
# One resident model at a time prevents overlapping CPU/GPU allocations.
while self.active or ticket != min(self.waiters):
await asyncio.sleep(0.05)
self.waiters.remove(ticket)
self.active[ticket] = key
self.active_files[ticket] = {str(Path(payload[name]).resolve()) for name in ("source", "reference") if payload.get(name)}
# Deletion may have occurred while this request was queued.
if read_state(key)["status"] != "installed":
raise ProviderError("LOCAL_MODEL_NOT_INSTALLED", "模型文件已被删除。")
from app.services.usage_service import UsageAttempt
attempt = UsageAttempt("local-models", CATALOG[key].repository, "local", operation, source="local")
env = {**os.environ, "HF_HUB_OFFLINE": "1", "TRANSFORMERS_OFFLINE": "1",
"HF_HUB_DISABLE_TELEMETRY": "1", "OMP_NUM_THREADS": str(config.cpu_threads),
"PYTHONIOENCODING": "utf-8"}
process = await asyncio.create_subprocess_exec(str(interpreter()), str(Path(__file__).with_name("worker.py")),
stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL,
env=env, limit=16 * 1024 * 1024, **({"creationflags": 0x08000000} if os.name == "nt" else {}))
request = {"key": key, "operation": operation, "model_path": str(model_path(key).resolve()),
"config": config.model_dump(), "payload": payload}
async def receive():
process.stdin.write(json.dumps(request).encode())
await process.stdin.drain()
process.stdin.close()
final = None
while line := await process.stdout.readline():
if len(line) > 16 * 1024 * 1024:
raise ProviderError("LOCAL_MODEL_INVALID_RESPONSE", "本地模型输出超限。")
message = json.loads(line)
if "progress" in message:
callback = runtime_progress.get()
if callback:
callback(message)
else:
final = message
await process.wait()
return final
try:
result = await asyncio.wait_for(receive(), config.timeout_seconds)
except TimeoutError as exc:
raise ProviderError("LOCAL_MODEL_TIMEOUT", "本地模型处理超时。") from exc
if process.returncode != 0:
raise ProviderError("LOCAL_MODEL_PROCESS_FAILED", "本地模型进程退出,请检查依赖与资源预算。")
if not isinstance(result, dict):
raise ProviderError("LOCAL_MODEL_INVALID_RESPONSE", "本地模型进程未返回有效结果。")
if "error_code" in result:
raise ProviderError(result["error_code"], result.get("message", "本地推理失败。"))
attempt.observe(result)
attempt.completed = True
self.diagnostics.append({"model": CATALOG[key].repository, "revision": CATALOG[key].revision,
**result.get("diagnostics", {})})
self.diagnostics = self.diagnostics[-100:]
return result["result"]
finally:
if ticket in self.waiters:
self.waiters.remove(ticket)
if process is not None and process.returncode is None:
process.kill()
await process.wait()
self.active.pop(ticket, None)
self.active_files.pop(ticket, None)
if attempt:
attempt.persist()
runtime = Runtime()
class LocalEmbedding:
dim = 384
@property
def model_id(self):
spec = CATALOG[configuration().embedding_model]
return f"{spec.repository}@{spec.revision}"
@property
def version(self):
return CATALOG[configuration().embedding_model].revision
@property
def available(self):
return read_state(configuration().embedding_model)["status"] == "installed" and interpreter().is_file()
async def embed_documents(self, texts):
return await runtime.infer(configuration().embedding_model, "embedding", {"texts": texts}, priority=0)
async def embed_query(self, query):
return (await self.embed_documents([query]))[0]
class LocalSpeech:
@property
def available(self):
return self.available_for("transcription")
def available_for(self, capability):
key = "qwen3-asr" if capability == "transcription" else "eres2netv2"
return read_state(key)["status"] == "installed" and interpreter().is_file()
async def transcribe(self, source, language):
from app.providers.routing import RoutedTranscript
from app.contracts import TranscriptSegment
result = await runtime.infer("qwen3-asr", "transcription", {"source": str(source.resolve()), "language": language})
return RoutedTranscript(text=result["text"], source="local",
segments=[TranscriptSegment(**s) for s in result["segments"]])
async def match(self, source, reference):
result = await runtime.infer("eres2netv2", "speaker_matching",
{"source": str(source.resolve()), "reference": str(reference.resolve())}, priority=0)
return result["score"]
+175
View File
@@ -0,0 +1,175 @@
"""One offline inference process. Heavy libraries stay out of the API process."""
from __future__ import annotations
import contextlib
import json
import os
import sys
import threading
import time
def decode(path, *, limit_seconds=3600):
import av
import numpy as np
frames = []
samples = 0
with av.open(path, options={"protocol_whitelist": "file,pipe"}) as container:
if not container.streams.audio:
raise ValueError("Media has no audio track")
resampler = av.AudioResampler(format="fltp", layout="mono", rate=16000)
for frame in container.decode(audio=0):
for output in resampler.resample(frame):
audio = output.to_ndarray().reshape(-1)
samples += len(audio)
if samples > limit_seconds * 16000:
raise ValueError("Audio exceeds one hour")
frames.append(audio)
for output in resampler.resample(None):
frames.append(output.to_ndarray().reshape(-1))
if not frames:
raise ValueError("Audio is empty")
audio = np.concatenate(frames).astype(np.float32)
if not np.isfinite(audio).all() or len(audio) < 1600:
raise ValueError("Invalid or too short audio")
return audio
def speech_regions(audio):
"""Energy-based segmentation, not word alignment; retain original sample offsets."""
import numpy as np
window = 480
energies = [float(np.sqrt(np.mean(audio[i:i + window] ** 2))) for i in range(0, len(audio), window)]
threshold = max(0.002, float(np.percentile(energies, 20)) * 2)
active = [i for i, energy in enumerate(energies) if energy >= threshold]
if not active:
return []
regions, start, previous = [], active[0], active[0]
for index in active[1:]:
if index - previous > 20 or (index - start) * window >= 20 * 16000:
regions.append((max(0, start * window - 2400), min(len(audio), (previous + 1) * window + 2400)))
start = index
previous = index
regions.append((max(0, start * window - 2400), min(len(audio), (previous + 1) * window + 2400)))
return regions
def speaker_model(path, device):
import torch
from modelscope.models.audio.sv.ERes2NetV2 import ERes2NetV2
from pathlib import Path
model = ERes2NetV2(baseWidth=26, scale=2, expansion=2, embed_dim=192)
weights = torch.load(Path(path) / "pretrained_eres2netv2.ckpt", map_location="cpu", weights_only=True)
model.load_state_dict(weights, strict=True)
return model.to(device).eval()
def voice_embedding(model, audio, device):
import torch
import torchaudio.compliance.kaldi as kaldi
if len(audio) < 16000:
raise ValueError("Speaker comparison needs at least one second of audio")
features = kaldi.fbank(torch.from_numpy(audio).unsqueeze(0), num_mel_bins=80, sample_frequency=16000)
features -= features.mean(dim=0, keepdim=True)
with torch.inference_mode():
vector = model(features.unsqueeze(0).to(device)).flatten()
return torch.nn.functional.normalize(vector, dim=0)
def run(request):
import torch
import psutil
config, payload = request["config"], request["payload"]
torch.set_num_threads(config["cpu_threads"])
requested = config["device"]
device = "cuda:0" if requested == "cuda" and torch.cuda.is_available() else "cpu"
if device != "cpu":
total = torch.cuda.get_device_properties(0).total_memory
torch.cuda.set_per_process_memory_fraction(min(1.0, config["gpu_memory_limit_mb"] * 1024 ** 2 / total))
process = psutil.Process()
peak = [0]
stop = threading.Event()
def monitor():
while not stop.wait(0.2):
used = process.memory_info().rss
peak[0] = max(peak[0], used)
if used > config["memory_limit_mb"] * 1024 ** 2:
os._exit(75)
threading.Thread(target=monitor, daemon=True).start()
started = time.monotonic()
path, operation = request["model_path"], request["operation"]
try:
usage = {}
if operation == "embedding":
from sentence_transformers import SentenceTransformer
model = SentenceTransformer(path, device=device, local_files_only=True, trust_remote_code=False,
model_kwargs={"attn_implementation": "sdpa"})
loaded = time.monotonic()
result = model.encode(payload["texts"], batch_size=4, normalize_embeddings=True, show_progress_bar=False).tolist()
# Count the tokenizer's actual encoded input, not characters or words.
usage = {"input_tokens": int(model.tokenize(payload["texts"])["attention_mask"].sum())}
elif operation == "transcription":
from qwen_asr import Qwen3ASRModel
model = Qwen3ASRModel.from_pretrained(path, dtype=torch.float32 if device == "cpu" else torch.float16,
device_map=device, attn_implementation="sdpa", max_inference_batch_size=1, max_new_tokens=512)
loaded = time.monotonic()
audio = decode(payload["source"])
regions = speech_regions(audio)
language = {"zh": "Chinese", "en": "English", "ja": "Japanese", "yue": "Cantonese"}.get(payload.get("language"), payload.get("language"))
segments = []
for start, end in regions:
output = model.transcribe(audio=(audio[start:end], 16000), language=language)[0]
if output.text.strip():
segments.append({"segment_id": f"segment_{len(segments) + 1}", "start_time": start / 16000,
"end_time": end / 16000, "text": output.text, "language": output.language})
sys.__stdout__.write(json.dumps({"progress": end / len(audio), "segment": segments[-1]}, ensure_ascii=False) + "\n")
sys.__stdout__.flush()
result = {"text": "\n".join(s["text"] for s in segments), "segments": segments}
elif operation == "speaker_matching":
model = speaker_model(path, device)
loaded = time.monotonic()
first = voice_embedding(model, decode(payload["source"]), device)
second = voice_embedding(model, decode(payload["reference"]), device)
# Similarity, not a calibrated identity probability.
result = {"score": max(0.0, min(1.0, float(torch.dot(first, second))))}
elif operation == "diarization":
model = speaker_model(path, device)
loaded = time.monotonic()
audio = decode(payload["source"])
centroids, speakers = [], []
for segment in payload["segments"]:
sample = audio[int(segment["start_time"] * 16000):int(segment["end_time"] * 16000)]
if len(sample) < 16000:
speakers.append(None)
continue
vector = voice_embedding(model, sample, device)
similarities = [float(torch.dot(vector, c)) for c in centroids]
best = max(range(len(similarities)), key=similarities.__getitem__) if similarities else None
if best is None or similarities[best] < 0.36:
best = len(centroids)
centroids.append(vector)
speakers.append(f"speaker_{best + 1}")
result = {"speakers": speakers}
else:
raise ValueError("Unknown inference operation")
return {"result": result, "usage": usage, "diagnostics": {"requested_device": requested, "actual_device": device,
"fallback_reason": "CUDA_UNAVAILABLE" if requested == "cuda" and device == "cpu" else None,
"load_seconds": loaded - started, "inference_seconds": time.monotonic() - loaded,
"peak_memory_bytes": max(peak[0], process.memory_info().rss), "operation": operation}}
finally:
stop.set()
if __name__ == "__main__":
request = json.loads(sys.stdin.buffer.read())
# Third-party progress/logging must never corrupt the protocol or leak into API errors.
with contextlib.redirect_stdout(sys.stderr):
try:
response = run(request)
except (ImportError, ModuleNotFoundError):
response = {"error_code": "LOCAL_RUNTIME_DEPENDENCY_MISSING", "message": "本地模型运行依赖不完整,请重新运行安装脚本。"}
except Exception:
response = {"error_code": "LOCAL_INFERENCE_FAILED", "message": "本地推理失败,请检查媒体格式、模型和设备配置。"}
sys.stdout.buffer.write((json.dumps(response, ensure_ascii=False, allow_nan=False) + "\n").encode("utf-8"))