# sumityadav329/book-recommender-app architecture

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

- Interactive diagram: https://gitdiagram.com/sumityadav329/book-recommender-app
- Repository: https://github.com/sumityadav329/book-recommender-app

## Overview

##### Project Type and Purpose
- This is a full-stack web application focused on delivering book recommendations.
- The app uses Natural Language Processing (NLP) methods (TF-IDF vectorization and cosine similarity) to process book data and generate recommendations.
- The user interface is built with Streamlit, with a backend that handles data ingest, preprocessing, model training, and recommendation logic.

##### Analyzing the File Structure
- Top-Level Files and Directories:
- Files like app.py, Dockerfile, requirements.txt, setup.py, and LICENSE form the core of the deployment and execution.
- The artifacts folder contains both the raw data file (books.csv) and persisted models (vectorizer.pkl and cosine_sim.pkl).
- The src directory holds the key backend components: data_loader.py, data_preprocessor.py, model_trainer.py, recommender.py etc., representing a modular approach to separate responsibilities like data ingestion, preprocessing, model operations, and logging/exception handling.
- Notebooks directory provides exploratory analysis (e.g., a notebook for the recommender) for prototyping or demonstration purposes.
- Test.py indicates unit testing for overall functionality.

##### Insights from README
- The README provides a clear system workflow detailing data download, data preprocessing, model training and persistence, and the recommendation engine workflow.
- It outlines that the app interacts with model artifacts to quickly serve recommendations without repeated retraining.
- Technologies involved include Python (core), Streamlit (frontend), scikit-learn for NLP, Pandas, and Joblib for model saving.

##### Main Components
a. Data Ingestion & Preprocessing:
- Data Loader: Fetches and loads books.csv from an external URL.
- Data Preprocessor: Cleans and transforms the data into a suitable format for vectorization.
b. Model Training and Persistence:
- Model Trainer: Processes text using TF-IDF, computes cosine similarity, and produces models.
- Model Saver: Persists the trained models (TF-IDF vectorizer and cosine similarity matrix) to disk.
- Model Loader: Loads saved models for inference.
c. Recommendation Engine:
- Core recommender logic that leverages the cosine similarity matrix to provide similar book suggestions.
d. Web Interface:
- The Streamlit-based UI (app.py) that interacts with users, takes book title input, and displays recommendations.
e. Supporting Components:
- Custom logger (logger.py) and exception handling (exception.py) modules.
- The artifacts directory serves as the storage for both input data and trained model files.

##### Relationships and Interactions
- Data Flow:
- Initially, the data loader downloads the dataset which then flows to the data preprocessor.
- Preprocessed data is used by the model trainer to train the TF-IDF vectorizer and compute the cosine similarity matrix.
- Trained models are stored in the artifacts directory and later loaded by the model loader when a recommendation is requested.
- Interaction with the UI:
- The Streamlit web app (app.py) ties the front-end with the backend modules.
- User input flows from the UI to the recommender module, which in turn uses the precomputed models to fetch and display recommendations.
- External Interfaces:
- Dockerfile indicates containerized deployment.
- The setup.py file suggests package encapsulation for environment consistency.

- Data Ingestion (Data Loader)
- Data Preprocessing
- Model Training (TF-IDF and Cosine Similarity computation)
- Model Persistence (Saving and Loading modules)
- Recommendation Engine
- Streamlit Web UI
- Auxiliary modules (Logging, Exception handling)
- External interaction points including Docker container deployment and any potential API endpoints if applicable.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Ingestion and Preprocessing Layer
    subgraph "Data Ingestion & Preprocessing"
        DL["Data Loader"]:::backend
        DP["Data Preprocessor"]:::backend
    end

    %% Model Training and Persistence Layer
    subgraph "Model Training & Persistence"
        MT["Model Trainer"]:::backend
        MS["Model Saver"]:::backend
        ART["Artifacts Storage"]:::storage
        ML["Model Loader"]:::backend
    end

    %% Recommendation and UI Layer
    subgraph "Recommendation & UI"
        REC["Recommendation Engine"]:::backend
        UI["Streamlit UI"]:::frontend
    end

    %% Supporting Components
    subgraph "Supporting Components"
        LOG["Logger"]:::support
        EXC["Exception Handling"]:::support
    end

    %% Data Flow Connections
    DL -->|"processes"| DP
    DP -->|"feeds"| MT
    MT -->|"saves models"| MS
    MS -->|"stores in"| ART
    ART -->|"loaded by"| ML
    ML -->|"used by"| REC
    REC -->|"display results"| UI

    %% Supporting Components Connections (dashed)
    MT ---|"logs"| LOG
    MT ---|"exceptions"| EXC
    UI ---|"logs"| LOG
    UI ---|"exceptions"| EXC

    %% Click Events
    click DL "https://github.com/sumityadav329/book-recommender-app/blob/main/src/data_loader.py"
    click DP "https://github.com/sumityadav329/book-recommender-app/blob/main/src/data_preprocessor.py"
    click MT "https://github.com/sumityadav329/book-recommender-app/blob/main/src/model_trainer.py"
    click MS "https://github.com/sumityadav329/book-recommender-app/blob/main/src/model_saver.py"
    click ML "https://github.com/sumityadav329/book-recommender-app/blob/main/src/model_loader.py"
    click REC "https://github.com/sumityadav329/book-recommender-app/blob/main/src/recommender.py"
    click UI "https://github.com/sumityadav329/book-recommender-app/blob/main/app.py"
    click ART "https://github.com/sumityadav329/book-recommender-app/tree/main/artifacts"
    click LOG "https://github.com/sumityadav329/book-recommender-app/blob/main/src/logger.py"
    click EXC "https://github.com/sumityadav329/book-recommender-app/blob/main/src/exception.py"

    %% Styles
    classDef backend fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef frontend fill:#F5B7B1,stroke:#943126,stroke-width:2px;
    classDef support fill:#D5F5E3,stroke:#1E8449,stroke-width:2px;
    classDef storage fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;
```
