# vrishin-reddy/emotion_detection architecture

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

- Interactive diagram: https://gitdiagram.com/vrishin-reddy/emotion_detection
- Repository: https://github.com/vrishin-reddy/emotion_detection

## Overview

##### Project Type and Purpose
- This is a machine learning–based emotion detection system. It processes text through traditional ML algorithms (TF-IDF with models like Logistic Regression, SVM, etc.) as well as advanced deep learning (fine-tuned BERT) and then provides real-time predictions via a web interface, built using a Streamlit application.
- The project is aimed at analyzing input text, classifying it into emotion categories, and displaying interactive results and insights.

##### Analyzing the File Structure
- Notebooks (8_Mod.ipynb and BERT_Mod.ipynb): These files denote the data preprocessing, model training, hyperparameter tuning, and evaluation stages.
- Application Files (app.py and app_Streamlit.py): One of these (app_Streamlit.py) is explicitly noted as the Streamlit app for user interaction, while app.py might be another entry point.
- Model Saving and Evaluation (model_saves folder): This contains saved models, configuration files, metrics, and evaluation results (e.g., CSV files, JSON reports). This directory is key for showcasing how trained models and performance analytics are managed.
- Static and Templates (static, templates folders): These provide front-end assets (CSS for styling) and HTML views for web pages (error pages and index) which support the web interface.
- Support Files (requirements.txt, .devcontainer for development container): They indicate the dependencies and the development environment configuration.

##### Main Components
a. Data Input and Preprocessing:
- Input Text Data: Users send text input.
- Preprocessing Pipeline: TF-IDF vectorization, tokenization (using resources like tokenizer.json, tokenizer_config.json, and vocab.txt). This is described within the notebooks.

b. Machine Learning Module:
- Traditional ML Models: Models such as Logistic Regression, SVM, KNN, Decision Tree, Naïve Bayes, and Lasso Regression (as seen in the model_saves folder with their saved states and evaluation results).
- BERT Module: A fine-tuned BERT model (separated in its own notebook) that likely has additional handling for hyperparameter tuning and more accurate predictions.
- Model Evaluation and Cross-validation: Components that generate CSV/JSON reports (e.g., cross_validation_results, evaluation_metrics.json) to validate model performance.

c. Model Management:
- Model Storage: The model_saves directory acts as a repository that organizes models, metrics, and configuration files (config.json, special_tokens_map.json, tfidf_vect.pk, etc.).
- Evaluation Results: Stores both training and model evaluation metrics, guiding improvements and debugging.

d. Web Application (Frontend):
- Streamlit App: The app_Streamlit.py is responsible for interacting with the end-user, fetching predictions, and displaying model insights and visualizations.
- Templates and Static Assets: Serve as the HTML/CSS layer that supports the web interface (templates for error pages and main index).

e. Development/Deployment Environment:
- .devcontainer folder: Defines a containerized development environment to ensure consistency across setups.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Split between data processing/model training (back-end ML pipelines) and the user interface (front-end via Streamlit). This decoupling allows independent updates.
- Modular Design: Each component (e.g., preprocess/notebooks, ML models, model evaluation, web server) is self-contained.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Nodes
    User["User Text Input"]:::frontend

    %% Data Preprocessing & Feature Extraction Subgraph
    subgraph "Data Preprocessing & Feature Extraction"
        dp_nb["8_Mod.ipynb"]:::backend
        dp_tok_json["tokenizer.json"]:::backend
        dp_tok_config["tokenizer_config.json"]:::backend
        dp_vocab["vocab.txt"]:::backend
        dp_tfidf["tfidf_vect.pk"]:::backend
    end

    %% Traditional ML Models Module Subgraph
    subgraph "Traditional ML Models Module"
        tm_logistic["Logistic Regression Model"]:::backend
        tm_naive["Naïve Bayes Model"]:::backend
        tm_decision["Decision Tree Model"]:::backend
        tm_knn["KNN Model"]:::backend
        tm_lasso["Lasso Regression Model"]:::backend
        tm_svm["SVM Model"]:::backend
        tm_xgb["XGBoost Model"]:::backend
        subgraph "CV & Evaluation Data"
            cv_decision["DecisionTree_CV"]:::backend
            cv_knn["KNN_CV"]:::backend
            cv_lasso["Lasso_CV"]:::backend
            cv_logistic["LogisticRegression_CV"]:::backend
            cv_naive["NaiveBayes_CV"]:::backend
            cv_svm["SVM_CV"]:::backend
            cv_xgb["XGBoost_CV"]:::backend
            evaluation["ModelFit_Evaluation"]:::backend
        end
        tm_output["Traditional ML Output"]:::backend
    end

    %% BERT Fine-tuning Module Subgraph
    subgraph "BERT Fine-tuning Module"
        bert_nb["BERT_Mod.ipynb"]:::backend
        bert_config["Special Tokens Map"]:::backend
        bert_output["BERT Output"]:::backend
    end

    %% Model Storage & Evaluation Metrics Subgraph
    subgraph "Model Storage & Evaluation Metrics"
        ms_dir["Model Repository"]:::storage
        ms_conf["Configuration File"]:::storage
        ms_eval["Evaluation Metrics"]:::storage
        ms_train["Training Metrics"]:::storage
    end

    %% Web Application (Frontend) Subgraph
    subgraph "Web Application (Frontend)"
        web_streamlit["Streamlit App"]:::frontend
        web_app["Application Entry"]:::frontend
        web_templates["Templates (HTML)"]:::frontend
        web_static["Static Assets (CSS)"]:::frontend
    end

    %% Development/Deployment Environment Subgraph
    subgraph "Development/Deployment Environment"
        dev_dir["Dev Container Directory"]:::dev
        dev_config["Dev Container Config"]:::dev
    end

    %% Data Flow Connections
    User --> dp_nb
    dp_nb --> dp_tok_json
    dp_nb --> dp_tok_config
    dp_nb --> dp_vocab
    dp_nb --> dp_tfidf

    %% Branch from Preprocessing to ML Modules
    dp_nb --> tm_logistic
    dp_nb --> bert_nb

    %% Traditional ML Models to Output and then to Model Storage
    tm_logistic --> tm_output
    tm_naive --> tm_output
    tm_decision --> tm_output
    tm_knn --> tm_output
    tm_lasso --> tm_output
    tm_svm --> tm_output
    tm_xgb --> tm_output
    tm_output --> ms_dir

    %% BERT Module flow
    bert_config --> bert_output
    bert_output --> ms_dir

    %% Model Storage flows to Web Application
    ms_dir --> web_streamlit

    %% Development Environment supports Data Preprocessing and Web App
    dev_dir --> dp_nb
    dev_dir --> web_streamlit

    %% Styling Classes
    classDef frontend fill:#aee1f9,stroke:#333,stroke-width:2px;
    classDef backend fill:#f9e1a5,stroke:#333,stroke-width:2px;
    classDef storage fill:#d1f9a5,stroke:#333,stroke-width:2px;
    classDef dev fill:#f9a5e1,stroke:#333,stroke-width:2px;

    %% Click Events for Data Preprocessing Artifacts
    click dp_nb "https://github.com/vrishin-reddy/emotion_detection/blob/main/8_Mod.ipynb"
    click dp_tok_json "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/tokenizer.json"
    click dp_tok_config "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/tokenizer_config.json"
    click dp_vocab "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/vocab.txt"
    click dp_tfidf "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/tfidf_vect.pk"

    %% Click Events for Traditional ML Models
    click tm_logistic "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/logistic_regression_text_emotion_model.sav"
    click tm_naive "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/naive_bayes_text_emotion_model.sav"
    click tm_decision "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/decision_tree_text_emotion_model.sav"
    click tm_knn "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/knn_text_emotion_model.sav"
    click tm_lasso "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/lasso_regression_text_emotion_model.sav"
    click tm_svm "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/svm_text_emotion_model.sav"
    click tm_xgb "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/xgboost_text_emotion_model.sav"

    %% Click Events for CV & Evaluation Data
    click cv_decision "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/Decision Tree_cross_validation_results.csv"
    click cv_knn "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/KNN_cross_validation_results.csv"
    click cv_lasso "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/Lasso Regression_cross_validation_results.csv"
    click cv_logistic "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/Logistic Regression_cross_validation_results.csv"
    click cv_naive "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/Naive Bayes_cross_validation_results.csv"
    click cv_svm "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/SVM_cross_validation_results.csv"
    click cv_xgb "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/XGBoost_cross_validation_results.csv"
    click evaluation "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/model_fit_evaluation.csv"

    %% Click Events for BERT Fine-tuning Module
    click bert_nb "https://github.com/vrishin-reddy/emotion_detection/blob/main/BERT_Mod.ipynb"
    click bert_config "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/special_tokens_map.json"

    %% Click Events for Model Storage & Evaluation Metrics
    click ms_dir "https://github.com/vrishin-reddy/emotion_detection/tree/main/model_saves"
    click ms_conf "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/config.json"
    click ms_eval "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/metrics/evaluation_metrics.json"
    click ms_train "https://github.com/vrishin-reddy/emotion_detection/blob/main/model_saves/metrics/training_metrics.json"

    %% Click Events for Web Application (Frontend)
    click web_streamlit "https://github.com/vrishin-reddy/emotion_detection/blob/main/app_Streamlit.py"
    click web_app "https://github.com/vrishin-reddy/emotion_detection/blob/main/app.py"
    click web_templates "https://github.com/vrishin-reddy/emotion_detection/tree/main/templates"
    click web_static "https://github.com/vrishin-reddy/emotion_detection/blob/main/static/style.css"

    %% Click Events for Development/Deployment Environment
    click dev_dir "https://github.com/vrishin-reddy/emotion_detection/blob/main/.devcontainer"
    click dev_config "https://github.com/vrishin-reddy/emotion_detection/blob/main/.devcontainer/devcontainer.json"
```
