| # π 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) |
|
|