LogReg / predict.py
subbunanepalli's picture
Update predict.py
f8cea34 verified
from fastapi import HTTPException
import pandas as pd
import joblib
from validate import TransactionData
from utils import create_text_input
# === Path to saved model ===
MODEL_PATH = "models/logreg_model.pkl"
def predict(request: TransactionData):
try:
# Load the model pipeline (TfidfVectorizer + MultiOutputClassifier)
model = joblib.load(MODEL_PATH)
# Safety check to ensure it's a model
if not hasattr(model, "predict"):
raise ValueError("Loaded object is not a model pipeline")
# Prepare input
input_df = pd.DataFrame([request.dict()]).fillna("")
text_input = create_text_input(input_df.iloc[0])
# Make prediction
prediction = model.predict([text_input])[0]
# Return predictions as dict
return {
"Maker_Action": prediction[0],
"Escalation_Level": prediction[1],
"Risk_Category": prediction[2],
"Risk_Drivers": prediction[3],
"Investigation_Outcome": prediction[4],
"Alert_Status": prediction[5]
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))