Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pickle
|
| 4 |
+
import xgboost as xgb
|
| 5 |
+
|
| 6 |
+
# 1. LOAD THE PICKLE FILE
|
| 7 |
+
# Ensure 'model.pkl' is uploaded to your Hugging Face Space files
|
| 8 |
+
with open("model.pkl", "rb") as f:
|
| 9 |
+
model = pickle.load(f)
|
| 10 |
+
|
| 11 |
+
# 2. FULL FEATURE LIST (Ordered exactly as per your screenshot)
|
| 12 |
+
ALL_FEATURES = [
|
| 13 |
+
'InternetService_1', 'Contract_0', 'tenure', 'InternetService_0',
|
| 14 |
+
'Contract_1', 'MultipleLines_1', 'PaperlessBilling_0', 'StreamingMovies_1',
|
| 15 |
+
'SeniorCitizen', 'PaymentMethod_0', 'TotalCharges', 'StreamingTV_1',
|
| 16 |
+
'MonthlyCharges', 'PaymentMethod_1', 'OnlineSecurity_1', 'TechSupport_1',
|
| 17 |
+
'MultipleLines_0', 'PhoneService_0', 'OnlineBackup_0', 'OnlineSecurity_0',
|
| 18 |
+
'StreamingMovies_0', 'StreamingTV_0', 'DeviceProtection_0',
|
| 19 |
+
'OnlineBackup_1', 'DeviceProtection_1', 'TechSupport_0'
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def predict_churn(tenure, internet_service_fiber, contract_month_to_month):
|
| 23 |
+
# Initialize all 26 features to 0
|
| 24 |
+
input_dict = {feature: [0.0] for feature in ALL_FEATURES}
|
| 25 |
+
|
| 26 |
+
# Update the 3 features the user interacts with
|
| 27 |
+
input_dict['tenure'] = [float(tenure)]
|
| 28 |
+
input_dict['InternetService_1'] = [1.0 if internet_service_fiber else 0.0]
|
| 29 |
+
input_dict['Contract_0'] = [1.0 if contract_month_to_month else 0.0]
|
| 30 |
+
|
| 31 |
+
# IMPORTANT: Set sensible defaults for key continuous variables
|
| 32 |
+
# if they aren't provided by the user (prevents skewed results)
|
| 33 |
+
input_dict['MonthlyCharges'] = [65.0]
|
| 34 |
+
input_dict['TotalCharges'] = [2000.0]
|
| 35 |
+
|
| 36 |
+
# Create DataFrame and ensure column order matches ALL_FEATURES exactly
|
| 37 |
+
input_data = pd.DataFrame(input_dict)[ALL_FEATURES]
|
| 38 |
+
|
| 39 |
+
# 3. RUN PREDICTION
|
| 40 |
+
# We use predict_proba to get the confidence level
|
| 41 |
+
prediction_proba = model.predict_proba(input_data)[0][1]
|
| 42 |
+
prediction = "Churn Risk" if prediction_proba > 0.5 else "Stay"
|
| 43 |
+
|
| 44 |
+
return f"Result: {prediction} (Confidence: {prediction_proba:.2%})"
|
| 45 |
+
|
| 46 |
+
# 4. DEFINE THE UI
|
| 47 |
+
demo = gr.Interface(
|
| 48 |
+
fn=predict_churn,
|
| 49 |
+
inputs=[
|
| 50 |
+
gr.Slider(0, 72, label="Tenure (Months)", value=12),
|
| 51 |
+
gr.Checkbox(label="Internet Service: Fiber Optic?"),
|
| 52 |
+
gr.Checkbox(label="Contract: Month-to-Month?")
|
| 53 |
+
],
|
| 54 |
+
outputs="text",
|
| 55 |
+
title="Telco Churn Prediction Agent",
|
| 56 |
+
description="Using the model's top drivers to assess customer loyalty."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
if __name__ == "__main__":
|
| 60 |
+
demo.launch()
|