Download pace_inference.py from rocky250/PACE: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/rocky250/PACE/resolve/main/pace_inference.py
- Command line
-
hf download hf://rocky250/PACE/pace_inference.py
-
curl -L -o pace_inference.py https://huggingface.co/rocky250/PACE/resolve/main/pace_inference.py
1.92 kB
| import json | |
| from pathlib import Path | |
| import cloudpickle | |
| import numpy as np | |
| import pandas as pd | |
| class PacePredictor: | |
| def __init__(self, artifact_dir="."): | |
| self.artifact_dir = Path(artifact_dir) | |
| self.config = json.loads((self.artifact_dir / "model_config.json").read_text(encoding="utf-8")) | |
| with (self.artifact_dir / self.config["model_file"]).open("rb") as model_file: | |
| self.model = cloudpickle.load(model_file) | |
| self.feature_names = list(self.config["feature_order"]) | |
| self.threshold = float(self.config["decision_threshold"]) | |
| self.positive_class = self.config.get("positive_class", 1) | |
| classes = list(np.asarray(self.model.classes_)) | |
| if self.positive_class not in classes: | |
| raise ValueError("The configured positive class is missing from the fitted model.") | |
| self.positive_index = classes.index(self.positive_class) | |
| def predict(self, records): | |
| if isinstance(records, pd.DataFrame): | |
| frame = records.copy() | |
| elif isinstance(records, dict): | |
| frame = pd.DataFrame([records]) | |
| else: | |
| frame = pd.DataFrame(records) | |
| missing = [name for name in self.feature_names if name not in frame.columns] | |
| if missing: | |
| raise ValueError("Missing required model inputs: " + ", ".join(missing)) | |
| frame = frame.reindex(columns=self.feature_names) | |
| probabilities = self.model.predict_proba(frame)[:, self.positive_index] | |
| predictions = (probabilities >= self.threshold).astype(int) | |
| return pd.DataFrame({ | |
| "positive_class_probability": probabilities, | |
| "probability_percent": probabilities * 100.0, | |
| "predicted_label": predictions, | |
| "decision_threshold": self.threshold | |
| }, index=frame.index) | |
| def load_predictor(artifact_dir="."): | |
| return PacePredictor(artifact_dir=artifact_dir) | |