# yash2010/semantic-movie-recommendation-system architecture

> Architecture diagram of the GitHub repository yash2010/semantic-movie-recommendation-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yash2010/semantic-movie-recommendation-system
- Repository: https://github.com/yash2010/semantic-movie-recommendation-system

## Overview

#### Purpose and Entry Points
This is a semantic movie recommendation service: it ingests TMDB catalog data, builds sentence-transformer embeddings, and serves title, semantic, and similarity search through FastAPI and a static browser UI. Runtime entry point is `api.py`; operational entry points include `scripts/fetch_tmdb.py`, `scripts/csv2db.py`, and `scripts/build_index.py`.

#### Data Acquisition and Preparation
The TMDB pipeline fetches multilingual movie records, applies filtering/normalization, and exports a durable catalog file with checkpointing for resumable collection. `data_pipeline/pipeline.py` orchestrates fetch → process → export, with TMDB access isolated in `data_pipeline/fetcher.py` and output handling in `data_pipeline/exporter.py`. Its external boundary is the TMDB API, authenticated by `TMDB_TOKEN`.

#### Catalog Storage and Feedback State
The imported catalog and user interaction state share SQLite storage. `scripts/csv2db.py` materializes processed catalog data into `data/movies.db`; `database.py` owns database access for movies, users, and click/rating/watch interactions. Feedback is currently persisted and exposed as history/statistics, rather than incorporated into recommendation ranking.

#### Retrieval Index and Recommendation Core
`scripts/build_index.py` encodes every movie using `sentence-transformers/all-MiniLM-L6-v2` and writes aligned embedding and movie metadata artifacts. `recommender.py` loads these artifacts with the movie database and performs title lookup, filtered semantic retrieval, cosine-similarity ranking, and movie-to-movie similarity. The critical artifact boundary is `artifacts/embeddings.npy` plus corresponding movie metadata; index generation must remain aligned with catalog identity/order.

#### Query Expansion Layer
Expansion is pluggable behind `expanders/base.py`. `expanders/ollama_expander.py` delegates vague natural-language queries to a locally served Ollama model, while `expanders/local_expander.py` loads a trained in-repo model and falls back to Ollama if no usable local checkpoint exists. Expansion precedes embedding only when requested/configured, creating a clean boundary between generative rewriting and deterministic retrieval.

#### Custom Expander Training Stack
The optional local expander is a PyTorch seq2seq transformer implemented from scratch: model assembly in `expander_model/expander_model.py`, encoder/decoder structure in `expander_model/encoder.py` and `expander_model/decoder.py`, and attention primitives in `expander_model/attention.py`. `expander_train.py` trains it from generated query-expansion pairs and saves versioned runs under artifacts; tokenizer selection is implemented under `expander_model/tokenizers/`.

#### API and Client Delivery
`api.py` creates the FastAPI application, manages startup loading, optional shared-secret request protection, and endpoints for recommendation, similar movies, catalog search, feedback, history, and health. It serves `static/index.html` directly, so the UI is a static client rather than a separate web runtime. API behavior and expander mode are controlled by `config/api.yaml`; key runtime dependencies are Python/FastAPI, SQLite, PyTorch, Sentence Transformers, Ollama (optional), and TMDB (ingestion only).

Main entry points: [`api.py`](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/api.py) (FastAPI application), [`database.py`](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/database.py) (Movie & feedback DB), [`expanders/local_expander.py`](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expanders/local_expander.py) (Local expander), [`recommender.py`](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/recommender.py) (Recommendation engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ingestion["Catalog Ingestion"]
  node_tmdb{{"TMDB API<br/>external API"}}
  node_pipeline["Catalog pipeline<br/>orchestrator<br/>[pipeline.py]"]
  node_fetcher["TMDB fetcher<br/>ingestion adapter<br/>[fetcher.py]"]
  node_exporter["Catalog exporter<br/>pipeline output<br/>[exporter.py]"]
  node_csv_import["CSV-to-DB import<br/>operational script<br/>[csv2db.py]"]
end

subgraph group_retrieval["Retrieval Core"]
  node_movie_db[("Movie &amp; feedback DB<br/>SQLite access layer<br/>[database.py]")]
  node_sqlite[("movies.db<br/>SQLite database")]
  node_index_builder["Embedding index builder<br/>operational script<br/>[build_index.py]"]
  node_artifacts["Embedding artifacts<br/>aligned index data"]
  node_recommender["Recommendation engine<br/>retrieval service<br/>[recommender.py]"]
end

subgraph group_expansion["Query Expansion"]
  node_expander_base["Expander interface<br/>pluggable boundary<br/>[base.py]"]
  node_local_expander["Local expander<br/>query rewriter<br/>[local_expander.py]"]
  node_ollama_expander["Ollama expander<br/>query rewriter<br/>[ollama_expander.py]"]
  node_ollama{{"Local Ollama model<br/>optional model service"}}
  node_expander_training["Expander training<br/>PyTorch training script<br/>[expander_train.py]"]
  node_seq2seq_model["Seq2seq transformer<br/>custom model<br/>[expander_model.py]"]
end

subgraph group_delivery["API Delivery"]
  node_api["FastAPI application<br/>service entry point<br/>[api.py]"]
  node_static_ui["Browser UI<br/>static client<br/>[index.html]"]
end

node_tmdb -->|"catalog records"| node_fetcher
node_fetcher -->|"fetched data"| node_pipeline
node_pipeline -->|"normalized catalog"| node_exporter
node_exporter -->|"durable catalog file"| node_csv_import
node_csv_import -->|"materializes catalog"| node_sqlite
node_movie_db -->|"reads and writes"| node_sqlite
node_sqlite -->|"catalog movies"| node_index_builder
node_index_builder -->|"embeddings + metadata"| node_artifacts
node_artifacts -->|"loads aligned index"| node_recommender
node_movie_db -->|"movie records"| node_recommender
node_expander_base -->|"implementation"| node_local_expander
node_local_expander -.->|"fallback"| node_ollama_expander
node_ollama_expander -->|"rewrites query"| node_ollama
node_expander_training -->|"trains"| node_seq2seq_model
node_expander_training -->|"versioned checkpoint"| node_local_expander
node_api -->|"optional expansion"| node_expander_base
node_api -->|"search and recommendations"| node_recommender
node_api -->|"feedback and history"| node_movie_db
node_api -->|"serves"| node_static_ui

click node_pipeline "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/data_pipeline/pipeline.py"
click node_fetcher "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/data_pipeline/fetcher.py"
click node_exporter "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/data_pipeline/exporter.py"
click node_csv_import "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/scripts/csv2db.py"
click node_movie_db "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/database.py"
click node_index_builder "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/scripts/build_index.py"
click node_recommender "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/recommender.py"
click node_expander_base "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expanders/base.py"
click node_local_expander "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expanders/local_expander.py"
click node_ollama_expander "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expanders/ollama_expander.py"
click node_expander_training "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expander_train.py"
click node_seq2seq_model "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expander_model/expander_model.py"
click node_api "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/api.py"
click node_static_ui "https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/static/index.html"

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_tmdb,node_pipeline,node_fetcher,node_exporter,node_csv_import toneBlue
class node_movie_db,node_sqlite,node_index_builder,node_artifacts,node_recommender toneAmber
class node_expander_base,node_local_expander,node_ollama_expander,node_ollama,node_expander_training,node_seq2seq_model toneMint
class node_api,node_static_ui toneRose
```

## Components

### Catalog Ingestion

- **TMDB API**: external API · Authenticated catalog source.
- [Catalog pipeline](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/data_pipeline/pipeline.py): orchestrator · Fetches, processes, and exports movie records. · `data_pipeline/pipeline.py`
- [TMDB fetcher](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/data_pipeline/fetcher.py): ingestion adapter · `data_pipeline/fetcher.py`
- [Catalog exporter](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/data_pipeline/exporter.py): pipeline output · `data_pipeline/exporter.py`
- [CSV-to-DB import](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/scripts/csv2db.py): operational script · `scripts/csv2db.py`

### Retrieval Core

- [Movie & feedback DB](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/database.py): SQLite access layer · Owns movies, users, and interaction history. · `database.py`
- **movies.db**: SQLite database · Shared catalog and feedback storage.
- [Embedding index builder](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/scripts/build_index.py): operational script · Encodes catalog movies with all-MiniLM-L6-v2. · `scripts/build_index.py`
- **Embedding artifacts**: aligned index data · Embeddings and movie metadata must retain catalog identity and order.
- [Recommendation engine](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/recommender.py): retrieval service · Performs title, semantic, filtered, and similar-movie retrieval. · `recommender.py`

### Query Expansion

- [Expander interface](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expanders/base.py): pluggable boundary · `expanders/base.py`
- [Local expander](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expanders/local_expander.py): query rewriter · Uses a trained checkpoint or falls back to Ollama. · `expanders/local_expander.py`
- [Ollama expander](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expanders/ollama_expander.py): query rewriter · `expanders/ollama_expander.py`
- **Local Ollama model**: optional model service
- [Expander training](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expander_train.py): PyTorch training script · Trains seq2seq rewriting models from generated query pairs. · `expander_train.py`
- [Seq2seq transformer](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/expander_model/expander_model.py): custom model · Encoder-decoder model with attention and selectable tokenizers. · `expander_model/expander_model.py`

### API Delivery

- [FastAPI application](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/api.py): service entry point · Loads runtime components and exposes search, recommendations, feedback, history, and health. · `api.py`
- [Browser UI](https://github.com/yash2010/semantic-movie-recommendation-system/blob/main/static/index.html): static client · Served directly by FastAPI. · `static/index.html`

## Connections

- TMDB API → TMDB fetcher: catalog records
- TMDB fetcher → Catalog pipeline: fetched data
- Catalog pipeline → Catalog exporter: normalized catalog
- Catalog exporter → CSV-to-DB import: durable catalog file
- CSV-to-DB import → movies.db: materializes catalog
- Movie & feedback DB → movies.db: reads and writes
- movies.db → Embedding index builder: catalog movies
- Embedding index builder → Embedding artifacts: embeddings + metadata
- Embedding artifacts → Recommendation engine: loads aligned index
- Movie & feedback DB → Recommendation engine: movie records
- Expander interface → Local expander: implementation
- Local expander → Ollama expander: fallback
- Ollama expander → Local Ollama model: rewrites query
- Expander training → Seq2seq transformer: trains
- Expander training → Local expander: versioned checkpoint
- FastAPI application → Expander interface: optional expansion
- FastAPI application → Recommendation engine: search and recommendations
- FastAPI application → Movie & feedback DB: feedback and history
- FastAPI application → Browser UI: serves
