Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- src/__pycache__/utils.cpython-311.pyc +0 -0
- src/utils.py +27 -0
src/__pycache__/utils.cpython-311.pyc
ADDED
|
Binary file (2.56 kB). View file
|
|
|
src/utils.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from joblib import dump, load
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class LoadClassifierThreshold:
|
| 5 |
+
def __init__(self, model_path, threshold_path):
|
| 6 |
+
try:
|
| 7 |
+
self.model = load(model_path)
|
| 8 |
+
except Exception as e:
|
| 9 |
+
raise ValueError(f"Failed to load model from {model_path}: {str(e)}")
|
| 10 |
+
|
| 11 |
+
try:
|
| 12 |
+
with open(threshold_path, "r") as threshold_file:
|
| 13 |
+
self.threshold = float(threshold_file.read())
|
| 14 |
+
except Exception as e:
|
| 15 |
+
raise ValueError(f"Failed to load threshold from {threshold_path}: {str(e)}")
|
| 16 |
+
|
| 17 |
+
def predict_with_threshold(self, testset):
|
| 18 |
+
if not hasattr(self, 'model') or not hasattr(self, 'threshold'):
|
| 19 |
+
raise ValueError("Model or threshold not loaded correctly.")
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
# Use the predicted probabilities and compare with the threshold
|
| 23 |
+
predicted_probabilities = self.model.predict_proba(testset)[:, 1]
|
| 24 |
+
predictions = (predicted_probabilities >= self.threshold).astype(int)
|
| 25 |
+
return predictions
|
| 26 |
+
except Exception as e:
|
| 27 |
+
raise ValueError(f"Prediction failed: {str(e)}")
|