A full-stack Book Recommendation System powered by Word2Vec embeddings and cosine similarity. FastAPI backend, Streamlit frontend, Dockerized and ready for deployment.
-
POST /recommendGET /healthGET /stats
This project provides book recommendations using Word2Vec embeddings (to embed book descriptions) and cosine similarity (to find nearest neighbors). It exposes a FastAPI backend for recommendation queries and a Streamlit frontend for an interactive UI. The system is containerized with Docker and ready to deploy (e.g., Render, Heroku, or your preferred provider).
- Text-based recommendations from natural-language descriptions
- Configurable number of recommendations (1β20)
- FastAPI REST API with health and stats endpoints
- Streamlit web UI for quick experimentation
- Model serialization for fast startup
- Dockerfile for easy containerization
- Python 3.11+
- FastAPI (backend)
- Uvicorn (ASGI server)
- Streamlit (frontend)
- Gensim (Word2Vec)
- scikit-learn (cosine similarity)
- pandas / NumPy
- joblib (model serialization)
- Docker
- Python 3.11 or later
- pip (or pipx)
- Docker (optional, for containerized runs)
# clone the repo
git clone https://github.com/Manish3451/Book-Recommendation-System.git
cd Book-Recommendation-System
# create venv and activate
python -m venv .venv
# Windows
# .venv\Scripts\activate
# macOS / Linux
source .venv/bin/activate
# install dependencies
pip install -r requirements.txtRun backend (FastAPI + Uvicorn):
# from project root
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000Run Streamlit frontend (default port 8501):
streamlit run app/frontend.py --server.port 8501Open the Streamlit app at: http://localhost:8501
If you prefer to run both with a single command, consider using
tmux,make, or a small shell script.
Build the image and run containers (example):
# build image
docker build -t book-recs:latest .
# run container (exposes 8000)
docker run -p 8000:8000 book-recs:latestIf using docker-compose a docker-compose.yml may run both backend and frontend on their respective ports.
Open the Streamlit app at http://localhost:8501.
Choose your input method:
- Enter a book description in the text area
- Or select a book index from the dataset
Then:
- Set number of recommendations (1β20)
- Click Get Recommendations
- View results in table and list format
Python example
import requests
# Get recommendations by description
response = requests.post("http://localhost:8000/recommend", json={
"description": "A young wizard battles dark forces",
"num_recommendations": 5
})
recommendations = response.json()
print(recommendations)Try these sample descriptions:
- "A detective solving mysterious crimes in Victorian London"
- "Space exploration and alien civilizations"
- "Romance novel set in medieval times"
- "Coming of age story about friendship"
Get book recommendations based on description.
Request Body (JSON)
{
"description": "string",
"num_recommendations": 5
}Response (JSON)
{
"recommendations": [
{
"title": "Book Title",
"author": "Author Name",
"genre": "Genre",
"similarity_score": 0.95,
"description": "Book description..."
}
],
"query": "original query",
"total_found": 5
}Health check endpoint.
Response
{ "status": "healthy" }Get system statistics.
Response
{
"total_books": 1000,
"uptime": "2h 30m",
"requests_served": 150
}A suggested project layout β adapt to your repo:
book-recommendation-system/
βββ app/
β βββ main.py # FastAPI app entrypoint
β βββ api.py # route definitions
β βββ model.py # model loading & inference helpers
β βββ utils.py # utility functions
β βββ frontend.py # Streamlit front-end (if inside same repo)
β βββ data/
β βββ books.csv
β βββ embeddings.joblib
βββ Dockerfile
βββ docker-compose.yml
βββ requirements.txt
βββ README.md
βββ tests/
βββ test_api.py
Use environment variables for configurable parameters (example):
PORTβ backend port (default:8000)MODEL_PATHβ path to serialized embeddings or modelDATA_PATHβ path to dataset (CSV)
Example .env file:
PORT=8000
MODEL_PATH=app/data/embeddings.joblib
DATA_PATH=app/data/books.csvbooks.csvshould contain at minimum:title, author, genre, description.- Precompute embeddings for all descriptions and serialize them (e.g., with
joblib) for fast lookup.
Tips:
- Keep a mapping
index -> book metadataand aNumPymatrix of embeddings for similarity search. - Normalize and clean text before training Word2Vec (lowercase, remove punctuation, strip stopwords as appropriate).
Run unit tests with pytest:
pytest -qInclude tests for:
- API response shapes
- Similarity ranking sanity checks
- Model loading and health checks
Contributions welcome! Please open an issue or pull request with a clear description of changes.
Suggested workflow:
- Fork the repo
- Create a feature branch
feat/your-feature - Commit and push
- Open a Pull Request with a description and tests
This project is provided under the MIT License. See the LICENSE file for details.
Made with β€οΈ β happy recommending!

