Skip to content

Nazmul1005/Cancer_Risk_Simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🩺 Cancer Risk Simulator

AI-Powered Cancer Risk Prediction with Real-Time ML Analysis

Python React FastAPI XGBoost TailwindCSS

An interactive web application that predicts cancer risk using machine learning trained on real medical data with 93.7% accuracy

Features β€’ Demo β€’ Installation β€’ Usage β€’ Model


πŸ“‹ Table of Contents


🌟 Overview

The Cancer Risk Simulator is a full-stack machine learning application that provides real-time cancer risk predictions based on 8 key health factors. Built with a modern tech stack, it combines a powerful XGBoost model (trained on 1,500 real patient records) with an intuitive React interface featuring live visualizations and instant feedback.

🎯 Key Highlights

  • 🧠 93.7% Accuracy - XGBoost model trained on real medical data
  • ⚑ Real-Time Predictions - Instant risk calculation as you adjust parameters
  • πŸ“Š Interactive Visualizations - Beautiful charts with Recharts
  • 🎨 Modern UI - Glassmorphism design with Tailwind CSS
  • πŸ”„ Optimal Risk Comparison - See potential improvements with lifestyle changes
  • πŸš€ RESTful API - FastAPI backend with automatic documentation

✨ Features

🩺 Health Metrics Analysis

  • 8 Comprehensive Factors: Age, Gender, BMI, Smoking Status, Genetic Risk, Physical Activity, Alcohol Intake, Cancer History
  • Real-Time Updates: Predictions update automatically with debouncing (500ms)
  • Risk Categorization: Low, Moderate, High, Very High with color-coded indicators

πŸ“ˆ Visualization & Insights

  • Current vs Optimal Risk: Compare your risk with the best possible scenario
  • Interactive Charts: Bar charts showing risk comparison
  • Potential Improvement: Calculate possible risk reduction percentage
  • Color-Coded Results: Visual feedback with dynamic color schemes

πŸ”¬ Machine Learning Backend

  • XGBoost Classifier: Gradient boosting optimized for Mac M4
  • ROC-AUC Score: 0.94+ on test data
  • Feature Importance: Transparent model with interpretable features
  • Automatic Model Loading: Serialized model (model.pkl) loaded on startup

🎨 User Experience

  • Glassmorphism Design: Modern, elegant UI with blur effects
  • Responsive Layout: Works seamlessly on desktop and mobile
  • Smooth Animations: Polished transitions and hover effects
  • Error Handling: Clear error messages and loading states

🎬 Demo

Live Interface

πŸ–₯️  Frontend: http://localhost:5173
πŸ“‘  Backend API: http://localhost:8000
πŸ“–  API Docs: http://localhost:8000/docs

Example Prediction Flow

  1. Adjust Health Metrics β†’ 2. See Real-Time Risk Update β†’ 3. View Optimal Comparison β†’ 4. Analyze Improvement Potential

πŸ› οΈ Tech Stack

Backend

Technology Purpose Version
Python Core Language 3.11+
FastAPI Web Framework 0.104.1
XGBoost ML Model 2.0.3
Scikit-learn ML Tools 1.3.0+
Pandas Data Processing 2.0.0+
NumPy Numerical Computing 1.24.0+

Frontend

Technology Purpose Version
React UI Library 18.3.1
Vite Build Tool 6.0.11
TailwindCSS Styling 3.4.17
Recharts Data Visualization 2.15.0
Axios HTTP Client 1.7.9

πŸ“ Project Structure

Cancer-Risk-Simulator/
β”œβ”€β”€ πŸ“‚ backend/                      # Python FastAPI backend
β”‚   β”œβ”€β”€ main.py                      # FastAPI application & endpoints
β”‚   β”œβ”€β”€ train_model.py               # XGBoost model training script
β”‚   β”œβ”€β”€ model.pkl                    # Serialized trained model
β”‚   β”œβ”€β”€ requirements.txt             # Python dependencies
β”‚   β”œβ”€β”€ The_Cancer_data_1500_V2.csv  # Training dataset (1,500 records)
β”‚   └── __pycache__/                 # Python cache
β”‚
β”œβ”€β”€ πŸ“‚ frontend/                     # React + Vite frontend
β”‚   β”œβ”€β”€ πŸ“‚ src/
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ components/
β”‚   β”‚   β”‚   └── RiskCalculator.jsx   # Main component (408 lines)
β”‚   β”‚   β”œβ”€β”€ πŸ“‚ assets/               # Static assets
β”‚   β”‚   β”œβ”€β”€ App.jsx                  # Root component
β”‚   β”‚   β”œβ”€β”€ main.jsx                 # Entry point
β”‚   β”‚   β”œβ”€β”€ App.css                  # Component styles
β”‚   β”‚   └── index.css                # Global styles + Tailwind
β”‚   β”œβ”€β”€ πŸ“‚ public/                   # Public assets
β”‚   β”œβ”€β”€ index.html                   # HTML template
β”‚   β”œβ”€β”€ package.json                 # Node dependencies
β”‚   β”œβ”€β”€ vite.config.js               # Vite configuration
β”‚   β”œβ”€β”€ tailwind.config.js           # Tailwind configuration
β”‚   β”œβ”€β”€ postcss.config.js            # PostCSS configuration
β”‚   └── eslint.config.js             # ESLint configuration
β”‚
└── README.md                        # This file

πŸš€ Installation

Prerequisites

Clone Repository

git clone https://github.com/yourusername/Cancer-Risk-Simulator.git
cd Cancer-Risk-Simulator

Backend Setup

1️⃣ Navigate to backend directory

cd backend

2️⃣ Create virtual environment

python3 -m venv .venv
source .venv/bin/activate  # macOS/Linux
# .venv\Scripts\activate   # Windows

3️⃣ Install dependencies

pip install -r requirements.txt

4️⃣ Train the model (optional - model.pkl included)

python train_model.py

Output:

βœ“ Model loaded successfully!
Accuracy: 0.9367
ROC-AUC Score: 0.9412

5️⃣ Start backend server

uvicorn main:app --reload --host 0.0.0.0 --port 8000

βœ… Backend running at: http://localhost:8000
πŸ“– API Docs: http://localhost:8000/docs

Frontend Setup

1️⃣ Navigate to frontend directory (new terminal)

cd frontend

2️⃣ Install dependencies

npm install

3️⃣ Start development server

npm run dev

βœ… Frontend running at: http://localhost:5173


πŸ’» Usage

Running the Application

  1. Start Backend (Terminal 1):

    cd backend
    source .venv/bin/activate
    uvicorn main:app --reload --port 8000
  2. Start Frontend (Terminal 2):

    cd frontend
    npm run dev
  3. Open Browser: Navigate to http://localhost:5173

  4. Adjust Health Metrics: Use sliders to input your health data

  5. View Results: See real-time risk prediction and analysis

Building for Production

Frontend:

cd frontend
npm run build
npm run preview  # Preview production build

Backend:

# Use production ASGI server
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4

Parameter Constraints

Parameter Type Range Description
age int 18-100 Age in years
gender int 0-1 0=Female, 1=Male
bmi float 10-50 Body Mass Index
smoking int 0-1 0=No, 1=Yes
genetic_risk int 0-2 0=Low, 1=Medium, 2=High
physical_activity float 0-25 Hours per week
alcohol_intake float 0-10 Drinks per week
cancer_history int 0-1 0=No, 1=Yes

🧠 Model Details

Training Data

  • Dataset: Real cancer medical records
  • Size: 1,500 patient records
  • Features: 8 health metrics
  • Target: Binary classification (Diagnosis: 0=No Cancer, 1=Cancer)

Model Architecture

  • Algorithm: XGBoost Classifier
  • Tree Method: Histogram-based (optimized for CPU)
  • Hyperparameters:
    • n_estimators: 150
    • max_depth: 6
    • learning_rate: 0.1
    • eval_metric: logloss

Performance Metrics

Metric Score
Accuracy 93.67%
ROC-AUC 0.9412
Precision ~0.92
Recall ~0.94

Feature Importance

Top factors influencing predictions:

  1. 🧬 Genetic Risk - Highest importance
  2. πŸ‘€ Age - Strong predictor
  3. 🚬 Smoking Status - Significant impact
  4. πŸ“‹ Cancer History - Critical factor
  5. βš–οΈ BMI - Moderate importance
  6. πŸƒ Physical Activity - Protective factor
  7. 🍷 Alcohol Intake - Risk factor
  8. βš₯ Gender - Baseline risk difference

Risk Level Categories

  • Low: < 25% probability (🟒 Green)
  • Moderate: 25-50% (🟑 Yellow)
  • High: 50-75% (🟠 Orange)
  • Very High: > 75% (πŸ”΄ Red)

⚠️ Disclaimer

IMPORTANT MEDICAL DISCLAIMER

This application is for educational and research purposes only. While the model achieves 93.7% accuracy on test data:

  • ❌ NOT a substitute for professional medical advice
  • ❌ NOT a diagnostic tool for clinical use
  • ❌ NOT validated for actual medical decision-making

Always consult qualified healthcare professionals for:

  • Medical diagnoses
  • Treatment plans
  • Health screenings
  • Cancer risk assessments

The predictions are based on statistical models and should be interpreted as educational demonstrations of machine learning capabilities.


About

An interactive web application that predicts cancer risk using machine learning.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors