# victorcabrejos/laptop-price-predictor architecture

> Architecture diagram of the GitHub repository victorcabrejos/laptop-price-predictor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-05). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/victorcabrejos/laptop-price-predictor
- Repository: https://github.com/victorcabrejos/laptop-price-predictor

## Overview

##### Project type and purpose
- This is a full-stack machine-learning web application (Laptop Price Predictor) built in Python with a FastAPI backend and an HTML/CSS/Chart.js frontend.
- It ingests laptop specs, runs them through interchangeable ML models, and renders predictions and charts.

##### File structure
- Top-level:
- main.py (FastAPI entrypoint)
- config.json (application configuration)
- laptop_price.csv (dataset)
- Directories:
- models/ – ML model interfaces & implementations, Factory, Prototype, Singleton
- visualizations/ – chart abstractions & Factory, chart manager
- preprocessors/ – data cleaning/preprocessing
- templates/ – Jinja2/HTML pages for index, model info, predictions, visualizations
- static/ – CSS, JS, generated chart images
- docs/ – architecture docs and design pattern tutorials
- Key design patterns:
- Factory for model & visualization instantiation
- Prototype for cloning model configurations
- Singleton for config and manager classes

##### README for additional insights
- Backend: Python 3.11, FastAPI, scikit-learn
- Frontend: HTML, TailwindCSS, Chart.js
- Visualization libs: Matplotlib, Seaborn
- Planned future phases include an API layer, database persistence, user auth

a. Components
- Client browser (HTML + Chart.js)
- FastAPI web server (main.py)
- Request handlers / Controllers (prediction endpoint, model info endpoint, visualization endpoint)
- DataPreprocessor (preprocessors/data_preprocessor.py)
- Model subsystem:
– ModelFactory (models/model_factory.py)
– ModelManager (models/model_manager.py, singleton)
– Concrete models (linear_regression_model, random_forest_model)
– ModelConfig & Prototype registry (models/model_config*.py)
- Visualization subsystem:
– VisualizationFactory (visualizations/visualization_factory.py)
– VisualizationManager (visualizations/visualization_manager.py, singleton)
– Concrete charts (scatter_plot, bar_plot, histogram_plot)
- Configuration manager (models/config_manager.py, singleton)
- Data store (CSV file)
- Template renderer (Jinja2 via FastAPI)
b. Relationships & data flow
1. User submits laptop specs via HTML form → HTTP POST to FastAPI
2. Controller invokes DataPreprocessor → cleaned feature vector
3. Controller asks ModelFactory (via ModelManager singleton) for the selected model
4. Model uses feature vector → returns price prediction and metrics
5. Controller asks VisualizationManager to build charts → Matplotlib/Seaborn outputs
6. FastAPI renders templates with prediction, metrics, and embedded charts (or Chart.js JSON data)
c. Architectural patterns & principles
- Separate concerns: presentation (templates/static), business logic (controllers, managers), ML logic (models), visualization logic
- Dependency injection via factories
- Centralized configuration via singleton ConfigManager
- Extendibility: add new models or charts by registering in factories
d. Key technologies
- FastAPI (web framework & HTTP routing)
- scikit-learn (ML algorithms)
- Matplotlib, Seaborn (chart generation) + Chart.js (client-side interactive charts)
- TailwindCSS (styling)

##### Essential elements to capture
- All design patterns (Factory, Prototype, Singleton) as separate modules
- The two distinct subsystems (models vs. visualizations)
- Template rendering and static asset serving
- Data flow paths from user input through to prediction output and chart display
- External dependencies (CSV file, third-party libraries)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        Browser["Client Browser<br/>(HTML + Chart.js)"]:::frontend
        IndexForm["Laptop Specs Form"]:::frontend
        ChartsDisplay["Charts Display<br/>(Chart.js)"]:::frontend
        Templates["HTML Templates"]:::frontend
        StaticAssets["Static Assets<br/>(CSS/JS/Charts)"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        %% Controllers & Routing
        subgraph "Controllers & Routing"
            Server["FastAPI Server<br/>(main.py)"]:::controllers
            PredictionEndpoint["Prediction Endpoint"]:::controllers
            ModelInfoEndpoint["Model Info Endpoint"]:::controllers
            VisualizationEndpoint["Visualization Endpoint"]:::controllers
            TemplateRenderer["Template Renderer"]:::controllers
            Server --> PredictionEndpoint
            Server --> ModelInfoEndpoint
            Server --> VisualizationEndpoint
        end

        %% Preprocessing Layer
        subgraph "Preprocessing Layer"
            Preprocessor["DataPreprocessor"]:::ml
        end

        %% ML Subsystem
        subgraph "ML Subsystem"
            ConfigManager["ConfigManager<br/>(Singleton)"]:::config
            ModelFactory["ModelFactory<br/>(Factory)"]:::ml
            ModelManager["ModelManager<br/>(Singleton)"]:::ml
            ModelInterface["PredictionModel Interface"]:::ml
            ModelConfig["ModelConfig Prototype"]:::ml
            ModelRegistry["ModelConfig Registry"]:::ml
            LRModel["LinearRegressionModel"]:::ml
            RFModel["RandomForestModel"]:::ml

            ConfigManager --> ModelManager
            ModelManager --> ModelFactory
            ModelFactory --> ModelInterface
            ModelFactory --> LRModel
            ModelFactory --> RFModel
            ModelConfig --> ModelRegistry
        end

        %% Visualization Subsystem
        subgraph "Visualization Subsystem"
            VizInterface["Visualization Interface"]:::viz
            BaseViz["BaseVisualization"]:::viz
            VizFactory["VisualizationFactory<br/>(Factory)"]:::viz
            VizManager["VisualizationManager<br/>(Singleton)"]:::viz
            ScatterPlot["ScatterPlot"]:::viz
            BarPlot["BarPlot"]:::viz
            HistPlot["HistogramPlot"]:::viz

            BaseViz --> VizInterface
            VizFactory --> VizInterface
            VizFactory --> ScatterPlot
            VizFactory --> BarPlot
            VizFactory --> HistPlot
            VizManager --> VizFactory
        end

        %% Data Storage
        subgraph "Data Storage"
            CSV["CSV Data Store<br/>(laptop_price.csv)"]:::external
        end
    end

    %% Data Flows
    Browser -->|submits specs| IndexForm
    IndexForm -->|HTTP POST| Server
    PredictionEndpoint -->|calls| Preprocessor
    Preprocessor -->|reads data| CSV
    Preprocessor -->|returns cleaned data| PredictionEndpoint
    PredictionEndpoint -->|requests model| ModelManager
    ModelManager -->|uses factory| ModelFactory
    ModelFactory -->|builds model| LRModel
    ModelFactory -->|builds model| RFModel
    PredictionEndpoint -->|predicts| LRModel
    PredictionEndpoint -->|predicts| RFModel
    LRModel -->|returns price & metrics| PredictionEndpoint
    RFModel -->|returns price & metrics| PredictionEndpoint
    PredictionEndpoint -->|requests charts| VizManager
    VizManager -->|uses factory| VizFactory
    VizFactory -->|creates charts| ScatterPlot
    VizFactory -->|creates charts| BarPlot
    VizFactory -->|creates charts| HistPlot
    PredictionEndpoint -->|renders| TemplateRenderer
    TemplateRenderer -->|renders HTML| Browser
    TemplateRenderer -->|uses templates| Templates
    TemplateRenderer -->|serves assets| StaticAssets

    %% Click Events
    click Server "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/main.py"
    click ConfigManager "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/config_manager.py"
    click CSV "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/laptop_price.csv"
    click Preprocessor "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/preprocessors/data_preprocessor.py"
    click ModelInterface "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/prediction_model.py"
    click ModelFactory "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/model_factory.py"
    click ModelManager "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/model_manager.py"
    click ModelConfig "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/model_config.py"
    click ModelRegistry "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/model_config_registry.py"
    click LRModel "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/linear_regression_model.py"
    click RFModel "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/models/random_forest_model.py"
    click VizInterface "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/visualizations/visualization.py"
    click BaseViz "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/visualizations/base_visualization.py"
    click VizFactory "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/visualizations/visualization_factory.py"
    click VizManager "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/visualizations/visualization_manager.py"
    click ScatterPlot "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/visualizations/scatter_plot.py"
    click BarPlot "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/visualizations/bar_plot.py"
    click HistPlot "https://github.com/victorcabrejos/laptop-price-predictor/blob/main/visualizations/histogram_plot.py"
    click Templates "https://github.com/victorcabrejos/laptop-price-predictor/tree/main/templates/"
    click StaticAssets "https://github.com/victorcabrejos/laptop-price-predictor/tree/main/static/"

    %% Styles
    classDef frontend fill:#e0f7fa,stroke:#0288d1,color:#000;
    classDef controllers fill:#e8f5e9,stroke:#43a047,color:#000;
    classDef ml fill:#fff3e0,stroke:#fb8c00,color:#000;
    classDef viz fill:#f3e5f5,stroke:#8e24aa,color:#000;
    classDef config fill:#fff9c4,stroke:#fbc02d,color:#000;
    classDef external fill:#eceff1,stroke:#90a4ae,color:#000;
```
