# dnchkw/toxic-text-detection architecture

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

- Interactive diagram: https://gitdiagram.com/dnchkw/toxic-text-detection
- Repository: https://github.com/dnchkw/toxic-text-detection

## Overview

##### Main Components
- Frontend: Located in the “frontend” directory. This includes the index.html, script.js, and style.css which collectively deliver the UI that users interact with in their browser.
- Backend: The “backend” folder holds app.py, indicating the server-side logic, likely handling API requests between the UI and the ML model.
- Entry Point: The main.py file in the root is the central orchestrator that likely ties together the backend and ML model serving.
- Machine Learning Model: The “model” directory containing model_pipeline.joblib represents the trained toxic text detection pipeline.
- Data Sources and Utilities:
– The “data” directory holds data.csv, which might have been used for model training or evaluation.
– The “tokenizer.py” and the “utils” folder (with nltk_download.py) indicate that text preprocessing (tokenization and any necessary NLTK downloads) is an important part of the workflow.
- Experimentation/Prototyping: The “notebooks” directory (Notebook.ipynb) shows that prototype experiments or EDA were performed to fine-tune model behavior.

##### Relationships and Interactions
- User Interaction Flow: Users open a browser and navigate to localhost:8000 (as mentioned in the README). The request is handled by the backend server (backend/app.py or main.py invoking it).
- Data Flow: The front end sends user input (text) as an API call to the backend. The backend then communicates with the ML model (model_pipeline.joblib) — potentially via a call encapsulated in main.py — and returns a prediction (toxic or not toxic).
- Preprocessing: The tokenizer (tokenizer.py) and any initialization utilities (in utils/nltk_download.py) are used during either model loading or in the prediction workflow to ensure proper text preprocessing.
- Model Metrics: Screenshots show evaluation metrics (Precision-Recall curve), suggesting that model performance has been graphically documented. This can be noted as an additional monitoring component if desired.

- Separation of Concerns: Frontend/UI, Backend server, ML Model, and Data/Preprocessing utilities are clearly separated.
- Layered Approach: The web layer (frontend) communicates with an application layer (backend) which, in turn, integrates with model prediction services.
- Modular Design: Dedicated directories for model artifacts, utilities, and experiments support modularity and future extensibility.

##### Technologies and Frameworks
- Python is used extensively (Python version specified in .python-version and required by the project).
- uv (mentioned in the README and uv.lock) suggests a lightweight environment or a particular web runner setup.
- Standard web technologies for the frontend (HTML, CSS, JavaScript) and possibly a web framework in the backend (such as Flask or FastAPI, even if not explicitly stated) are in use.
- Joblib is used for the ML pipeline persistence in the model folder.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    UB["User Browser"]:::browser

    %% Frontend Layer
    subgraph "Frontend (UI)"
        F1["Index.html"]:::frontend
        F2["Script.js"]:::frontend
        F3["Style.css"]:::frontend
    end

    %% Application Orchestration Layer
    subgraph "Application Orchestration"
        M["Entry Point\n(main.py)"]:::app
        BA["Backend (API Server)"]:::app
    end

    %% Machine Learning Layer
    subgraph "Machine Learning"
        ML["ML Model\n(Pipeline)"]:::ml
        T["Tokenizer\n(tokenizer.py)"]:::mlutil
        NLTK["NLTK Utility\n(nltk_download.py)"]:::mlutil
        DS["Data Source\n(data.csv)"]:::data
    end

    %% Experimentation / Prototyping
    EX["Notebooks\n(Notebook.ipynb)"]:::exp

    %% Connections
    UB -->|"sends request"| F1
    F1 -->|"initiates orchestration"| M
    M -->|"delegates API calls"| BA
    BA -->|"calls prediction"| ML
    DS -->|"supplies data"| ML
    ML -->|"uses preprocessing"| T
    ML -->|"uses preprocessing"| NLTK
    ML -->|"informs"| EX
    DS -->|"supports EDA"| EX

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef app fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef ml fill:#FFD580,stroke:#000,stroke-width:2px;
    classDef mlutil fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef data fill:#D3D3D3,stroke:#000,stroke-width:2px;
    classDef exp fill:#E6E6FA,stroke:#000,stroke-width:2px;
    classDef browser fill:#F0E68C,stroke:#000,stroke-width:2px;

    %% Click Events
    click F1 "https://github.com/dnchkw/toxic-text-detection/blob/master/frontend/index.html"
    click F2 "https://github.com/dnchkw/toxic-text-detection/blob/master/frontend/script.js"
    click F3 "https://github.com/dnchkw/toxic-text-detection/blob/master/frontend/style.css"
    click BA "https://github.com/dnchkw/toxic-text-detection/blob/master/backend/app.py"
    click M "https://github.com/dnchkw/toxic-text-detection/blob/master/main.py"
    click ML "https://github.com/dnchkw/toxic-text-detection/blob/master/model/model_pipeline.joblib"
    click DS "https://github.com/dnchkw/toxic-text-detection/blob/master/data/data.csv"
    click T "https://github.com/dnchkw/toxic-text-detection/blob/master/tokenizer.py"
    click NLTK "https://github.com/dnchkw/toxic-text-detection/blob/master/utils/nltk_download.py"
    click EX "https://github.com/dnchkw/toxic-text-detection/blob/master/notebooks/Notebook.ipynb"
```
