abhisheksingh100 commited on
Commit
5ec67b2
·
verified ·
1 Parent(s): 06e9f94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -31
app.py CHANGED
@@ -4,64 +4,64 @@ import pandas as pd
4
  import joblib
5
  import gradio as gr
6
 
7
- # Load saved files - make sure these are in the same folder as app.py
8
  feature_columns = joblib.load('feature_columns.pkl')
9
  num_cols = joblib.load('num_cols.pkl')
10
  scaler = joblib.load('scaler.pkl')
11
  best_model = joblib.load('best_model.pkl')
12
 
13
- # Define Gradio prediction function
14
  def predict_churn(SeniorCitizen, tenure, MonthlyCharges, TotalCharges,
15
  gender, Partner, Dependents, PhoneService, MultipleLines,
16
  InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,
17
  TechSupport, StreamingTV, StreamingMovies, Contract,
18
  PaperlessBilling, PaymentMethod):
19
  try:
20
- # Create a dict of inputs
21
  input_data = {
22
- "SeniorCitizen": SeniorCitizen,
23
- "tenure": float(tenure),
24
- "MonthlyCharges": float(MonthlyCharges),
25
- "TotalCharges": float(TotalCharges),
26
- "gender": gender,
27
- "Partner": Partner,
28
- "Dependents": Dependents,
29
- "PhoneService": PhoneService,
30
- "MultipleLines": MultipleLines,
31
- "InternetService": InternetService,
32
- "OnlineSecurity": OnlineSecurity,
33
- "OnlineBackup": OnlineBackup,
34
- "DeviceProtection": DeviceProtection,
35
- "TechSupport": TechSupport,
36
- "StreamingTV": StreamingTV,
37
- "StreamingMovies": StreamingMovies,
38
- "Contract": Contract,
39
- "PaperlessBilling": PaperlessBilling,
40
- "PaymentMethod": PaymentMethod
41
- }
42
 
43
  # Convert to DataFrame
44
  df = pd.DataFrame([input_data])
45
 
46
- # One-hot encode categorical features
47
- df_encoded = pd.get_dummies(df) 
48
 
49
- # Align columns with training features (add missing cols with 0)
50
  df_encoded = df_encoded.reindex(columns=feature_columns, fill_value=0)
51
 
52
- # Scale numerical features
53
  df_encoded[num_cols] = scaler.transform(df_encoded[num_cols])
54
 
55
- # Predict churn
56
  pred = best_model.predict(df_encoded)[0]
 
57
  return "✅ Churn: Yes" if pred == 1 else "❎ Churn: No"
58
 
59
  except Exception as e:
60
  return f"❌ Error occurred: {str(e)}"
61
 
62
- # Define Gradio interface inputs 
63
  inputs = [
64
- gr.Radio([0, 1], label="SeniorCitizen"), 
65
  gr.Textbox(label="tenure"),
66
  gr.Textbox(label="MonthlyCharges"),
67
  gr.Textbox(label="TotalCharges"),
@@ -82,7 +82,7 @@ inputs = [
82
  gr.Dropdown(["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"], label="PaymentMethod")
83
  ]
84
 
85
- # Launch Gradio Interface   
86
  interface = gr.Interface(
87
  fn=predict_churn,
88
  inputs=inputs,
 
4
  import joblib
5
  import gradio as gr
6
 
7
+ # Load saved objects (make sure these files are in the same directory as app.py)
8
  feature_columns = joblib.load('feature_columns.pkl')
9
  num_cols = joblib.load('num_cols.pkl')
10
  scaler = joblib.load('scaler.pkl')
11
  best_model = joblib.load('best_model.pkl')
12
 
 
13
  def predict_churn(SeniorCitizen, tenure, MonthlyCharges, TotalCharges,
14
  gender, Partner, Dependents, PhoneService, MultipleLines,
15
  InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,
16
  TechSupport, StreamingTV, StreamingMovies, Contract,
17
  PaperlessBilling, PaymentMethod):
18
  try:
19
+ # Prepare input data as a dictionary
20
  input_data = {
21
+ "SeniorCitizen": SeniorCitizen,
22
+ "tenure": float(tenure),
23
+ "MonthlyCharges": float(MonthlyCharges),
24
+ "TotalCharges": float(TotalCharges),
25
+ "gender": gender,
26
+ "Partner": Partner,
27
+ "Dependents": Dependents,
28
+ "PhoneService": PhoneService,
29
+ "MultipleLines": MultipleLines,
30
+ "InternetService": InternetService,
31
+ "OnlineSecurity": OnlineSecurity,
32
+ "OnlineBackup": OnlineBackup,
33
+ "DeviceProtection": DeviceProtection,
34
+ "TechSupport": TechSupport,
35
+ "StreamingTV": StreamingTV,
36
+ "StreamingMovies": StreamingMovies,
37
+ "Contract": Contract,
38
+ "PaperlessBilling": PaperlessBilling,
39
+ "PaymentMethod": PaymentMethod
40
+ }
41
 
42
  # Convert to DataFrame
43
  df = pd.DataFrame([input_data])
44
 
45
+ # One-hot encode categorical variables
46
+ df_encoded = pd.get_dummies(df)
47
 
48
+ # Align with training features - fill missing columns with 0
49
  df_encoded = df_encoded.reindex(columns=feature_columns, fill_value=0)
50
 
51
+ # Scale numerical columns
52
  df_encoded[num_cols] = scaler.transform(df_encoded[num_cols])
53
 
54
+ # Make prediction
55
  pred = best_model.predict(df_encoded)[0]
56
+
57
  return "✅ Churn: Yes" if pred == 1 else "❎ Churn: No"
58
 
59
  except Exception as e:
60
  return f"❌ Error occurred: {str(e)}"
61
 
62
+ # Define Gradio inputs
63
  inputs = [
64
+ gr.Radio([0, 1], label="SeniorCitizen"),
65
  gr.Textbox(label="tenure"),
66
  gr.Textbox(label="MonthlyCharges"),
67
  gr.Textbox(label="TotalCharges"),
 
82
  gr.Dropdown(["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"], label="PaymentMethod")
83
  ]
84
 
85
+ # Create the Gradio interface
86
  interface = gr.Interface(
87
  fn=predict_churn,
88
  inputs=inputs,