Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +138 -0
- model_bt.joblib +3 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# +++
|
2 |
+
import os
|
3 |
+
import uuid
|
4 |
+
import joblib
|
5 |
+
import json
|
6 |
+
|
7 |
+
# IMPORTANT: I already installed the package "gradio" in my current Virtual Environment (VEnvDSDIL_gpu_Py3.12) as: pip install -q gradio_client
|
8 |
+
# Do NOT install "gradio_client" package again in Anaconda otherwise it will mess up the package.
|
9 |
+
import gradio as gr
|
10 |
+
import pandas as pd
|
11 |
+
|
12 |
+
# must install the package "huggingface_hub" first in the current python Virtual Environment, with pip, not with conda, as follows
|
13 |
+
# pip install huggingface_hub
|
14 |
+
# i.e., in the command line interface within the activated Virtual Environment:
|
15 |
+
# (VEnvDSDIL_gpu_Py3.12) epalvarez@DSDILmStation01:~ $ pip install huggingface_hub
|
16 |
+
from huggingface_hub import CommitScheduler
|
17 |
+
from pathlib import Path
|
18 |
+
|
19 |
+
# path = Path.cwd()
|
20 |
+
|
21 |
+
log_file = Path("logs/") / f"data_{uuid.uuid4()}.json"
|
22 |
+
log_folder = log_file.parent
|
23 |
+
|
24 |
+
hf_token = os.environ.get('HF_TOKEN')
|
25 |
+
print(hf_token)
|
26 |
+
|
27 |
+
# Scheduler will log every 2 API calls:
|
28 |
+
scheduler = CommitScheduler(
|
29 |
+
repo_id="term-deposit-logs",
|
30 |
+
repo_type="dataset",
|
31 |
+
folder_path=log_folder,
|
32 |
+
path_in_repo="data",
|
33 |
+
every=2
|
34 |
+
)
|
35 |
+
|
36 |
+
term_deposit_predictor = joblib.load('model_bt.joblib')
|
37 |
+
|
38 |
+
age_input = gr.Number(label="Age")
|
39 |
+
duration_input = gr.Number(label='Duration(Sec)')
|
40 |
+
cc_contact_freq_input = gr.Number(label='CC Contact Freq')
|
41 |
+
days_since_pc_input = gr.Number(label='Days Since PC')
|
42 |
+
pc_contact_freq_input = gr.Number(label='PC Contact Freq')
|
43 |
+
job_input = gr.Dropdown(['admin.', 'blue-collar', 'technician', 'services', 'management',
|
44 |
+
'retired', 'entrepreneur', 'self-employed', 'housemaid', 'unemployed',
|
45 |
+
'student', 'unknown'], label="Job")
|
46 |
+
marital_status_input = gr.Dropdown(['married', 'single', 'divorced', 'unknown'], label='Marital Status')
|
47 |
+
education_input = gr.Dropdown(['experience', 'university degree', 'high school', 'professional.course',
|
48 |
+
'Others', 'illiterate'], label='Education')
|
49 |
+
defaulter_input = gr.Dropdown(['no', 'unknown', 'yes'], label='Defaulter')
|
50 |
+
home_loan_input = gr.Dropdown(['yes', 'no', 'unknown'], label='Home Loan')
|
51 |
+
personal_loan_input = gr.Dropdown(['yes', 'no', 'unknown'], label='Personal Loan')
|
52 |
+
communication_type_input = gr.Dropdown(['cellular', 'telephone'], label='Communication Type')
|
53 |
+
last_contacted_input = gr.Dropdown(['mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], label='Last Contacted')
|
54 |
+
day_of_week_input = gr.Dropdown(['mon', 'tue', 'wed', 'thu', 'fri'], label='Day of Week')
|
55 |
+
pc_outcome_input = gr.Dropdown(['nonexistent', 'failure', 'success'], label='PC Outcome')
|
56 |
+
|
57 |
+
|
58 |
+
model_output = gr.Label(label="Subscribed")
|
59 |
+
|
60 |
+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------
|
61 |
+
def predict_term_deposit(age, duration, cc_contact_freq, days_since_pc, pc_contact_freq, job, marital_status, education,
|
62 |
+
defaulter, home_loan, personal_loan, communication_type, last_contacted,
|
63 |
+
day_of_week, pc_outcome):
|
64 |
+
sample = {
|
65 |
+
'Age': age,
|
66 |
+
'Duration(Sec)': duration,
|
67 |
+
'CC Contact Freq': cc_contact_freq,
|
68 |
+
'Days Since PC': days_since_pc,
|
69 |
+
'PC Contact Freq': pc_contact_freq,
|
70 |
+
'Job': job,
|
71 |
+
'Marital Status': marital_status,
|
72 |
+
'Education': education,
|
73 |
+
'Defaulter': defaulter,
|
74 |
+
'Home Loan': home_loan,
|
75 |
+
'Personal Loan': personal_loan,
|
76 |
+
'Communication Type': communication_type,
|
77 |
+
'Last Contacted': last_contacted,
|
78 |
+
'Day of Week': day_of_week,
|
79 |
+
'PC Outcome': pc_outcome,
|
80 |
+
}
|
81 |
+
data_point = pd.DataFrame([sample])
|
82 |
+
prediction = term_deposit_predictor.predict(data_point).tolist()
|
83 |
+
|
84 |
+
# Push prediction to a dataset repo for logging
|
85 |
+
# Each time we get a prediction we will determine if we should log it to a hugging_face dataset according to the schedule definition outside this function
|
86 |
+
with scheduler.lock:
|
87 |
+
with log_file.open("a") as f:
|
88 |
+
f.write(json.dumps(
|
89 |
+
{
|
90 |
+
'Age': age,
|
91 |
+
'Duration(Sec)': duration,
|
92 |
+
'CC Contact Freq': cc_contact_freq,
|
93 |
+
'Days Since PC': days_since_pc,
|
94 |
+
'PC Contact Freq': pc_contact_freq,
|
95 |
+
'Job': job,
|
96 |
+
'Marital Status': marital_status,
|
97 |
+
'Education': education,
|
98 |
+
'Defaulter': defaulter,
|
99 |
+
'Home Loan': home_loan,
|
100 |
+
'Personal Loan': personal_loan,
|
101 |
+
'Communication Type': communication_type,
|
102 |
+
'Last Contacted': last_contacted,
|
103 |
+
'Day of Week': day_of_week,
|
104 |
+
'PC Outcome': pc_outcome,
|
105 |
+
'prediction': prediction[0]
|
106 |
+
}
|
107 |
+
))
|
108 |
+
f.write("\n")
|
109 |
+
|
110 |
+
return prediction[0]
|
111 |
+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------
|
112 |
+
|
113 |
+
demo = gr.Interface(
|
114 |
+
fn=predict_term_deposit,
|
115 |
+
inputs=[age_input,
|
116 |
+
duration_input,
|
117 |
+
cc_contact_freq_input,
|
118 |
+
days_since_pc_input,
|
119 |
+
pc_contact_freq_input,
|
120 |
+
job_input,
|
121 |
+
marital_status_input,
|
122 |
+
education_input,
|
123 |
+
defaulter_input,
|
124 |
+
home_loan_input,
|
125 |
+
personal_loan_input,
|
126 |
+
communication_type_input,
|
127 |
+
last_contacted_input,
|
128 |
+
day_of_week_input,
|
129 |
+
pc_outcome_input],
|
130 |
+
outputs=model_output,
|
131 |
+
title="Term Deposit Prediction",
|
132 |
+
description="This API allows you to predict the person who are going to likely subscribe to the term deposit",
|
133 |
+
allow_flagging="auto", # automatically push to the HuggingFace Dataset
|
134 |
+
concurrency_limit=8
|
135 |
+
)
|
136 |
+
|
137 |
+
demo.queue()
|
138 |
+
demo.launch(share=False) # To create a public link, set "share=True" in launch() .... but if I execute this app.py locally, then I have to have my computer on for the public users to access the browser interface
|
model_bt.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e319c10defff26d0aacf01d0e705911d9fe897b788c1fd32b45913c5cf7410e8
|
3 |
+
size 9335
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
scikit-learn==1.5.0
|
2 |
+
joblib=1.4.0
|
3 |
+
pandas==2.2.2
|
4 |
+
numpy==2.0.0
|