maregu2023 commited on
Commit
ba67644
Β·
1 Parent(s): d2c2086

Add a markdown note for HF deployment

Browse files
Files changed (1) hide show
  1. docs/HUGGINGFACE_DEPLOYMENT.md +309 -0
docs/HUGGINGFACE_DEPLOYMENT.md ADDED
@@ -0,0 +1,309 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hosting on Hugging Face Spaces
2
+
3
+ This guide covers deploying the Brain Lesion Segmentation web app (VTK.js frontend + FastAPI backend) to Hugging Face Spaces.
4
+
5
+ ---
6
+
7
+ ## Recommended: Docker Space (Full-Stack)
8
+
9
+ For your VTK.js frontend + FastAPI backend architecture, the best approach is a **Docker Space**. This bundles both your FastAPI backend and static frontend in one deployment.
10
+
11
+ ### Why Docker Space?
12
+
13
+ | Approach | Pros | Cons |
14
+ |----------|------|------|
15
+ | **Docker Space** βœ… | Full control, GPU support, single deployment | Requires Dockerfile |
16
+ | Gradio Space | Simple setup | Not suitable for custom JS frontend |
17
+ | Static Space | Free, fast CDN | No backend/API support |
18
+
19
+ ---
20
+
21
+ ## Step-by-Step Deployment
22
+
23
+ ### 1. Create a Dockerfile
24
+
25
+ Create `Dockerfile` in your project root:
26
+
27
+ ```dockerfile
28
+ # Dockerfile
29
+ FROM python:3.10-slim
30
+
31
+ # Install system dependencies
32
+ RUN apt-get update && apt-get install -y \
33
+ build-essential \
34
+ curl \
35
+ git \
36
+ && rm -rf /var/lib/apt/lists/*
37
+
38
+ WORKDIR /app
39
+
40
+ # Copy requirements and install Python dependencies
41
+ COPY requirements.txt .
42
+ RUN pip install --no-cache-dir -r requirements.txt
43
+
44
+ # Copy application code
45
+ COPY . .
46
+
47
+ # Hugging Face Spaces expects port 7860
48
+ EXPOSE 7860
49
+
50
+ # Start FastAPI serving both API and static files
51
+ CMD ["uvicorn", "seg_app.backend.api:app", "--host", "0.0.0.0", "--port", "7860"]
52
+ ```
53
+
54
+ ### 2. Update FastAPI to Serve Static Frontend
55
+
56
+ Add the following to `seg_app/backend/api.py` (at the END of the file, after all API routes):
57
+
58
+ ```python
59
+ from fastapi.staticfiles import StaticFiles
60
+ from fastapi.responses import FileResponse
61
+ import os
62
+
63
+ # Serve static files for VTK.js slicer UI
64
+ static_dir = os.path.join(os.path.dirname(__file__), "..", "ui_slicer")
65
+ app.mount("/slicer", StaticFiles(directory=static_dir, html=True), name="slicer")
66
+
67
+ # Root redirect to slicer UI
68
+ @app.get("/")
69
+ async def root():
70
+ return FileResponse(os.path.join(static_dir, "index.html"))
71
+ ```
72
+
73
+ ### 3. Update Frontend API URL
74
+
75
+ In `seg_app/ui_slicer/api-client.js`, update the base URL to work in both local and production:
76
+
77
+ ```javascript
78
+ class ApiClient {
79
+ constructor() {
80
+ // Use same origin in production, localhost for development
81
+ this.baseUrl = window.location.hostname === 'localhost'
82
+ ? 'http://localhost:8000'
83
+ : window.location.origin;
84
+ }
85
+ // ... rest of the class
86
+ }
87
+ ```
88
+
89
+ ### 4. Update README.md with HF Spaces Metadata
90
+
91
+ Add YAML frontmatter to the top of your `README.md`:
92
+
93
+ ```yaml
94
+ ---
95
+ title: Brain Lesion Segmentation
96
+ emoji: 🧠
97
+ colorFrom: blue
98
+ colorTo: purple
99
+ sdk: docker
100
+ app_port: 7860
101
+ pinned: false
102
+ license: mit
103
+ ---
104
+
105
+ # Brain Lesion Segmentation Tool
106
+ ... (rest of your README)
107
+ ```
108
+
109
+ ### 5. Create .dockerignore
110
+
111
+ Create `.dockerignore` to exclude unnecessary files:
112
+
113
+ ```
114
+ __pycache__/
115
+ *.pyc
116
+ *.pyo
117
+ .git/
118
+ .gitignore
119
+ *.md
120
+ !README.md
121
+ .env
122
+ .vscode/
123
+ *.ipynb
124
+ test_*.py
125
+ ```
126
+
127
+ ### 6. Deploy to Hugging Face
128
+
129
+ #### Option A: Using HF CLI
130
+
131
+ ```bash
132
+ # Install Hugging Face CLI
133
+ pip install huggingface_hub
134
+
135
+ # Login to Hugging Face
136
+ huggingface-cli login
137
+
138
+ # Create a new Space
139
+ huggingface-cli repo create YOUR_USERNAME/brain-lesion-seg --type space --space_sdk docker
140
+
141
+ # Add HF as remote and push
142
+ git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/brain-lesion-seg
143
+ git push hf main
144
+ ```
145
+
146
+ #### Option B: Using Web Interface
147
+
148
+ 1. Go to [huggingface.co/new-space](https://huggingface.co/new-space)
149
+ 2. Select **Docker** as the SDK
150
+ 3. Choose a hardware tier (see recommendations below)
151
+ 4. Upload files or connect your GitHub repo
152
+
153
+ ---
154
+
155
+ ## Hardware Recommendations
156
+
157
+ | Tier | Cost | RAM | Best For |
158
+ |------|------|-----|----------|
159
+ | **CPU Basic** (free) | $0 | 16GB | Testing, demos |
160
+ | **CPU Upgrade** | ~$0.03/hr | 32GB | Small volumes |
161
+ | **T4 Small** βœ… | ~$0.60/hr | 16GB + GPU | SAM-Med3D, nnUNet |
162
+ | **T4 Medium** | ~$0.90/hr | 32GB + GPU | Large volumes |
163
+ | **A10G Small** | ~$1.50/hr | 24GB VRAM | Fast inference |
164
+
165
+ **Recommendation**: Start with **CPU Basic** for testing, upgrade to **T4 Small** for GPU inference with deep learning models.
166
+
167
+ ---
168
+
169
+ ## Environment Variables & Secrets
170
+
171
+ For sensitive configuration (model paths, API keys), use HF Spaces Secrets:
172
+
173
+ 1. Go to Space Settings β†’ Repository secrets
174
+ 2. Add secrets like:
175
+ - `HF_TOKEN` (if loading private models)
176
+ - `MODEL_PATH` (custom model locations)
177
+
178
+ Access in Python:
179
+ ```python
180
+ import os
181
+ hf_token = os.environ.get("HF_TOKEN")
182
+ ```
183
+
184
+ ---
185
+
186
+ ## GPU Support for Deep Learning Models
187
+
188
+ If using SAM-Med3D or nnUNet, ensure GPU support:
189
+
190
+ ### Update Dockerfile for GPU:
191
+
192
+ ```dockerfile
193
+ FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime
194
+
195
+ # ... rest of Dockerfile
196
+ ```
197
+
198
+ ### Update requirements.txt:
199
+
200
+ ```
201
+ torch>=2.0.0
202
+ monai>=1.3.0
203
+ nnunetv2>=2.2
204
+ # ... other dependencies
205
+ ```
206
+
207
+ ---
208
+
209
+ ## Troubleshooting
210
+
211
+ ### Build Fails
212
+
213
+ Check the build logs in your Space's "Logs" tab. Common issues:
214
+ - Missing system dependencies β†’ Add to `apt-get install`
215
+ - Python package conflicts β†’ Pin versions in requirements.txt
216
+
217
+ ### App Not Loading
218
+
219
+ - Ensure port 7860 is exposed and used
220
+ - Check that static files are correctly mounted
221
+ - Verify API routes don't conflict with static file routes
222
+
223
+ ### Slow Inference
224
+
225
+ - Upgrade to GPU hardware tier
226
+ - Enable model caching (load once, reuse)
227
+ - Consider model quantization for faster inference
228
+
229
+ ### CORS Issues
230
+
231
+ If frontend/backend are on different origins, add CORS middleware:
232
+
233
+ ```python
234
+ from fastapi.middleware.cors import CORSMiddleware
235
+
236
+ app.add_middleware(
237
+ CORSMiddleware,
238
+ allow_origins=["*"], # Or specify your frontend URL
239
+ allow_credentials=True,
240
+ allow_methods=["*"],
241
+ allow_headers=["*"],
242
+ )
243
+ ```
244
+
245
+ ---
246
+
247
+ ## Local Testing with Docker
248
+
249
+ Before deploying, test locally:
250
+
251
+ ```bash
252
+ # Build the image
253
+ docker build -t brain-seg-app .
254
+
255
+ # Run the container
256
+ docker run -p 7860:7860 brain-seg-app
257
+
258
+ # Open in browser
259
+ # http://localhost:7860
260
+ ```
261
+
262
+ ---
263
+
264
+ ## File Structure for Deployment
265
+
266
+ ```
267
+ web_app/
268
+ β”œβ”€β”€ Dockerfile # Container definition
269
+ β”œβ”€β”€ .dockerignore # Exclude unnecessary files
270
+ β”œβ”€β”€ README.md # With HF YAML frontmatter
271
+ β”œβ”€β”€ requirements.txt # Python dependencies
272
+ β”œβ”€β”€ seg_app/
273
+ β”‚ β”œβ”€β”€ backend/
274
+ β”‚ β”‚ └── api.py # FastAPI + static file serving
275
+ β”‚ β”œβ”€β”€ ui_slicer/
276
+ β”‚ β”‚ β”œβ”€β”€ index.html # VTK.js frontend
277
+ β”‚ β”‚ β”œβ”€β”€ app.js
278
+ β”‚ β”‚ β”œβ”€β”€ api-client.js # Updated for production URLs
279
+ β”‚ β”‚ └── ...
280
+ β”‚ └── models/
281
+ β”‚ └── ...
282
+ ```
283
+
284
+ ---
285
+
286
+ ## Alternative: Split Deployment
287
+
288
+ For more flexibility, deploy frontend and backend separately:
289
+
290
+ | Component | Space Type | URL |
291
+ |-----------|------------|-----|
292
+ | Frontend | Static Space (free) | `your-user/brain-seg-frontend` |
293
+ | Backend | Docker Space (GPU) | `your-user/brain-seg-api` |
294
+
295
+ This allows:
296
+ - Free hosting for static files
297
+ - GPU only when needed for inference
298
+ - Independent scaling
299
+
300
+ Configure the frontend to point to the backend API URL.
301
+
302
+ ---
303
+
304
+ ## Useful Links
305
+
306
+ - [HF Spaces Documentation](https://huggingface.co/docs/hub/spaces)
307
+ - [Docker Spaces Guide](https://huggingface.co/docs/hub/spaces-sdks-docker)
308
+ - [Spaces GPU Guide](https://huggingface.co/docs/hub/spaces-gpus)
309
+ - [Secrets & Environment Variables](https://huggingface.co/docs/hub/spaces-overview#managing-secrets)