CoolFace
Apppublic

ZeniTsuIsSimP/Water-Contamination-Detection

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
App README

๐Ÿ’ง Smart Water Contamination Detection

AI-powered water quality monitoring system using IoT sensor data, Machine Learning, Docker, and CI/CD โ€” built with a professional MLOps pipeline.

๐Ÿ”— Live Project

  • โ€”Hugging Face Spaces: https://huggingface.co/spaces/ZeniTsuisSimp/Water-Contamination-Detection

๐Ÿ—๏ธ Project Structure

โ”œโ”€โ”€ app/                    # Streamlit web application
โ”‚   โ””โ”€โ”€ main.py
โ”œโ”€โ”€ src/                    # ML pipeline source code
โ”‚   โ”œโ”€โ”€ data_preprocessing.py   # Data loading, imputation, splitting
โ”‚   โ”œโ”€โ”€ train.py                # Model training + MLflow logging
โ”‚   โ”œโ”€โ”€ evaluate.py             # Evaluation + report generation
โ”‚   โ””โ”€โ”€ predict.py              # Prediction + anomaly detection
โ”œโ”€โ”€ data/
โ”‚   โ”œโ”€โ”€ raw/                # Original, unprocessed CSVs
โ”‚   โ””โ”€โ”€ processed/          # Classified & cleaned datasets
โ”œโ”€โ”€ notebooks/
โ”‚   โ””โ”€โ”€ model_training.ipynb    # Exploratory notebook (EDA + training)
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ validate_model.py       # CI model validation test
โ”œโ”€โ”€ reports/                # Auto-generated metrics, plots
โ”œโ”€โ”€ mlruns/                 # MLflow experiment tracking data
โ”œโ”€โ”€ params.yaml             # Centralized hyperparameters & config
โ”œโ”€โ”€ dvc.yaml                # DVC pipeline definition
โ”œโ”€โ”€ Dockerfile              # Docker containerization
โ”œโ”€โ”€ docker-compose.yml      # Docker Compose for local dev
โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ””โ”€โ”€ .github/workflows/
    โ””โ”€โ”€ ci-cd.yml           # GitHub Actions CI/CD pipeline

๐Ÿš€ Quick Start

1. Setup

bash
# Clone the repository
git clone <repo-url>
cd Water-Contamination-Detection

# Create virtual environment
python -m venv venv
venv\Scripts\activate        # Windows
# source venv/bin/activate   # Linux/Mac

# Install dependencies
pip install -r requirements.txt

2. Train the Model

bash
python -m src.train

This trains the model, logs to MLflow, and saves water_model.pkl.

3. Evaluate

bash
python -m src.evaluate

Generates classification reports and confusion matrix plots in reports/.

4. Run the App

bash
streamlit run app/main.py

Open http://localhost:8501 in your browser.

5. Run with Docker

bash
docker-compose up --build

๐Ÿ”ฌ ML Pipeline

StageCommandOutput
Preprocesspython -m src.data_preprocessingreports/data_summary.json
Trainpython -m src.trainwater_model.pkl, reports/metrics.json
Evaluatepython -m src.evaluatereports/eval_metrics.json, reports/confusion_matrix.png

DVC Pipeline

bash
dvc repro        # Run full pipeline
dvc status       # Check if pipeline is up-to-date
dvc dag          # Visualize pipeline DAG

MLflow

bash
mlflow ui        # Open experiment tracker at http://localhost:5000

๐Ÿ”ง Configuration

All hyperparameters and thresholds are in `params.yaml`:

  • โ€”Data paths โ€” raw/processed data locations
  • โ€”Feature names โ€” pH, Solids (TDS)
  • โ€”Model hyperparameters โ€” RF trees
  • โ€”Anomaly thresholds โ€” critical pH/TDS limits

๐Ÿง  Hybrid Prediction Logic

  1. 1.Rule-Based Safety Check โ€” Catches critical anomalies (pH < 4 or > 10, TDS > 3000)
  2. 2.ML Model โ€” Random Forest predicts potability for non-critical inputs
  3. 3.Confidence Smoothing โ€” Prevents unrealistic 100%/0% probabilities

๐Ÿšข CI/CD Pipeline

StageDescription
Lint & TestFlake8 linting + model validation test
Docker BuildBuild image + health check
DeployPush to Hugging Face Spaces (main branch only)

๐Ÿ“Š Tech Stack

  • โ€”ML: scikit-learn (Random Forest)
  • โ€”App: Streamlit + Altair charts
  • โ€”Experiment Tracking: MLflow
  • โ€”Data Versioning: DVC
  • โ€”Containerization: Docker
  • โ€”CI/CD: GitHub Actions โ†’ Hugging Face Spaces