|
Download docs/tutorials/03-deploy-huggingface.md from Alfayadh/vela-app: direct link, hf CLI and curl.
- Browser
- Download file 7.45 kB
-
https://huggingface.co/spaces/Alfayadh/vela-app/resolve/main/docs/tutorials/03-deploy-huggingface.md
- Command line
-
hf download hf://spaces/Alfayadh/vela-app/docs/tutorials/03-deploy-huggingface.md
-
curl -L -o 03-deploy-huggingface.md https://huggingface.co/spaces/Alfayadh/vela-app/resolve/main/docs/tutorials/03-deploy-huggingface.md
7.45 kB
| # Tutorial 3: Deployment ke Hugging Face Spaces (ZeroGPU + Gradio + FastAPI) | |
| > **Tujuan tutorial ini**: Men-deploy Vela Personal AI Ecosystem ke Hugging Face Spaces dengan hardware **ZeroGPU (A100/L40S)** gratis, menggunakan entrypoint `app.py`. | |
| --- | |
| ## 1. Gambaran Umum (Big Picture) | |
| Hugging Face Spaces menyediakan lingkungan cloud berbasis kontainer Docker/Linux. Dengan **ZeroGPU**, aplikasi mendapat akses GPU kelas tinggi (Nvidia A100 / L40S) secara *on-demand* — hanya saat ada pemrosesan AI. | |
| ### Diagram Alur Utama (Dari Lokal ke Cloud) | |
| ```mermaid | |
| flowchart TD | |
| A[1. Komputer Lokal Anda] -->|git push hf deploy-hf:main| B[2. Repositori Cloud Hugging Face] | |
| B --> C[3. Hugging Face Booting Kontainer Linux] | |
| subgraph Booting Sequence di Cloud app.py | |
| C --> D[Langkah A: Handshake 5-Detik ke Orchestrator HF] | |
| D --> E[Langkah B: Download Otomatis Hermes CLI & Node.js] | |
| E --> F[Langkah C: Restorasi Kredensial Secrets Base64] | |
| F --> G[Langkah D: Auto-Patching Regex Python 3.10] | |
| G --> H[Langkah E: Socket Port Selector Dinamis] | |
| H --> I[Langkah F: Mount FastAPI SPA + Gradio UI] | |
| end | |
| I --> J[4. Web Live & Siap Digunakan Pengguna] | |
| ``` | |
| ### Berkas Penting untuk Deployment | |
| | Nama File | Lokasi | Peran & Fungsi | | |
| | :--- | :--- | :--- | | |
| | `app.py` | Root proyek | **Pintu masuk utama (entrypoint)** yang dibaca Hugging Face saat booting | | |
| | `dashboard.py` | Root proyek | Backend utama FastAPI (rute API, SQLite, logika bisnis) | | |
| | `dashboard-static/` | Root proyek | Frontend SPA (HTML, CSS, JS, aset dasbor) | | |
| | `requirements.txt` | Root proyek | Package Python yang diinstall otomatis (`fastapi`, `uvicorn`, `gradio`, dll.) | | |
| --- | |
| ## 2. Apa yang Terjadi Saat Server Booting? (`app.py`) | |
| Saat repositori di-push, `app.py` dieksekusi dari atas ke bawah: | |
| ``` | |
| [Start Booting] | |
| ├── 1. Handshake Orchestrator → mencegah kill switch 5-detik | |
| ├── 2. Auto-Bootstrap → clone hermes-agent + download Node.js v18 portable | |
| ├── 3. Restorasi Supabase & Secret → pulihkan state.db, habits.db, chat_history.db & token Base64 | |
| ├── 4. Background Auto-Sync Thread → backup DB ke Supabase tiap 5 menit | |
| ├── 5. Patching Regex Python 3.10 → cegah crash re.error | |
| ├── 6. Mount Dual-UI → SPA di '/' + tab Gradio di '/gradio' | |
| └── 7. Dynamic Port Selector → launch Uvicorn pada port kosong | |
| ``` | |
| ### Langkah 1: Handshake Orchestrator (`_zerogpu_handshake`) | |
| Orchestrator ZeroGPU membunuh kontainer jika tidak menerima sinyal *startup report* dalam 5 detik. Fungsi ini mendaftarkan stub GPU dan mengirim sinyal: | |
| ```python | |
| def _zerogpu_handshake() -> None: | |
| import spaces | |
| from spaces.zero import client as zero_client | |
| spaces.GPU(_zerogpu_handshake) # Daftarkan stub GPU | |
| zero_client.startup_report() # Kirim sinyal HTTP ke Orchestrator HF | |
| ``` | |
| ### Langkah 2: Download Otomatis Dependensi | |
| Kontainer HF bersih (*clean container*) — belum ada CLI `hermes-agent` atau `node`: | |
| 1. `app.py` mengecek folder `hermes-agent`; jika belum ada, menjalankan `git clone https://github.com/nousresearch/hermes-agent.git`. | |
| 2. Jika perintah `node` tidak ditemukan, mengunduh tarball Node.js v18 portable, mengekstraknya, dan memasukkannya ke `PATH`. | |
| ### Langkah 3: Restorasi Memori Supabase & Secret Base64 | |
| HF Spaces me-reset file system setiap rebuild/sleep, maka: | |
| 1. **Memori (Supabase)**: Jika `SUPABASE_URL` dan `SUPABASE_KEY` diset di Secrets, aplikasi mengunduh `state.db`, `habits.db`, `chat_history.db`, dan `caffeine.db` terbaru dari bucket `hermes_memory` via Native REST API (urllib). Jika kosong/gagal, fallback ke decode Base64 lokal. | |
| 2. **Secret (Base64)**: Token rahasia disimpan sebagai teks Base64 di Repository Secrets dan didekode saat booting. | |
| 3. **Auto-Sync**: Background thread memantau perubahan DB lokal setiap 5 menit dan meng-upload kembali ke Supabase secara diam-diam. | |
| ### Langkah 4: Auto-Patching Regex Python 3.10 | |
| Python 3.10 memicu `re.error: multiple repeat` pada quantifier `++`/`*+`. `app.py` membaca `agent/redact.py` sebelum di-import, mengganti `++` → `+` dan `*+` → `*`, lalu menyimpannya kembali. | |
| ### Langkah 5: Socket Port Selector Dinamis (`_pick_port`) | |
| Mencegah `OSError: [Errno 98] Address already in use`: `app.py` menguji keterisian TCP socket; jika port `7860` sibuk, mencoba `7861`, `7862`, dst. Selain itu `GRADIO_SSR_MODE="false"` dipaksa agar SSR Gradio tidak konflik. | |
| ### Langkah 6: Mount Dual-UI (FastAPI + Gradio) | |
| Rute `/` menyajikan dashboard SPA penuh layar, sedangkan tab akselerator Gradio tersedia di `/gradio`: | |
| ```python | |
| @fastapi_app.get("/") | |
| def serve_dashboard_ui(): | |
| return FileResponse("dashboard-static/index.html") | |
| app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio") | |
| ``` | |
| --- | |
| ## 3. Panduan Langkah Demi Langkah | |
| ### Langkah 1: Buat Space Baru di Hugging Face | |
| 1. Buka [huggingface.co/new-space](https://huggingface.co/new-space). | |
| 2. Isi **Space Name** (contoh: `vela`). | |
| 3. Pilih SDK **Gradio**. | |
| 4. Pilih Hardware **ZeroGPU (Nvidia A100/L40S)**. | |
| ### Langkah 2: Encode Token Lokal ke Base64 | |
| ```bash | |
| # Encode token Google | |
| base64 -w 0 ~/.hermes/google_token.json | |
| # Encode token Fitbit | |
| base64 -w 0 ~/.hermes/fitbit_tokens.json | |
| ``` | |
| ### Langkah 3: Masukkan Secrets ke Hugging Face | |
| Buka Space Anda → tab **Settings** → bagian **Repository Secrets** → **New Secret**, tambahkan: | |
| | Secret | Nilai | | |
| | :--- | :--- | | |
| | `SUPABASE_URL` | URL REST API Supabase Anda | | |
| | `SUPABASE_KEY` | Service Role Key Supabase (server-side only) | | |
| | `GOOGLE_TOKEN_B64` | Hasil encode `google_token.json` | | |
| | `FITBIT_TOKENS_B64` | Hasil encode `fitbit_tokens.json` | | |
| | `WHATSAPP_SESSION_B64` | Base64 sesi WhatsApp Baileys | | |
| | `HERMES_DASH_TOKEN` | Password login dashboard (mis. `gymku123`) | | |
| | `OPENROUTER_API_KEY` | API key OpenRouter | | |
| | `HERMES_CUSTOM_API_GROQ_COM_API_KEY` | API key provider alternatif | | |
| ### Langkah 4: Push Kode ke Hugging Face | |
| ```bash | |
| # 1. Tambahkan remote repository Hugging Face | |
| git remote add hf https://huggingface.co/spaces/Alfayadh/vela | |
| # 2. Pindah ke branch khusus deployment | |
| git checkout -b deploy-hf | |
| # 3. Add & commit file | |
| git add app.py requirements.txt dashboard.py dashboard-static/ Dockerfile | |
| git commit -m "Deploy Vela Superapp to Hugging Face ZeroGPU" | |
| # 4. Push ke branch main di Hugging Face | |
| git push hf deploy-hf:main --force | |
| ``` | |
| > [!NOTE] | |
| > File `README.md` harus mempertahankan YAML frontmatter (`sdk: gradio`, `app_file: app.py`, dll.) karena dibaca oleh Hugging Face untuk konfigurasi Space. | |
| --- | |
| ## 4. Matriks Troubleshooting Error Umum | |
| | Gejala Error | Penyebab | Solusi (ditangani `app.py`) | | |
| | :--- | :--- | :--- | | |
| | `Exit code: 3` (Container Killed) | Orchestrator tidak menerima sinyal dalam 5 detik | `_zerogpu_handshake()` mengirim `zero_client.startup_report()` | | |
| | `Errno 98 Address already in use` | Port 7860 sedang terpakai | `_pick_port()` mendeteksi port kosong secara dinamis | | |
| | `hermes: command not found` | CLI agent belum terpasang di kontainer cloud | Auto-clone `hermes-agent` saat booting (+ guard terhadap clone gagal/rate-limit) | | |
| | `re.error: multiple repeat` | Python 3.10 tidak mendukung quantifier `++` | Auto-patching pada `agent/redact.py` | | |
| --- | |
| ## Terkait | |
| - [Arsitektur Sistem](../explanation/architecture.md) | |
| - [Variabel Lingkungan Lengkap](../how-to/variabel-lingkungan.md) | |
| - [Setup Supabase](../how-to/setup-supabase-akademik.md) | |