# sjors-berendsen/pws architecture

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

- Interactive diagram: https://gitdiagram.com/sjors-berendsen/pws
- Repository: https://github.com/sjors-berendsen/pws

## Overview

##### The AI Module
- Located in the “ai” directory.
- Contains data (data.xlsx, input.txt) and scripts for training (train_model.py) and prediction (predict.py), along with a model definition (model.py).
- Represents the core machine learning functionality.

##### The API Component
- All of its code is located in the “api” directory.
- Likely built as a web service (e.g., using Flask or a similar framework in app.py), this module provides endpoints that can serve the AI model’s predictions.
- It also contains its own copies of model.py and predict.py, which indicates that the API layer might wrap or adapt the AI functionality to expose it externally.
- Includes a test HTML file (test.html) for possibly testing or demoing the endpoint responses and a domains.txt file, which might be used for configuration or filtering.

##### The Browser Extension
- Housed in the “extension/src” directory.
- Provides the frontend integration by interacting with online platforms (e.g., Gmail and other websites) to prevent scams. The extension’s code includes HTML, CSS, and JavaScript files such as various popups (e.g., aipopup.html, downloadpopup.html, popup.html, settings.html, warning.html) and their associated scripts.
- The manifest.json file indicates it’s structured as a typical browser extension.
- This module captures user interactions and communicates with the API component to fetch predictions or validate content.

a. Main Components:
- Browser Extension:
– Sub-components: Content scripts (content.js), background scripts (background.js), and multiple UI popups (e.g., aipopup.html, downloadpopup.html) along with their JavaScript and CSS files.
- API Server:
– The “app.py” script forms the central controller that handles requests.
– Supporting files for prediction and model integration (model.py, predict.py, test.html, domains.txt).
- AI Model/Engine:
– Training and inference modules (train_model.py, predict.py, model.py) along with data input files (data.xlsx, input.txt).

b. Relationships and Interactions:
- The Browser Extension acts as the client-side interface. It sends requests (e.g., to check content for scam detection) to the API Server.
- The API Server, upon receiving requests from the extension, uses the AI model’s prediction functionality to process the inputs.
- The AI Module (either called directly by the API or indirectly refreshed via a pre-trained model) processes the data and returns results to the API, which then communicates back to the Browser Extension.
- Configuration or static data (domains.txt, input.txt) as supplementary resources that feed into the behavior of the API/AI components.

c. Important Architectural Patterns:
- Separation of Concerns: Each module is self-contained—training/prediction is isolated from API exposure and from the user interaction layer provided by the extension.
- Service Orientation: The API layer abstracts the complexity of the AI module, exposing standardized endpoints to the extension.
- Modularity and Reusability: Both “ai” and “api” have similar scripts for prediction, suggesting versioning or adaptation of the core AI logic for production use.
- Client-Server Interaction: The extension (client) communicates with the API (server) to access AI-driven services in real time.

d. Relevant Technologies and Detail Their Roles:
- AI/ML Libraries and Data Handling (implied within Python scripts in the “ai” folder).
- Web Service Framework (likely in “api/app.py”) to manage HTTP requests.
- Browser extension frameworks, with the manifest.json hinting at Chrome/Firefox extension APIs.
- HTML/CSS/JavaScript for the extension’s user interface, along with JQuery (jquery.js) for DOM manipulation.

- Separation between the frontend (browser extension) and the backend (API and AI Modules).
- Data flow:
– User interactions trigger events in the Browser Extension.
– These events result in API calls reaching the API Server.
– The API Server routes the request to the AI Module for scam detection.
– The output (prediction result) is passed back up the chain, ultimately informing the user via the extension.

f. Instructing the Principal Software Engineer:
– Label one block “Browser Extension” with sub-components such as popup, background, and content scripts.
– Label another block “API Server” where app.py is the centerpiece.
– Label another block “AI Module” capturing training, prediction, and data management.
– From “Browser Extension” -> “API Server” for sending user requests.
– From “API Server” -> “AI Module” for invoking predictions.
– And then back from “AI Module” -> “API Server” -> “Browser Extension” for results.
- Static and external resources (e.g., data.xlsx, input.txt, and domains.txt) next to their corresponding modules.

## Diagram (Mermaid)

```mermaid
flowchart TD

    subgraph "Browser Extension"
        Ext_Manifest["Manifest"]:::ext
        Ext_Aipopup_HTML["aipopup.html"]:::ext
        Ext_Aipopup_JS["aipopup.js"]:::ext
        Ext_Downloadpopup_HTML["downloadpopup.html"]:::ext
        Ext_Downloadpopup_JS["downloadpopup.js"]:::ext
        Ext_Popup_HTML["popup.html"]:::ext
        Ext_Popup_JS["popup.js"]:::ext
        Ext_Popup_CSS["popup.css"]:::ext
        Ext_Settings_HTML["settings.html"]:::ext
        Ext_Settings_JS["settings.js"]:::ext
        Ext_Settings_CSS["settings.css"]:::ext
        Ext_Warning_HTML["warning.html"]:::ext
        Ext_Warning_JS["warning.js"]:::ext
        Ext_Background["background.js"]:::ext
        Ext_Content["content.js"]:::ext
        Ext_JQuery["jquery.js"]:::ext
    end

    subgraph "API Server"
        API_App["app.py"]:::api
        API_Model["model.py"]:::api
        API_Predict["predict.py"]:::api
        API_Domains["domains.txt"]:::api
        API_Test["test.html"]:::api
    end

    subgraph "AI Module"
        AI_Data["data.xlsx"]:::ai
        AI_Input["input.txt"]:::ai
        AI_Model["model.py"]:::ai
        AI_Predict["predict.py"]:::ai
        AI_Train["train_model.py"]:::ai
    end

    %% Inter-component connections (Client -> API -> AI and back)
    Ext_Content ---|"request"| API_App
    API_App ---|"calls_prediction"| API_Predict
    API_Predict ---|"invokes_prediction"| AI_Predict
    AI_Predict ---|"returns_result"| API_Predict
    API_Predict ---|"aggregates_result"| API_App
    API_App ---|"sends_response"| Ext_Content

    %% API internal resource links
    API_Domains ---|"configures"| API_App
    API_Test ---|"tests_response"| API_App
    API_Model ---|"syncs_with"| AI_Model
    AI_Model ---|"updated_by"| AI_Train

    %% AI Module internal data flow
    AI_Data ---|"provides_data"| AI_Model
    AI_Input ---|"feeds_input"| AI_Model

    %% Styles
    classDef ai fill:#fef3bd,stroke:#333,stroke-width:2px;
    classDef api fill:#d0e0f0,stroke:#333,stroke-width:2px;
    classDef ext fill:#d7f9d3,stroke:#333,stroke-width:2px;

    %% Click Events for AI Module
    click AI_Data "https://github.com/sjors-berendsen/pws/blob/main/ai/data.xlsx"
    click AI_Input "https://github.com/sjors-berendsen/pws/blob/main/ai/input.txt"
    click AI_Model "https://github.com/sjors-berendsen/pws/blob/main/ai/model.py"
    click AI_Predict "https://github.com/sjors-berendsen/pws/blob/main/ai/predict.py"
    click AI_Train "https://github.com/sjors-berendsen/pws/blob/main/ai/train_model.py"

    %% Click Events for API Component
    click API_App "https://github.com/sjors-berendsen/pws/blob/main/api/app.py"
    click API_Domains "https://github.com/sjors-berendsen/pws/blob/main/api/domains.txt"
    click API_Model "https://github.com/sjors-berendsen/pws/blob/main/api/model.py"
    click API_Predict "https://github.com/sjors-berendsen/pws/blob/main/api/predict.py"
    click API_Test "https://github.com/sjors-berendsen/pws/blob/main/api/test.html"

    %% Click Events for Browser Extension
    click Ext_Manifest "https://github.com/sjors-berendsen/pws/blob/main/extension/src/manifest.json"
    click Ext_Aipopup_HTML "https://github.com/sjors-berendsen/pws/blob/main/extension/src/aipopup.html"
    click Ext_Aipopup_JS "https://github.com/sjors-berendsen/pws/blob/main/extension/src/aipopup.js"
    click Ext_Downloadpopup_HTML "https://github.com/sjors-berendsen/pws/blob/main/extension/src/downloadpopup.html"
    click Ext_Downloadpopup_JS "https://github.com/sjors-berendsen/pws/blob/main/extension/src/downloadpopup.js"
    click Ext_Popup_HTML "https://github.com/sjors-berendsen/pws/blob/main/extension/src/popup.html"
    click Ext_Popup_JS "https://github.com/sjors-berendsen/pws/blob/main/extension/src/popup.js"
    click Ext_Popup_CSS "https://github.com/sjors-berendsen/pws/blob/main/extension/src/popup.css"
    click Ext_Settings_HTML "https://github.com/sjors-berendsen/pws/blob/main/extension/src/settings.html"
    click Ext_Settings_JS "https://github.com/sjors-berendsen/pws/blob/main/extension/src/settings.js"
    click Ext_Settings_CSS "https://github.com/sjors-berendsen/pws/blob/main/extension/src/settings.css"
    click Ext_Warning_HTML "https://github.com/sjors-berendsen/pws/blob/main/extension/src/warning.html"
    click Ext_Warning_JS "https://github.com/sjors-berendsen/pws/blob/main/extension/src/warning.js"
    click Ext_Background "https://github.com/sjors-berendsen/pws/blob/main/extension/src/background.js"
    click Ext_Content "https://github.com/sjors-berendsen/pws/blob/main/extension/src/content.js"
    click Ext_JQuery "https://github.com/sjors-berendsen/pws/blob/main/extension/src/jquery.js"
```
