Add governance risk inference and tests
Browse files- README.md +33 -0
- inference.py +87 -0
- tests/test_inference.py +37 -0
README.md
CHANGED
|
@@ -155,3 +155,36 @@ Website: https://govbase.dev
|
|
| 155 |
## License
|
| 156 |
|
| 157 |
Apache-2.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
## License
|
| 156 |
|
| 157 |
Apache-2.0
|
| 158 |
+
|
| 159 |
+
## Inference
|
| 160 |
+
|
| 161 |
+
The repository includes `inference.py` for local predictions.
|
| 162 |
+
|
| 163 |
+
Example:
|
| 164 |
+
|
| 165 |
+
```bash
|
| 166 |
+
python inference.py --json '{
|
| 167 |
+
"sector": "financial_services",
|
| 168 |
+
"impact": "high",
|
| 169 |
+
"decision_autonomy": "automated",
|
| 170 |
+
"human_oversight": "none",
|
| 171 |
+
"monitoring": "none",
|
| 172 |
+
"traceability": "none",
|
| 173 |
+
"technical_documentation": "partial"
|
| 174 |
+
}'
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
Example output:
|
| 178 |
+
|
| 179 |
+
```json
|
| 180 |
+
{
|
| 181 |
+
"risk_tier": "unacceptable",
|
| 182 |
+
"class_probabilities": {
|
| 183 |
+
"high": 0.24612,
|
| 184 |
+
"lower": 0.08766,
|
| 185 |
+
"unacceptable": 0.66622
|
| 186 |
+
}
|
| 187 |
+
}
|
| 188 |
+
```
|
| 189 |
+
|
| 190 |
+
The returned class probabilities are model scores from the logistic-regression baseline. They should not be interpreted as calibrated probabilities of legal or real-world governance risk.
|
inference.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
import joblib
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
MODEL_PATH = Path(__file__).parent / "model.joblib"
|
| 10 |
+
|
| 11 |
+
FEATURES = [
|
| 12 |
+
"sector",
|
| 13 |
+
"impact",
|
| 14 |
+
"decision_autonomy",
|
| 15 |
+
"human_oversight",
|
| 16 |
+
"monitoring",
|
| 17 |
+
"traceability",
|
| 18 |
+
"technical_documentation",
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def load_model():
|
| 23 |
+
if not MODEL_PATH.exists():
|
| 24 |
+
raise FileNotFoundError(
|
| 25 |
+
f"Model artifact not found: {MODEL_PATH}"
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
return joblib.load(MODEL_PATH)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def validate_input(data: dict) -> None:
|
| 32 |
+
missing = [feature for feature in FEATURES if feature not in data]
|
| 33 |
+
|
| 34 |
+
if missing:
|
| 35 |
+
raise ValueError(
|
| 36 |
+
"Missing required features: " + ", ".join(missing)
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def predict_governance_risk(data: dict) -> dict:
|
| 41 |
+
validate_input(data)
|
| 42 |
+
|
| 43 |
+
model = load_model()
|
| 44 |
+
|
| 45 |
+
frame = pd.DataFrame(
|
| 46 |
+
[{feature: data[feature] for feature in FEATURES}]
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
prediction = model.predict(frame)[0]
|
| 50 |
+
|
| 51 |
+
result = {
|
| 52 |
+
"risk_tier": str(prediction),
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
if hasattr(model, "predict_proba"):
|
| 56 |
+
probabilities = model.predict_proba(frame)[0]
|
| 57 |
+
classes = model.classes_
|
| 58 |
+
|
| 59 |
+
result["class_probabilities"] = {
|
| 60 |
+
str(label): round(float(probability), 6)
|
| 61 |
+
for label, probability in zip(classes, probabilities)
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
return result
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def main():
|
| 68 |
+
parser = argparse.ArgumentParser(
|
| 69 |
+
description="Run the AIGov governance risk classifier."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
parser.add_argument(
|
| 73 |
+
"--json",
|
| 74 |
+
required=True,
|
| 75 |
+
help="Governance scenario encoded as JSON.",
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
args = parser.parse_args()
|
| 79 |
+
|
| 80 |
+
data = json.loads(args.json)
|
| 81 |
+
result = predict_governance_risk(data)
|
| 82 |
+
|
| 83 |
+
print(json.dumps(result, indent=2))
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
main()
|
tests/test_inference.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
ROOT = Path(__file__).resolve().parents[1]
|
| 5 |
+
sys.path.insert(0, str(ROOT))
|
| 6 |
+
|
| 7 |
+
from inference import predict_governance_risk
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_high_risk_scenario():
|
| 11 |
+
result = predict_governance_risk({
|
| 12 |
+
"sector": "financial_services",
|
| 13 |
+
"impact": "high",
|
| 14 |
+
"decision_autonomy": "automated",
|
| 15 |
+
"human_oversight": "none",
|
| 16 |
+
"monitoring": "none",
|
| 17 |
+
"traceability": "none",
|
| 18 |
+
"technical_documentation": "partial",
|
| 19 |
+
})
|
| 20 |
+
|
| 21 |
+
assert result["risk_tier"] == "unacceptable"
|
| 22 |
+
assert "class_probabilities" in result
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_lower_risk_scenario():
|
| 26 |
+
result = predict_governance_risk({
|
| 27 |
+
"sector": "enterprise_productivity",
|
| 28 |
+
"impact": "low",
|
| 29 |
+
"decision_autonomy": "assistive",
|
| 30 |
+
"human_oversight": "strong",
|
| 31 |
+
"monitoring": "strong",
|
| 32 |
+
"traceability": "strong",
|
| 33 |
+
"technical_documentation": "strong",
|
| 34 |
+
})
|
| 35 |
+
|
| 36 |
+
assert result["risk_tier"] == "lower"
|
| 37 |
+
assert "class_probabilities" in result
|