# granthgg/news-headline-sentiment-analysis-stock-prediction architecture

> Architecture diagram of the GitHub repository granthgg/news-headline-sentiment-analysis-stock-prediction, 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/granthgg/news-headline-sentiment-analysis-stock-prediction
- Repository: https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction

## Overview

##### Main Components
- User Interface (Frontend) – The Streamlit app (located in WebApp/main.py) that serves as the entry point for users. It displays the UI, accepts user input (news headlines), and allows model selection using radio buttons.
- ML Prediction Engine – This consists of the pre-trained ML models (Logistic Regression, Naive Bayes, Random Forest) stored as pickle files (lr_classifier.pkl, nb_classifier.pkl, rf_classifier.pkl) that are loaded to make predictions.
- Data Resources – CSV files (Stock Headlines.csv available in both the root and WebApp directories) might be used for either training, testing, or displaying live stock headlines. There’s also a training corpus stored in train_corpus.pkl.
- Experimentation/Development Notebook – The Stock Sentiment Analysis.ipynb indicates exploratory analysis/development which might have been used to develop and validate the ML models.

##### Relationships and Interactions
- User actions (entering a headline and selecting a prediction model) trigger the Streamlit app (frontend) to call the appropriate prediction function.
- The main application logic (in main.py) loads the selected ML model from its respective pickle file and uses the headline input to generate a prediction.
- After processing, the application returns the result to the UI where the prediction (e.g., “stock price goes up or down”) is displayed to the user.
- Additionally, the UI fetches and displays the top 10 world news headlines, which might be stored in one of the CSV files.

##### Architectural Patterns and Design Principles
- Clearly separated UI and prediction logic – Although integrated in a single script, it is conceptually divided into a presentation layer (Streamlit interface) and a business logic layer (prediction engine).
- Modularity – The existence of separate pickle files for different ML models allows for plug-and-play flexibility, with the user choosing which model to use.
- Data Handling – The CSV files and the training corpus indicate a separation between static resources and dynamic computation (the classification logic).

##### Relevant Technologies and Frameworks
- Streamlit for building the web UI.
- Python as the programming language underpinning the application logic.
- Machine Learning libraries (e.g., scikit-learn, implied by the classifiers used) for the creation of the ML models.
- Serialization with pickle for model storage and retrieval.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    U["User"]:::user

    %% Frontend Layer
    subgraph "Frontend Layer"
        FE["Streamlit Frontend"]:::frontend
    end

    %% ML Prediction Engine
    subgraph "ML Prediction Engine"
        LR["Logistic Regression Model"]:::ml
        NB["Naive Bayes Model"]:::ml
        RF["Random Forest Model"]:::ml
    end

    %% Data Resources
    subgraph "Data Resources"
        CSV1["Stock Headlines CSV (Root)"]:::data
        CSV2["Stock Headlines CSV (WebApp)"]:::data
        TC["Training Corpus"]:::data
    end

    %% Experimentation/Development Notebook
    NBK["Experimentation Notebook"]:::notebook

    %% Connections
    U -->|"user_input"| FE
    FE -->|"model_selection"| LR
    FE -->|"model_selection"| NB
    FE -->|"model_selection"| RF

    LR -->|"prediction_result"| FE
    NB -->|"prediction_result"| FE
    RF -->|"prediction_result"| FE

    CSV1 -->|"news_headlines"| FE
    CSV2 -->|"news_headlines"| FE

    CSV1 -->|"data_feed"| LR
    CSV1 -->|"data_feed"| NB
    CSV1 -->|"data_feed"| RF
    TC -->|"training_data"| LR
    TC -->|"training_data"| NB
    TC -->|"training_data"| RF

    NBK -->|"development_insights"| LR
    NBK -->|"development_insights"| NB
    NBK -->|"development_insights"| RF

    %% Click Events
    click FE "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/WebApp/main.py"
    click LR "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/WebApp/lr_classifier.pkl"
    click NB "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/WebApp/nb_classifier.pkl"
    click RF "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/WebApp/rf_classifier.pkl"
    click CSV1 "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/Stock Headlines.csv"
    click CSV2 "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/WebApp/Stock Headlines.csv"
    click TC "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/WebApp/train_corpus.pkl"
    click NBK "https://github.com/granthgg/news-headline-sentiment-analysis-stock-prediction/blob/main/Stock Sentiment Analysis.ipynb"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef ml fill:#98FB98,stroke:#000,stroke-width:2px;
    classDef data fill:#FFFFE0,stroke:#000,stroke-width:2px;
    classDef notebook fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef user fill:#D3D3D3,stroke:#000,stroke-width:2px;
```
