subbunanepalli commited on
Commit
ba2c803
·
verified ·
1 Parent(s): 6896d95

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi 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 XGBoost)
10
+ TFIDF_PATH = "models/tfidf_vectorizer.pkl"
11
+ MODEL_PATH = "models/xgb_models.pkl" # <- XGBoost model dictionary
12
+ ENCODER_PATH = "models/label_encoders.pkl"
13
+
14
+ tfidf_vectorizer = joblib.load(TFIDF_PATH)
15
+ models = joblib.load(MODEL_PATH) # Dict of models per label
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": "XGBoost 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
+ # Construct text features as per your use case
96
+ text_input = f"""
97
+ Transaction ID: {input_data['Transaction_Id'].iloc[0]}
98
+ Origin: {input_data['Origin'].iloc[0]}
99
+ Designation: {input_data['Designation'].iloc[0]}
100
+ Keywords: {input_data['Keywords'].iloc[0]}
101
+ Name: {input_data['Name'].iloc[0]}
102
+ SWIFT Tag: {input_data['SWIFT_Tag'].iloc[0]}
103
+ Currency: {input_data['Currency'].iloc[0]}
104
+ Entity: {input_data['Entity'].iloc[0]}
105
+ Message: {input_data['Message'].iloc[0]}
106
+ City: {input_data['City'].iloc[0]}
107
+ Country: {input_data['Country'].iloc[0]}
108
+ State: {input_data['State'].iloc[0]}
109
+ Hit Type: {input_data['Hit_Type'].iloc[0]}
110
+ Record Matching String: {input_data['Record_Matching_String'].iloc[0]}
111
+ WatchList Match String: {input_data['WatchList_Match_String'].iloc[0]}
112
+ Payment Sender: {input_data['Payment_Sender_Name'].iloc[0]}
113
+ Payment Receiver: {input_data['Payment_Reciever_Name'].iloc[0]}
114
+ Swift Message Type: {input_data['Swift_Message_Type'].iloc[0]}
115
+ Text Sanction Data: {input_data['Text_Sanction_Data'].iloc[0]}
116
+ Matched Sanctioned Entity: {input_data['Matched_Sanctioned_Entity'].iloc[0]}
117
+ Red Flag Reason: {input_data['Red_Flag_Reason'].iloc[0]}
118
+ Risk Level: {input_data['Risk_Level'].iloc[0]}
119
+ Risk Score: {input_data['Risk_Score'].iloc[0]}
120
+ CDD Level: {input_data['CDD_Level'].iloc[0]}
121
+ PEP Status: {input_data['PEP_Status'].iloc[0]}
122
+ Sanction Description: {input_data['Sanction_Description'].iloc[0]}
123
+ Checker Notes: {input_data['Checker_Notes'].iloc[0]}
124
+ Sanction Context: {input_data['Sanction_Context'].iloc[0]}
125
+ Maker Action: {input_data['Maker_Action'].iloc[0]}
126
+ Customer Type: {input_data['Customer_Type'].iloc[0]}
127
+ Industry: {input_data['Industry'].iloc[0]}
128
+ Transaction Type: {input_data['Transaction_Type'].iloc[0]}
129
+ Transaction Channel: {input_data['Transaction_Channel'].iloc[0]}
130
+ Geographic Origin: {input_data['Geographic_Origin'].iloc[0]}
131
+ Geographic Destination: {input_data['Geographic_Destination'].iloc[0]}
132
+ Risk Category: {input_data['Risk_Category'].iloc[0]}
133
+ Risk Drivers: {input_data['Risk_Drivers'].iloc[0]}
134
+ Alert Status: {input_data['Alert_Status'].iloc[0]}
135
+ Investigation Outcome: {input_data['Investigation_Outcome'].iloc[0]}
136
+ Source of Funds: {input_data['Source_Of_Funds'].iloc[0]}
137
+ Purpose of Transaction: {input_data['Purpose_Of_Transaction'].iloc[0]}
138
+ Beneficial Owner: {input_data['Beneficial_Owner'].iloc[0]}
139
+ """
140
+
141
+ X_tfidf = tfidf_vectorizer.transform([text_input])
142
+ response = {}
143
+
144
+ for label, model in models.items():
145
+ # XGBoost returns class as integer directly
146
+ pred = model.predict(X_tfidf)[0]
147
+ decoded = label_encoders[label].inverse_transform([int(pred)])[0]
148
+ response[label] = decoded
149
+
150
+ return response
151
+
152
+ except Exception as e:
153
+ raise HTTPException(status_code=500, detail=str(e))