bubuuunel commited on
Commit
a1e9861
1 Parent(s): 4064b1f

Upload app.py

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