# 03-princy/co2-emission-by-cars architecture

> Architecture diagram of the GitHub repository 03-princy/co2-emission-by-cars, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/03-princy/co2-emission-by-cars
- Repository: https://github.com/03-princy/co2-emission-by-cars

## Overview

##### Project type and purpose
- This is a small full-stack Python web application (Flask) that serves an ML model for predicting CO₂ emissions of cars.
- Key indicators:
– app.py and wsgi.py: Flask entry points
– templates/ and static/: standard Flask folder structure
– finalized_model.pkl: serialized machine-learning model
– notebook/: Jupyter notebook for data exploration and model training
– Procfile: Heroku deployment

##### File structure
- Top-level scripts:
– app.py: defines HTTP routes, loads model, handles form input and prediction
– wsgi.py: exposes Flask app to WSGI servers
- Configuration/deployment:
– requirements.txt: Python dependencies
– Procfile: declares web process for Heroku (e.g., “web: gunicorn wsgi:app”)
– .gitignore
- Static assets and templates:
– static/: CSS, JS, images served to client
– templates/index.html: user interface for inputting car specs and displaying results
- Data & model artifacts:
– final_co2.csv: raw or intermediate dataset
– finalized_model.pkl: trained model persisted
- Notebook:
– notebook/co2-emission-by-cars.ipynb: exploratory data analysis, model training pipeline

##### Missing README
- No documented architecture section; we infer design from files.

a. Main components
– Web Browser (Client)
– Flask Web Server (app.py + wsgi.py)
– ML Model Loader & Predictor (finalized_model.pkl)
– Data Store (final_co2.csv or external DB if adopted later)
– Static Assets & Templates
– Deployment Platform (Heroku/Gunicorn)
b. Relationships & data flows
1. User enters car parameters in the browser UI (templates/index.html).
2. Browser sends HTTP POST request to Flask endpoint (app.py).
3. Flask app receives request, loads/retains the ML model from finalized_model.pkl.
4. Input is preprocessed, prediction is computed.
5. Prediction result is sent back to the client and rendered via the template.
c. Architectural patterns & principles
– Model-View-Controller (MVC) separation:
- View: HTML templates + static files
- Controller: Flask routes in app.py
- Model: ML model artifact and any data access logic
– Single process, synchronous request handling via Gunicorn (Procfile → Gunicorn → wsgi.py)
– Separation of concerns: ML training in notebook; serving logic in Flask app
d. Key technologies & libraries
– Python 3.x, Flask
– Gunicorn (WSGI server)
– pandas, scikit-learn (for model)
– Jinja2 (templating)
– Heroku platform (Procfile)

##### Final recommendations
- Essential elements without over-complicating: keep it high-level for clarity.
- Future components you plan to add (e.g., database, cache).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        Browser["Web Browser"]:::frontend
        Templates["HTML Templates (index.html)"]:::frontend
        StaticAssets["Static Assets"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        Gunicorn["Gunicorn WSGI Server"]:::deploy
        WSGI["WSGI Entry Point (wsgi.py)"]:::backend
        FlaskApp["Flask App Server (app.py)"]:::backend
        ModelArtifact["ML Model Artifact (finalized_model.pkl)"]:::data
        Dataset["Dataset (final_co2.csv)"]:::data
    end

    %% Model Training
    subgraph "Model Training Notebook"
        direction TB
        Notebook["Notebook (co2-emission-by-cars.ipynb)"]:::data
    end

    %% Deployment & Config
    subgraph "Deployment & Config"
        direction TB
        ProcfileNode["Procfile"]:::deploy
        Requirements["requirements.txt"]:::deploy
    end

    %% Connections
    Browser -->|"loads"| Templates
    Browser -->|"loads"| StaticAssets
    Browser -->|"POST car data"| Gunicorn
    Gunicorn -->|"forwards request"| WSGI
    WSGI -->|"calls routes"| FlaskApp
    FlaskApp -->|"loads model"| ModelArtifact
    FlaskApp -->|"reads data"| Dataset
    FlaskApp -->|"renders with Jinja2"| Templates
    Templates -->|"HTTP response"| Browser

    Notebook --- Dataset
    Notebook --- ModelArtifact

    ProcfileNode --> Gunicorn
    Requirements --> FlaskApp

    %% Click Events
    click FlaskApp "https://github.com/03-princy/co2-emission-by-cars/blob/main/app.py"
    click WSGI "https://github.com/03-princy/co2-emission-by-cars/blob/main/wsgi.py"
    click ModelArtifact "https://github.com/03-princy/co2-emission-by-cars/blob/main/finalized_model.pkl"
    click Dataset "https://github.com/03-princy/co2-emission-by-cars/blob/main/final_co2.csv"
    click Templates "https://github.com/03-princy/co2-emission-by-cars/blob/main/templates/index.html"
    click StaticAssets "https://github.com/03-princy/co2-emission-by-cars/tree/main/static/"
    click Notebook "https://github.com/03-princy/co2-emission-by-cars/blob/main/notebook/co2-emission-by-cars.ipynb"
    click ProcfileNode "https://github.com/03-princy/co2-emission-by-cars/tree/main/Procfile"
    click Requirements "https://github.com/03-princy/co2-emission-by-cars/blob/main/requirements.txt"

    %% Styles
    classDef frontend fill:#D6E9FE,stroke:#333,stroke-width:1px
    classDef backend fill:#DFF2BF,stroke:#333,stroke-width:1px
    classDef data fill:#FEEFB3,stroke:#333,stroke-width:1px
    classDef deploy fill:#E6E6E6,stroke:#333,stroke-width:1px
```
