Spaces:
Sleeping
Sleeping
Commit
•
e6a4114
1
Parent(s):
eb121d8
4 Files
Browse files- app.py +105 -0
- requirements.txt +71 -0
- scaler.pkl +3 -0
- xgboost_model_new.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gradio
|
2 |
+
import joblib as joblib
|
3 |
+
import pip
|
4 |
+
# pip install gradio
|
5 |
+
# pip install joblib
|
6 |
+
# pip install xgboost
|
7 |
+
# pip install scikit-learn
|
8 |
+
|
9 |
+
import joblib
|
10 |
+
import numpy as np
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
# Load the XGBoost model
|
14 |
+
xgboost_model = joblib.load('/Users/rak/PycharmProject/Credit_Card_Fraud_Model/xgboost_model_new.pkl')
|
15 |
+
|
16 |
+
# Load the StandardScaler
|
17 |
+
scaler = joblib.load('/Users/rak/PycharmProject/Credit_Card_Fraud_Model/scaler.pkl')
|
18 |
+
|
19 |
+
month_to_number = {
|
20 |
+
"January": 1,
|
21 |
+
"February": 2,
|
22 |
+
"March": 3,
|
23 |
+
"April": 4,
|
24 |
+
"May": 5,
|
25 |
+
"June": 6,
|
26 |
+
"July": 7,
|
27 |
+
"August": 8,
|
28 |
+
"September": 9,
|
29 |
+
"October": 10,
|
30 |
+
"November": 11,
|
31 |
+
"December": 12,
|
32 |
+
}
|
33 |
+
|
34 |
+
def time_of_dayy(hour):
|
35 |
+
if 6 <= hour < 12:
|
36 |
+
return 'Morning'
|
37 |
+
elif 12 <= hour < 18:
|
38 |
+
return 'Afternoon'
|
39 |
+
elif 18 <= hour < 24:
|
40 |
+
return 'Evening'
|
41 |
+
else:
|
42 |
+
return 'Night'
|
43 |
+
|
44 |
+
# Define category options
|
45 |
+
category_options = [
|
46 |
+
'Food/Dining',
|
47 |
+
'Gas/Transport',
|
48 |
+
'Online Grocery',
|
49 |
+
'In-Person Grocery',
|
50 |
+
'Health/Fitness',
|
51 |
+
'Home',
|
52 |
+
'Kids/Pets',
|
53 |
+
'Miscellaneous Online',
|
54 |
+
'Miscellaneous In-Person',
|
55 |
+
'Personal Care',
|
56 |
+
'Shopping Online',
|
57 |
+
'Shopping In-Person',
|
58 |
+
'Travel'
|
59 |
+
]
|
60 |
+
|
61 |
+
def predict_credit_card_fraud(amount, city_pop, month, hour, age, gender, category):
|
62 |
+
# Map the input month name to its corresponding number
|
63 |
+
month = month_to_number[month]
|
64 |
+
|
65 |
+
time_of_day = time_of_dayy(hour)
|
66 |
+
|
67 |
+
# Prepare input data with dummy variables for category
|
68 |
+
input_data = np.array([[amount, city_pop, month, hour, age, int(gender == 'M'),
|
69 |
+
int(time_of_day == 'Night'), int(time_of_day == 'Evening'), int(time_of_day == 'Morning')] +
|
70 |
+
[int(category == cat) for cat in category_options]])
|
71 |
+
|
72 |
+
# Scale the input data using the loaded StandardScaler
|
73 |
+
input_data[:, 0:2] = scaler.transform(input_data[:, 0:2])
|
74 |
+
|
75 |
+
# Use predict_proba to get probability scores for class 1
|
76 |
+
probability = xgboost_model.predict_proba(input_data)[:, 1]
|
77 |
+
|
78 |
+
# Return the probability score
|
79 |
+
return round(probability[0], 2)
|
80 |
+
|
81 |
+
gender_options = ["M", "F"]
|
82 |
+
months = list(month_to_number.keys())
|
83 |
+
|
84 |
+
iface = gr.Interface(fn=predict_credit_card_fraud,
|
85 |
+
inputs=[
|
86 |
+
gr.Number(label="Amount", info="Enter the Amount of the Transaction in Dollars"),
|
87 |
+
gr.Number(label="City Population", info="Enter the City Population"),
|
88 |
+
gr.Dropdown(
|
89 |
+
months,
|
90 |
+
label="Month",
|
91 |
+
info="Select the month of the transaction"
|
92 |
+
),
|
93 |
+
gr.Slider(label="Hour", info="Enter the Hour in which the Transaction Occurred", minimum=0, maximum=23, step=1),
|
94 |
+
gr.Slider(label="Age", minimum=10, maximum=100, step=1),
|
95 |
+
gr.Radio(label="Gender", choices=gender_options),
|
96 |
+
gr.Dropdown(
|
97 |
+
category_options,
|
98 |
+
label="Category",
|
99 |
+
info="Select the Category of Purchase"
|
100 |
+
)
|
101 |
+
],
|
102 |
+
outputs="text")
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
altair==5.1.2
|
3 |
+
annotated-types==0.6.0
|
4 |
+
anyio==3.7.1
|
5 |
+
attrs==23.1.0
|
6 |
+
certifi==2023.7.22
|
7 |
+
charset-normalizer==3.3.2
|
8 |
+
click==8.1.7
|
9 |
+
colorama==0.4.6
|
10 |
+
contourpy==1.2.0
|
11 |
+
cycler==0.12.1
|
12 |
+
exceptiongroup==1.1.3
|
13 |
+
fastapi==0.104.1
|
14 |
+
ffmpy==0.3.1
|
15 |
+
filelock==3.13.1
|
16 |
+
fonttools==4.44.0
|
17 |
+
fsspec==2023.10.0
|
18 |
+
gradio==4.1.2
|
19 |
+
gradio_client==0.7.0
|
20 |
+
h11==0.14.0
|
21 |
+
httpcore==1.0.1
|
22 |
+
httpx==0.25.1
|
23 |
+
huggingface-hub==0.18.0
|
24 |
+
idna==3.4
|
25 |
+
importlib-resources==6.1.1
|
26 |
+
Jinja2==3.1.2
|
27 |
+
joblib==1.3.2
|
28 |
+
jsonschema==4.19.2
|
29 |
+
jsonschema-specifications==2023.7.1
|
30 |
+
kiwisolver==1.4.5
|
31 |
+
learn==1.0.0
|
32 |
+
markdown-it-py==3.0.0
|
33 |
+
MarkupSafe==2.1.3
|
34 |
+
matplotlib==3.8.1
|
35 |
+
mdurl==0.1.2
|
36 |
+
numpy==1.26.1
|
37 |
+
orjson==3.9.10
|
38 |
+
packaging==23.2
|
39 |
+
pandas==2.1.2
|
40 |
+
Pillow==10.1.0
|
41 |
+
pydantic==2.4.2
|
42 |
+
pydantic_core==2.10.1
|
43 |
+
pydub==0.25.1
|
44 |
+
Pygments==2.16.1
|
45 |
+
pyparsing==3.1.1
|
46 |
+
python-dateutil==2.8.2
|
47 |
+
python-multipart==0.0.6
|
48 |
+
pytz==2023.3.post1
|
49 |
+
PyYAML==6.0.1
|
50 |
+
referencing==0.30.2
|
51 |
+
requests==2.31.0
|
52 |
+
rich==13.6.0
|
53 |
+
rpds-py==0.12.0
|
54 |
+
scikit-learn==1.3.2
|
55 |
+
scipy==1.11.3
|
56 |
+
semantic-version==2.10.0
|
57 |
+
shellingham==1.5.4
|
58 |
+
six==1.16.0
|
59 |
+
sniffio==1.3.0
|
60 |
+
starlette==0.27.0
|
61 |
+
threadpoolctl==3.2.0
|
62 |
+
tomlkit==0.12.0
|
63 |
+
toolz==0.12.0
|
64 |
+
tqdm==4.66.1
|
65 |
+
typer==0.9.0
|
66 |
+
typing_extensions==4.8.0
|
67 |
+
tzdata==2023.3
|
68 |
+
urllib3==2.0.7
|
69 |
+
uvicorn==0.24.0.post1
|
70 |
+
websockets==11.0.3
|
71 |
+
xgboost==2.0.1
|
scaler.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b913deb7ff1b02212fd4d3512996b74a9840e058ed0d58a9dc316fa6edbdde77
|
3 |
+
size 983
|
xgboost_model_new.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:25c781be94764459752dfe626c2c475a8718793089657e80076046f238f82fc2
|
3 |
+
size 258523
|