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 | |
| ```bash | |
| cd backend | |
| pip install -r requirements.txt | |
| ``` | |
| ### 2. Get Gemini API Key | |
| 1. Visit: https://makersuite.google.com/app/apikey | |
| 2. Sign in with your Google account | |
| 3. Click "Create API Key" | |
| 4. Copy the generated key | |
| ### 3. Configure Environment | |
| ```bash | |
| # 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 | |
| ```bash | |
| python manage.py shell | |
| ``` | |
| In the Python shell: | |
| ```python | |
| 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:** | |
| ```json | |
| { | |
| "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: | |
| ```javascript | |
| 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-flash` for 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`: | |
| ```python | |
| self.model = genai.GenerativeModel('gemini-1.5-pro') # More accurate, higher cost | |
| ``` | |
| ## Monitoring | |
| Check logs for classification results: | |
| ```python | |
| logger.info(f"Product classified as: {condition}") | |
| ``` | |
| ## Troubleshooting | |
| ### "GEMINI_API_KEY not found" | |
| - Ensure `.env` file exists in backend directory | |
| - Check that `GEMINI_API_KEY` is set correctly | |
| - Restart Django server after updating `.env` | |
| ### "google-generativeai not installed" | |
| ```bash | |
| 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 `.env` file to git (already in .gitignore) | |
| - Never share your API key publicly | |
| - Rotate API keys regularly | |
| - Use environment variables in production | |
| ## Production Deployment | |
| 1. Set `DEBUG=False` in settings | |
| 2. Use production-grade API key with rate limits | |
| 3. Add monitoring/alerting for classification failures | |
| 4. Consider adding image size limits (max 10MB recommended) | |
| 5. Add rate limiting to prevent API abuse | |