ganeshkonapalli commited on
Commit
bea5dee
·
verified ·
1 Parent(s): 37b9bc4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +165 -0
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import pandas as pd
4
+ from typing import Optional
5
+ import joblib
6
+ import os
7
+
8
+ app = FastAPI()
9
+
10
+ # Load vectorizer, models and encoders
11
+ TFIDF_VECTORIZER_PATH = "models/tfidf_vectorizer.pkl"
12
+ MODELS_PATH = "models/logreg_model.pkl" # Change here for logistic regression
13
+ LABEL_ENCODERS_PATH = "models/label_encoders.pkl"
14
+
15
+ tfidf_vectorizer = joblib.load(TFIDF_VECTORIZER_PATH)
16
+ models = joblib.load(MODELS_PATH)
17
+ label_encoders = joblib.load(LABEL_ENCODERS_PATH)
18
+
19
+ class TransactionData(BaseModel):
20
+ Transaction_Id: str
21
+ Hit_Seq: int
22
+ Hit_Id_List: str
23
+ Origin: str
24
+ Designation: str
25
+ Keywords: str
26
+ Name: str
27
+ SWIFT_Tag: str
28
+ Currency: str
29
+ Entity: str
30
+ Message: str
31
+ City: str
32
+ Country: str
33
+ State: str
34
+ Hit_Type: str
35
+ Record_Matching_String: str
36
+ WatchList_Match_String: str
37
+ Payment_Sender_Name: Optional[str] = ""
38
+ Payment_Reciever_Name: Optional[str] = ""
39
+ Swift_Message_Type: str
40
+ Text_Sanction_Data: str
41
+ Matched_Sanctioned_Entity: str
42
+ Is_Match: int
43
+ Red_Flag_Reason: str
44
+ Risk_Level: str
45
+ Risk_Score: float
46
+ Risk_Score_Description: str
47
+ CDD_Level: str
48
+ PEP_Status: str
49
+ Value_Date: str
50
+ Last_Review_Date: str
51
+ Next_Review_Date: str
52
+ Sanction_Description: str
53
+ Checker_Notes: str
54
+ Sanction_Context: str
55
+ Maker_Action: str
56
+ Customer_ID: int
57
+ Customer_Type: str
58
+ Industry: str
59
+ Transaction_Date_Time: str
60
+ Transaction_Type: str
61
+ Transaction_Channel: str
62
+ Originating_Bank: str
63
+ Beneficiary_Bank: str
64
+ Geographic_Origin: str
65
+ Geographic_Destination: str
66
+ Match_Score: float
67
+ Match_Type: str
68
+ Sanctions_List_Version: str
69
+ Screening_Date_Time: str
70
+ Risk_Category: str
71
+ Risk_Drivers: str
72
+ Alert_Status: str
73
+ Investigation_Outcome: str
74
+ Case_Owner_Analyst: str
75
+ Escalation_Level: str
76
+ Escalation_Date: str
77
+ Regulatory_Reporting_Flags: bool
78
+ Audit_Trail_Timestamp: str
79
+ Source_Of_Funds: str
80
+ Purpose_Of_Transaction: str
81
+ Beneficial_Owner: str
82
+ Sanctions_Exposure_History: bool
83
+
84
+ class PredictionRequest(BaseModel):
85
+ transaction_data: TransactionData
86
+
87
+ @app.get("/")
88
+ async def root():
89
+ return {"status": "healthy", "message": "LogReg TF-IDF API is running"}
90
+
91
+ @app.post("/predict")
92
+ async def predict(request: PredictionRequest):
93
+ try:
94
+ input_data = pd.DataFrame([request.transaction_data.dict()])
95
+
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
+
143
+ response = {}
144
+ for label, model in models.items():
145
+ proba = model.predict_proba(X_tfidf)[0]
146
+ pred = proba.argmax()
147
+ decoded_label = label_encoders[label].inverse_transform([pred])[0]
148
+ class_probs = {
149
+ label_encoders[label].classes_[i]: float(prob)
150
+ for i, prob in enumerate(proba)
151
+ }
152
+ response[label] = {
153
+ "prediction": decoded_label,
154
+ "probabilities": class_probs
155
+ }
156
+
157
+ return response
158
+
159
+ except Exception as e:
160
+ raise HTTPException(status_code=500, detail=str(e))
161
+
162
+ if __name__ == "__main__":
163
+ import uvicorn
164
+ port = int(os.environ.get("PORT", 7860))
165
+ uvicorn.run(app, host="0.0.0.0", port=port)