AshenH commited on
Commit
91c65e4
·
verified ·
1 Parent(s): f4dc602

Update tools/predict_tool.py

Browse files
Files changed (1) hide show
  1. tools/predict_tool.py +31 -31
tools/predict_tool.py CHANGED
@@ -1,32 +1,32 @@
1
- import os
2
- import pandas as pd
3
- import joblib
4
- from huggingface_hub import hf_hub_download
5
- from ..utils.config import AppConfig
6
- from ..utils.tracing import Tracer
7
-
8
- class PredictTool:
9
- def __init__(self, cfg: AppConfig, tracer: Tracer):
10
- self.cfg = cfg
11
- self.tracer = tracer
12
- self._model = None
13
- self._feature_meta = None
14
-
15
- def _ensure_loaded(self):
16
- if self._model is None:
17
- path = hf_hub_download(repo_id=self.cfg.hf_model_repo, filename="model.pkl", token=os.getenv("HF_TOKEN"))
18
- self._model = joblib.load(path)
19
- meta = hf_hub_download(repo_id=self.cfg.hf_model_repo, filename="feature_metadata.json", token=os.getenv("HF_TOKEN"))
20
- import json
21
- with open(meta, "r") as f:
22
- self._feature_meta = json.load(f)
23
-
24
- def run(self, df: pd.DataFrame) -> pd.DataFrame:
25
- self._ensure_loaded()
26
- use_cols = self._feature_meta.get("feature_order", list(df.columns))
27
- X = df[use_cols].copy()
28
- preds = self._model.predict_proba(X)[:, 1] if hasattr(self._model, "predict_proba") else self._model.predict(X)
29
- out = df.copy()
30
- out[self._feature_meta.get("prediction_column", "prediction")] = preds
31
- self.tracer.trace_event("predict", {"rows": len(out)})
32
  return out
 
1
+ import os
2
+ import pandas as pd
3
+ import joblib
4
+ from huggingface_hub import hf_hub_download
5
+ from utils.config import AppConfig
6
+ from utils.tracing import Tracer
7
+
8
+ class PredictTool:
9
+ def __init__(self, cfg: AppConfig, tracer: Tracer):
10
+ self.cfg = cfg
11
+ self.tracer = tracer
12
+ self._model = None
13
+ self._feature_meta = None
14
+
15
+ def _ensure_loaded(self):
16
+ if self._model is None:
17
+ path = hf_hub_download(repo_id=self.cfg.hf_model_repo, filename="model.pkl", token=os.getenv("HF_TOKEN"))
18
+ self._model = joblib.load(path)
19
+ meta = hf_hub_download(repo_id=self.cfg.hf_model_repo, filename="feature_metadata.json", token=os.getenv("HF_TOKEN"))
20
+ import json
21
+ with open(meta, "r") as f:
22
+ self._feature_meta = json.load(f)
23
+
24
+ def run(self, df: pd.DataFrame) -> pd.DataFrame:
25
+ self._ensure_loaded()
26
+ use_cols = self._feature_meta.get("feature_order", list(df.columns))
27
+ X = df[use_cols].copy()
28
+ preds = self._model.predict_proba(X)[:, 1] if hasattr(self._model, "predict_proba") else self._model.predict(X)
29
+ out = df.copy()
30
+ out[self._feature_meta.get("prediction_column", "prediction")] = preds
31
+ self.tracer.trace_event("predict", {"rows": len(out)})
32
  return out