Spaces:
Running on Zero
Running on Zero
File size: 1,101 Bytes
b964596 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | from pathlib import Path
import joblib
ROOT = Path(__file__).resolve().parents[1]
MODEL_PATH = (
ROOT /
"train" /
"saved_models" /
"bow_pipeline.joblib"
)
class BowModel:
def __init__(self):
self.pipeline = joblib.load(MODEL_PATH)
def predict(self, text):
prediction = self.pipeline.predict([text])[0]
probabilities = self.pipeline.predict_proba([text])[0]
vectorizer = self.pipeline.named_steps["vectorizer"]
X = vectorizer.transform([text])
vector = X.toarray()[0]
vocab = vectorizer.get_feature_names_out()
representation = {}
for palavra, contagem in zip(vocab, vector):
if contagem > 0:
representation[palavra] = int(contagem)
return {
"prediction": prediction,
"probabilities": {
classe: float(prob)
for classe, prob in zip(
self.pipeline.classes_,
probabilities
)
},
"representation": representation
} |