Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,10 +7,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:
|
|
@@ -30,7 +26,7 @@ def load_models():
|
|
| 30 |
scaler = joblib.load('models/scaler.pkl')
|
| 31 |
return model, encoders, scaler
|
| 32 |
except FileNotFoundError:
|
| 33 |
-
print("Error: Model files not found
|
| 34 |
return None, None, None
|
| 35 |
except Exception as e:
|
| 36 |
print(f"An unexpected error occurred while loading models: {e}")
|
|
@@ -64,22 +60,35 @@ def predict_churn(customer_id, region, plan_type, monthly_charges, total_charges
|
|
| 64 |
return "๐ด **Error:** Model components are not loaded. Please check the server logs.", 0.0
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
'
|
| 72 |
-
'
|
| 73 |
-
'
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
'
|
| 79 |
-
'
|
| 80 |
-
'
|
| 81 |
-
'
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# 2. Encode Categorical Features
|
| 85 |
for col, encoder in encoders.items():
|
|
@@ -113,7 +122,7 @@ def predict_churn(customer_id, region, plan_type, monthly_charges, total_charges
|
|
| 113 |
|
| 114 |
except Exception as e:
|
| 115 |
# --- Gemini Error Handling ---
|
| 116 |
-
print(f"Prediction error: {e}")
|
| 117 |
if GOOGLE_API_KEY:
|
| 118 |
try:
|
| 119 |
gemini_model = genai.GenerativeModel('gemini-2.5-flash')
|
|
@@ -126,16 +135,15 @@ def predict_churn(customer_id, region, plan_type, monthly_charges, total_charges
|
|
| 126 |
response = gemini_model.generate_content(prompt)
|
| 127 |
return response.text, 0.0
|
| 128 |
except Exception as gemini_e:
|
| 129 |
-
print(f"Gemini API error: {gemini_e}")
|
| 130 |
return "An unexpected error occurred. Please verify your inputs and try again.", 0.0
|
| 131 |
else:
|
| 132 |
return "An unexpected error occurred. Please check your inputs are valid and try again.", 0.0
|
| 133 |
|
| 134 |
|
| 135 |
# --- Gradio UI ---
|
| 136 |
-
|
| 137 |
with gr.Blocks(title="Telecom Churn Prediction - BRBRAITT Group 5", theme=gr.themes.Soft()) as app:
|
| 138 |
-
# Header
|
| 139 |
gr.Markdown("""
|
| 140 |
# ๐ฎ Telecom Churn Prediction System
|
| 141 |
**TIRTC Course: Advance AI/ML Training (Nokia) | Institution: BRBRAITT, Jabalpur | Group 5**
|
|
@@ -176,11 +184,7 @@ with gr.Blocks(title="Telecom Churn Prediction - BRBRAITT Group 5", theme=gr.the
|
|
| 176 |
with gr.Column(scale=1):
|
| 177 |
gr.Markdown("### ๐ Prediction Result")
|
| 178 |
prediction_output = gr.Markdown(value="*Results will be displayed here...*")
|
| 179 |
-
|
| 180 |
-
# ===== THIS IS THE FIX =====
|
| 181 |
-
# Replaced gr.Gauge with gr.Number to fix the crash
|
| 182 |
probability_gauge = gr.Number(label="Churn Probability", value=0.0, interactive=False)
|
| 183 |
-
# ==========================
|
| 184 |
|
| 185 |
predict_btn.click(
|
| 186 |
fn=predict_churn,
|
|
@@ -198,15 +202,11 @@ with gr.Blocks(title="Telecom Churn Prediction - BRBRAITT Group 5", theme=gr.the
|
|
| 198 |
- **Accuracy:** 90%
|
| 199 |
- **AUC Score:** 0.95
|
| 200 |
- **Best Algorithm:** Random Forest Classifier
|
| 201 |
-
|
| 202 |
#### ๐ผ Business Impact
|
| 203 |
-
- **Current Churn Rate:** 50% in the sample dataset.
|
| 204 |
- **Monthly Revenue at Risk:** Over โน12,250.
|
| 205 |
- **Potential Annual Loss:** Over โน147,000.
|
| 206 |
-
- **Savings Opportunity:** A 25% reduction in churn could save over โน36,750 annually.
|
| 207 |
-
|
| 208 |
#### ๐ด Top Churn Drivers
|
| 209 |
-
1. **Contract Type:** `Month-to-month` customers
|
| 210 |
2. **Tenure:** New customers (0-12 months) are most likely to churn.
|
| 211 |
3. **Complaints:** A single open complaint doubles the likelihood of churn.
|
| 212 |
""")
|
|
@@ -234,11 +234,10 @@ with gr.Blocks(title="Telecom Churn Prediction - BRBRAITT Group 5", theme=gr.the
|
|
| 234 |
### ๐ Academic Project Details
|
| 235 |
- **Course:** TIRTC - Advance AI/ML Training (Nokia)
|
| 236 |
- **Institution:** BRBRAITT, Jabalpur
|
| 237 |
-
- **Project:** Capstone Project 1
|
| 238 |
- **Team (Group 5):** Abhay Gupta, Jay Kumar, Kripanshu Gupta, Ruhy Namdeo
|
| 239 |
- **Tech Stack:** Scikit-learn, Pandas, Gradio, Gemini, Hugging Face
|
| 240 |
---
|
| 241 |
-
**๐ Project Status:** Complete | **๐
Last Updated:** October 2025 | **๐ข Version:** 1.
|
| 242 |
""")
|
| 243 |
|
| 244 |
gr.Markdown("--- \n ยฉ 2025 BRBRAITT Group 5 | TIRTC Advance AI/ML Training")
|
|
|
|
| 7 |
import google.generativeai as genai
|
| 8 |
|
| 9 |
# --- Gemini API Configuration ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
|
| 12 |
if GOOGLE_API_KEY:
|
|
|
|
| 26 |
scaler = joblib.load('models/scaler.pkl')
|
| 27 |
return model, encoders, scaler
|
| 28 |
except FileNotFoundError:
|
| 29 |
+
print("Error: Model files not found in 'models/' directory.")
|
| 30 |
return None, None, None
|
| 31 |
except Exception as e:
|
| 32 |
print(f"An unexpected error occurred while loading models: {e}")
|
|
|
|
| 60 |
return "๐ด **Error:** Model components are not loaded. Please check the server logs.", 0.0
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# ===== THIS IS THE FIX =====
|
| 64 |
+
# Define the exact column order that the model was trained on.
|
| 65 |
+
# This prevents errors caused by mismatched column orders.
|
| 66 |
+
TRAINING_COLUMNS = [
|
| 67 |
+
'region', 'plan_type', 'monthly_charges', 'total_charges', 'tenure_months',
|
| 68 |
+
'contract_type', 'paperless_billing', 'payment_method', 'data_usage_gb',
|
| 69 |
+
'call_minutes', 'sms_count', 'complaint_status', 'complaint_count'
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
# 1. Create a dictionary with input data
|
| 73 |
+
input_dict = {
|
| 74 |
+
'region': region,
|
| 75 |
+
'plan_type': plan_type,
|
| 76 |
+
'monthly_charges': float(monthly_charges),
|
| 77 |
+
'total_charges': float(total_charges),
|
| 78 |
+
'tenure_months': int(tenure_months),
|
| 79 |
+
'contract_type': contract_type,
|
| 80 |
+
'paperless_billing': 1 if paperless_billing else 0,
|
| 81 |
+
'payment_method': payment_method,
|
| 82 |
+
'data_usage_gb': float(data_usage_gb),
|
| 83 |
+
'call_minutes': int(call_minutes),
|
| 84 |
+
'sms_count': int(sms_count),
|
| 85 |
+
'complaint_status': complaint_status,
|
| 86 |
+
'complaint_count': int(complaint_count)
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
# Create the DataFrame, enforcing the correct column order
|
| 90 |
+
input_data = pd.DataFrame([input_dict], columns=TRAINING_COLUMNS)
|
| 91 |
+
# ==========================
|
| 92 |
|
| 93 |
# 2. Encode Categorical Features
|
| 94 |
for col, encoder in encoders.items():
|
|
|
|
| 122 |
|
| 123 |
except Exception as e:
|
| 124 |
# --- Gemini Error Handling ---
|
| 125 |
+
print(f"Prediction error: {e}")
|
| 126 |
if GOOGLE_API_KEY:
|
| 127 |
try:
|
| 128 |
gemini_model = genai.GenerativeModel('gemini-2.5-flash')
|
|
|
|
| 135 |
response = gemini_model.generate_content(prompt)
|
| 136 |
return response.text, 0.0
|
| 137 |
except Exception as gemini_e:
|
| 138 |
+
print(f"Gemini API error: {gemini_e}")
|
| 139 |
return "An unexpected error occurred. Please verify your inputs and try again.", 0.0
|
| 140 |
else:
|
| 141 |
return "An unexpected error occurred. Please check your inputs are valid and try again.", 0.0
|
| 142 |
|
| 143 |
|
| 144 |
# --- Gradio UI ---
|
|
|
|
| 145 |
with gr.Blocks(title="Telecom Churn Prediction - BRBRAITT Group 5", theme=gr.themes.Soft()) as app:
|
| 146 |
+
# Header and other UI elements... (No changes needed here, keeping it the same as before)
|
| 147 |
gr.Markdown("""
|
| 148 |
# ๐ฎ Telecom Churn Prediction System
|
| 149 |
**TIRTC Course: Advance AI/ML Training (Nokia) | Institution: BRBRAITT, Jabalpur | Group 5**
|
|
|
|
| 184 |
with gr.Column(scale=1):
|
| 185 |
gr.Markdown("### ๐ Prediction Result")
|
| 186 |
prediction_output = gr.Markdown(value="*Results will be displayed here...*")
|
|
|
|
|
|
|
|
|
|
| 187 |
probability_gauge = gr.Number(label="Churn Probability", value=0.0, interactive=False)
|
|
|
|
| 188 |
|
| 189 |
predict_btn.click(
|
| 190 |
fn=predict_churn,
|
|
|
|
| 202 |
- **Accuracy:** 90%
|
| 203 |
- **AUC Score:** 0.95
|
| 204 |
- **Best Algorithm:** Random Forest Classifier
|
|
|
|
| 205 |
#### ๐ผ Business Impact
|
|
|
|
| 206 |
- **Monthly Revenue at Risk:** Over โน12,250.
|
| 207 |
- **Potential Annual Loss:** Over โน147,000.
|
|
|
|
|
|
|
| 208 |
#### ๐ด Top Churn Drivers
|
| 209 |
+
1. **Contract Type:** `Month-to-month` customers are at highest risk.
|
| 210 |
2. **Tenure:** New customers (0-12 months) are most likely to churn.
|
| 211 |
3. **Complaints:** A single open complaint doubles the likelihood of churn.
|
| 212 |
""")
|
|
|
|
| 234 |
### ๐ Academic Project Details
|
| 235 |
- **Course:** TIRTC - Advance AI/ML Training (Nokia)
|
| 236 |
- **Institution:** BRBRAITT, Jabalpur
|
|
|
|
| 237 |
- **Team (Group 5):** Abhay Gupta, Jay Kumar, Kripanshu Gupta, Ruhy Namdeo
|
| 238 |
- **Tech Stack:** Scikit-learn, Pandas, Gradio, Gemini, Hugging Face
|
| 239 |
---
|
| 240 |
+
**๐ Project Status:** Complete | **๐
Last Updated:** October 2025 | **๐ข Version:** 1.3.0
|
| 241 |
""")
|
| 242 |
|
| 243 |
gr.Markdown("--- \n ยฉ 2025 BRBRAITT Group 5 | TIRTC Advance AI/ML Training")
|