A FastAPI-powered product recommendation system with PostgreSQL backend. The system provides text-based search functionality and generates product recommendations using both content-based (text similarity) and collaborative filtering approaches.
- Programming Language: Python
- Frameworks: FastAPI/Flask/Django (depending on your implementation)
- Database: PostgreSQL/MySQL
- Libraries:
- Sentence Transformers: For generating embeddings from textual data.
- Scikit-learn: For calculating cosine similarity.
- NLTK: For tokenizing and cleaning text data.
- Levenshtein: For fuzzy string matching in search.
- SQLAlchemy: For database ORM.
- Other Tools:
- Docker: For containerized deployments (if applicable).
- Cloud Storage: (Optional, for scalable deployments)
The Recommendation Service combines collaborative filtering with semantic similarity using pre-trained text embeddings.
-
Collaborative Filtering:
- Identifies products purchased by a customer.
- Recommends similar products based on shared tags or attributes.
-
Semantic Similarity:
- Preprocesses product descriptions and user inputs.
- Converts texts into vector embeddings using a pre-trained model (
paraphrase-MiniLM-L6-v2). - Measures cosine similarity between product vectors and user preferences.
- Filters and ranks products based on similarity scores.
- Input: User Query (e.g., "Wireless headphones")
- Output: Top N recommendations based on matching product descriptions, tags, and categories.
The Search Service allows users to search for products using flexible filters and fuzzy matching.
-
Fuzzy Matching:
- Compares the user's search term with product names, descriptions, tags, and brands.
- Computes similarity scores using the Levenshtein distance.
-
Search Filtering:
- Filters results by product attributes such as category, brand, and price range.
-
Sorting and Ranking:
- Results are sorted based on fuzzy matching scores and presented to the user.
- Input: Search Term (e.g., "Bluetooth earbuds"), Filters (e.g., Category: "Electronics", Price: "< 3000")
- Output: Sorted product list matching the query and filters.
Let me know if you’d like additional refinements!
- Text-based product search with fuzzy matching
- Content-based recommendations using sentence transformers
- Collaborative filtering based on user purchase history
- Complete CRUD operations for products, customers, and transactions
- Sample data generation utility
- Detailed API documentation with OpenAPI/Swagger UI
- Python (3.8 - 3.11)
- Note: Python 3.12 is not yet fully supported by all dependencies
- PostgreSQL 12+
- Poetry (recommended) or pip
- Clone the repository:
git clone https://github.com/yourusername/product-recommendation-system.git
cd product-recommendation-system- Create and activate a virtual environment:
python -m venv venv
source venv/bin/activate # Linux/Mac
# or
.\venv\Scripts\activate # Windows- Install dependencies:
pip install -r requirements.txt-
Set up PostgreSQL:
- Create a new PostgreSQL database
- Update the database connection settings in
.envfile (use.env.exampleas template)
-
Generate sample data:
python -m app.utils.data_generator- Start the API server:
uvicorn app.main:app --reload- Access the API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
GET /api/v1/search/: Search products with optional filtersGET /api/v1/recommendations/similar/: Get similar products based on text similarityGET /api/v1/recommendations/collaborative/{customer_id}: Get recommendations based on purchase history
GET /api/v1/products/: List all productsGET /api/v1/products/{product_id}: Get specific product
GET /api/v1/customers/: List all customers
GET /api/v1/transactions/: List all transactionsGET /api/v1/transactions/customer/{customer_id}: Get customer's transactions
product_recommendation_system/
├── app/
│ ├── api/
│ │ ├── __init__.py
│ │ └── endpoints.py
│ ├── core/
│ │ ├── __init__.py
│ │ └── config.py
│ ├── db/
│ │ ├── __init__.py
│ │ └── base.py
│ ├── models/
│ │ ├── __init__.py
│ │ ├── customer.py
│ │ ├── product.py
│ │ └── transaction.py
│ ├── schemas/
│ │ ├── __init__.py
│ │ ├── customer.py
│ │ ├── product.py
│ │ └── transaction.py
│ ├── services/
│ │ ├── __init__.py
│ │ ├── recommendation.py
│ │ └── search.py
│ ├── utils/
│ │ ├── __init__.py
│ │ └── data_generator.py
│ ├── __init__.py
│ └── main.py
├── requirements.txt
└── README.md
Create a .env file in the project root with the following variables:
POSTGRES_SERVER=localhost
POSTGRES_USER=your_user
POSTGRES_PASSWORD=your_password
POSTGRES_DB=product_recommendation
POSTGRES_PORT=5432