import gradio as gr import pandas as pd import joblib import os # Load the models preprocessor_path = os.path.join('.', 'preprocessor.joblib') model_path = os.path.join('.', 'Best_model.joblib') preprocessor = joblib.load(preprocessor_path) best_model = joblib.load(model_path) # Define prediction function def predict(age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed): # Create a DataFrame for the input data data = pd.DataFrame({ 'age': [age], 'job': [job], 'marital': [marital], 'education': [education], 'default': [default], 'housing': [housing], 'loan': [loan], 'contact': [contact], 'month': [month], 'day_of_week': [day_of_week], 'duration': [duration], 'campaign': [campaign], 'pdays': [pdays], 'previous': [previous], 'poutcome': [poutcome], 'emp.var.rate': [emp_var_rate], 'cons.price.idx': [cons_price_idx], 'cons.conf.idx': [cons_conf_idx], 'euribor3m': [euribor3m], 'nr.employed': [nr_employed] }) # Preprocess the data preprocessed_data = preprocessor.transform(data) # Make predictions prediction = best_model.predict(preprocessed_data) probability = best_model.predict_proba(preprocessed_data) return { "Prediction": prediction[0], "Probability (Yes)": probability[0][1], "Probability (No)": probability[0][0] } # Define the interface def gradio_interface(): with gr.Blocks() as app: gr.Markdown("# Term Deposit Subscription Prediction") with gr.Row(): age = gr.Number(label="Age", value=30) job = gr.Dropdown(["housemaid", "services", "admin.", "blue-collar", "technician", "retired", "management", "unemployed", "self-employed", "unknown", "entrepreneur", "student"], label="Job") marital = gr.Dropdown(["married", "single", "divorced", "unknown"], label="Marital Status") education = gr.Dropdown(["basic.4y", "high.school", "basic.6y", "basic.9y", "professional.course", "unknown", "university.degree", "illiterate", "tertiary", "secondary", "primary"], label="Education") with gr.Row(): default = gr.Dropdown(["no", "unknown", "yes"], label="Default") housing = gr.Dropdown(["no", "yes", "unknown"], label="Housing Loan") loan = gr.Dropdown(["no", "yes", "unknown"], label="Personal Loan") contact = gr.Dropdown(["telephone", "cellular", "unknown"], label="Contact Type") with gr.Row(): month = gr.Dropdown(["may", "jun", "jul", "aug", "oct", "nov", "dec", "mar", "apr", "sep", "jan", "feb"], label="Month") day_of_week = gr.Dropdown(["mon", "tue", "wed", "thu", "fri"], label="Day of Week") with gr.Row(): duration = gr.Number(label="Call Duration (seconds)", value=100) campaign = gr.Number(label="Number of Contacts during Campaign", value=1) pdays = gr.Number(label="Days since Last Contact", value=999) previous = gr.Number(label="Number of Contacts before Campaign", value=0) poutcome = gr.Dropdown(["nonexistent", "failure", "success", "unknown", "other"], label="Previous Outcome") with gr.Row(): emp_var_rate = gr.Number(label="Employment Variation Rate", value=1.1) cons_price_idx = gr.Number(label="Consumer Price Index", value=93.994) cons_conf_idx = gr.Number(label="Consumer Confidence Index", value=-36.4) euribor3m = gr.Number(label="Euribor 3-Month Rate", value=4.857) nr_employed = gr.Number(label="Number of Employees", value=5191.0) predict_btn = gr.Button("Predict") output = gr.JSON() predict_btn.click( predict, inputs=[age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed], outputs=output ) return app # Launch the app if __name__ == "__main__": app = gradio_interface() app.launch()