Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,280 +1,246 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import numpy as np
|
| 5 |
-
import os
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
print(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
-
|
| 196 |
-
-
|
| 197 |
-
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
-
|
| 201 |
-
-
|
| 202 |
-
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
("
|
| 216 |
-
("
|
| 217 |
-
("
|
| 218 |
-
("
|
| 219 |
-
("
|
| 220 |
-
]
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
img
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
### ๐ ๏ธ Technical Stack
|
| 248 |
-
- **Machine Learning:** scikit-learn, Random Forest Classifier
|
| 249 |
-
- **Data Processing:** pandas, numpy
|
| 250 |
-
- **Visualization:** matplotlib, seaborn
|
| 251 |
-
- **Interface:** Gradio
|
| 252 |
-
- **Deployment:** Hugging Face Spaces
|
| 253 |
-
|
| 254 |
-
### ๐ Project Scope
|
| 255 |
-
This end-to-end machine learning project demonstrates:
|
| 256 |
-
- Data engineering and ETL pipeline
|
| 257 |
-
- Advanced ML model development
|
| 258 |
-
- Business intelligence and insights generation
|
| 259 |
-
- Production deployment capabilities
|
| 260 |
-
|
| 261 |
-
### ๐ฏ Learning Outcomes
|
| 262 |
-
- Real-world problem solving in telecom domain
|
| 263 |
-
- Complete ML pipeline implementation
|
| 264 |
-
- Business value creation through AI/ML
|
| 265 |
-
- Model deployment and productionization
|
| 266 |
-
|
| 267 |
-
---
|
| 268 |
-
|
| 269 |
-
**๐ Project Status:** Complete | **๐
Last Updated:** October 2024 | **๐ข Version:** 1.0.0
|
| 270 |
-
""")
|
| 271 |
-
|
| 272 |
-
# Footer
|
| 273 |
-
gr.Markdown("""
|
| 274 |
-
---
|
| 275 |
-
**ยฉ 2024 BRBRAITT Group 5 | TIRTC Advance AI/ML Training | Telecom Data Analytics**
|
| 276 |
-
""")
|
| 277 |
-
|
| 278 |
-
# Launch the app
|
| 279 |
-
if __name__ == "__main__":
|
| 280 |
-
app.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import google.generativeai as genai
|
| 8 |
+
|
| 9 |
+
# --- Gemini API Configuration ---
|
| 10 |
+
# IMPORTANT: Set your GOOGLE_API_KEY as an environment variable
|
| 11 |
+
# For local testing, you can uncomment the line below and paste your key
|
| 12 |
+
# os.environ['GOOGLE_API_KEY'] = "YOUR_GOOGLE_API_KEY"
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
| 16 |
+
if GOOGLE_API_KEY:
|
| 17 |
+
genai.configure(api_key=GOOGLE_API_KEY)
|
| 18 |
+
else:
|
| 19 |
+
print("Warning: GOOGLE_API_KEY not found. Gemini error handling will be disabled.")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error configuring Gemini: {e}")
|
| 22 |
+
|
| 23 |
+
# --- Model and Asset Loading ---
|
| 24 |
+
|
| 25 |
+
def load_models():
|
| 26 |
+
"""Loads the ML model, encoders, and scaler from disk."""
|
| 27 |
+
try:
|
| 28 |
+
model = joblib.load('models/churn_model.pkl')
|
| 29 |
+
encoders = joblib.load('models/label_encoders.pkl')
|
| 30 |
+
scaler = joblib.load('models/scaler.pkl')
|
| 31 |
+
return model, encoders, scaler
|
| 32 |
+
except FileNotFoundError:
|
| 33 |
+
print("Error: Model files not found. Please ensure 'churn_model.pkl', 'label_encoders.pkl', and 'scaler.pkl' are in the 'models/' directory.")
|
| 34 |
+
return None, None, None
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"An unexpected error occurred while loading models: {e}")
|
| 37 |
+
return None, None, None
|
| 38 |
+
|
| 39 |
+
model, encoders, scaler = load_models()
|
| 40 |
+
|
| 41 |
+
def load_image(image_name):
|
| 42 |
+
"""Loads an image from the 'images' folder."""
|
| 43 |
+
try:
|
| 44 |
+
img_path = os.path.join("images", image_name)
|
| 45 |
+
return Image.open(img_path) if os.path.exists(img_path) else None
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print(f"Error loading image {image_name}: {e}")
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
# --- Feature Constants ---
|
| 51 |
+
REGIONS = ['North', 'South', 'East', 'West', 'Central']
|
| 52 |
+
PLAN_TYPES = ['Prepaid', 'Postpaid']
|
| 53 |
+
CONTRACT_TYPES = ['Month-to-month', 'One year', 'Two year']
|
| 54 |
+
COMPLAINT_STATUS = ['Open', 'Closed', 'Not Applicable']
|
| 55 |
+
PAYMENT_METHODS = ['Electronic check', 'Mailed check', 'Bank transfer', 'Credit card']
|
| 56 |
+
|
| 57 |
+
# --- Prediction Logic ---
|
| 58 |
+
|
| 59 |
+
def predict_churn(customer_id, region, plan_type, monthly_charges, total_charges,
|
| 60 |
+
tenure_months, contract_type, paperless_billing, payment_method,
|
| 61 |
+
data_usage_gb, call_minutes, sms_count, complaint_status, complaint_count):
|
| 62 |
+
"""Predicts customer churn and generates a detailed result."""
|
| 63 |
+
if model is None or encoders is None or scaler is None:
|
| 64 |
+
return "๐ด **Error:** Model components are not loaded. Please check the server logs.", 0.0
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
# 1. Create Input DataFrame
|
| 68 |
+
input_data = pd.DataFrame({
|
| 69 |
+
'region': [region],
|
| 70 |
+
'plan_type': [plan_type],
|
| 71 |
+
'monthly_charges': [float(monthly_charges)],
|
| 72 |
+
'total_charges': [float(total_charges)],
|
| 73 |
+
'tenure_months': [int(tenure_months)],
|
| 74 |
+
'contract_type': [contract_type],
|
| 75 |
+
'paperless_billing': [1 if paperless_billing else 0],
|
| 76 |
+
'payment_method': [payment_method],
|
| 77 |
+
'data_usage_gb': [float(data_usage_gb)],
|
| 78 |
+
'call_minutes': [int(call_minutes)],
|
| 79 |
+
'sms_count': [int(sms_count)],
|
| 80 |
+
'complaint_status': [complaint_status],
|
| 81 |
+
'complaint_count': [int(complaint_count)]
|
| 82 |
+
})
|
| 83 |
+
|
| 84 |
+
# 2. Encode Categorical Features
|
| 85 |
+
for col, encoder in encoders.items():
|
| 86 |
+
if col in input_data.columns:
|
| 87 |
+
input_data[col] = input_data[col].apply(lambda x: encoder.transform([x])[0] if x in encoder.classes_ else -1)
|
| 88 |
+
|
| 89 |
+
# 3. Scale Numerical Features
|
| 90 |
+
scaled_data = scaler.transform(input_data)
|
| 91 |
+
|
| 92 |
+
# 4. Make Prediction
|
| 93 |
+
churn_probability = model.predict_proba(scaled_data)[0][1]
|
| 94 |
+
|
| 95 |
+
# 5. Format Output
|
| 96 |
+
if churn_probability > 0.7:
|
| 97 |
+
risk_level = "๐ด HIGH RISK"
|
| 98 |
+
recommendation = "Immediate retention action is strongly recommended."
|
| 99 |
+
elif churn_probability > 0.4:
|
| 100 |
+
risk_level = "๐ก MEDIUM RISK"
|
| 101 |
+
recommendation = "Proactively monitor and engage with personalized offers."
|
| 102 |
+
else:
|
| 103 |
+
risk_level = "๐ข LOW RISK"
|
| 104 |
+
recommendation = "Standard service and relationship maintenance."
|
| 105 |
+
|
| 106 |
+
result = f"""
|
| 107 |
+
### Prediction for Customer `{customer_id}`
|
| 108 |
+
- **Churn Risk Level:** **{risk_level}**
|
| 109 |
+
- **Probability of Churn:** **{churn_probability:.1%}**
|
| 110 |
+
- **Recommendation:** {recommendation}
|
| 111 |
+
"""
|
| 112 |
+
return result, churn_probability
|
| 113 |
+
|
| 114 |
+
except Exception as e:
|
| 115 |
+
# --- Gemini Error Handling ---
|
| 116 |
+
print(f"Prediction error: {e}") # Log the real error for debugging
|
| 117 |
+
if GOOGLE_API_KEY:
|
| 118 |
+
try:
|
| 119 |
+
# UPDATED to use the faster Flash model
|
| 120 |
+
gemini_model = genai.GenerativeModel('gemini-2.5-flash')
|
| 121 |
+
prompt = f"""
|
| 122 |
+
An error occurred in a telecom churn prediction app. The technical error was: '{str(e)}'.
|
| 123 |
+
Generate a concise, friendly, non-technical message for the user.
|
| 124 |
+
The message should suggest they double-check their inputs (like ensuring Total Charges are not less than Monthly Charges) and try again.
|
| 125 |
+
Do not mention the technical error details. Start with 'Oops! Something went wrong.'
|
| 126 |
+
"""
|
| 127 |
+
response = gemini_model.generate_content(prompt)
|
| 128 |
+
return response.text, 0.0
|
| 129 |
+
except Exception as gemini_e:
|
| 130 |
+
print(f"Gemini API error: {gemini_e}") # Log Gemini error
|
| 131 |
+
return "An unexpected error occurred. Please verify your inputs and try again.", 0.0
|
| 132 |
+
else:
|
| 133 |
+
return "An unexpected error occurred. Please check your inputs are valid and try again.", 0.0
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
# --- Gradio UI ---
|
| 137 |
+
|
| 138 |
+
with gr.Blocks(title="Telecom Churn Prediction - BRBRAITT Group 5", theme=gr.themes.Soft()) as app:
|
| 139 |
+
# Header
|
| 140 |
+
gr.Markdown("""
|
| 141 |
+
# ๐ฎ Telecom Churn Prediction System
|
| 142 |
+
**TIRTC Course: Advance AI/ML Training (Nokia) | Institution: BRBRAITT, Jabalpur | Group 5**
|
| 143 |
+
---
|
| 144 |
+
This AI-powered system predicts customer churn with over **90% accuracy** using a Random Forest model.
|
| 145 |
+
""")
|
| 146 |
+
|
| 147 |
+
with gr.Tabs():
|
| 148 |
+
# Tab 1: Prediction Interface
|
| 149 |
+
with gr.TabItem("๐ฏ Churn Prediction"):
|
| 150 |
+
with gr.Row():
|
| 151 |
+
with gr.Column(scale=2):
|
| 152 |
+
gr.Markdown("### Enter Customer Details")
|
| 153 |
+
with gr.Row():
|
| 154 |
+
customer_id = gr.Textbox(label="Customer ID", value="CUST-001")
|
| 155 |
+
region = gr.Dropdown(choices=REGIONS, label="Region", value="North")
|
| 156 |
+
plan_type = gr.Dropdown(choices=PLAN_TYPES, label="Plan Type", value="Postpaid")
|
| 157 |
+
with gr.Row():
|
| 158 |
+
contract_type = gr.Dropdown(choices=CONTRACT_TYPES, label="Contract Type", value="Month-to-month")
|
| 159 |
+
payment_method = gr.Dropdown(choices=PAYMENT_METHODS, label="Payment Method", value="Electronic check")
|
| 160 |
+
paperless_billing = gr.Checkbox(label="Paperless Billing", value=True)
|
| 161 |
+
gr.Markdown("#### Service Usage & Charges")
|
| 162 |
+
with gr.Row():
|
| 163 |
+
monthly_charges = gr.Number(label="Monthly Charges (โน)", value=1000)
|
| 164 |
+
total_charges = gr.Number(label="Total Charges (โน)", value=12000)
|
| 165 |
+
tenure_months = gr.Number(label="Tenure (Months)", value=12)
|
| 166 |
+
with gr.Row():
|
| 167 |
+
data_usage_gb = gr.Number(label="Data Usage (GB)", value=15)
|
| 168 |
+
call_minutes = gr.Number(label="Call Minutes", value=500)
|
| 169 |
+
sms_count = gr.Number(label="SMS Count", value=100)
|
| 170 |
+
gr.Markdown("#### Customer Complaints")
|
| 171 |
+
with gr.Row():
|
| 172 |
+
complaint_status = gr.Dropdown(choices=COMPLAINT_STATUS, label="Last Complaint Status", value="Not Applicable")
|
| 173 |
+
complaint_count = gr.Number(label="Total Complaint Count", value=0)
|
| 174 |
+
|
| 175 |
+
predict_btn = gr.Button("๐ฎ Predict Churn Risk", variant="primary", size="lg")
|
| 176 |
+
|
| 177 |
+
with gr.Column(scale=1):
|
| 178 |
+
gr.Markdown("### ๐ Prediction Result")
|
| 179 |
+
prediction_output = gr.Markdown(value="*Results will be displayed here...*")
|
| 180 |
+
probability_gauge = gr.Gauge(label="Churn Probability", value=0.0, show_label=True)
|
| 181 |
+
|
| 182 |
+
predict_btn.click(
|
| 183 |
+
fn=predict_churn,
|
| 184 |
+
inputs=[customer_id, region, plan_type, monthly_charges, total_charges,
|
| 185 |
+
tenure_months, contract_type, paperless_billing, payment_method,
|
| 186 |
+
data_usage_gb, call_minutes, sms_count, complaint_status, complaint_count],
|
| 187 |
+
outputs=[prediction_output, probability_gauge]
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
# Tab 2: Business Insights
|
| 191 |
+
with gr.TabItem("๐ก Business Insights"):
|
| 192 |
+
gr.Markdown("### Key Findings & Recommendations")
|
| 193 |
+
gr.Markdown("""
|
| 194 |
+
#### ๐ฏ Model Performance
|
| 195 |
+
- **Accuracy:** 90%
|
| 196 |
+
- **AUC Score:** 0.95
|
| 197 |
+
- **Best Algorithm:** Random Forest Classifier
|
| 198 |
+
|
| 199 |
+
#### ๐ผ Business Impact
|
| 200 |
+
- **Current Churn Rate:** 50% in the sample dataset.
|
| 201 |
+
- **Monthly Revenue at Risk:** Over โน12,250.
|
| 202 |
+
- **Potential Annual Loss:** Over โน147,000.
|
| 203 |
+
- **Savings Opportunity:** A 25% reduction in churn could save over โน36,750 annually.
|
| 204 |
+
|
| 205 |
+
#### ๐ด Top Churn Drivers
|
| 206 |
+
1. **Contract Type:** `Month-to-month` customers have a near 100% churn rate in high-risk groups.
|
| 207 |
+
2. **Tenure:** New customers (0-12 months) are most likely to churn.
|
| 208 |
+
3. **Complaints:** A single open complaint doubles the likelihood of churn.
|
| 209 |
+
""")
|
| 210 |
+
|
| 211 |
+
# Tab 3: Visualizations
|
| 212 |
+
with gr.TabItem("๐ Visualizations"):
|
| 213 |
+
gr.Markdown("### Data Analysis Dashboard")
|
| 214 |
+
image_files = [
|
| 215 |
+
("churn_distribution.png", "Overall Churn Distribution"),
|
| 216 |
+
("churn_by_contract.png", "Churn by Contract Type"),
|
| 217 |
+
("revenue_vs_churn.png", "Revenue Impact Analysis"),
|
| 218 |
+
("complaints_analysis.png", "Complaints Impact on Churn"),
|
| 219 |
+
("correlation_matrix.png", "Feature Correlation Matrix"),
|
| 220 |
+
]
|
| 221 |
+
for img_file, title in image_files:
|
| 222 |
+
img = load_image(img_file)
|
| 223 |
+
if img:
|
| 224 |
+
gr.Image(img, label=title, show_label=True)
|
| 225 |
+
else:
|
| 226 |
+
gr.Markdown(f"*{title} - Image not available*")
|
| 227 |
+
|
| 228 |
+
# Tab 4: About Project (UPDATED YEAR)
|
| 229 |
+
with gr.TabItem("โน๏ธ About"):
|
| 230 |
+
gr.Markdown("""
|
| 231 |
+
### ๐ Academic Project Details
|
| 232 |
+
- **Course:** TIRTC - Advance AI/ML Training (Nokia)
|
| 233 |
+
- **Institution:** BRBRAITT, Jabalpur
|
| 234 |
+
- **Project:** Capstone Project 1
|
| 235 |
+
- **Team (Group 5):** Abhay Gupta, Jay Kumar, Kripanshu Gupta, Ruhy Namdeo
|
| 236 |
+
- **Tech Stack:** Scikit-learn, Pandas, Gradio, Gemini, Hugging Face
|
| 237 |
+
---
|
| 238 |
+
**๐ Project Status:** Complete | **๐
Last Updated:** October 2025 | **๐ข Version:** 1.1.0
|
| 239 |
+
""")
|
| 240 |
+
|
| 241 |
+
# Footer (UPDATED YEAR)
|
| 242 |
+
gr.Markdown("--- \n ยฉ 2025 BRBRAITT Group 5 | TIRTC Advance AI/ML Training")
|
| 243 |
+
|
| 244 |
+
# Launch the app
|
| 245 |
+
if __name__ == "__main__":
|
| 246 |
+
app.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|