How yash2010/semantic-movie-recommendation-system works
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 FastAPI application · database.py Movie & feedback DB · local_expander.py Local expander · recommender.py Recommendation engine
Components 18
Catalog Ingestion
- TMDB API · Authenticated catalog source.
- Catalog pipeline · Fetches, processes, and exports movie records.
data_pipeline/pipeline.py - TMDB fetcher
data_pipeline/fetcher.py - Catalog exporter
data_pipeline/exporter.py - CSV-to-DB import
scripts/csv2db.py
Retrieval Core
- Movie & feedback DB · Owns movies, users, and interaction history.
database.py - movies.db · Shared catalog and feedback storage.
- Embedding index builder · Encodes catalog movies with all-MiniLM-L6-v2.
scripts/build_index.py - Embedding artifacts · Embeddings and movie metadata must retain catalog identity and order.
- Recommendation engine · Performs title, semantic, filtered, and similar-movie retrieval.
recommender.py
Query Expansion
- Expander interface
expanders/base.py - Local expander · Uses a trained checkpoint or falls back to Ollama.
expanders/local_expander.py - Ollama expander
expanders/ollama_expander.py - Local Ollama model
- Expander training · Trains seq2seq rewriting models from generated query pairs.
expander_train.py - Seq2seq transformer · Encoder-decoder model with attention and selectable tokenizers.
expander_model/expander_model.py
API Delivery
- FastAPI application · Loads runtime components and exposes search, recommendations, feedback, history, and health.
api.py - Browser UI · Served directly by FastAPI.
static/index.html
Connections 19
- 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