nethke0009 commited on
Commit
8a76912
1 Parent(s): 9e9465a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -124
app.py CHANGED
@@ -1,124 +1,125 @@
1
- import os
2
- import uuid
3
- import joblib
4
- import json
5
-
6
- import gradio as gr
7
- import pandas as pd
8
-
9
- from huggingface_hub import CommitScheduler
10
- from pathlib import Path
11
-
12
-
13
- os.system("python train.py")
14
-
15
- term_deposit_predictor = joblib.load('model.joblib')
16
-
17
- log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
18
- log_folder = log_file.parent
19
-
20
- scheduler = CommitScheduler(
21
- repo_id="term-deposit-logs",
22
- repo_type="dataset",
23
- folder_path=log_folder,
24
- path_in_repo="data",
25
- every=2
26
- )
27
-
28
- age_input = gr.Number(label="Age")
29
- duration_input = gr.Number(label='Duration(Sec)')
30
- cc_contact_freq_input = gr.Number(label='CC Contact Freq')
31
- days_since_pc_input = gr.Number(label='Days Since PC')
32
- pc_contact_freq_input = gr.Number(label='Pc Contact Freq')
33
- job_input = gr.Dropdown(['admin.', 'blue-collar', 'technician', 'services', 'management',
34
- 'retired', 'entrepreneur', 'self-employed', 'housemaid', 'unemployed',
35
- 'student', 'unknown'],label="Job")
36
- marital_input = gr.Dropdown(['married', 'single', 'divorced', 'unknown'],label='Marital Status')
37
- education_input = gr.Dropdown(['experience', 'university degree', 'high school', 'professional.course',
38
- 'Others', 'illiterate'],label='Education')
39
- defaulter_input = gr.Dropdown(['no', 'unknown', 'yes'],label='Defaulter')
40
- home_loan_input = gr.Dropdown(['yes', 'no', 'unknown'],label='Home Loan')
41
- personal_loan_input = gr.Dropdown(['yes', 'no', 'unknown'],label='Personal Loan')
42
- communication_type_input = gr.Dropdown(['cellular', 'telephone'],label='Communication Type')
43
- last_contacted_input = gr.Dropdown(['may', 'jul', 'aug', 'jun', 'nov', 'apr', 'oct', 'mar', 'sep', 'dec'],label='Last Contacted')
44
- day_of_week_input = gr.Dropdown(['thu', 'mon', 'wed', 'tue', 'fri'],label='Day of Week')
45
- pc_outcome_input = gr.Dropdown(['nonexistent', 'failure', 'success'], label='PC Outcome')
46
-
47
-
48
- model_output = gr.Label(label="Subscribed")
49
-
50
- def predict_term_deposit(age, duration, cc_contact_freq, days_since_pc, pc_contact_freq, job, marital_status, education,
51
- defaulter, home_loan, personal_loan, communication_type, last_contacted,
52
- day_of_week, pc_outcome):
53
- sample = {
54
- 'Age': age,
55
- 'Duration(Sec)': duration,
56
- 'CC Contact Freq': cc_contact_freq,
57
- 'Days Since PC': days_since_pc,
58
- 'PC Contact Freq': pc_contact_freq,
59
- 'Job': job,
60
- 'Marital Status': marital_status,
61
- 'Education': education,
62
- 'Defaulter': defaulter,
63
- 'Home Loan': home_loan,
64
- 'Personal Loan': personal_loan,
65
- 'Communication Type': communication_type,
66
- 'Last Contacted': last_contacted,
67
- 'Day of Week': day_of_week,
68
- 'PC Outcome': pc_outcome,
69
- }
70
- data_point = pd.DataFrame([sample])
71
- prediction = term_deposit_predictor.predict(data_point).tolist()
72
-
73
- with scheduler.lock:
74
- with log_file.open("a") as f:
75
- f.write(json.dumps(
76
- {
77
- 'Age': age,
78
- 'Duration(Sec)': duration,
79
- 'CC Contact Freq': cc_contact_freq,
80
- 'Days Since PC': days_since_pc,
81
- 'PC Contact Freq': pc_contact_freq,
82
- 'Job': job,
83
- 'Marital Status': marital_status,
84
- 'Education': education,
85
- 'Defaulter': defaulter,
86
- 'Home Loan': home_loan,
87
- 'Personal Loan': personal_loan,
88
- 'Communication Type': communication_type,
89
- 'Last Month Contacted': last_contacted,
90
- 'Day of Week': day_of_week,
91
- 'PC Outcome': pc_outcome,
92
- 'prediction': prediction[0]
93
- }
94
- ))
95
- f.write("\n")
96
-
97
- return prediction[0]
98
-
99
- demo = gr.Interface(
100
- fn=predict_term_deposit,
101
- inputs=[age_input,
102
- duration_input,
103
- cc_contact_freq_input,
104
- days_since_pc_input,
105
- pc_contact_freq_input,
106
- job_input,
107
- marital_input,
108
- education_input,
109
- defaulter_input,
110
- home_loan_input,
111
- personal_loan_input,
112
- communication_type_input,
113
- last_contacted_input,
114
- day_of_week_input,
115
- pc_outcome_input],
116
- outputs=model_output,
117
- title="Term Deposit Prediction",
118
- description="This API allows you to predict the person who are going to likely subscribe the term deposit",
119
- allow_flagging="auto",
120
- concurrency_limit=10
121
- )
122
-
123
- demo.queue()
124
- demo.launch(share=False)
 
 
1
+ import os
2
+ import uuid
3
+ import joblib
4
+ import json
5
+
6
+ import gradio as gr
7
+ import pandas as pd
8
+
9
+ from huggingface_hub import CommitScheduler
10
+ from pathlib import Path
11
+
12
+
13
+ os.system("python train.py")
14
+
15
+ term_deposit_predictor = joblib.load('model.joblib')
16
+
17
+ log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
18
+ log_folder = log_file.parent
19
+
20
+ scheduler = CommitScheduler(
21
+ repo_id="term-deposit-logs",
22
+ repo_type="dataset",
23
+ folder_path=log_folder,
24
+ path_in_repo="data",
25
+ every=2,
26
+ token=HF_Telemarketing
27
+ )
28
+
29
+ age_input = gr.Number(label="Age")
30
+ duration_input = gr.Number(label='Duration(Sec)')
31
+ cc_contact_freq_input = gr.Number(label='CC Contact Freq')
32
+ days_since_pc_input = gr.Number(label='Days Since PC')
33
+ pc_contact_freq_input = gr.Number(label='Pc Contact Freq')
34
+ job_input = gr.Dropdown(['admin.', 'blue-collar', 'technician', 'services', 'management',
35
+ 'retired', 'entrepreneur', 'self-employed', 'housemaid', 'unemployed',
36
+ 'student', 'unknown'],label="Job")
37
+ marital_input = gr.Dropdown(['married', 'single', 'divorced', 'unknown'],label='Marital Status')
38
+ education_input = gr.Dropdown(['experience', 'university degree', 'high school', 'professional.course',
39
+ 'Others', 'illiterate'],label='Education')
40
+ defaulter_input = gr.Dropdown(['no', 'unknown', 'yes'],label='Defaulter')
41
+ home_loan_input = gr.Dropdown(['yes', 'no', 'unknown'],label='Home Loan')
42
+ personal_loan_input = gr.Dropdown(['yes', 'no', 'unknown'],label='Personal Loan')
43
+ communication_type_input = gr.Dropdown(['cellular', 'telephone'],label='Communication Type')
44
+ last_contacted_input = gr.Dropdown(['may', 'jul', 'aug', 'jun', 'nov', 'apr', 'oct', 'mar', 'sep', 'dec'],label='Last Contacted')
45
+ day_of_week_input = gr.Dropdown(['thu', 'mon', 'wed', 'tue', 'fri'],label='Day of Week')
46
+ pc_outcome_input = gr.Dropdown(['nonexistent', 'failure', 'success'], label='PC Outcome')
47
+
48
+
49
+ model_output = gr.Label(label="Subscribed")
50
+
51
+ def predict_term_deposit(age, duration, cc_contact_freq, days_since_pc, pc_contact_freq, job, marital_status, education,
52
+ defaulter, home_loan, personal_loan, communication_type, last_contacted,
53
+ day_of_week, pc_outcome):
54
+ sample = {
55
+ 'Age': age,
56
+ 'Duration(Sec)': duration,
57
+ 'CC Contact Freq': cc_contact_freq,
58
+ 'Days Since PC': days_since_pc,
59
+ 'PC Contact Freq': pc_contact_freq,
60
+ 'Job': job,
61
+ 'Marital Status': marital_status,
62
+ 'Education': education,
63
+ 'Defaulter': defaulter,
64
+ 'Home Loan': home_loan,
65
+ 'Personal Loan': personal_loan,
66
+ 'Communication Type': communication_type,
67
+ 'Last Contacted': last_contacted,
68
+ 'Day of Week': day_of_week,
69
+ 'PC Outcome': pc_outcome,
70
+ }
71
+ data_point = pd.DataFrame([sample])
72
+ prediction = term_deposit_predictor.predict(data_point).tolist()
73
+
74
+ with scheduler.lock:
75
+ with log_file.open("a") as f:
76
+ f.write(json.dumps(
77
+ {
78
+ 'Age': age,
79
+ 'Duration(Sec)': duration,
80
+ 'CC Contact Freq': cc_contact_freq,
81
+ 'Days Since PC': days_since_pc,
82
+ 'PC Contact Freq': pc_contact_freq,
83
+ 'Job': job,
84
+ 'Marital Status': marital_status,
85
+ 'Education': education,
86
+ 'Defaulter': defaulter,
87
+ 'Home Loan': home_loan,
88
+ 'Personal Loan': personal_loan,
89
+ 'Communication Type': communication_type,
90
+ 'Last Month Contacted': last_contacted,
91
+ 'Day of Week': day_of_week,
92
+ 'PC Outcome': pc_outcome,
93
+ 'prediction': prediction[0]
94
+ }
95
+ ))
96
+ f.write("\n")
97
+
98
+ return prediction[0]
99
+
100
+ demo = gr.Interface(
101
+ fn=predict_term_deposit,
102
+ inputs=[age_input,
103
+ duration_input,
104
+ cc_contact_freq_input,
105
+ days_since_pc_input,
106
+ pc_contact_freq_input,
107
+ job_input,
108
+ marital_input,
109
+ education_input,
110
+ defaulter_input,
111
+ home_loan_input,
112
+ personal_loan_input,
113
+ communication_type_input,
114
+ last_contacted_input,
115
+ day_of_week_input,
116
+ pc_outcome_input],
117
+ outputs=model_output,
118
+ title="Term Deposit Prediction",
119
+ description="This API allows you to predict the person who are going to likely subscribe the term deposit",
120
+ allow_flagging="auto",
121
+ concurrency_limit=10
122
+ )
123
+
124
+ demo.queue()
125
+ demo.launch(share=False)