Download utils/json_utils.py from Ariyan-Pro/enterprise-adversarial-ml-governance-engine: direct link, hf CLI and curl.
- Browser
- Download file 739 Bytes
-
https://huggingface.co/Ariyan-Pro/enterprise-adversarial-ml-governance-engine/resolve/main/utils/json_utils.py
- Command line
-
hf download hf://Ariyan-Pro/enterprise-adversarial-ml-governance-engine/utils/json_utils.py
-
curl -L -o json_utils.py https://huggingface.co/Ariyan-Pro/enterprise-adversarial-ml-governance-engine/resolve/main/utils/json_utils.py
739 Bytes
| import json | |
| import numpy as np | |
| class NumpyEncoder(json.JSONEncoder): | |
| """Custom JSON encoder for numpy types""" | |
| def default(self, obj): | |
| if isinstance(obj, (np.integer, np.int32, np.int64)): | |
| return int(obj) | |
| elif isinstance(obj, (np.floating, np.float32, np.float64)): | |
| return float(obj) | |
| elif isinstance(obj, np.ndarray): | |
| return obj.tolist() | |
| elif isinstance(obj, np.bool_): | |
| return bool(obj) | |
| return super().default(obj) | |
| def safe_json_dump(data, file_path, indent=2): | |
| """Safely dump data to JSON file, handling numpy types""" | |
| with open(file_path, 'w', encoding='utf-8') as f: | |
| json.dump(data, f, indent=indent, cls=NumpyEncoder) | |