34 lines
866 B
Python
34 lines
866 B
Python
"""
|
|
ONNX 模型 sidecar metadata 辅助工具。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
|
|
def model_metadata_path(model_path: str | Path) -> Path:
|
|
return Path(model_path).with_suffix(".meta.json")
|
|
|
|
|
|
def write_model_metadata(model_path: str | Path, metadata: dict) -> Path:
|
|
path = model_metadata_path(model_path)
|
|
payload = {
|
|
"version": 1,
|
|
**metadata,
|
|
}
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
with path.open("w", encoding="utf-8") as f:
|
|
json.dump(payload, f, ensure_ascii=True, indent=2, sort_keys=True)
|
|
f.write("\n")
|
|
return path
|
|
|
|
|
|
def load_model_metadata(model_path: str | Path) -> dict | None:
|
|
path = model_metadata_path(model_path)
|
|
if not path.exists():
|
|
return None
|
|
with path.open("r", encoding="utf-8") as f:
|
|
return json.load(f)
|