# saitejasrivilli/sentimentanalysis architecture

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

- Interactive diagram: https://gitdiagram.com/saitejasrivilli/sentimentanalysis
- Repository: https://github.com/saitejasrivilli/sentimentanalysis

## Overview

##### Project's Main Purpose and Type
- This is a full-stack web application focused on sentiment analysis. Its primary function is to receive user text input through an HTML interface and return a sentiment prediction using a PyTorch LSTM model (with attention and negation handling).
- The backend is built with FastAPI, and it deploys using Docker, indicating a production-ready, scalable design.

##### File Structure
- Top-level files: README.md (project description), main.py (likely the entry point for the FastAPI app), analyzer.py (probably contains the logic to preprocess input and run the sentiment analysis using the PyTorch models), and requirements.txt (dependency listing).
- Model Files: ‘sentiment_model_with_negations.pth’ and ‘sentiment_pytorch_model.pth’ represent the trained models.
- Templates Directory: Contains HTML files (form.html for user input and result.html to display outputs) that comprise the frontend.

##### Main Components to Illustrate
a. User Interface (Frontend):
- Web Browser: The client interacts with the application.
- HTML Templates: form.html and result.html serve as the input form and display page, respectively.

b. Backend Server:
- FastAPI Application (main.py): This is the primary entry point handling HTTP requests.
- Analyzer Module (analyzer.py): Contains the core sentiment analysis logic. It loads the PyTorch models from the .pth files and uses them to predict sentiment.

c. Inference Models:
- Two Model Files: Represent the underlying sentiment analysis techniques (one that handles negations explicitly).

d. (Optional) Deployment Infrastructure:
- Docker: Although not directly visible in the file tree, the README mentions Docker for deployment; include a container boundary that wraps the entire backend service.

##### Relationships and Interactions
- A typical flow begins when the user inputs text via the frontend form. The input is sent over HTTP (or AJAX) to an endpoint provided by the FastAPI backend.
- The FastAPI application (in main.py) then calls functions in analyzer.py to process the text, load the appropriate PyTorch LSTM models, and run inference.
- The resulting sentiment (Positive/Negative) is packaged into a response and rendered via result.html on the frontend.
- Dependencies such as PyTorch and FastAPI (as listed in requirements.txt) underpin these interactions.

##### Architectural Patterns and Design Principles
- Separation of their concerns is evident: the frontend (templates) is decoupled from the backend (business logic in analyzer.py and main.py).
- The structure aligns with a typical Model-View-Controller (MVC)-like pattern even if loosely implemented.
- Use of containerization via Docker ensures scalability and deployability.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        User("User Browser"):::frontend
        Form("Form Template"):::frontend
        Result("Result Template"):::frontend
    end

    %% Backend Layer inside Docker Container
    subgraph "Backend (Docker Container)"
        API("FastAPI Server (main.py)"):::backend
        Analyzer("Sentiment Analyzer (analyzer.py)"):::backend
        subgraph "Inference Models"
            ModelNeg("PyTorch LSTM Model with Negation"):::model
            ModelStd("PyTorch LSTM Model"):::model
        end
    end

    %% Data Flow
    User -->|"submits_data"| Form
    Form -->|"POST_/analyze"| API
    API -->|"calls_analyzer"| Analyzer
    Analyzer -->|"loads_model"| ModelNeg
    Analyzer -->|"loads_model"| ModelStd
    Analyzer -->|"returns_sentiment"| API
    API -->|"renders_result"| Result

    %% Click Events
    click Form "https://github.com/saitejasrivilli/sentimentanalysis/blob/main/templates/form.html"
    click Result "https://github.com/saitejasrivilli/sentimentanalysis/blob/main/templates/result.html"
    click API "https://github.com/saitejasrivilli/sentimentanalysis/blob/main/main.py"
    click Analyzer "https://github.com/saitejasrivilli/sentimentanalysis/blob/main/analyzer.py"
    click ModelNeg "https://github.com/saitejasrivilli/sentimentanalysis/blob/main/sentiment_model_with_negations.pth"
    click ModelStd "https://github.com/saitejasrivilli/sentimentanalysis/blob/main/sentiment_pytorch_model.pth"

    %% Styles
    classDef frontend fill:#f9f,stroke:#333,stroke-width:2px;
    classDef backend fill:#ccf,stroke:#333,stroke-width:2px;
    classDef model fill:#cfc,stroke:#333,stroke-width:2px;
```
