forked from longtao.wu/droboticflow
193 lines
7.8 KiB
Python
193 lines
7.8 KiB
Python
from __future__ import annotations
|
||
|
||
import hashlib
|
||
import json
|
||
import math
|
||
import pathlib
|
||
from typing import Any, Callable
|
||
|
||
|
||
HelperMap = dict[str, Callable[..., Any]]
|
||
|
||
|
||
def model_input_items(inputs: list[dict[str, Any]], params: dict[str, str], helpers: HelperMap) -> list[Any]:
|
||
payloads = helpers["payloads_from_inputs"](inputs)
|
||
inline_items = helpers["parse_json_param"](params, "INPUT_JSON", None)
|
||
if inline_items is not None:
|
||
if isinstance(inline_items, list):
|
||
return inline_items
|
||
if isinstance(inline_items, dict):
|
||
if isinstance(inline_items.get("items"), list):
|
||
return inline_items["items"]
|
||
return [inline_items]
|
||
return [{"value": inline_items}]
|
||
if not payloads:
|
||
raise RuntimeError("模型推理需要上游输入或 INPUT_JSON")
|
||
return helpers["collect_items"](payloads)
|
||
|
||
|
||
def canonical_model_bytes(model: dict[str, Any]) -> bytes:
|
||
return json.dumps(model, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
||
|
||
|
||
def model_digest(model: dict[str, Any]) -> str:
|
||
return hashlib.sha256(canonical_model_bytes(model)).hexdigest()
|
||
|
||
|
||
def load_inference_model(params: dict[str, str], helpers: HelperMap) -> tuple[dict[str, Any], str]:
|
||
inline_model = helpers["parse_json_param"](params, "MODEL_JSON", None, dict)
|
||
if isinstance(inline_model, dict):
|
||
return inline_model, "MODEL_JSON"
|
||
model_path = params.get("MODEL_PATH", "").strip()
|
||
if model_path:
|
||
payload = helpers["load_json_file"](model_path, None)
|
||
if isinstance(payload, dict):
|
||
return payload, f"MODEL_PATH:{pathlib.Path(model_path)}"
|
||
raise RuntimeError("MODEL_PATH must point to a JSON object model")
|
||
raise RuntimeError("模型推理需要 MODEL_JSON 或 MODEL_PATH;自定义命令请使用 Shell 命令算子")
|
||
|
||
|
||
def numeric_feature_value(item: Any, feature: str, helpers: HelperMap) -> float:
|
||
value = helpers["get_field_value"](item, feature) if isinstance(item, dict) else None
|
||
if value is None and feature == "value":
|
||
value = item
|
||
try:
|
||
parsed = float(value)
|
||
except Exception as exc:
|
||
raise RuntimeError(f"feature {feature} must be numeric") from exc
|
||
if not math.isfinite(parsed):
|
||
raise RuntimeError(f"feature {feature} must be finite")
|
||
return parsed
|
||
|
||
|
||
def model_features(item: Any, features: list[str], helpers: HelperMap) -> list[float]:
|
||
return [numeric_feature_value(item, feature, helpers) for feature in features]
|
||
|
||
|
||
def compare_model_value(left: Any, operator: str, right: Any) -> bool:
|
||
if operator in {"exists", "missing"}:
|
||
exists = left is not None
|
||
return exists if operator == "exists" else not exists
|
||
if operator in {"contains", "in"}:
|
||
if operator == "contains":
|
||
return str(right) in str(left or "")
|
||
if isinstance(right, list):
|
||
return left in right
|
||
return False
|
||
try:
|
||
left_value = float(left)
|
||
right_value = float(right)
|
||
except Exception:
|
||
left_value = left
|
||
right_value = right
|
||
if operator == "eq":
|
||
return left_value == right_value
|
||
if operator == "ne":
|
||
return left_value != right_value
|
||
if operator in {"gt", "gte", "lt", "lte"} and not isinstance(left_value, (int, float)):
|
||
raise RuntimeError(f"model rule operator {operator} requires numeric values")
|
||
if operator == "gt":
|
||
return left_value > right_value
|
||
if operator == "gte":
|
||
return left_value >= right_value
|
||
if operator == "lt":
|
||
return left_value < right_value
|
||
if operator == "lte":
|
||
return left_value <= right_value
|
||
raise RuntimeError(f"unsupported model rule operator {operator}")
|
||
|
||
|
||
def predict_with_model(item: Any, model: dict[str, Any], helpers: HelperMap) -> dict[str, Any]:
|
||
model_type = str(model.get("type", "")).strip().lower()
|
||
if model_type == "constant":
|
||
raise RuntimeError("constant model type is not accepted because it hardcodes inference output")
|
||
|
||
if model_type == "rule":
|
||
rules = model.get("rules")
|
||
if not isinstance(rules, list) or not rules:
|
||
raise RuntimeError("rule model requires non-empty rules array")
|
||
for rule in rules:
|
||
if not isinstance(rule, dict):
|
||
continue
|
||
field = str(rule.get("field", "")).strip()
|
||
operator = str(rule.get("operator", "eq")).strip().lower()
|
||
value = helpers["get_field_value"](item, field) if field else item
|
||
if compare_model_value(value, operator, rule.get("value")):
|
||
return {"label": rule.get("label", True), "score": rule.get("score", 1.0), "matchedRule": rule.get("id", field)}
|
||
return {"label": model.get("defaultLabel", False), "score": model.get("defaultScore", 0.0)}
|
||
|
||
features = model.get("features")
|
||
if not isinstance(features, list) or not all(isinstance(feature, str) and feature.strip() for feature in features):
|
||
raise RuntimeError(f"{model_type or 'model'} model requires features array")
|
||
vector = model_features(item, [feature.strip() for feature in features], helpers)
|
||
|
||
if model_type == "linear":
|
||
weights = model.get("weights")
|
||
if not isinstance(weights, list) or len(weights) != len(vector):
|
||
raise RuntimeError("linear model weights length must match features length")
|
||
bias = float(model.get("bias", 0.0) or 0.0)
|
||
score = bias + sum(float(weight) * value for weight, value in zip(weights, vector))
|
||
threshold = float(model.get("threshold", 0.0) or 0.0)
|
||
return {
|
||
"label": model.get("positiveLabel", True) if score >= threshold else model.get("negativeLabel", False),
|
||
"score": score,
|
||
"threshold": threshold,
|
||
}
|
||
|
||
if model_type == "nearest_centroid":
|
||
labels = model.get("labels")
|
||
centroids = model.get("centroids")
|
||
if not isinstance(labels, list) or not isinstance(centroids, list) or len(labels) != len(centroids) or not labels:
|
||
raise RuntimeError("nearest_centroid model requires matching labels and centroids arrays")
|
||
best_label = None
|
||
best_distance = math.inf
|
||
for label, centroid in zip(labels, centroids):
|
||
if not isinstance(centroid, list) or len(centroid) != len(vector):
|
||
raise RuntimeError("nearest_centroid centroid length must match features length")
|
||
distance = math.sqrt(sum((value - float(center)) ** 2 for value, center in zip(vector, centroid)))
|
||
if distance < best_distance:
|
||
best_distance = distance
|
||
best_label = label
|
||
return {"label": best_label, "distance": best_distance, "score": 1.0 / (1.0 + best_distance)}
|
||
|
||
raise RuntimeError("MODEL_JSON type must be rule, linear, or nearest_centroid")
|
||
|
||
|
||
def run(
|
||
run_id: str,
|
||
node_id: str,
|
||
output_dir: Any,
|
||
node_data: dict[str, Any],
|
||
inputs: list[dict[str, Any]],
|
||
helpers: HelperMap,
|
||
) -> list[dict[str, Any]]:
|
||
params = helpers["hyperparams"](node_data)
|
||
model, model_source = load_inference_model(params, helpers)
|
||
items = model_input_items(inputs, params, helpers)
|
||
predictions = [
|
||
{
|
||
"index": index,
|
||
"input": helpers["json_compatible"](item),
|
||
"prediction": predict_with_model(item, model, helpers),
|
||
}
|
||
for index, item in enumerate(items)
|
||
]
|
||
payload = {
|
||
"modelType": str(model.get("type", "")).strip(),
|
||
"modelName": str(model.get("name", "")).strip(),
|
||
"modelSource": model_source,
|
||
"modelSha256": model_digest(model),
|
||
"inputCount": len(items),
|
||
"predictions": predictions,
|
||
}
|
||
return [
|
||
helpers["write_json_artifact"](
|
||
run_id,
|
||
node_id,
|
||
output_dir,
|
||
"inference-result.json",
|
||
payload,
|
||
output=helpers["output_binding"](node_data),
|
||
)
|
||
]
|