Spaces:
Paused
Paused
Update the CyberForge ML service
Browse files451 unmodified lines45245345445545645745845946046146246346446560 unmodified lines52652752852953053186 unmodified lines618619620621622623451 unmodified linesexcept Exception as error: # the importances are for the explain endpoint; training does not depend on themreport["feature_importances_error"] = f"{type(error).__name__}: {error}"reasons = []if holdout["auc"] is None or holdout["auc"] < GATE["min_auc"]:reasons.append(f"held-out AUC {holdout['auc']} is below {GATE['min_auc']}")if holdout["false_positive_rate"] > GATE["max_false_positive_rate"]:reasons.append(f"held-out false-positive rate {holdout['false_positive_rate']} is above {GATE['max_false_positive_rate']}")if len(report["known_legitimate"]["flagged"]) > GATE["max_known_legitimate_flagged"]:reasons.append(f"{len(report['known_legitimate']['flagged'])} well-known legitimate URL(s) scored 0.5 or more")report["gate"] = {"rules": GATE, "passed": not reasons, "reasons": reasons}return {"model": model, "report": report}60 unmodified linesrng = random.Random(settings["seed"])# Features a run is asked to leave out, to measure whether a model depends# on something it should not. The list is recorded with the model.dropped = [name for name in (options.get("drop_features") or []) if isinstance(name, str)]lexical = [name for name in LEXICAL_FEATURES if name not in dropped]attack = [name for name in ATTACK_FEATURES if name not in dropped]86 unmodified linesreports[name] = reportif report["gate"]["passed"]:accepted[name] = result["model"]uploaded = {}if accepted and not dry_run:451 unmodified lines45245345445545645745845946046146246346446546646746860 unmodified lines52953053153253353453586 unmodified lines622623624625626627628629630631632633634635636637638639640641642643644451 unmodified linesexcept Exception as error: # the importances are for the explain endpoint; training does not depend on themreport["feature_importances_error"] = f"{type(error).__name__}: {error}"reasons, failed = [], []if holdout["auc"] is None or holdout["auc"] < GATE["min_auc"]:reasons.append(f"held-out AUC {holdout['auc']} is below {GATE['min_auc']}")failed.append("min_auc")if holdout["false_positive_rate"] > GATE["max_false_positive_rate"]:reasons.append(f"held-out false-positive rate {holdout['false_positive_rate']} is above {GATE['max_false_positive_rate']}")failed.append("max_false_positive_rate")if len(report["known_legitimate"]["flagged"]) > GATE["max_known_legitimate_flagged"]:reasons.append(f"{len(report['known_legitimate']['flagged'])} well-known legitimate URL(s) scored 0.5 or more")failed.append("max_known_legitimate_flagged")report["gate"] = {"rules": GATE, "passed": not reasons, "reasons": reasons, "failed_rules": failed}return {"model": model, "report": report}60 unmodified linesrng = random.Random(settings["seed"])# Features a run is asked to leave out, to measure whether a model depends# on something it should not. The list is recorded with the model.overrides = options.get("accept_despite_gate") if isinstance(options.get("accept_despite_gate"), dict) else {}dropped = [name for name in (options.get("drop_features") or []) if isinstance(name, str)]lexical = [name for name in LEXICAL_FEATURES if name not in dropped]attack = [name for name in ATTACK_FEATURES if name not in dropped]86 unmodified linesreports[name] = reportif report["gate"]["passed"]:accepted[name] = result["model"]continue# The gate is not loosened for anyone. The owner can still decide to put# a classifier into service when the only rule it fails is the list of# well-known legitimate URLs, the measured rules having passed. The# decision, its reason and what was failed are written into the model's# metadata, where anyone reading the model later will find them.reason = overrides.get(name)if isinstance(reason, str) and len(reason.strip()) >= 20 and report["gate"].get("failed_rules") == ["max_known_legitimate_flagged"]:report["gate"]["accepted_by_owner"] = {"reason": reason.strip()[:600], "decided_at": trained_at,"exceptions": report["known_legitimate"]["flagged"],}accepted[name] = result["model"]elif reason:report["gate"]["override_refused"] = ("an owner decision can only accept a classifier whose measured rules passed and whose only failure is the ""well-known legitimate URLs, and it needs a reason of at least 20 characters")uploaded = {}if accepted and not dry_run:
trainer.py+23-2