gauravkoradiya SoooSlooow commited on
Commit
9df1de7
·
0 Parent(s):

Duplicate from SoooSlooow/CreditCardsApp

Browse files

Co-authored-by: Andrey Ovsyannikov <[email protected]>

.gitattributes ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zst filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
32
+ models/final_model.pkl filter=lfs diff=lfs merge=lfs -text
33
+ models/other/unique_column_values.pkl filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: CreditCardsApp
3
+ emoji: 💻
4
+ colorFrom: gray
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.3.1
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: SoooSlooow/CreditCardsApp
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import random
3
+ from typing import Any
4
+
5
+ import gradio as gr
6
+ import joblib
7
+ import numpy as np
8
+ import pandas as pd
9
+
10
+ OUTPUT_DATA_PATH = "data/processed/app_dataset.csv"
11
+ PREDICTIONS_PATH = "models/predictions/app_predictions.csv"
12
+ UNIQUE_VALUES_PATH = "models/other/unique_column_values.pkl"
13
+ MODEL_PATH = "models/final_model.pkl"
14
+
15
+
16
+ def predict(*args: tuple) -> Any:
17
+ app_df = pd.DataFrame(data=[args], columns=columns, index=[0])
18
+ app_df.to_csv(OUTPUT_DATA_PATH, index=False)
19
+ model = joblib.load(MODEL_PATH)
20
+ predictions = model.predict_proba(app_df)
21
+ print(predictions)
22
+ if predictions[0][0] < 0.99:
23
+ message = "Client is considered bad. Issuance of credit is not recommended."
24
+ else:
25
+ message = "Client is considered good. Issuance of credit is allowed."
26
+ return round(predictions[0][0], 3), message
27
+
28
+
29
+ columns = (
30
+ "YEARS_BIRTH",
31
+ "CODE_GENDER",
32
+ "AMT_INCOME_TOTAL",
33
+ "NAME_INCOME_TYPE",
34
+ "YEARS_EMPLOYED",
35
+ "OCCUPATION_TYPE",
36
+ "NAME_EDUCATION_TYPE",
37
+ "CNT_FAM_MEMBERS",
38
+ "CNT_CHILDREN",
39
+ "NAME_FAMILY_STATUS",
40
+ "FLAG_OWN_CAR",
41
+ "FLAG_OWN_REALTY",
42
+ "NAME_HOUSING_TYPE",
43
+ "FLAG_PHONE",
44
+ "FLAG_WORK_PHONE",
45
+ "FLAG_EMAIL",
46
+ )
47
+ unique_values = joblib.load(UNIQUE_VALUES_PATH)
48
+
49
+ with gr.Blocks() as demo:
50
+ with gr.Row():
51
+ with gr.Column():
52
+ age = gr.Slider(label="Age", minimum=18, maximum=90, step=1, randomize=True)
53
+ sex = gr.Dropdown(
54
+ label="Sex",
55
+ choices=unique_values["CODE_GENDER"],
56
+ value=lambda: random.choice(unique_values["CODE_GENDER"]),
57
+ )
58
+ annual_income = gr.Slider(
59
+ label="Annual income",
60
+ minimum=0,
61
+ maximum=1000000,
62
+ step=10000,
63
+ randomize=True,
64
+ )
65
+ income_type = gr.Dropdown(
66
+ label="Income type",
67
+ choices=unique_values["NAME_INCOME_TYPE"],
68
+ value=lambda: random.choice(unique_values["NAME_INCOME_TYPE"]),
69
+ )
70
+ work_experience = gr.Slider(
71
+ label="Work experience at current position",
72
+ minimum=0,
73
+ maximum=75,
74
+ step=1,
75
+ randomize=True,
76
+ )
77
+ occupation_type = gr.Dropdown(
78
+ label="Occupation type",
79
+ choices=unique_values["OCCUPATION_TYPE"],
80
+ value=lambda: random.choice(unique_values["OCCUPATION_TYPE"]),
81
+ )
82
+ education_type = gr.Dropdown(
83
+ label="Education type",
84
+ choices=unique_values["NAME_EDUCATION_TYPE"],
85
+ value=lambda: random.choice(unique_values["NAME_EDUCATION_TYPE"]),
86
+ )
87
+ amount_of_family_members = gr.Slider(
88
+ label="Amount of family members",
89
+ minimum=0,
90
+ maximum=12,
91
+ step=1,
92
+ randomize=True,
93
+ )
94
+ amount_of_children = gr.Slider(
95
+ label="Amount of children",
96
+ minimum=0,
97
+ maximum=10,
98
+ step=1,
99
+ randomize=True,
100
+ )
101
+
102
+ with gr.Column():
103
+ family_status = gr.Dropdown(
104
+ label="Family status",
105
+ choices=unique_values["NAME_FAMILY_STATUS"],
106
+ value=lambda: random.choice(unique_values["NAME_FAMILY_STATUS"]),
107
+ )
108
+ flag_own_car = gr.Dropdown(
109
+ label="Having a car",
110
+ choices=unique_values["FLAG_OWN_REALTY"],
111
+ value=lambda: random.choice(unique_values["FLAG_OWN_REALTY"]),
112
+ )
113
+ flag_own_realty = gr.Dropdown(
114
+ label="Having a realty",
115
+ choices=unique_values["FLAG_OWN_REALTY"],
116
+ value=lambda: random.choice(unique_values["FLAG_OWN_REALTY"]),
117
+ )
118
+ housing_type = gr.Dropdown(
119
+ label="Housing type",
120
+ choices=unique_values["NAME_HOUSING_TYPE"],
121
+ value=lambda: random.choice(unique_values["NAME_HOUSING_TYPE"]),
122
+ )
123
+ flag_phone = gr.Dropdown(
124
+ label="Having a phone",
125
+ choices=unique_values["FLAG_PHONE"],
126
+ value=lambda: random.choice(unique_values["FLAG_PHONE"]),
127
+ )
128
+ flag_work_phone = gr.Dropdown(
129
+ label="Having a work phone",
130
+ choices=unique_values["FLAG_WORK_PHONE"],
131
+ value=lambda: random.choice(unique_values["FLAG_WORK_PHONE"]),
132
+ )
133
+ flag_email = gr.Dropdown(
134
+ label="Having an email",
135
+ choices=unique_values["FLAG_EMAIL"],
136
+ value=lambda: random.choice(unique_values["FLAG_EMAIL"]),
137
+ )
138
+
139
+ with gr.Column():
140
+ label_1 = gr.Label(label="Client rating")
141
+ label_2 = gr.Textbox(label="Client verdict (client is considered bad if client rating < 0.99)")
142
+ with gr.Row():
143
+ predict_btn = gr.Button(value="Predict")
144
+ predict_btn.click(
145
+ predict,
146
+ inputs=[
147
+ age,
148
+ sex,
149
+ annual_income,
150
+ income_type,
151
+ work_experience,
152
+ occupation_type,
153
+ education_type,
154
+ amount_of_family_members,
155
+ amount_of_children,
156
+ family_status,
157
+ flag_own_car,
158
+ flag_own_realty,
159
+ housing_type,
160
+ flag_phone,
161
+ flag_work_phone,
162
+ flag_email,
163
+ ],
164
+ outputs=[label_1, label_2],
165
+ )
166
+
167
+ demo.launch()
data/processed/app_dataset.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ YEARS_BIRTH,CODE_GENDER,AMT_INCOME_TOTAL,NAME_INCOME_TYPE,YEARS_EMPLOYED,OCCUPATION_TYPE,NAME_EDUCATION_TYPE,CNT_FAM_MEMBERS,CNT_CHILDREN,NAME_FAMILY_STATUS,FLAG_OWN_CAR,FLAG_OWN_REALTY,NAME_HOUSING_TYPE,FLAG_PHONE,FLAG_WORK_PHONE,FLAG_EMAIL
2
+ 51,F,1540000,Pensioner,59,High skill tech staff,Academic degree,6,7,Separated,Yes,Yes,Rented apartment,Yes,Yes,No
models/final_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:12919987df9acc9b14a30e37dcb292abf3a8d5586112c41e3384dabe006b7668
3
+ size 1137472
models/other/.gitkeep ADDED
File without changes
models/other/unique_column_values.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c662605d2104301585c83e2a6a9f7f3ceeae44bea0f08858618a8b3cbd11b523
3
+ size 992
models/predictions/app_predictions.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ proba_0,proba_1,label
2
+ 0.9597238767799152,0.04027612322008478,1.0
requirements.txt ADDED
Binary file (2.43 kB). View file
 
src/__init__.py ADDED
File without changes
src/models/__init__.py ADDED
File without changes
src/models/make_predictions.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import click
2
+ import joblib
3
+ import numpy as np
4
+ import pandas as pd
5
+
6
+
7
+ @click.command()
8
+ @click.argument("input_data_path", type=click.Path(exists=True))
9
+ @click.argument("input_model_path", type=click.Path(exists=True))
10
+ @click.argument("output_predictions_path", type=click.Path())
11
+ def make_predictions(
12
+ input_data_path: str, input_model_path: str, output_predictions_path: str
13
+ ) -> None:
14
+ """
15
+ Предсказывает значения меток в входных данных, используя подаваемую на вход модель.
16
+ Предсказания записываются в csv-файл с тремя столбцами. В первые два столбца записываются вероятности
17
+ отнесения объекта к классу 0 и 1 соответственно, в третий - предсказываемая метка объекта на основе
18
+ выбранного порога вероятности.
19
+ :param input_data_path: путь к данным
20
+ :param input_model_path: путь к обученной модели
21
+ :param output_predictions_path: путь к файлу с получаемыми предсказаниями
22
+ """
23
+ df = pd.read_csv(input_data_path)
24
+ X = df.drop(["BAD_CLIENT"], axis=1, errors="ignore")
25
+
26
+ model = joblib.load(input_model_path)
27
+ probas = model.predict_proba(X)
28
+ labels = (probas[:, 1] > 0.01).astype(int)
29
+ predictions = pd.DataFrame(
30
+ data=np.column_stack([probas, labels]), columns=["proba_0", "proba_1", "label"]
31
+ )
32
+
33
+ predictions.to_csv(output_predictions_path, index=False)
34
+
35
+
36
+ if __name__ == "__main__":
37
+ make_predictions()
38
+
39
+ """
40
+ python -m src.models.make_predictions processed/processed/test_dataset.csv models/final_model.pkl reports/predictions.csv
41
+ """