A production-ready, content-based product recommendation engine with an interactive Streamlit frontend Link [ https://imhs14-product-recommender-app-dvqzp4.streamlit.app/ ]
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β 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 β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Python 3.11+ β check with
python3 --version - VS Code with the Python extension installed (recommended)
Place all four files in the same folder:
product_recommender/
βββ data_generator.py
βββ engine.py
βββ app.py
βββ requirements.txt
cd product_recommender
python3 -m venv venvsource venv/bin/activateYour terminal prompt will now show (venv).
pip install --upgrade pip
pip install -r requirements.txtM4 note: NumPy and scikit-learn ship native ARM64 wheels β no Rosetta overhead.
python data_generator.pyExpected output:
β
Generated 520 products β /path/to/product_recommender/products.csv
streamlit run app.pyStreamlit will open http://localhost:8501 in your default browser automatically.
| 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 |
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.
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
)
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.
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.
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.
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
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."
| 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 |
- Open the
product_recommender/folder: File β Open Folder - Select the Python interpreter:
Ctrl+Shift+Pβ Python: Select Interpreter β choose./venv/bin/python - Install recommended extensions: Python, Pylance, Ruff
To run the app from the VS Code integrated terminal:
source venv/bin/activate
streamlit run app.pyEdit the CATEGORIES dict in data_generator.py, add a hex colour to CATEGORY_COLOURS in app.py, then re-run python data_generator.py.
Replace products.csv with your own CSV. Ensure it contains the same column names: Product_ID, Product_Name, Category, Description, Tags, Price.
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.
| 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 |
MIT β free to use, modify, and distribute.