# farazkhanai/yt_sentiment_analysis architecture

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

- Interactive diagram: https://gitdiagram.com/farazkhanai/yt_sentiment_analysis
- Repository: https://github.com/farazkhanai/yt_sentiment_analysis

## Overview

This is a Chrome extension and Flask service for analyzing YouTube comment sentiment. The user opens a YouTube video and starts analysis from the popup; the extension obtains the video ID, requests a background job, then polls for progress and sentiment counts. The service fetches comments and replies, runs batched DistilBERT/ONNX inference, and reports results. The README also documents CPU-optimized inference and partial-result warnings. The model files are described as separately downloaded and are not present in the supplied tree; the map shows the documented dependency, not inspected model code.

Main entry points: [`backend/app.py`](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/app.py) (Flask API), [`extension/popup.js`](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/popup.js) (Popup UI and polling), [`extension/content.js`](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/content.js) (Video ID extraction), [`extension/background.js`](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/background.js) (API request worker)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_extension["Chrome extension"]
  node_popup["Popup UI and polling<br/>[popup.js]"]
  node_content["Video ID extraction<br/>[content.js]"]
  node_background["API request worker<br/>[background.js]"]
end

subgraph group_service["Analysis service"]
  node_flask["Flask API<br/>[app.py]"]
  node_jobs["Background job worker<br/>[app.py]"]
  node_jobstate["In-memory job state<br/>[app.py]"]
end

subgraph group_inference["Comment analysis"]
  node_videoid["Video ID parser<br/>[utils.py]"]
  node_commentfetch["Comment and reply fetcher<br/>[utils.py]"]
  node_analyzer["Sentiment analyzer<br/>[utils.py]"]
end

subgraph group_integrations["External systems"]
  node_model["DistilBERT ONNX files"]
  node_youtubeapi["YouTube Data API"]
  node_browser["Chrome runtime"]
end

node_user(("YouTube viewer"))
node_page["YouTube video page"]

node_user -->|"starts analysis"| node_popup
node_popup -->|"queries active tab"| node_browser
node_popup -->|"requests video ID"| node_content
node_content -->|"reads video URL"| node_page
node_content -->|"returns video ID"| node_popup
node_popup -->|"sends analysis request"| node_background
node_background -->|"POSTs video ID"| node_flask
node_flask -->|"parses URL if needed"| node_videoid
node_flask -->|"creates job record"| node_jobstate
node_flask -->|"starts worker"| node_jobs
node_jobs -->|"updates progress"| node_jobstate
node_jobs -->|"fetches comments"| node_commentfetch
node_commentfetch -->|"requests comments and replies"| node_youtubeapi
node_jobs -->|"predicts sentiment batches"| node_analyzer
node_analyzer -->|"loads and runs inference"| node_model
node_jobs -->|"stores final counts"| node_jobstate
node_popup -->|"polls job status"| node_flask
node_flask -->|"reads job status"| node_jobstate
node_flask -->|"returns progress or results"| node_popup

click node_popup "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/popup.js"
click node_content "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/content.js"
click node_background "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/background.js"
click node_flask "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/app.py"
click node_jobs "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/app.py"
click node_jobstate "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/app.py"
click node_videoid "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/utils.py"
click node_commentfetch "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/utils.py"
click node_analyzer "https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/utils.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_popup,node_content,node_background,node_user toneBlue
class node_flask,node_jobs,node_jobstate toneAmber
class node_videoid,node_commentfetch,node_analyzer toneMint
class node_model,node_youtubeapi,node_browser toneRose
class node_page toneIndigo
```

## Components

### Chrome extension

- [Popup UI and polling](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/popup.js): `extension/popup.js`
- [Video ID extraction](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/content.js): `extension/content.js`
- [API request worker](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/extension/background.js): `extension/background.js`

### Analysis service

- [Flask API](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/app.py): `backend/app.py`
- [Background job worker](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/app.py): `backend/app.py`
- [In-memory job state](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/app.py): `backend/app.py`

### Comment analysis

- [Video ID parser](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/utils.py): `backend/utils.py`
- [Comment and reply fetcher](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/utils.py): `backend/utils.py`
- [Sentiment analyzer](https://github.com/farazkhanai/yt_sentiment_analysis/blob/main/backend/utils.py): `backend/utils.py`

### External systems

- **DistilBERT ONNX files**
- **YouTube Data API**
- **Chrome runtime**

### Other components

- **YouTube viewer**
- **YouTube video page**

## Connections

- YouTube viewer → Popup UI and polling: starts analysis
- Popup UI and polling → Chrome runtime: queries active tab
- Popup UI and polling → Video ID extraction: requests video ID
- Video ID extraction → YouTube video page: reads video URL
- Video ID extraction → Popup UI and polling: returns video ID
- Popup UI and polling → API request worker: sends analysis request
- API request worker → Flask API: POSTs video ID
- Flask API → Video ID parser: parses URL if needed
- Flask API → In-memory job state: creates job record
- Flask API → Background job worker: starts worker
- Background job worker → In-memory job state: updates progress
- Background job worker → Comment and reply fetcher: fetches comments
- Comment and reply fetcher → YouTube Data API: requests comments and replies
- Background job worker → Sentiment analyzer: predicts sentiment batches
- Sentiment analyzer → DistilBERT ONNX files: loads and runs inference
- Background job worker → In-memory job state: stores final counts
- Popup UI and polling → Flask API: polls job status
- Flask API → In-memory job state: reads job status
- Flask API → Popup UI and polling: returns progress or results
