Spaces:
Sleeping
Sleeping
Product Classification Service - Setup Guide
Overview
This service uses Google Gemini Vision AI to classify product condition from uploaded images. It determines whether products are resellable, refurbishable, or scrap.
Prerequisites
- Python 3.8+
- Google Gemini API key
Installation
1. Install Dependencies
cd backend
pip install -r requirements.txt
2. Get Gemini API Key
- Visit: https://makersuite.google.com/app/apikey
- Sign in with your Google account
- Click "Create API Key"
- Copy the generated key
3. Configure Environment
# Copy the example env file
cp .env.example .env
# Edit .env and add your Gemini API key
# GEMINI_API_KEY=your_actual_api_key_here
4. Test the Service
python manage.py shell
In the Python shell:
from store.gemini_classification_service import get_classification_service
# This will test if your API key works
service = get_classification_service()
print("Service initialized successfully!")
Usage
API Endpoint
POST /api/inference/
Request:
- Content-Type:
multipart/form-data - Body:
image(file)
Response:
{
"status": "resellable" // or "refurb" or "scrap"
}
Classification Logic
RESELLABLE
- Pristine or near-pristine condition
- No visible damage
- Can be sold as-is
REFURB (Refurbishable)
- Minor to moderate damage
- Repairable economically
- Needs cleaning or minor repairs
SCRAP
- Severe damage
- Not economically viable to repair
- Safety hazards
Frontend Integration
The Exchange.jsx page already integrates with this service:
const formData = new FormData();
formData.append('image', imageFile);
const response = await api.post('/inference/', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
});
console.log(response.data.status); // "resellable", "refurb", or "scrap"
Error Handling
The service includes robust error handling:
- If Gemini API fails, defaults to "refurb" (safe middle ground)
- Invalid responses are logged and fallback to "refurb"
- All errors are logged for monitoring
Cost Optimization
- Uses
gemini-1.5-flashfor cost-effective analysis - Low temperature (0.1) for consistent results
- Max 10 tokens output (only need one word)
To use higher accuracy model, edit gemini_classification_service.py:
self.model = genai.GenerativeModel('gemini-1.5-pro') # More accurate, higher cost
Monitoring
Check logs for classification results:
logger.info(f"Product classified as: {condition}")
Troubleshooting
"GEMINI_API_KEY not found"
- Ensure
.envfile exists in backend directory - Check that
GEMINI_API_KEYis set correctly - Restart Django server after updating
.env
"google-generativeai not installed"
pip install google-generativeai
Classification always returns "refurb"
- Check API key is valid
- Check internet connection
- Review logs for error messages
- Ensure image file is valid (JPEG, PNG)
Security Notes
- Never commit
.envfile to git (already in .gitignore) - Never share your API key publicly
- Rotate API keys regularly
- Use environment variables in production
Production Deployment
- Set
DEBUG=Falsein settings - Use production-grade API key with rate limits
- Add monitoring/alerting for classification failures
- Consider adding image size limits (max 10MB recommended)
- Add rate limiting to prevent API abuse