# kuldip-1662/document-sentiment-analyzer-chatbot architecture

> Architecture diagram of the GitHub repository kuldip-1662/document-sentiment-analyzer-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kuldip-1662/document-sentiment-analyzer-chatbot
- Repository: https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot

## Overview

##### Project type and purpose
- This is a full-stack, AI-powered PDF Emotion Analyzer & Chatbot web application.
- The frontend is built in Streamlit; the backend is in FastAPI (api.py/app.py) and Python scripts handle PDF processing, sentiment prediction, and chat logic via LangChain.

##### File structure
Top-level files & directories:
- PromptTemplate.py, api.py, app.py, chat.py, config.py, model_prediction.py, pdf_processing.py – core backend logic and configurations
- README.md, requirements.txt – documentation & dependencies
- sentiment/ – helper modules for sentiment model loading & text extraction
- sentiment_model_training/ – standalone training pipeline

##### README for additional insights
- Two main features: Sentiment Analysis (using ModernBERT+PyTorch) and Chat (using LangChain + Groq for data generation).
- Tech stack: ModernBERT, PyTorch, LangChain, FastAPI, Streamlit.

##### Main components & interactions
a. Components
1. Streamlit Frontend
2. FastAPI Backend
- PDF Processing Module (pdf_processing.py)
- Sentiment Prediction Module (model_prediction.py + sentiment/model_loading.py + sentiment/text_extraction.py)
- Chat Module (chat.py leveraging LangChain, embeddings, PromptTemplate)
3. Sentiment Training Pipeline (sentiment_model_training/)
4. External Services
- Groq (dataset generation)
- LLM provider (for LangChain chat)
5. Configuration (config.py)
b. Relationships & Data Flows
- User uploads PDF via Streamlit → HTTP POST to FastAPI
- Backend → pdf_processing extracts text → either
– Sentiment path → model_prediction invokes ModernBERT PyTorch model → return emotion scores
– Chat path → LangChain document loader & retriever + LLM chain → return conversational answer
- Results returned to Frontend for display
- Training pipeline is offline; uses sentiment_data.csv → dataset.py, text_cleaning, preprocessing, model.py, train.py to produce a fine-tuned model stored for loading.
c. Architectural patterns & design principles
- Separation of concerns: frontend vs. API vs. model logic vs. training pipeline
- Modular design: each feature in its own module
- Dependency injection via config.py for keys, model paths, LLM endpoints
d. Key technologies
- Streamlit (UI)
- FastAPI (HTTP API)
- ModernBERT + PyTorch (sentiment)
- LangChain (chat orchestration)
- Groq (data augmentation)

##### Elements
- Labels for each component (e.g., “PDF Processor”, “LangChain Chat Engine”)
– Blue for frontend
– Green for backend services
– Orange for ML model/training
– Gray for external services

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        A[Streamlit Frontend]:::frontend
    end

    %% API Layer
    subgraph "API Layer"
        B[FastAPI Backend]:::backend

        subgraph "Services"
            C[PDF Processing Module]:::backend
            subgraph "Sentiment Prediction Module"
                D1[model_prediction.py]:::backend
                D2[sentiment/model_loading.py]:::backend
                D3[sentiment/text_extraction.py]:::backend
            end
            subgraph "Chat Module"
                E1[chat.py]:::backend
                E2[PromptTemplate.py]:::backend
            end
            F[config.py]:::backend
        end
    end

    %% Training Pipeline
    subgraph "Sentiment Training Pipeline" 
        G[Pipeline Root]:::ml
        subgraph "Pipeline Steps"
            G1[src/config.py]:::ml
            G2[src/dataset.py]:::ml
            G3[src/text_cleaning.py]:::ml
            G4[src/preprocessing.py]:::ml
            G5[src/model.py]:::ml
            G6[src/train.py]:::ml
            G7[src/evaluate.py]:::ml
            G8[src/main.py]:::ml
            G9[data/sentiment_data.csv]:::ml
        end
    end

    %% External Services
    subgraph "External Services"
        H[Groq - Data Generation]:::ext
        I[LLM Provider]:::ext
    end

    %% Data Flows
    A -->|"Upload PDF (HTTP POST)"| C
    C -->|"Extracted Text"| D1
    C -->|"Extracted Text"| E1
    D2 -->|"Loads Fine-Tuned Model"| D1
    D3 -->|"Extracts Text"| D1
    D1 -->|"Emotion Scores"| B
    E1 -->|"Chat Response"| B
    B -->|"API Response"| A

    %% Config Injection
    F --> C
    F --> D1
    F --> D2
    F --> D3
    F --> E1
    F --> E2

    %% External Integrations
    H -->|"Generates Data"| G
    G -->|"Fine-Tuned Model"| D2
    E1 -->|"LLM Calls"| I

    %% Click Events
    click A "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/app.py"
    click B "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/api.py"
    click C "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/pdf_processing.py"
    click D1 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/model_prediction.py"
    click D2 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment/model_loading.py"
    click D3 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment/text_extraction.py"
    click E1 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/chat.py"
    click E2 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/PromptTemplate.py"
    click F "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/config.py"
    click G "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/tree/main/sentiment_model_training/"
    click G1 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/config.py"
    click G2 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/dataset.py"
    click G3 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/text_cleaning.py"
    click G4 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/preprocessing.py"
    click G5 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/model.py"
    click G6 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/train.py"
    click G7 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/evaluate.py"
    click G8 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/src/main.py"
    click G9 "https://github.com/kuldip-1662/document-sentiment-analyzer-chatbot/blob/main/sentiment_model_training/data/sentiment_data.csv"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef backend fill:#90EE90,stroke:#333,stroke-width:1px
    classDef ml fill:#FFA500,stroke:#333,stroke-width:1px
    classDef ext fill:#D3D3D3,stroke:#333,stroke-width:1px
```
