Spaces:
Running on Zero
Running on Zero
File size: 3,809 Bytes
e7c0048 2f8cafc e7c0048 2f8cafc e7c0048 2f8cafc e7c0048 2f8cafc | 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 | ---
title: BrainScan AI
emoji: 🧠
colorFrom: red
colorTo: blue
sdk: gradio
app_file: app.py
pinned: false
license: mit
---
# BrainScan AI — Hybrid EfficientNet-ViT
Klasifikasi CT-Scan/MRI otak (Alzheimer, Intracranial Hemorrhage, Normal,
Ischemic Stroke, Brain Tumor) menggunakan model hybrid EfficientNet-B3 +
custom Vision Transformer dengan Cross-Modal Attention Fusion.
Model checkpoint diambil otomatis dari:
https://huggingface.co/Marksnb/brain-hybrid-efficientnet-vit
## Cara Pakai lewat Endpoint (API)
Space ini otomatis punya REST API bawaan Gradio di endpoint `/analyze`
(nama ini diatur lewat `api_name="analyze"` di `app.py`). Ganti `SPACE_URL`
di bawah dengan URL Space kamu, contoh: `https://username-brainscan-ai.hf.space`.
### Opsi 1 — Pakai `gradio_client` (Python, paling mudah)
```bash
pip install gradio_client
```
```python
from gradio_client import Client, handle_file
client = Client("SPACE_URL") # atau "username/brainscan-ai" kalau Space public
result = client.predict(
handle_file("path/ke/gambar_otak.jpg"),
api_name="/analyze"
)
label_scores, heatmap_path, summary = result
print(label_scores) # dict probabilitas tiap kelas
print(summary) # ringkasan teks prediksi
print(heatmap_path) # path lokal file heatmap hasil download otomatis
```
Kalau Space kamu private, tambahkan token:
```python
client = Client("SPACE_URL", hf_token="hf_xxxxxxxxxxxxxxxxxxxx")
```
### Opsi 2 — REST API langsung (curl / bahasa apa pun)
Gradio (versi 5+) memakai pola *submit lalu poll*: POST dulu untuk submit job,
lalu GET untuk stream hasilnya pakai `event_id` yang didapat.
**Langkah 1 — Submit gambar (base64):**
```bash
curl -X POST "SPACE_URL/gradio_api/call/analyze" \
-H "Content-Type: application/json" \
-d '{
"data": [
{
"path": null,
"url": "data:image/jpeg;base64,'"$(base64 -w0 gambar_otak.jpg)"'",
"meta": {"_type": "gradio.FileData"}
}
]
}'
```
Response berisi `event_id`, contoh:
```json
{"event_id": "abc123..."}
```
**Langkah 2 — Ambil hasil pakai event_id:**
```bash
curl -N "SPACE_URL/gradio_api/call/analyze/abc123..."
```
Response berupa Server-Sent Events (SSE), baris terakhir bertipe `event: complete`
berisi array JSON hasil: `[label_scores, heatmap_fileinfo, summary_markdown]`.
### Opsi 3 — Upload file lewat endpoint upload bawaan Gradio
Kalau gambar berupa file (bukan base64), upload dulu ke endpoint `/gradio_api/upload`,
lalu pakai path hasil upload itu di payload `data` pada langkah submit di atas:
```bash
curl -X POST "SPACE_URL/gradio_api/upload" \
-F "files=@gambar_otak.jpg"
# -> mengembalikan array path, misal: ["/tmp/xxx/gambar_otak.jpg"]
curl -X POST "SPACE_URL/gradio_api/call/analyze" \
-H "Content-Type: application/json" \
-d '{
"data": [
{"path": "/tmp/xxx/gambar_otak.jpg", "meta": {"_type": "gradio.FileData"}}
]
}'
```
### Format Output
Endpoint `/analyze` mengembalikan 3 nilai (urut sesuai `outputs=` di `app.py`):
| # | Nilai | Tipe | Keterangan |
|---|-------------------|--------------------------------|-----------------------------------------------|
| 1 | `label_scores` | `dict[str, float]` | Probabilitas tiap kelas (0–1) |
| 2 | `heatmap` | file gambar (FileData) | Overlay peta atensi ViT |
| 3 | `summary` | `string` (markdown) | Ringkasan prediksi + confidence + disclaimer |
### Cara paling praktis melihat contoh payload persis
Buka `SPACE_URL/?view=api` di browser (halaman "Use via API" bawaan Gradio) —
di situ tersedia contoh kode Python, JavaScript, dan cURL yang sudah otomatis
disesuaikan dengan skema input/output Space ini. |