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i import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import Optional
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import joblib
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Load models (change model path for LogReg)
|
| 10 |
+
TFIDF_PATH = "models/tfidf_vectorizer.pkl"
|
| 11 |
+
MODEL_PATH = "models/logreg_model.pkl" # <- Changed from lgbm_models.pkl
|
| 12 |
+
ENCODER_PATH = "models/label_encoders.pkl"
|
| 13 |
+
|
| 14 |
+
tfidf_vectorizer = joblib.load(TFIDF_PATH)
|
| 15 |
+
models = joblib.load(MODEL_PATH)
|
| 16 |
+
label_encoders = joblib.load(ENCODER_PATH)
|
| 17 |
+
|
| 18 |
+
class TransactionData(BaseModel):
|
| 19 |
+
Transaction_Id: str
|
| 20 |
+
Hit_Seq: int
|
| 21 |
+
Hit_Id_List: str
|
| 22 |
+
Origin: str
|
| 23 |
+
Designation: str
|
| 24 |
+
Keywords: str
|
| 25 |
+
Name: str
|
| 26 |
+
SWIFT_Tag: str
|
| 27 |
+
Currency: str
|
| 28 |
+
Entity: str
|
| 29 |
+
Message: str
|
| 30 |
+
City: str
|
| 31 |
+
Country: str
|
| 32 |
+
State: str
|
| 33 |
+
Hit_Type: str
|
| 34 |
+
Record_Matching_String: str
|
| 35 |
+
WatchList_Match_String: str
|
| 36 |
+
Payment_Sender_Name: Optional[str] = ""
|
| 37 |
+
Payment_Reciever_Name: Optional[str] = ""
|
| 38 |
+
Swift_Message_Type: str
|
| 39 |
+
Text_Sanction_Data: str
|
| 40 |
+
Matched_Sanctioned_Entity: str
|
| 41 |
+
Is_Match: int
|
| 42 |
+
Red_Flag_Reason: str
|
| 43 |
+
Risk_Level: str
|
| 44 |
+
Risk_Score: float
|
| 45 |
+
Risk_Score_Description: str
|
| 46 |
+
CDD_Level: str
|
| 47 |
+
PEP_Status: str
|
| 48 |
+
Value_Date: str
|
| 49 |
+
Last_Review_Date: str
|
| 50 |
+
Next_Review_Date: str
|
| 51 |
+
Sanction_Description: str
|
| 52 |
+
Checker_Notes: str
|
| 53 |
+
Sanction_Context: str
|
| 54 |
+
Maker_Action: str
|
| 55 |
+
Customer_ID: int
|
| 56 |
+
Customer_Type: str
|
| 57 |
+
Industry: str
|
| 58 |
+
Transaction_Date_Time: str
|
| 59 |
+
Transaction_Type: str
|
| 60 |
+
Transaction_Channel: str
|
| 61 |
+
Originating_Bank: str
|
| 62 |
+
Beneficiary_Bank: str
|
| 63 |
+
Geographic_Origin: str
|
| 64 |
+
Geographic_Destination: str
|
| 65 |
+
Match_Score: float
|
| 66 |
+
Match_Type: str
|
| 67 |
+
Sanctions_List_Version: str
|
| 68 |
+
Screening_Date_Time: str
|
| 69 |
+
Risk_Category: str
|
| 70 |
+
Risk_Drivers: str
|
| 71 |
+
Alert_Status: str
|
| 72 |
+
Investigation_Outcome: str
|
| 73 |
+
Case_Owner_Analyst: str
|
| 74 |
+
Escalation_Level: str
|
| 75 |
+
Escalation_Date: str
|
| 76 |
+
Regulatory_Reporting_Flags: bool
|
| 77 |
+
Audit_Trail_Timestamp: str
|
| 78 |
+
Source_Of_Funds: str
|
| 79 |
+
Purpose_Of_Transaction: str
|
| 80 |
+
Beneficial_Owner: str
|
| 81 |
+
Sanctions_Exposure_History: bool
|
| 82 |
+
|
| 83 |
+
class PredictionRequest(BaseModel):
|
| 84 |
+
transaction_data: TransactionData
|
| 85 |
+
|
| 86 |
+
@app.get("/")
|
| 87 |
+
async def root():
|
| 88 |
+
return {"status": "healthy", "message": "LogReg TF-IDF API is running"}
|
| 89 |
+
|
| 90 |
+
@app.post("/predict")
|
| 91 |
+
async def predict(request: PredictionRequest):
|
| 92 |
+
try:
|
| 93 |
+
input_data = pd.DataFrame([request.transaction_data.dict()])
|
| 94 |
+
|
| 95 |
+
text_input = f"""
|
| 96 |
+
Transaction ID: {input_data['Transaction_Id'].iloc[0]}
|
| 97 |
+
Origin: {input_data['Origin'].iloc[0]}
|
| 98 |
+
Designation: {input_data['Designation'].iloc[0]}
|
| 99 |
+
Keywords: {input_data['Keywords'].iloc[0]}
|
| 100 |
+
Name: {input_data['Name'].iloc[0]}
|
| 101 |
+
SWIFT Tag: {input_data['SWIFT_Tag'].iloc[0]}
|
| 102 |
+
Currency: {input_data['Currency'].iloc[0]}
|
| 103 |
+
Entity: {input_data['Entity'].iloc[0]}
|
| 104 |
+
Message: {input_data['Message'].iloc[0]}
|
| 105 |
+
City: {input_data['City'].iloc[0]}
|
| 106 |
+
Country: {input_data['Country'].iloc[0]}
|
| 107 |
+
State: {input_data['State'].iloc[0]}
|
| 108 |
+
Hit Type: {input_data['Hit_Type'].iloc[0]}
|
| 109 |
+
Record Matching String: {input_data['Record_Matching_String'].iloc[0]}
|
| 110 |
+
WatchList Match String: {input_data['WatchList_Match_String'].iloc[0]}
|
| 111 |
+
Payment Sender: {input_data['Payment_Sender_Name'].iloc[0]}
|
| 112 |
+
Payment Receiver: {input_data['Payment_Reciever_Name'].iloc[0]}
|
| 113 |
+
Swift Message Type: {input_data['Swift_Message_Type'].iloc[0]}
|
| 114 |
+
Text Sanction Data: {input_data['Text_Sanction_Data'].iloc[0]}
|
| 115 |
+
Matched Sanctioned Entity: {input_data['Matched_Sanctioned_Entity'].iloc[0]}
|
| 116 |
+
Red Flag Reason: {input_data['Red_Flag_Reason'].iloc[0]}
|
| 117 |
+
Risk Level: {input_data['Risk_Level'].iloc[0]}
|
| 118 |
+
Risk Score: {input_data['Risk_Score'].iloc[0]}
|
| 119 |
+
CDD Level: {input_data['CDD_Level'].iloc[0]}
|
| 120 |
+
PEP Status: {input_data['PEP_Status'].iloc[0]}
|
| 121 |
+
Sanction Description: {input_data['Sanction_Description'].iloc[0]}
|
| 122 |
+
Checker Notes: {input_data['Checker_Notes'].iloc[0]}
|
| 123 |
+
Sanction Context: {input_data['Sanction_Context'].iloc[0]}
|
| 124 |
+
Maker Action: {input_data['Maker_Action'].iloc[0]}
|
| 125 |
+
Customer Type: {input_data['Customer_Type'].iloc[0]}
|
| 126 |
+
Industry: {input_data['Industry'].iloc[0]}
|
| 127 |
+
Transaction Type: {input_data['Transaction_Type'].iloc[0]}
|
| 128 |
+
Transaction Channel: {input_data['Transaction_Channel'].iloc[0]}
|
| 129 |
+
Geographic Origin: {input_data['Geographic_Origin'].iloc[0]}
|
| 130 |
+
Geographic Destination: {input_data['Geographic_Destination'].iloc[0]}
|
| 131 |
+
Risk Category: {input_data['Risk_Category'].iloc[0]}
|
| 132 |
+
Risk Drivers: {input_data['Risk_Drivers'].iloc[0]}
|
| 133 |
+
Alert Status: {input_data['Alert_Status'].iloc[0]}
|
| 134 |
+
Investigation Outcome: {input_data['Investigation_Outcome'].iloc[0]}
|
| 135 |
+
Source of Funds: {input_data['Source_Of_Funds'].iloc[0]}
|
| 136 |
+
Purpose of Transaction: {input_data['Purpose_Of_Transaction'].iloc[0]}
|
| 137 |
+
Beneficial Owner: {input_data['Beneficial_Owner'].iloc[0]}
|
| 138 |
+
"""
|
| 139 |
+
|
| 140 |
+
X_tfidf = tfidf_vectorizer.transform([text_input])
|
| 141 |
+
response = {}
|
| 142 |
+
|
| 143 |
+
for label, model in models.items():
|
| 144 |
+
proba = model.predict_proba(X_tfidf)[0]
|
| 145 |
+
pred_idx = proba.argmax()
|
| 146 |
+
decoded = label_encoders[label].inverse_transform([pred_idx])[0]
|
| 147 |
+
response[label] = {
|
| 148 |
+
"prediction": decoded,
|
| 149 |
+
"probabilities": {
|
| 150 |
+
label_encoders[label].classes_[i]: float(p)
|
| 151 |
+
for i, p in enumerate(proba)
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
return response
|
| 156 |
+
|
| 157 |
+
except Exception as e:
|
| 158 |
+
raise HTTPException(status_code=500, detail=str(e))
|