File size: 1,181 Bytes
f4e32bc
 
 
 
 
 
f8cea34
f4e32bc
 
 
 
f8cea34
f4e32bc
f8cea34
 
 
 
 
 
f4e32bc
 
f8cea34
 
f4e32bc
 
f8cea34
f4e32bc
 
 
 
 
 
 
 
f8cea34
f4e32bc
 
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
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))