File size: 5,371 Bytes
72b9a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# πŸ”— Setup Koneksi Supabase untuk Python API

Python API sekarang bisa menyimpan hasil prediksi langsung ke Supabase database.

## πŸ“‹ Yang Dibutuhkan

Anda perlu 2 environment variables:
1. `SUPABASE_URL` - URL project Supabase Anda
2. `SUPABASE_ANON_KEY` - Anonymous key dari Supabase

## πŸ”‘ Cara Mendapatkan Credentials

### Dari File .env Project

Credentials sudah ada di file `.env` project Anda:

```
SUPABASE_URL=https://xyddxrfiacdcnipdclas.supabase.co
SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```

### Atau dari Supabase Dashboard

1. Login ke https://supabase.com
2. Pilih project Anda
3. Sidebar β†’ Settings β†’ API
4. Copy:
   - **Project URL** β†’ `SUPABASE_URL`
   - **anon public** key β†’ `SUPABASE_ANON_KEY`

## πŸš€ Setup per Platform Deployment

### Hugging Face Spaces

1. **Via Web Interface:**
   - Buka Space Settings
   - Tab "Variables and secrets"
   - Klik "New secret"
   - Tambahkan 2 secrets:

   ```
   Name: SUPABASE_URL
   Value: https://xyddxrfiacdcnipdclas.supabase.co

   Name: SUPABASE_ANON_KEY
   Value: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
   ```

2. **Restart Space** untuk apply changes

### Railway

```bash
# Set secrets via CLI
railway variables set SUPABASE_URL=https://xyddxrfiacdcnipdclas.supabase.co
railway variables set SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

# Atau via Dashboard:
# Project β†’ Variables β†’ Add Variable
```

### Google Cloud Run

```bash
gcloud run services update palm-classifier \
  --set-env-vars SUPABASE_URL=https://xyddxrfiacdcnipdclas.supabase.co,SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```

### Local Testing

Buat file `.env` di folder `python-api/`:

```env
PORT=5000
SUPABASE_URL=https://xyddxrfiacdcnipdclas.supabase.co
SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
```

Lalu jalankan dengan:

```bash
cd python-api
pip install python-dotenv
python -c "from dotenv import load_dotenv; load_dotenv(); import app"
# Atau install dan gunakan python-dotenv di app.py
```

## βœ… Verifikasi Setup

### 1. Check Logs saat Startup

Setelah deploy/restart, cek logs. Anda harus melihat:

```
βœ“ Supabase client initialized
βœ“ SVM model loaded successfully
βœ“ Metadata loaded: ['3 Bulan', '6 Bulan', '9 Bulan']
```

Jika **TIDAK** melihat "βœ“ Supabase client initialized", berarti credentials belum diset dengan benar.

### 2. Test API Endpoint

```bash
# Health check
curl https://your-api-url.com/health

# Response harus include supabase status
{
  "status": "healthy",
  "model_loaded": true,
  "device": "cpu",
  "classes": ["3 Bulan", "6 Bulan", "9 Bulan"]
}
```

### 3. Test Classification & Save

```bash
# Classify image (dengan base64 image)
curl -X POST https://your-api-url.com/classify \
  -H "Content-Type: application/json" \
  -d '{"image": "data:image/jpeg;base64,..."}'

# Response harus include:
{
  "predicted_class": "6 Bulan",
  "confidence": 0.8543,
  "probabilities": {...},
  "mode": "real",
  "saved_to_db": true,    ← Ini yang penting!
  "id": "uuid-here"       ← ID dari database
}
```

Jika `saved_to_db: false`, berarti ada masalah dengan koneksi Supabase.

### 4. Check Database

1. Login Supabase Dashboard
2. Table Editor β†’ predictions
3. Anda harus melihat record baru setiap kali classify

## πŸ“Š Data yang Disimpan

Setiap prediction menyimpan:

```json
{
  "id": "uuid",
  "predicted_class": "6 Bulan",
  "confidence": 0.8543,
  "probabilities": {
    "3 Bulan": 0.0812,
    "6 Bulan": 0.8543,
    "9 Bulan": 0.0645
  },
  "mode": "real",
  "image_data": "data:image/...",  // First 1000 chars
  "created_at": "2024-01-01T12:00:00Z"
}
```

## ⚠️ Troubleshooting

### "Supabase credentials not found"

**Problem:** Environment variables tidak diset

**Solution:**
- Cek apakah sudah set `SUPABASE_URL` dan `SUPABASE_ANON_KEY`
- Restart service setelah set env vars
- Verifikasi nilai env vars tidak ada typo

### "Failed to initialize Supabase"

**Problem:** Credentials salah atau URL tidak valid

**Solution:**
- Double check URL dan key dari Supabase Dashboard
- Pastikan URL format: `https://xxx.supabase.co`
- Pastikan key adalah anon key (public), bukan service role key

### "saved_to_db: false"

**Problem:** API tidak bisa save ke database

**Solution:**
- Cek RLS policies di table `predictions`
- Pastikan anon role punya permission INSERT
- Cek logs untuk error message detail

### RLS Policy Error

Jika ada error "new row violates row-level security policy":

```sql
-- Tambahkan policy di Supabase SQL Editor:
CREATE POLICY "Allow anon insert"
ON predictions
FOR INSERT
TO anon
WITH CHECK (true);
```

## πŸ” Security Notes

1. **NEVER commit credentials** ke git (sudah di .gitignore)
2. **Use environment variables** untuk semua secrets
3. **Anon key is safe** untuk public API karena dilindungi RLS
4. **Monitor usage** via Supabase Dashboard

## πŸ’‘ Optional: Disable Database Saving

Jika Anda tidak ingin save ke database, cukup **jangan set** environment variables.

API akan tetap berjalan normal, tapi tidak save predictions ke database.

Response akan include: `"saved_to_db": false`

## πŸ“š Reference

- [Supabase Python Client Docs](https://supabase.com/docs/reference/python/introduction)
- [Environment Variables Best Practices](https://12factor.net/config)
- [RLS Policies Guide](https://supabase.com/docs/guides/auth/row-level-security)