Skip to content

Latest commit

Β 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›οΈ Product Recommendation System

A production-ready, content-based product recommendation engine with an interactive Streamlit frontend Link [ https://imhs14-product-recommender-app-dvqzp4.streamlit.app/ ]

πŸ“ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        app.py  (UI Layer)                   β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚   β”‚  Searchable  β”‚  β”‚  Recommendation  β”‚  β”‚ Cold Start  β”‚  β”‚
β”‚   β”‚  Dropdown    β”‚  β”‚  Gallery (grid)  β”‚  β”‚  Trending   β”‚  β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚ calls
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     engine.py  (ML Layer)                   β”‚
β”‚                                                             β”‚
β”‚   products.csv  β†’  TfidfVectorizer  β†’  Cosine Similarity    β”‚
β”‚                     (ngram 1-2)         matrix (float32)    β”‚
β”‚                                                             β”‚
β”‚   get_recommendations(product_id, top_n) β†’ pd.DataFrame    β”‚
β”‚   get_trending_products(n)               β†’ pd.DataFrame    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚ reads
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                 data_generator.py  (Data Layer)             β”‚
β”‚                                                             β”‚
β”‚   8 categories Γ— adj/noun combos β†’ 520 products β†’ CSV      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start (macOS / Apple M4)

Prerequisites

  • Python 3.11+ β€” check with python3 --version
  • VS Code with the Python extension installed (recommended)

Step 1 β€” Clone / Download the project

Place all four files in the same folder:

product_recommender/
β”œβ”€β”€ data_generator.py
β”œβ”€β”€ engine.py
β”œβ”€β”€ app.py
└── requirements.txt

Step 2 β€” Create a virtual environment

cd product_recommender
python3 -m venv venv

Step 3 β€” Activate the virtual environment

source venv/bin/activate

Your terminal prompt will now show (venv).

Step 4 β€” Install dependencies

pip install --upgrade pip
pip install -r requirements.txt

M4 note: NumPy and scikit-learn ship native ARM64 wheels β€” no Rosetta overhead.

Step 5 β€” Generate the synthetic dataset

python data_generator.py

Expected output:

βœ…  Generated 520 products β†’ /path/to/product_recommender/products.csv

Step 6 β€” Launch the Streamlit app

streamlit run app.py

Streamlit will open http://localhost:8501 in your default browser automatically.


πŸ“ File Reference

File Purpose
data_generator.py Generates products.csv with 520 synthetic products across 8 categories
engine.py ML recommendation engine (TF-IDF + Cosine Similarity, caching, public API)
app.py Interactive Streamlit UI (dropdown, gallery, cold-start section)
requirements.txt Pinned Python dependencies

🧠 How the Recommendation Engine Works

1. Corpus Construction

Each product's Description and Tags fields are concatenated into a single text corpus. Tags are repeated once to give them additional TF-IDF weight relative to prose descriptions.

2. TF-IDF Vectorisation

TfidfVectorizer(
    ngram_range=(1, 2),   # unigrams + bigrams
    sublinear_tf=True,    # log(1 + tf) dampening
    stop_words="english",
    max_features=8_000,   # vocabulary cap for M4 RAM
)

3. Cosine Similarity Matrix

A full n Γ— n pairwise cosine similarity matrix is computed once and stored as float32 (halves memory vs float64). On 520 products this is a ~1 MB matrix β€” trivial on M4.

4. Top-N Retrieval

numpy.argpartition (O(n) partial sort) is used instead of a full argsort for efficient top-N extraction. Results are cached with functools.lru_cache so repeated queries cost nothing.


πŸ–₯️ UI Features

Feature 1 β€” Searchable Product Dropdown

A Streamlit selectbox lists all products. Type any substring (name, category, price) to instantly filter the list. Category and price filters in the sidebar further narrow the selection.

Feature 2 β€” Recommendation Gallery

Once a product is selected, the engine returns the Top-N most similar products (configurable 1–10 via sidebar slider). Results render in a responsive 5-column card grid, each showing:

  • Category, name, truncated description
  • Tag pills
  • Price and similarity percentage badge

Feature 3 β€” Cold Start / Trending Section

When no product is selected, a curated "Trending Right Now" section is shown. It selects the highest-priced product per category (a proxy for premium/popular items) to ensure cross-category variety. After a product is selected this section remains as "Also Trending."


βš™οΈ Configuration

Constant File Default Description
NUM_PRODUCTS data_generator.py 520 Total products generated
TOP_N_DEFAULT engine.py 5 Default recommendation count
max_features engine.py 8_000 TF-IDF vocabulary cap
RANDOM_SEED data_generator.py 42 Reproducibility seed

πŸ”§ VS Code Setup (Recommended)

  1. Open the product_recommender/ folder: File β†’ Open Folder
  2. Select the Python interpreter: Ctrl+Shift+P β†’ Python: Select Interpreter β†’ choose ./venv/bin/python
  3. Install recommended extensions: Python, Pylance, Ruff

To run the app from the VS Code integrated terminal:

source venv/bin/activate
streamlit run app.py

πŸ› οΈ Extending the System

Add a new product category

Edit the CATEGORIES dict in data_generator.py, add a hex colour to CATEGORY_COLOURS in app.py, then re-run python data_generator.py.

Swap to a real dataset

Replace products.csv with your own CSV. Ensure it contains the same column names: Product_ID, Product_Name, Category, Description, Tags, Price.

Add collaborative filtering

Implement a user-item interaction matrix in a new collaborative_engine.py and surface a toggle in app.py to switch between content-based and collaborative recommendations.


πŸ“¦ Dependencies

Package Version Purpose
scikit-learn β‰₯ 1.4 TfidfVectorizer, cosine_similarity
numpy β‰₯ 1.26 Matrix ops, argpartition
pandas β‰₯ 2.2 Data loading and manipulation
streamlit β‰₯ 1.35 Interactive web UI

πŸ“„ License

MIT β€” free to use, modify, and distribute.

About

An end-to-end recommendation engine built with Python and Streamlit designed to deliver personalized item suggestions using collaborative and content-based filtering techniques. The system processes transactional user-item interactions to surface relevant products in real time through an interactive web dashboard.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages