A comprehensive AI-powered telecom churn prediction platform with FastAPI backend, modern web frontend, intelligent recommendations, dynamic model monitoring, and automated reporting capabilities.
churn_prediction/
βββ api/ # π FastAPI Backend
β βββ main.py # Main API application
β βββ schema.py # Pydantic schemas
β βββ inference_preprocess.py # Data preprocessing
β βββ agents/ # AI agents
β βββ recommendation_agent.py
β βββ monitoring_agent.py
βββ telecom_churn_frontend/ # π Web Frontend
β βββ index.html # Main web interface
β βββ style.css # Styling
β βββ app.js # Frontend logic
β βββ drift_report.html # Generated reports
βββ train/ # π€ Model Training
β βββ train.py # Training pipeline
β βββ prepare_telco.py # Data preparation
β βββ utils.py # Training utilities
βββ data/ # π Data Files
β βββ telco_raw.csv # Raw customer data
β βββ telco_train.csv # Training dataset
β βββ telco_scoring_sample.csv # Sample scoring data
β βββ baseline_train.pkl # Baseline for drift detection
βββ models/ # π― Trained Models
β βββ xgb_pipeline.joblib # XGBoost model pipeline
β βββ xgb_threshold.json # Optimal threshold
βββ app/ # π± Alternative UI
β βββ streamlit_app.py # Streamlit interface
βββ create_baseline.py # Baseline data creation
βββ requirements.txt # Python dependencies
βββ Makefile # Build automation
βββ README.md # This documentation
- FastAPI Backend: High-performance REST API with automatic documentation
- XGBoost Model: Pre-trained churn prediction with optimized thresholds
- CSV File Upload: Batch processing of customer data
- Top-K Selection: Configurable ranking of highest-risk customers
- Recommendation Agent: Generates personalized retention strategies
- Monitoring Agent: Real-time data drift detection and visualization
- Dynamic Reports: HTML reports with embedded charts and insights
- Responsive Design: Modern glassmorphism UI with mobile support
- File Upload: Drag-and-drop CSV upload with validation
- Interactive Results: Risk level badges, summary cards, and data tables
- Real-time Feedback: Toast notifications and loading states
- Data Drift Detection: Monitors numerical and categorical feature drift
- Visual Analytics: Base64-encoded charts for distribution comparisons
- Risk Stratification: CRITICAL/HIGH/MEDIUM/LOW risk categorization
- Revenue Impact: Calculates potential revenue at risk
- Email Integration: SMTP-based report delivery with attachments
- HTML Reports: Professional styled reports with CSS
- CSV Export: Downloadable prediction results
- Report Viewing: In-browser drift report visualization
# Create virtual environment
python -m venv .venv
# Activate virtual environment
# Windows:
.venv\Scripts\activate
# macOS/Linux:
source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt# Prepare training and scoring files
python train/prepare_telco.py
# Create baseline data for drift detection
python create_baseline.py
# Train the model
python train/train.py# Option 1: Using Makefile
make api # Start API server
make frontend # Start web frontend (in another terminal)
# Option 2: Manual commands
# Terminal 1: Start the FastAPI backend (from the main project folder)
uvicorn api.main:app --reload --host 0.0.0.0 --port 8000
# Terminal 2: Start the frontend server
cd telecom_churn_frontend
python -m http.server 3000- Frontend: http://localhost:3000
- API Documentation: http://localhost:8000/docs
- API Health: http://localhost:8000
The FastAPI backend provides three main endpoints:
POST /predict_churn?k_value=10
Content-Type: multipart/form-data
# Upload CSV file with customer data
# Returns top-K customers ranked by churn riskPOST /generate_recommendations_report
Content-Type: application/json
# Generates actionable retention strategies
# Creates HTML report with drift monitoringPOST /send_email
Content-Type: application/json
{
"recipient_email": "manager@company.com",
"results_csv_path": "path/to/results.csv"
}The modern web interface provides:
- File Upload: Drag-and-drop CSV upload with validation
- Risk Analysis: Interactive results with risk level badges
- Report Generation: One-click recommendations and drift reports
- Email Integration: Send reports directly from the interface
make help # Show all available commands
make install # Install dependencies
make prepare # Prepare data
make train # Train model
make baseline # Create baseline data
make api # Start API server
make frontend # Start web frontend
make ui # Start Streamlit UI
make test # Run tests
make clean # Clean cache files# Install dependencies
pip install -r requirements.txt
# Prepare data
python train/prepare_telco.py
# Train model
python train/train.py
# Create baseline
python create_baseline.py
# Start API
uvicorn api.main:app --reload --port 8000
# Start frontend
cd telecom_churn_frontend && python -m http.server 3000
# Start Streamlit UI
streamlit run app/streamlit_app.py --server.port 8501To enable email functionality, set these environment variables:
# Windows
set EMAIL_USERNAME=your-email@gmail.com
set EMAIL_PASSWORD=your-app-password
set SMTP_SERVER=smtp.gmail.com
set SMTP_PORT=587
# macOS/Linux
export EMAIL_USERNAME=your-email@gmail.com
export EMAIL_PASSWORD=your-app-password
export SMTP_SERVER=smtp.gmail.com
export SMTP_PORT=587- Import Errors: Ensure you're in the correct directory when running commands
- Model Not Found: Run
python train/train.pyto create model files - Port Already in Use: Change ports in Makefile or kill existing processes
- Email Not Working: Check environment variables and use app passwords for Gmail
Your CSV file should contain these columns:
customerID,gender,SeniorCitizen,Partner,Dependentstenure,PhoneService,MultipleLines,InternetServiceOnlineSecurity,OnlineBackup,DeviceProtection,TechSupportStreamingTV,StreamingMovies,Contract,PaperlessBillingPaymentMethod,MonthlyCharges,TotalCharges
The XGBoost model achieves:
- Accuracy: ~80%
- Precision: ~65%
- Recall: ~55%
- F1-Score: ~60%
- ROC-AUC: ~85%
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.