greykingreys commited on
Commit
9ccbfdc
·
verified ·
1 Parent(s): 4b8118e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -125
app.py CHANGED
@@ -1,126 +1,115 @@
1
-
2
- #Deployement avec Gradio
3
- #fonctions de predictions
4
- import gradio as gr
5
- import joblib
6
- import pandas as pd
7
- import numpy as np
8
- #importer les encoders
9
- encoder0 = joblib.load('job.joblib')
10
- encoder1 = joblib.load('marital.joblib')
11
- encoder2 = joblib.load('education.joblib')
12
- encoder3 = joblib.load('housing.joblib')
13
- encoder4 = joblib.load('loan.joblib')
14
- encoder5 = joblib.load('contact.joblib')
15
- encoder6 = joblib.load('month.joblib')
16
- encoder7 = joblib.load('day_of_week.joblib')
17
- encoder8 = joblib.load('poutcome.joblib')
18
-
19
- #Importer les listes
20
-
21
- job = joblib.load('job_list.joblib')
22
- marital = joblib.load('marital_list.joblib')
23
- education = joblib.load('education_list.joblib')
24
- housing = joblib.load('housing_list.joblib')
25
- loan = joblib.load('loan_list.joblib')
26
- contact = joblib.load('contact_list.joblib')
27
- month = joblib.load('month_list.joblib')
28
- day_of_week = joblib.load('day_of_week_list.joblib')
29
- poutcome = joblib.load('poutcome_list.joblib')
30
- y = joblib.load('y_list.joblib')
31
-
32
-
33
- #importer le model
34
- gb_model = joblib.load('gb_model.joblib')
35
- #importer le normaliser
36
- scaler = joblib.load('scaler.joblib')
37
-
38
- def pred_fun(age,job,marital,education,housing,loan,contact,month,day_of_week,duration,campaign,pdays,previous,poutcome):
39
- #Encoder les variables marque, transmission, quartier
40
- job = encoder0.transform([job])[0]
41
- marital = encoder1.transform([marital])[0]
42
- education = encoder2.transform([education])[0]
43
- housing = encoder3.transform([housing])[0]
44
- loan = encoder4.transform([loan])[0]
45
- contact = encoder5.transform([contact])[0]
46
- month = encoder6.transform([month])[0]
47
- day_of_week = encoder7.transform([day_of_week])[0]
48
- poutcome = encoder8.transform([poutcome])[0]
49
-
50
-
51
-
52
- #vecteurs des valeurs numerique
53
- x_new = np.array( (age,job,marital,education,housing,loan,contact,month,day_of_week,duration,campaign,pdays,previous,poutcome))
54
- x_new = x_new.reshape(1, -1)
55
- #normaliser les données
56
- x_new = scaler.transform(x_new)
57
- #predire
58
- y_pred = gb_model.predict(x_new)
59
- #arrondir
60
- y_pred = round(y_pred[0], 2)
61
- return f"{'Yes'if y_pred == 1 else 'no'}"
62
-
63
-
64
- def pred_fun_csv(file):
65
- #lire le fichier csv
66
- df = pd.read_csv(file)
67
- predictions = []
68
- for row in df.iloc[:, :].values:
69
- # new_row = np.array([
70
- # encoder0.transform([row[0]])[0],
71
- # row[1],
72
- # encoder1.transform([row[2]])[0],
73
- # row[3],
74
- # encoder2.transform([row[4]])[0]
75
- # ])
76
- # new_row = new_row.reshape(1, -1)
77
- # new_row = scaler.transform(new_row)
78
-
79
- # y_pred = gb_model.predict(new_row)
80
- predictions.append(pred_fun(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
81
- df['y'] = predictions
82
- df.to_csv('predictions.csv', index = False)
83
- return 'predictions.csv'
84
-
85
-
86
- demo = gr.Blocks(theme = gr.themes.Monochrome())
87
- #creer les inputes
88
- inputs = [
89
- gr.Number(label = 'age'),
90
- gr.Dropdown(choices = job, label = 'job'),
91
- gr.Dropdown(choices = marital, label = 'martial'),
92
- gr.Dropdown(choices = education, label = 'education'),
93
- gr.Dropdown(choices = housing, label = 'housing'),
94
- gr.Dropdown(choices = loan, label = 'loan'),
95
- gr.Dropdown(choices = contact, label = 'contact'),
96
- gr.Dropdown(choices = month, label = 'month'),
97
- gr.Dropdown(choices = day_of_week, label = 'day_of_week'),
98
- gr.Number(label = 'duration'),
99
- gr.Number(label = 'campaign'),
100
- gr.Number(label = 'pdays'),
101
- gr.Number(label = 'previous'),
102
- gr.Dropdown(choices = poutcome, label = 'poutcome')
103
- ]
104
-
105
- Outputs = gr.Textbox(label = 'Etat')
106
-
107
- interface1 = gr.Interface(
108
- fn = pred_fun,
109
- inputs = inputs,
110
- outputs = Outputs,
111
- title = "Saisir les donnees",
112
- description = """Cette modele predi si un client va s'ouscrire ou non a partir de quelques informations"""
113
- )
114
-
115
- interface2 = gr.Interface(
116
- fn = pred_fun_csv,
117
- inputs = gr.File(label = 'Televerser le fichier csv'),
118
- outputs = gr.File(label = 'Telecharger le ficher csv'),
119
- title = "Televerser un fichier csv",
120
- description = """Cette modele predi si une voiture est une voiture d'occasion ou bien si elle est Venante a partir de quelques informations"""
121
- )
122
-
123
- with demo:
124
- gr.TabbedInterface([interface1, interface2], ['simple prediction', 'multiple predictions'])
125
-
126
  demo.launch()
 
1
+
2
+ #Deployement avec Gradio
3
+ #fonctions de predictions
4
+ import gradio as gr
5
+ import joblib
6
+ import pandas as pd
7
+ import numpy as np
8
+ #importer les encoders
9
+ encoder0 = joblib.load('job.joblib')
10
+ encoder1 = joblib.load('marital.joblib')
11
+ encoder2 = joblib.load('education.joblib')
12
+ encoder3 = joblib.load('housing.joblib')
13
+ encoder4 = joblib.load('loan.joblib')
14
+ encoder5 = joblib.load('contact.joblib')
15
+ encoder6 = joblib.load('month.joblib')
16
+ encoder7 = joblib.load('day_of_week.joblib')
17
+ encoder8 = joblib.load('poutcome.joblib')
18
+
19
+ #Importer les listes
20
+
21
+ job = joblib.load('job_list.joblib')
22
+ marital = joblib.load('marital_list.joblib')
23
+ education = joblib.load('education_list.joblib')
24
+ housing = joblib.load('housing_list.joblib')
25
+ loan = joblib.load('loan_list.joblib')
26
+ contact = joblib.load('contact_list.joblib')
27
+ month = joblib.load('month_list.joblib')
28
+ day_of_week = joblib.load('day_of_week_list.joblib')
29
+ poutcome = joblib.load('poutcome_list.joblib')
30
+ y = joblib.load('y_list.joblib')
31
+
32
+
33
+ #importer le model
34
+ gb_model = joblib.load('gb_model.joblib')
35
+ #importer le normaliser
36
+ scaler = joblib.load('scaler.joblib')
37
+
38
+ def pred_fun(age,job,marital,education,housing,loan,contact,month,day_of_week,duration,campaign,pdays,previous,poutcome):
39
+ #Encoder les variables marque, transmission, quartier
40
+ job = encoder0.transform([job])[0]
41
+ marital = encoder1.transform([marital])[0]
42
+ education = encoder2.transform([education])[0]
43
+ housing = encoder3.transform([housing])[0]
44
+ loan = encoder4.transform([loan])[0]
45
+ contact = encoder5.transform([contact])[0]
46
+ month = encoder6.transform([month])[0]
47
+ day_of_week = encoder7.transform([day_of_week])[0]
48
+ poutcome = encoder8.transform([poutcome])[0]
49
+
50
+
51
+
52
+ #vecteurs des valeurs numerique
53
+ x_new = np.array( (age,job,marital,education,housing,loan,contact,month,day_of_week,duration,campaign,pdays,previous,poutcome))
54
+ x_new = x_new.reshape(1, -1)
55
+ #normaliser les données
56
+ x_new = scaler.transform(x_new)
57
+ #predire
58
+ y_pred = gb_model.predict(x_new)
59
+ #arrondir
60
+ y_pred = round(y_pred[0], 2)
61
+ return f"{'Yes'if y_pred == 1 else 'no'}"
62
+
63
+
64
+ def pred_fun_csv(file):
65
+ #lire le fichier csv
66
+ df = pd.read_csv(file)
67
+ predictions = []
68
+ for row in df.iloc[:, :].values:
69
+ predictions.append(pred_fun(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13]))
70
+ df['y'] = predictions
71
+ df.to_csv('predictions.csv', index = False)
72
+ return 'predictions.csv'
73
+
74
+
75
+ demo = gr.Blocks(theme = gr.themes.Monochrome())
76
+ #creer les inputes
77
+ inputs = [
78
+ gr.Number(label = 'age'),
79
+ gr.Dropdown(choices = job, label = 'job'),
80
+ gr.Dropdown(choices = marital, label = 'martial'),
81
+ gr.Dropdown(choices = education, label = 'education'),
82
+ gr.Dropdown(choices = housing, label = 'housing'),
83
+ gr.Dropdown(choices = loan, label = 'loan'),
84
+ gr.Dropdown(choices = contact, label = 'contact'),
85
+ gr.Dropdown(choices = month, label = 'month'),
86
+ gr.Dropdown(choices = day_of_week, label = 'day_of_week'),
87
+ gr.Number(label = 'duration'),
88
+ gr.Number(label = 'campaign'),
89
+ gr.Number(label = 'pdays'),
90
+ gr.Number(label = 'previous'),
91
+ gr.Dropdown(choices = poutcome, label = 'poutcome')
92
+ ]
93
+
94
+ Outputs = gr.Textbox(label = 'Etat')
95
+
96
+ interface1 = gr.Interface(
97
+ fn = pred_fun,
98
+ inputs = inputs,
99
+ outputs = Outputs,
100
+ title = "Saisir les donnees",
101
+ description = """Cette modele predi si un client va s'ouscrire ou non a partir de quelques informations"""
102
+ )
103
+
104
+ interface2 = gr.Interface(
105
+ fn = pred_fun_csv,
106
+ inputs = gr.File(label = 'Televerser le fichier csv'),
107
+ outputs = gr.File(label = 'Telecharger le ficher csv'),
108
+ title = "Televerser un fichier csv",
109
+ description = """Cette modele predi si un client va s'ouscrire ou non a partir de quelques informations"""
110
+ )
111
+
112
+ with demo:
113
+ gr.TabbedInterface([interface1, interface2], ['simple prediction', 'multiple predictions'])
114
+
 
 
 
 
 
 
 
 
 
 
 
115
  demo.launch()