Spaces:
Sleeping
Sleeping
Commit ·
cf575e8
1
Parent(s): 98c4545
Update README.md with the steps on how to run both the Gradio and API based web apps
Browse files
README.md
CHANGED
|
@@ -96,3 +96,270 @@ If you use this tool in your research, please cite:
|
|
| 96 |
## License
|
| 97 |
|
| 98 |
MIT License - See LICENSE file for details.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
## License
|
| 97 |
|
| 98 |
MIT License - See LICENSE file for details.
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## 🖥️ Running Locally
|
| 103 |
+
|
| 104 |
+
This project provides **two web application interfaces**:
|
| 105 |
+
|
| 106 |
+
1. **Gradio UI** – A simple, server-rendered interface ideal for quick inference and demos
|
| 107 |
+
2. **VTK.js Slicer** – A professional WebGL-based frontend with smooth MPR navigation (requires FastAPI backend)
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
### Prerequisites
|
| 112 |
+
|
| 113 |
+
Before running either interface, ensure you have:
|
| 114 |
+
|
| 115 |
+
1. **Python 3.10+** installed
|
| 116 |
+
2. **Conda** (recommended) or a Python virtual environment
|
| 117 |
+
3. **Git** (to clone the repository)
|
| 118 |
+
4. **CUDA-capable GPU** (recommended for inference, optional for testing)
|
| 119 |
+
|
| 120 |
+
#### Step 1: Clone the Repository
|
| 121 |
+
|
| 122 |
+
```powershell
|
| 123 |
+
git clone https://github.com/your-org/web_app.git
|
| 124 |
+
cd web_app
|
| 125 |
+
```
|
| 126 |
+
|
| 127 |
+
#### Step 2: Create and Activate the Conda Environment
|
| 128 |
+
|
| 129 |
+
```powershell
|
| 130 |
+
# Create a new conda environment
|
| 131 |
+
conda create -n seg_app python=3.10 -y
|
| 132 |
+
|
| 133 |
+
# Activate the environment
|
| 134 |
+
conda activate seg_app
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
#### Step 3: Install Dependencies
|
| 138 |
+
|
| 139 |
+
```powershell
|
| 140 |
+
# Install PyTorch with CUDA support (adjust CUDA version as needed)
|
| 141 |
+
# Visit https://pytorch.org/get-started/locally/ for the correct command
|
| 142 |
+
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
|
| 143 |
+
|
| 144 |
+
# Install project dependencies
|
| 145 |
+
pip install -r requirements.txt
|
| 146 |
+
```
|
| 147 |
+
|
| 148 |
+
#### Step 4: Verify Installation
|
| 149 |
+
|
| 150 |
+
```powershell
|
| 151 |
+
# Test that all imports work
|
| 152 |
+
python test_import.py
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
---
|
| 156 |
+
|
| 157 |
+
### Option A: Running the Gradio UI (Recommended for Quick Start)
|
| 158 |
+
|
| 159 |
+
The Gradio interface is a single-command solution that runs entirely on the server side. Best for quick demos and users unfamiliar with multi-service setups.
|
| 160 |
+
|
| 161 |
+
#### Start the Gradio App
|
| 162 |
+
|
| 163 |
+
```powershell
|
| 164 |
+
# Make sure you're in the web_app directory with conda environment activated
|
| 165 |
+
conda activate seg_app
|
| 166 |
+
|
| 167 |
+
# Run the Gradio app
|
| 168 |
+
python app.py
|
| 169 |
+
```
|
| 170 |
+
|
| 171 |
+
#### Access the Interface
|
| 172 |
+
|
| 173 |
+
Once started, you'll see output like:
|
| 174 |
+
|
| 175 |
+
```
|
| 176 |
+
Starting seg_app in local mode...
|
| 177 |
+
Running on local URL: http://127.0.0.1:7869
|
| 178 |
+
```
|
| 179 |
+
|
| 180 |
+
**Open your browser and navigate to: http://127.0.0.1:7869**
|
| 181 |
+
|
| 182 |
+
#### Gradio UI Workflow
|
| 183 |
+
|
| 184 |
+
1. **Upload Volume**: Click "Upload NIfTI" and select a `.nii` or `.nii.gz` file
|
| 185 |
+
2. **Select Model**: Choose between:
|
| 186 |
+
- **3D U-Net (Baseline)**: Fast automatic segmentation
|
| 187 |
+
- **Medical SAM 3D**: Interactive refinement with point prompts
|
| 188 |
+
3. **Add Prompts** (SAM only):
|
| 189 |
+
- Use the coordinate inputs to specify `(depth, height, width)`
|
| 190 |
+
- Click **"+ Positive"** for lesion points, **"+ Negative"** for background
|
| 191 |
+
4. **Run Segmentation**: Click the "Run Segmentation" button
|
| 192 |
+
5. **View Results**: Overlays appear on the multi-planar views with volume metrics
|
| 193 |
+
|
| 194 |
+
#### Stopping the Server
|
| 195 |
+
|
| 196 |
+
Press `Ctrl+C` in the terminal to stop the Gradio server.
|
| 197 |
+
|
| 198 |
+
---
|
| 199 |
+
|
| 200 |
+
### Option B: Running the VTK.js Slicer (Professional Frontend)
|
| 201 |
+
|
| 202 |
+
The VTK.js Slicer provides a professional medical imaging interface with smooth scrolling, WebGL rendering, and click-to-place prompts. This requires running **two services**:
|
| 203 |
+
|
| 204 |
+
1. **FastAPI Backend** (handles inference)
|
| 205 |
+
2. **Frontend Dev Server** (serves the VTK.js UI)
|
| 206 |
+
|
| 207 |
+
#### Terminal 1: Start the FastAPI Backend
|
| 208 |
+
|
| 209 |
+
```powershell
|
| 210 |
+
# Activate the conda environment
|
| 211 |
+
conda activate seg_app
|
| 212 |
+
|
| 213 |
+
# Navigate to the web_app directory
|
| 214 |
+
cd path\to\web_app
|
| 215 |
+
|
| 216 |
+
# Start the FastAPI backend with uvicorn
|
| 217 |
+
uvicorn seg_app.backend.api:app --reload --host 127.0.0.1 --port 8000
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
You should see output like:
|
| 221 |
+
|
| 222 |
+
```
|
| 223 |
+
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
|
| 224 |
+
INFO: Started reloader process
|
| 225 |
+
INFO: Started server process
|
| 226 |
+
INFO: Waiting for application startup.
|
| 227 |
+
INFO: Application startup complete.
|
| 228 |
+
```
|
| 229 |
+
|
| 230 |
+
> **Note**: The `--reload` flag enables auto-reloading when code changes. Remove it for production.
|
| 231 |
+
|
| 232 |
+
#### Terminal 2: Start the Frontend Server
|
| 233 |
+
|
| 234 |
+
Open a **new terminal** (keep the backend running):
|
| 235 |
+
|
| 236 |
+
```powershell
|
| 237 |
+
# Navigate to the ui_slicer directory
|
| 238 |
+
cd path\to\web_app\seg_app\ui_slicer
|
| 239 |
+
|
| 240 |
+
# Option A: Use the included Python server (recommended)
|
| 241 |
+
python serve_frontend.py
|
| 242 |
+
|
| 243 |
+
# Option B: Use npx serve (if you have Node.js installed)
|
| 244 |
+
npx serve . -p 5500
|
| 245 |
+
|
| 246 |
+
# Option C: Use VS Code Live Server extension
|
| 247 |
+
# Right-click on index.html → "Open with Live Server"
|
| 248 |
+
```
|
| 249 |
+
|
| 250 |
+
The Python server will show:
|
| 251 |
+
|
| 252 |
+
```
|
| 253 |
+
🚀 VTK.js Slicer Frontend Server
|
| 254 |
+
========================================
|
| 255 |
+
Serving: ...\seg_app\ui_slicer
|
| 256 |
+
URL: http://localhost:5500
|
| 257 |
+
========================================
|
| 258 |
+
|
| 259 |
+
Press Ctrl+C to stop.
|
| 260 |
+
```
|
| 261 |
+
|
| 262 |
+
#### Access the VTK.js Interface
|
| 263 |
+
|
| 264 |
+
**Open your browser and navigate to: http://localhost:5500**
|
| 265 |
+
|
| 266 |
+
#### VTK.js Slicer Workflow
|
| 267 |
+
|
| 268 |
+
1. **Upload Volume**: Click "Upload NIfTI" or drag-and-drop a `.nii`/`.nii.gz` file
|
| 269 |
+
2. **Navigate Slices**: Use **mouse wheel** to scroll through slices in any view
|
| 270 |
+
3. **Select Prompt Tool** (optional):
|
| 271 |
+
- Press **P** for positive prompts (mark lesion)
|
| 272 |
+
- Press **N** for negative prompts (mark background)
|
| 273 |
+
- **Click** on any view to place prompts
|
| 274 |
+
4. **Run Segmentation**: Click "Run Segmentation" or press **R**
|
| 275 |
+
5. **Refine**: Add more prompts and click "Refine with Prompts" or press **Shift+R**
|
| 276 |
+
6. **Clear Prompts**: Press **C** or click "Clear Prompts"
|
| 277 |
+
|
| 278 |
+
#### Keyboard Shortcuts
|
| 279 |
+
|
| 280 |
+
| Key | Action |
|
| 281 |
+
|-----|--------|
|
| 282 |
+
| `P` | Positive prompt mode |
|
| 283 |
+
| `N` | Negative prompt mode |
|
| 284 |
+
| `C` | Clear all prompts |
|
| 285 |
+
| `R` | Run segmentation |
|
| 286 |
+
| `Shift+R` | Refine with prompts |
|
| 287 |
+
| `Esc` | Cancel prompt mode |
|
| 288 |
+
|
| 289 |
+
#### Stopping the Servers
|
| 290 |
+
|
| 291 |
+
- Press `Ctrl+C` in **each terminal** to stop the respective server
|
| 292 |
+
|
| 293 |
+
---
|
| 294 |
+
|
| 295 |
+
### Comparison: Gradio vs VTK.js Slicer
|
| 296 |
+
|
| 297 |
+
| Feature | Gradio UI | VTK.js Slicer |
|
| 298 |
+
|---------|-----------|---------------|
|
| 299 |
+
| **Setup Complexity** | Single command | Two services |
|
| 300 |
+
| **Slice Navigation** | Button/slider based | Smooth mouse wheel |
|
| 301 |
+
| **Rendering** | Server-rendered PNG | Client-side WebGL (60fps) |
|
| 302 |
+
| **Prompt Placement** | Manual coordinate input | Click on image |
|
| 303 |
+
| **MPR Views** | Static images | Synchronized, real-time |
|
| 304 |
+
| **Best For** | Quick demos, remote access | Power users, research |
|
| 305 |
+
|
| 306 |
+
---
|
| 307 |
+
|
| 308 |
+
### Troubleshooting
|
| 309 |
+
|
| 310 |
+
#### Common Issues
|
| 311 |
+
|
| 312 |
+
**"Module not found" errors**
|
| 313 |
+
```powershell
|
| 314 |
+
# Ensure conda environment is activated
|
| 315 |
+
conda activate seg_app
|
| 316 |
+
|
| 317 |
+
# Reinstall dependencies
|
| 318 |
+
pip install -r requirements.txt
|
| 319 |
+
```
|
| 320 |
+
|
| 321 |
+
**"CUDA out of memory" or slow inference**
|
| 322 |
+
- Reduce input volume size or use CPU inference
|
| 323 |
+
- Close other GPU applications
|
| 324 |
+
|
| 325 |
+
**Backend not connected (VTK.js Slicer)**
|
| 326 |
+
- Ensure the FastAPI backend is running on port 8000
|
| 327 |
+
- Check browser console for CORS errors
|
| 328 |
+
- Verify the backend URL in `api-client.js` matches your setup
|
| 329 |
+
|
| 330 |
+
**Blank viewer in VTK.js**
|
| 331 |
+
- Check browser console for WebGL errors
|
| 332 |
+
- Ensure volume upload completed successfully
|
| 333 |
+
- Try a different browser (Chrome/Edge recommended)
|
| 334 |
+
|
| 335 |
+
**Gradio app crashes on startup**
|
| 336 |
+
- Check for port conflicts: change port in `app.py` if 7869 is in use
|
| 337 |
+
- Verify PyTorch installation: `python -c "import torch; print(torch.cuda.is_available())"`
|
| 338 |
+
|
| 339 |
+
#### Model Loading Issues
|
| 340 |
+
|
| 341 |
+
Models are lazy-loaded from Hugging Face Hub on first inference. If you experience issues:
|
| 342 |
+
|
| 343 |
+
```powershell
|
| 344 |
+
# Clear Hugging Face cache and re-download
|
| 345 |
+
rm -r ~/.cache/huggingface/hub/models--*sam*
|
| 346 |
+
|
| 347 |
+
# Or manually download the model
|
| 348 |
+
python -c "from huggingface_hub import hf_hub_download; hf_hub_download('blueyo0/SAM-Med3D', 'sam_med3d.pth')"
|
| 349 |
+
```
|
| 350 |
+
|
| 351 |
+
---
|
| 352 |
+
|
| 353 |
+
### API Documentation (Advanced)
|
| 354 |
+
|
| 355 |
+
When the FastAPI backend is running, you can access the interactive API documentation:
|
| 356 |
+
|
| 357 |
+
- **Swagger UI**: http://127.0.0.1:8000/docs
|
| 358 |
+
- **ReDoc**: http://127.0.0.1:8000/redoc
|
| 359 |
+
|
| 360 |
+
Key endpoints:
|
| 361 |
+
- `POST /volume/upload` – Upload a NIfTI volume
|
| 362 |
+
- `POST /segment` – Run segmentation on uploaded volume
|
| 363 |
+
- `POST /refine` – Refine segmentation with additional prompts
|
| 364 |
+
- `GET /mask/{volume_id}/data` – Download raw mask data
|
| 365 |
+
- `GET /health` – Health check endpoint
|