Spaces:
Sleeping
Sleeping
Create test.py
Browse files
test.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
# === Load pretrained components ===
|
| 5 |
+
tfidf = joblib.load("models/tfidf_vectorizer.pkl")
|
| 6 |
+
models = joblib.load("models/logreg_model.pkl")
|
| 7 |
+
encoders = joblib.load("models/label_encoders.pkl")
|
| 8 |
+
|
| 9 |
+
# === Sample structured input (as in your app.py logic) ===
|
| 10 |
+
sample_input = {
|
| 11 |
+
"Transaction_Id": "T001",
|
| 12 |
+
"Origin": "USA",
|
| 13 |
+
"Designation": "CEO",
|
| 14 |
+
"Keywords": "sanctioned entity",
|
| 15 |
+
"Name": "John Doe",
|
| 16 |
+
"SWIFT_Tag": "TAG001",
|
| 17 |
+
"Currency": "USD",
|
| 18 |
+
"Entity": "ABC Corp",
|
| 19 |
+
"Message": "Wire transfer from XYZ",
|
| 20 |
+
"City": "New York",
|
| 21 |
+
"Country": "USA",
|
| 22 |
+
"State": "NY",
|
| 23 |
+
"Hit_Type": "Manual",
|
| 24 |
+
"Record_Matching_String": "ABC",
|
| 25 |
+
"WatchList_Match_String": "XYZ",
|
| 26 |
+
"Payment_Sender_Name": "Jane Smith",
|
| 27 |
+
"Payment_Reciever_Name": "Robert",
|
| 28 |
+
"Swift_Message_Type": "103",
|
| 29 |
+
"Text_Sanction_Data": "Possible match with OFAC list",
|
| 30 |
+
"Matched_Sanctioned_Entity": "XYZ Entity",
|
| 31 |
+
"Red_Flag_Reason": "High Risk",
|
| 32 |
+
"Risk_Level": "High",
|
| 33 |
+
"Risk_Score": 85.5,
|
| 34 |
+
"Risk_Score_Description": "Above threshold",
|
| 35 |
+
"CDD_Level": "Enhanced",
|
| 36 |
+
"PEP_Status": "Yes",
|
| 37 |
+
"Sanction_Description": "Description text",
|
| 38 |
+
"Checker_Notes": "Some note",
|
| 39 |
+
"Sanction_Context": "Transaction linked with blacklisted entity",
|
| 40 |
+
"Maker_Action": "Hold",
|
| 41 |
+
"Customer_Type": "Individual",
|
| 42 |
+
"Industry": "Finance",
|
| 43 |
+
"Transaction_Type": "Credit",
|
| 44 |
+
"Transaction_Channel": "SWIFT",
|
| 45 |
+
"Geographic_Origin": "North America",
|
| 46 |
+
"Geographic_Destination": "Europe",
|
| 47 |
+
"Risk_Category": "Sanctions",
|
| 48 |
+
"Risk_Drivers": "PEP, Country",
|
| 49 |
+
"Alert_Status": "Open",
|
| 50 |
+
"Investigation_Outcome": "Pending",
|
| 51 |
+
"Source_Of_Funds": "Salary",
|
| 52 |
+
"Purpose_Of_Transaction": "Investment",
|
| 53 |
+
"Beneficial_Owner": "John Doe"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# === Combine into text format (same as in FastAPI logic) ===
|
| 57 |
+
text_input = f"""
|
| 58 |
+
Transaction ID: {sample_input['Transaction_Id']}
|
| 59 |
+
Origin: {sample_input['Origin']}
|
| 60 |
+
Designation: {sample_input['Designation']}
|
| 61 |
+
Keywords: {sample_input['Keywords']}
|
| 62 |
+
Name: {sample_input['Name']}
|
| 63 |
+
SWIFT Tag: {sample_input['SWIFT_Tag']}
|
| 64 |
+
Currency: {sample_input['Currency']}
|
| 65 |
+
Entity: {sample_input['Entity']}
|
| 66 |
+
Message: {sample_input['Message']}
|
| 67 |
+
City: {sample_input['City']}
|
| 68 |
+
Country: {sample_input['Country']}
|
| 69 |
+
State: {sample_input['State']}
|
| 70 |
+
Hit Type: {sample_input['Hit_Type']}
|
| 71 |
+
Record Matching String: {sample_input['Record_Matching_String']}
|
| 72 |
+
WatchList Match String: {sample_input['WatchList_Match_String']}
|
| 73 |
+
Payment Sender: {sample_input['Payment_Sender_Name']}
|
| 74 |
+
Payment Receiver: {sample_input['Payment_Reciever_Name']}
|
| 75 |
+
Swift Message Type: {sample_input['Swift_Message_Type']}
|
| 76 |
+
Text Sanction Data: {sample_input['Text_Sanction_Data']}
|
| 77 |
+
Matched Sanctioned Entity: {sample_input['Matched_Sanctioned_Entity']}
|
| 78 |
+
Red Flag Reason: {sample_input['Red_Flag_Reason']}
|
| 79 |
+
Risk Level: {sample_input['Risk_Level']}
|
| 80 |
+
Risk Score: {sample_input['Risk_Score']}
|
| 81 |
+
CDD Level: {sample_input['CDD_Level']}
|
| 82 |
+
PEP Status: {sample_input['PEP_Status']}
|
| 83 |
+
Sanction Description: {sample_input['Sanction_Description']}
|
| 84 |
+
Checker Notes: {sample_input['Checker_Notes']}
|
| 85 |
+
Sanction Context: {sample_input['Sanction_Context']}
|
| 86 |
+
Maker Action: {sample_input['Maker_Action']}
|
| 87 |
+
Customer Type: {sample_input['Customer_Type']}
|
| 88 |
+
Industry: {sample_input['Industry']}
|
| 89 |
+
Transaction Type: {sample_input['Transaction_Type']}
|
| 90 |
+
Transaction Channel: {sample_input['Transaction_Channel']}
|
| 91 |
+
Geographic Origin: {sample_input['Geographic_Origin']}
|
| 92 |
+
Geographic Destination: {sample_input['Geographic_Destination']}
|
| 93 |
+
Risk Category: {sample_input['Risk_Category']}
|
| 94 |
+
Risk Drivers: {sample_input['Risk_Drivers']}
|
| 95 |
+
Alert Status: {sample_input['Alert_Status']}
|
| 96 |
+
Investigation Outcome: {sample_input['Investigation_Outcome']}
|
| 97 |
+
Source of Funds: {sample_input['Source_Of_Funds']}
|
| 98 |
+
Purpose of Transaction: {sample_input['Purpose_Of_Transaction']}
|
| 99 |
+
Beneficial Owner: {sample_input['Beneficial_Owner']}
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
# === Vectorize ===
|
| 103 |
+
X_tfidf = tfidf.transform([text_input])
|
| 104 |
+
|
| 105 |
+
# === Predict ===
|
| 106 |
+
print(" Predictions:")
|
| 107 |
+
for label, model in models.items():
|
| 108 |
+
proba = model.predict_proba(X_tfidf)[0]
|
| 109 |
+
pred_idx = proba.argmax()
|
| 110 |
+
decoded = encoders[label].inverse_transform([pred_idx])[0]
|
| 111 |
+
print(f"{label}: {decoded}")
|