Create pipeline.py
Browse files- pipeline.py +33 -0
pipeline.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from typing import Any, Dict, List
|
| 3 |
+
|
| 4 |
+
import sklearn
|
| 5 |
+
import os
|
| 6 |
+
import joblib
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class PreTrainedPipeline():
|
| 12 |
+
def __init__(self, path: str):
|
| 13 |
+
# load the model
|
| 14 |
+
self.model = joblib.load((os.path.join(path, "pipeline.pkl"))
|
| 15 |
+
|
| 16 |
+
def __call__(self, inputs: str) -> List[Dict[str, float]]:
|
| 17 |
+
"""
|
| 18 |
+
Args:
|
| 19 |
+
inputs (:obj:`str`):
|
| 20 |
+
a string containing some text
|
| 21 |
+
Return:
|
| 22 |
+
A :obj:`list`:. The object returned should be a list of one list like [[{"label": 0.9939950108528137}]] containing:
|
| 23 |
+
- "label": A string representing what the label/class is. There can be multiple labels.
|
| 24 |
+
- "score": A score between 0 and 1 describing how confident the model is for this label/class.
|
| 25 |
+
"""
|
| 26 |
+
predictions = self.model.predict_proba([inputs])
|
| 27 |
+
labels = []
|
| 28 |
+
for cls in predictions[0]:
|
| 29 |
+
labels.append({
|
| 30 |
+
"label": f"LABEL_{cls}",
|
| 31 |
+
"score": predictions[0][cls],
|
| 32 |
+
})
|
| 33 |
+
return labels
|