paramasivan27/LLM_Product_Classification
0
LLM Product Classification System
A sophisticated product classification system that uses Large Language Models (LLMs) and advanced NLP models to categorize retail products into Google Product Taxonomy categories. The system combines LLM-based classification with semantic similarity and keyword-based fallback for robust product categorization.
Features
- ๐ค LLM Integration: OpenAI GPT and Hugging Face models for intelligent classification
- ๐ Google Product Taxonomy: Uses official Google Product Taxonomy categories
- ๐ง Advanced NLP Models: Sentence transformers for semantic understanding
- ๐ Fallback System: Automatic fallback to keyword matching if LLMs fail
- ๐จ Modern UI: Beautiful Streamlit interface with LLM configuration
- ๐ณ Docker Support: Easy deployment with Docker and Docker Compose
- ๐ป CLI Interface: Command-line tool with LLM options
- ๐ง Flexible Configuration: Support for multiple LLM providers
LLM Providers
OpenAI
- Models: GPT-3.5-turbo, GPT-4, GPT-4-turbo
- Features: High accuracy, structured JSON responses
- Setup: Requires OpenAI API key
Hugging Face
- Models: DialoGPT, GPT-2, DistilGPT-2
- Features: Local deployment, no API costs
- Setup: Automatic model download
Google Product Taxonomy Categories
The system uses the official Google Product Taxonomy with 24 major categories:
- Animals & Pet Supplies
- Apparel & Accessories
- Arts & Entertainment
- Baby & Toddler
- Beauty & Personal Care
- Books & Magazines
- Business & Industrial
- Cameras & Optics
- Clothing, Shoes & Jewelry
- Computers & Electronics
- Food, Beverages & Tobacco
- Furniture
- Hardware
- Health & Beauty
- Home & Garden
- Luggage & Bags
- Mature
- Media
- Office Products
- Religious & Ceremonial
- Software
- Sporting Goods
- Toys & Games
- Vehicles & Parts
Installation
Option 1: Local Installation
- Clone the repository:
git clone <repository-url>
cd product_classifier- Install dependencies:
pip install -r requirements.txt- (Optional) Set up OpenAI API key:
export OPENAI_API_KEY='your-api-key-here'- Run the application:
streamlit run streamlit_ui.pyOption 2: Docker Installation
- Build and run with Docker Compose:
docker-compose up --build- Or build and run manually:
docker build -t product-classifier .
docker run -p 8501:8501 -e OPENAI_API_KEY=your-key product-classifierUsage
Web Interface
- Open your browser and go to
http://localhost:8501 - Configure LLM settings in the sidebar
- Enter product title and description
- Choose whether to use LLM classification
- Click "Classify Product" to get results
Command Line Interface
# Basic classification
python classifier.py --title "Men's Running Shoes" --description "Comfortable athletic shoes" --size "US 10"
# With OpenAI LLM
python classifier.py --title "iPhone 15 Pro" --description "Latest smartphone" --llm-type openai
# With Hugging Face LLM
python classifier.py --title "Gaming Laptop" --description "High-performance laptop" --llm-type huggingface
# Disable LLM (use fallback methods only)
python classifier.py --title "Product" --description "Description" --no-llmOptions:
--title: Product title (required)--description: Product description (required)--size: Size or additional info (optional)--image: Path to product image (optional)--llm-type: LLM provider ("openai", "huggingface", "none")--no-llm: Disable LLM classification--verbose: Enable verbose output
Programmatic Usage
from product_classifier.classifier import get_classifier
# With OpenAI
classifier = get_classifier(
llm_type="openai",
llm_config={"api_key": "your-key", "model": "gpt-3.5-turbo"}
)
result = classifier.classify_product(
title="iPhone 15 Pro",
description="Latest smartphone with advanced features",
size="256GB",
use_llm=True
)
print(f"Category: {result['category']}")
print(f"Method: {result['method']}")
print(f"Confidence: {result['confidence']}")Configuration
Environment Variables
OPENAI_API_KEY: Your OpenAI API key for LLM classification
LLM Configuration
OpenAI
llm_config = {
"api_key": "your-openai-api-key",
"model": "gpt-3.5-turbo" # or "gpt-4", "gpt-4-turbo"
}Hugging Face
llm_config = {
"model_name": "microsoft/DialoGPT-medium" # or "gpt2", "distilgpt2"
}Technical Details
Classification Methods
- LLM Classification: Uses OpenAI GPT or Hugging Face models for intelligent classification
- Semantic Classification: Uses sentence transformers to compute embeddings and find similar categories
- Keyword Classification: Fallback method using keyword matching when ML models are unavailable
Models Used
- Primary LLM: OpenAI GPT-3.5-turbo or Hugging Face DialoGPT
- Semantic:
sentence-transformers/all-MiniLM-L6-v2for semantic similarity - Fallback: Keyword-based matching with Google Product Taxonomy
Performance
- Accuracy: High accuracy with LLM classification
- Speed: Fast classification with pre-computed embeddings
- Reliability: Robust fallback system ensures classification always works
- Cost: OpenAI API calls incur costs, Hugging Face models are free
Development
Running Tests
pytest tests/Project Structure
product_classifier/
โโโ classifier.py # Main classification logic
โโโ llm_taxonomy.py # LLM-based taxonomy classification
โโโ taxonomy.py # Legacy taxonomy (for fallback)
โโโ streamlit_ui.py # Web interface
โโโ demo.py # Demo script
โโโ requirements.txt # Python dependencies
โโโ Dockerfile # Docker configuration
โโโ docker-compose.yml # Docker Compose configuration
โโโ README.md # This fileAdding New LLM Providers
- Create a new class inheriting from
LLMTaxonomyClassifier - Implement the
classify_productmethod - Add the provider to the
get_llm_classifierfactory function - Update the UI to support the new provider
Troubleshooting
Common Issues
- OpenAI API Key Issues: Ensure
OPENAI_API_KEYis set correctly - Model Download Issues: Hugging Face models will download automatically
- Memory Issues: LLM models require significant RAM
- Network Issues: OpenAI requires internet connection
Error Messages
- "OpenAI API key not found": Set the
OPENAI_API_KEYenvironment variable - "LLM classification failed": Check API key and network connection
- "Model not loaded": Ensure sufficient memory and disk space
Logs
Enable verbose logging:
python classifier.py --verbose --title "test" --description "test"Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
License
This project is licensed under the MIT License - see the LICENSE file for details.
