A high-performance, modular FastAPI backend for e-commerce intelligence. This repository contains advanced machine learning models for search, product recommendations, and basket analysis.
- BM25 Keyword Search: Robust keyword matching for relevant product discovery.
- FAISS Semantic Similarity: Vector-based search using
nomic-embed-textembeddings from Ollama. - Typo Tolerance: Hybrid scoring mechanism to handle search inaccuracies.
- Retrieval-Augmented Generation: Treats shopping as a retrieval problem across a 3-million-basket historical corpus.
- Contrastive Learning: Uses a Transformer-based
BasketEncodertrained with NT-Xent loss to map shopping intent to dense vectors. - Maximal Marginal Relevance (MMR): Ensures diversity in retrieved candidates to prevent redundant recommendations.
- Faron's F1 Optimization: Dynamically determines the optimal number of items to recommend for maximum precision/recall.
- Autoregressive Transformer: A lightweight PyTorch-based model for predicting next-item probability sequences.
- RoPE Embeddings: Utilizes Rotary Position Embeddings for high-quality sequence modeling.
- Clean FastAPI routing (
/search,/recommendations,/basket-rag). - Environment-based configuration using
.env. - Sequential "Three-Engine Boot" process to prevent OpenMP thread collisions.
- Scalable backend for processing large product catalogs (up to 50k+ items).
- Framework: FastAPI, Uvicorn
- Search Logic: Rank-BM25, FAISS (CPU)
- ML/DL: PyTorch, Transformers, Scikit-learn
- Embeddings: Ollama (nomic-embed-text)
- Data Handling: Pandas
-
Clone the Repository:
git clone https://github.com/HamzaDevv/E-commerce-models.git cd E-commerce-models -
Set up Virtual Environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install Dependencies:
pip install -r ml_backend/requirements.txt
-
Environment Configuration: Create a
.envfile in theml_backend/directory:GEMINI_API_KEY=your_gemini_api_key
-
Run the Server (Development):
cd ml_backend uvicorn main:app --reload --port 8000 -
Run the Server (Production): The application uses a strict sequential
lifespaninitialization to prevent OpenMP crashes (PyTorch + Faiss collisions) and manages CPU-bound inference efficiently.cd ml_backend uvicorn main:app --workers 4 --host 0.0.0.0 --port 8000Note: The boot sequence follows: BasketGPT → Basket-RAG → Hybrid Search.
.
├── ml_backend/
│ ├── main.py # Entry point with sequential engine boot
│ ├── routers/ # Search, Recommendation, and Basket-RAG routers
│ ├── models/ # Transformer (GPT) and Contrastive (RAG) architectures
│ ├── data/ # (Excluded) Model weights and Vector Indices
│ └── requirements.txt # Python dependencies (pinned)
├── Dataset/ # (Excluded) Raw CSV datasets
├── Docs/ # Detailed Model Architectures & Journey
│ └── Model_readme/ # READMEs for BasketGPT and Basket-RAG
└── README.md
- Large datasets (
Dataset/) and generated indices (ml_backend/data/) are excluded via.gitignoreto maintain repository speed and size. - Ensure Ollama is running locally if using semantic search features.