Spaces:
Running
Running
github-actions[bot] commited on
Commit Β·
7621639
1
Parent(s): cc89c9b
Sync Flask API from GitHub 89eecab3443e80b69ba2fa5c60e62824483be099
Browse files
README.md
CHANGED
|
@@ -6,104 +6,230 @@ colorTo: blue
|
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
# π€ Flask API - BogorXplore Recommendation Engine
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
cd flask_api
|
| 19 |
pip install -r requirements.txt
|
| 20 |
python app.py
|
| 21 |
-
# API runs on http://localhost:5000
|
| 22 |
```
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
| Method | Endpoint | Deskripsi |
|
| 27 |
-
|--------|----------|-----------|
|
| 28 |
-
| GET | `/` | Health check |
|
| 29 |
-
| GET | `/api/places` | Get all places (paginated) |
|
| 30 |
-
| GET | `/api/places/<id>` | Get place detail |
|
| 31 |
-
| GET | `/api/search?q=<query>` | Semantic search |
|
| 32 |
-
| POST | `/api/recommendations` | Get ML recommendations |
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
```
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
```
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
```
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
```
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
```
|
| 46 |
-
POST /api/recommendations
|
| 47 |
-
Content-Type: application/json
|
| 48 |
|
|
|
|
|
|
|
|
|
|
| 49 |
{
|
| 50 |
-
|
| 51 |
-
|
| 52 |
}
|
| 53 |
```
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
**Content-Based Filtering dengan N-Gram + IndoBERT:**
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|----------|-----------|
|
| 61 |
-
| **N-Gram** | Unigram, Bigram, Trigram + TF-IDF (5000 features) |
|
| 62 |
-
| **IndoBERT** | Sentence embedding 768 dimensi |
|
| 63 |
-
| **Similarity** | Cosine Similarity |
|
| 64 |
-
| **Formula** | `sim_final = 0.5 Γ sim_ngram + 0.5 Γ sim_indobert` |
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
```
|
| 69 |
flask_api/
|
| 70 |
-
βββ
|
|
|
|
| 71 |
βββ models/
|
| 72 |
β βββ __init__.py
|
| 73 |
-
β βββ
|
| 74 |
-
β βββ
|
| 75 |
-
βββ
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
```
|
| 84 |
|
| 85 |
-
##
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
- PyTorch
|
| 92 |
- Sastrawi
|
| 93 |
-
-
|
| 94 |
|
| 95 |
-
##
|
| 96 |
|
| 97 |
-
|
| 98 |
-
---
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
short_description: Flask API for Bogor tourism recommendations
|
| 107 |
-
---
|
| 108 |
|
| 109 |
-
|
|
|
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
+
license: mit
|
| 10 |
+
short_description: Flask API for Bogor tourism recommendations
|
| 11 |
---
|
|
|
|
| 12 |
|
| 13 |
+
# BogorXplore Flask API
|
| 14 |
+
|
| 15 |
+
Flask API untuk data wisata, pencarian semantik, dan rekomendasi destinasi
|
| 16 |
+
BogorXplore. API ini dipakai oleh aplikasi Laravel di `../web_recommendation`.
|
| 17 |
+
|
| 18 |
+
## Ringkasan
|
| 19 |
|
| 20 |
+
- Local development port: `5000`.
|
| 21 |
+
- Docker/Hugging Face Space port: `7860`.
|
| 22 |
+
- Dataset mentah: 296 destinasi dari `data/bogor_tourism_data.csv`.
|
| 23 |
+
- Data rekomendasi efektif: 295 destinasi setelah baris dengan teks kosong
|
| 24 |
+
difilter saat startup.
|
| 25 |
+
- Search memakai IndoBERT similarity/query embedding.
|
| 26 |
+
- Rekomendasi detail memakai precomputed N-Gram similarity.
|
| 27 |
|
| 28 |
+
## Quick Start Lokal
|
| 29 |
+
|
| 30 |
+
Dari root repo:
|
| 31 |
+
|
| 32 |
+
```powershell
|
| 33 |
cd flask_api
|
| 34 |
pip install -r requirements.txt
|
| 35 |
python app.py
|
|
|
|
| 36 |
```
|
| 37 |
|
| 38 |
+
API berjalan di:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
```text
|
| 41 |
+
http://localhost:5000
|
| 42 |
```
|
| 43 |
+
|
| 44 |
+
Catatan: startup pertama dapat lebih lama karena model IndoBERT
|
| 45 |
+
`indobenchmark/indobert-base-p1` dimuat oleh Transformers.
|
| 46 |
+
|
| 47 |
+
## Endpoint
|
| 48 |
+
|
| 49 |
+
| Method | Endpoint | Fungsi |
|
| 50 |
+
| --- | --- | --- |
|
| 51 |
+
| GET | `/` | Health check dan daftar endpoint utama. |
|
| 52 |
+
| GET | `/api/places` | Daftar destinasi dengan pagination dan filter kategori. |
|
| 53 |
+
| GET | `/api/places/<id>` | Detail destinasi berdasarkan index internal API. |
|
| 54 |
+
| GET | `/api/search?q=<query>` | Pencarian semantik berbasis IndoBERT. |
|
| 55 |
+
| POST | `/api/recommendations` | Rekomendasi destinasi berdasarkan `place_name` atau `place_id`. |
|
| 56 |
+
|
| 57 |
+
## Contoh Request
|
| 58 |
+
|
| 59 |
+
Health check:
|
| 60 |
+
|
| 61 |
+
```powershell
|
| 62 |
+
curl http://localhost:5000/
|
| 63 |
```
|
| 64 |
|
| 65 |
+
Daftar destinasi:
|
| 66 |
+
|
| 67 |
+
```powershell
|
| 68 |
+
curl "http://localhost:5000/api/places?limit=20&offset=0&category=Alam"
|
| 69 |
```
|
| 70 |
+
|
| 71 |
+
Pencarian semantik:
|
| 72 |
+
|
| 73 |
+
```powershell
|
| 74 |
+
curl "http://localhost:5000/api/search?q=air%20terjun&limit=10"
|
| 75 |
```
|
| 76 |
|
| 77 |
+
Rekomendasi:
|
| 78 |
+
|
| 79 |
+
```powershell
|
| 80 |
+
curl -X POST "http://localhost:5000/api/recommendations" `
|
| 81 |
+
-H "Content-Type: application/json" `
|
| 82 |
+
-d "{\"place_name\":\"Curug Ciampea\",\"top_n\":5}"
|
| 83 |
```
|
|
|
|
|
|
|
| 84 |
|
| 85 |
+
Payload rekomendasi:
|
| 86 |
+
|
| 87 |
+
```json
|
| 88 |
{
|
| 89 |
+
"place_name": "Curug Ciampea",
|
| 90 |
+
"top_n": 5
|
| 91 |
}
|
| 92 |
```
|
| 93 |
|
| 94 |
+
`place_name` lebih aman dipakai dari Laravel karena `place_id` adalah index
|
| 95 |
+
internal data API, bukan primary key database MySQL Laravel.
|
|
|
|
| 96 |
|
| 97 |
+
## Struktur
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
```text
|
|
|
|
|
|
|
| 100 |
flask_api/
|
| 101 |
+
βββ api/ # ruang tambahan untuk modul API jika diperlukan
|
| 102 |
+
βββ data/ # dataset dan artefak ML precomputed
|
| 103 |
βββ models/
|
| 104 |
β βββ __init__.py
|
| 105 |
+
β βββ preprocessor.py
|
| 106 |
+
β βββ recommender.py
|
| 107 |
+
βββ .dockerignore
|
| 108 |
+
βββ .gitattributes
|
| 109 |
+
βββ .python-version
|
| 110 |
+
βββ app.py
|
| 111 |
+
βββ Dockerfile
|
| 112 |
+
βββ README.md
|
| 113 |
+
βββ reproduce_error.py
|
| 114 |
+
βββ requirements.txt
|
| 115 |
+
βββ test_api.py
|
| 116 |
+
βββ test_search.py
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
## File Data Penting
|
| 120 |
+
|
| 121 |
+
| File | Fungsi |
|
| 122 |
+
| --- | --- |
|
| 123 |
+
| `data/bogor_tourism_data.csv` | Dataset clean mentah, 296 destinasi. |
|
| 124 |
+
| `data/bogor_tourism_data.json` | Dataset clean dalam format JSON. |
|
| 125 |
+
| `data/data_preprocessed.csv` | Data hasil preprocessing notebook. |
|
| 126 |
+
| `data/data_with_keywords.csv` | Data dengan unigram, bigram, dan trigram; dipakai recommender. |
|
| 127 |
+
| `data/tfidf_matrix.npy` | Matrix TF-IDF. |
|
| 128 |
+
| `data/tfidf_vectorizer.pkl` | Vectorizer TF-IDF untuk query/search. |
|
| 129 |
+
| `data/ngram_similarity.npy` | Similarity N-Gram precomputed. |
|
| 130 |
+
| `data/indobert_embeddings.npy` | Embedding IndoBERT. |
|
| 131 |
+
| `data/indobert_similarity.npy` | Similarity IndoBERT precomputed. |
|
| 132 |
+
| `data/combined_similarity.npy` | Similarity gabungan dari pipeline notebook. |
|
| 133 |
+
|
| 134 |
+
## Model Rekomendasi
|
| 135 |
+
|
| 136 |
+
Pipeline ML utama dikembangkan di `../dataset`:
|
| 137 |
+
|
| 138 |
+
```text
|
| 139 |
+
Data clean
|
| 140 |
+
-> preprocessing
|
| 141 |
+
-> N-Gram + TF-IDF
|
| 142 |
+
-> IndoBERT embedding
|
| 143 |
+
-> cosine similarity
|
| 144 |
+
-> rekomendasi
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
Implementasi API saat ini:
|
| 148 |
+
|
| 149 |
+
- `/api/search` memakai IndoBERT. Jika query sama persis dengan nama tempat,
|
| 150 |
+
API memakai `indobert_similarity.npy`; jika tidak, query di-encode langsung.
|
| 151 |
+
- `/api/recommendations` mencari tempat dari `place_name` terlebih dahulu,
|
| 152 |
+
lalu mengambil rekomendasi dari `ngram_similarity.npy`.
|
| 153 |
+
- DataFrame difilter saat startup agar baris dengan `deskripsi_clean` kosong
|
| 154 |
+
tidak masuk ke index rekomendasi.
|
| 155 |
+
|
| 156 |
+
## Sinkronisasi Artefak
|
| 157 |
+
|
| 158 |
+
Jika notebook di `../dataset` menghasilkan artefak baru, salin file yang
|
| 159 |
+
dibutuhkan ke `flask_api/data` sebelum menjalankan API atau deploy ulang.
|
| 160 |
+
|
| 161 |
+
File yang biasanya disinkronkan:
|
| 162 |
+
|
| 163 |
+
```text
|
| 164 |
+
data_preprocessed.csv
|
| 165 |
+
data_with_keywords.csv
|
| 166 |
+
tfidf_matrix.npy
|
| 167 |
+
tfidf_vectorizer.pkl
|
| 168 |
+
ngram_similarity.npy
|
| 169 |
+
indobert_embeddings.npy
|
| 170 |
+
indobert_similarity.npy
|
| 171 |
+
combined_similarity.npy
|
| 172 |
+
tabel_perbandingan.csv
|
| 173 |
+
tabel_akurasi_kategori.csv
|
| 174 |
```
|
| 175 |
|
| 176 |
+
## Docker dan Hugging Face Space
|
| 177 |
|
| 178 |
+
Build lokal dari folder `flask_api`:
|
| 179 |
+
|
| 180 |
+
```powershell
|
| 181 |
+
docker build -t bogor-xplore-api .
|
| 182 |
+
docker run --rm -p 7860:7860 bogor-xplore-api
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
Docker menjalankan:
|
| 186 |
+
|
| 187 |
+
```text
|
| 188 |
+
gunicorn --bind 0.0.0.0:7860 --timeout 300 app:app
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
File `.github/workflows/sync-flask-api-to-hf.yml` melakukan sync folder
|
| 192 |
+
`flask_api/` ke Hugging Face Space saat ada push ke branch `main` yang mengubah
|
| 193 |
+
folder ini atau workflow tersebut. Workflow membutuhkan secret `HF_TOKEN`.
|
| 194 |
+
|
| 195 |
+
## Test Manual
|
| 196 |
+
|
| 197 |
+
Jalankan API dulu, lalu:
|
| 198 |
+
|
| 199 |
+
```powershell
|
| 200 |
+
python test_api.py
|
| 201 |
+
python test_search.py
|
| 202 |
+
```
|
| 203 |
+
|
| 204 |
+
`test_api.py` memanggil endpoint rekomendasi, sedangkan `test_search.py`
|
| 205 |
+
memanggil endpoint pencarian semantik.
|
| 206 |
+
|
| 207 |
+
## Dependencies
|
| 208 |
+
|
| 209 |
+
Daftar lengkap ada di `requirements.txt`.
|
| 210 |
+
|
| 211 |
+
Paket utama:
|
| 212 |
+
|
| 213 |
+
- Flask
|
| 214 |
+
- Flask-CORS
|
| 215 |
+
- Pandas
|
| 216 |
+
- NumPy
|
| 217 |
+
- scikit-learn
|
| 218 |
+
- Transformers
|
| 219 |
- PyTorch
|
| 220 |
- Sastrawi
|
| 221 |
+
- Gunicorn
|
| 222 |
|
| 223 |
+
## Troubleshooting
|
| 224 |
|
| 225 |
+
| Masalah | Solusi |
|
| 226 |
+
| --- | --- |
|
| 227 |
+
| API lama saat startup | Tunggu model IndoBERT selesai dimuat. |
|
| 228 |
+
| `ModuleNotFoundError` | Jalankan `pip install -r requirements.txt`. |
|
| 229 |
+
| File `.npy` atau `.pkl` tidak ditemukan | Sinkronkan artefak dari `../dataset/data` ke `flask_api/data`. |
|
| 230 |
+
| Rekomendasi tidak sesuai tempat Laravel | Kirim `place_name` selain `place_id` dari Laravel. |
|
| 231 |
+
| Deploy Hugging Face gagal | Pastikan `HF_TOKEN` ada dan Git LFS asset `flask_api/data/**` ikut ter-pull. |
|
| 232 |
+
|
| 233 |
+
## Lisensi
|
|
|
|
|
|
|
| 234 |
|
| 235 |
+
Educational Project - BogorXplore.
|