File size: 3,507 Bytes
e7b5120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# 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