# kvj-harsha/ecocarb architecture

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

- Interactive diagram: https://gitdiagram.com/kvj-harsha/ecocarb
- Repository: https://github.com/kvj-harsha/ecocarb

## Overview

##### Main Components
a. Frontend (Next.js Application):
- Represents the client user interface and handles basic web presentation.
- Contains subcomponents (components, pages, and UI parts) as visible in the “client/app/components”, “client/app/home”, etc.
- Uses Tailwind CSS and Aceternity UI.
- Integrates with Firebase for user authentication and database connectivity.

b. Backend (Flask API Server):
- Located in the “server” directory – includes app.py, main.py, and several ML model files (model.pkl, model2.pkl, etc.).
- Exposes REST endpoints (such as POST /predict) for handling requests from the frontend.
- Contains code to load and run ML models for prediction and classification tasks.

c. Machine Learning Models & External Services:
- ML models: XGBoost (for carbon emission prediction) and Random Forest (for harsh driving detection) are included as standalone modules.
- An integration with Gemini Flash 1.5 LLM for AI insights.

d. Database & Authentication (Firebase):
- As per the README, Firebase handles secure authentication and database management.
- Located in the “client/lib/firebase.js” — emphasize its role as a backend-as-a-service integrated with the client code.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Clear distinction between UI (client) and business logic/ML processing (backend).
- Microservice-esque API design with REST endpoints (e.g., POST /predict).
- Integration of ML models within the backend processing flow.
- Use of third-party services (Firebase, and the external LLM) for enhanced functionality.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Frontend (Next.js)
    subgraph "Frontend (Next.js)"
        FE["Next.js Frontend"]:::frontend
        UIC["UI Components (Navbar,Footer,Hero,etc.)"]:::frontend
        FirebaseClient["Firebase Integration"]:::firebase
        FE -->|"contains"| UIC
        FE -->|"includes"| FirebaseClient
    end

    %% Backend (Flask API Server)
    subgraph "Backend (Flask API Server)"
        Flask["Flask App (app.py/main.py)"]:::backend
        Predict["Prediction Endpoint(/predict)"]:::backend
        MLHandler["ML Model Handler"]:::backend
        Flask -->|"exposes"| Predict
        Predict -->|"processes"| MLHandler
    end

    %% Machine Learning Models and AI Insight
    subgraph "Machine Learning Models"
        XGB["XGBoost Model (Carbon Emission)"]:::ml
        RF["Random Forest Model (Harsh Driving)"]:::ml
        MLHandler -->|"invokes"| XGB
        MLHandler -->|"invokes"| RF
        Gemini["Gemini Flash 1.5 LLM"]:::external
        MLHandler -->|"integrates"| Gemini
    end

    %% External Services
    subgraph "External Services"
        Firebase["Firebase Service"]:::firebase
    end

    %% Data flow connections
    FE -->|"APIcall"| Flask
    FirebaseClient -->|"auth/data"| Firebase

    %% Click Events
    click FE "https://github.com/kvj-harsha/ecocarb/tree/master/client"
    click UIC "https://github.com/kvj-harsha/ecocarb/tree/master/client/app/components"
    click FirebaseClient "https://github.com/kvj-harsha/ecocarb/blob/master/client/lib/firebase.js"
    click Flask "https://github.com/kvj-harsha/ecocarb/blob/master/server/app.py"
    click XGB "https://github.com/kvj-harsha/ecocarb/blob/master/server/model.pkl"
    click RF "https://github.com/kvj-harsha/ecocarb/blob/master/server/randomforest.py"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px,dasharray: 5 5;
    classDef backend fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef ml fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef firebase fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef external fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
