Spaces:
Sleeping
Sleeping
fix: increase zerogpu duration
Browse files- src/mosaic/analysis.py +13 -10
src/mosaic/analysis.py
CHANGED
|
@@ -17,19 +17,22 @@ from loguru import logger
|
|
| 17 |
|
| 18 |
try:
|
| 19 |
import spaces
|
|
|
|
| 20 |
HAS_SPACES = True
|
| 21 |
except ImportError:
|
| 22 |
HAS_SPACES = False
|
|
|
|
| 23 |
# Create a no-op decorator if spaces is not available
|
| 24 |
class spaces:
|
| 25 |
@staticmethod
|
| 26 |
def GPU(fn):
|
| 27 |
return fn
|
| 28 |
|
|
|
|
| 29 |
from mosaic.inference import run_aeon, run_paladin
|
| 30 |
|
| 31 |
|
| 32 |
-
@spaces.GPU
|
| 33 |
def _run_gpu_inference(
|
| 34 |
coords,
|
| 35 |
slide_path,
|
|
@@ -41,7 +44,7 @@ def _run_gpu_inference(
|
|
| 41 |
progress,
|
| 42 |
):
|
| 43 |
"""Run GPU-intensive feature extraction and model inference.
|
| 44 |
-
|
| 45 |
This function is decorated with @spaces.GPU to allocate GPU resources only
|
| 46 |
when needed for GPU-intensive operations including:
|
| 47 |
- CTransPath feature extraction
|
|
@@ -49,7 +52,7 @@ def _run_gpu_inference(
|
|
| 49 |
- Optimus feature extraction
|
| 50 |
- Aeon cancer subtype inference
|
| 51 |
- Paladin biomarker prediction
|
| 52 |
-
|
| 53 |
Args:
|
| 54 |
coords: Tissue tile coordinates
|
| 55 |
slide_path: Path to the whole slide image file
|
|
@@ -59,7 +62,7 @@ def _run_gpu_inference(
|
|
| 59 |
cancer_subtype_name_map: Dictionary mapping cancer subtype names to codes
|
| 60 |
num_workers: Number of worker processes for feature extraction
|
| 61 |
progress: Gradio progress tracker for UI updates
|
| 62 |
-
|
| 63 |
Returns:
|
| 64 |
tuple: (aeon_results, paladin_results)
|
| 65 |
- aeon_results: DataFrame with cancer subtype predictions and confidence scores
|
|
@@ -214,15 +217,15 @@ def analyze_slide(
|
|
| 214 |
progress=gr.Progress(track_tqdm=True),
|
| 215 |
):
|
| 216 |
"""Analyze a whole slide image for cancer subtype and biomarker prediction.
|
| 217 |
-
|
| 218 |
This function performs a complete analysis pipeline including:
|
| 219 |
1. Tissue segmentation (CPU-only, no GPU required)
|
| 220 |
2. GPU-intensive feature extraction and model inference
|
| 221 |
-
|
| 222 |
The GPU-intensive operations are handled by a separate function decorated
|
| 223 |
with @spaces.GPU to efficiently manage GPU resources on Hugging Face Spaces.
|
| 224 |
Tissue segmentation runs on CPU and is not included in the GPU allocation.
|
| 225 |
-
|
| 226 |
Args:
|
| 227 |
slide_path: Path to the whole slide image file
|
| 228 |
seg_config: Segmentation configuration, one of "Biopsy", "Resection", or "TCGA"
|
|
@@ -232,13 +235,13 @@ def analyze_slide(
|
|
| 232 |
ihc_subtype: IHC subtype for breast cancer (optional)
|
| 233 |
num_workers: Number of worker processes for feature extraction
|
| 234 |
progress: Gradio progress tracker for UI updates
|
| 235 |
-
|
| 236 |
Returns:
|
| 237 |
tuple: (slide_mask, aeon_results, paladin_results)
|
| 238 |
- slide_mask: PIL Image of tissue segmentation visualization
|
| 239 |
- aeon_results: DataFrame with cancer subtype predictions and confidence scores
|
| 240 |
- paladin_results: DataFrame with biomarker predictions
|
| 241 |
-
|
| 242 |
Raises:
|
| 243 |
gr.Error: If no slide is provided
|
| 244 |
gr.Warning: If no tissue is detected in the slide
|
|
@@ -246,7 +249,7 @@ def analyze_slide(
|
|
| 246 |
"""
|
| 247 |
if slide_path is None:
|
| 248 |
raise gr.Error("Please upload a slide.")
|
| 249 |
-
|
| 250 |
# Step 1: Segment tissue (CPU-only, not GPU-intensive)
|
| 251 |
start_time = pd.Timestamp.now()
|
| 252 |
|
|
|
|
| 17 |
|
| 18 |
try:
|
| 19 |
import spaces
|
| 20 |
+
|
| 21 |
HAS_SPACES = True
|
| 22 |
except ImportError:
|
| 23 |
HAS_SPACES = False
|
| 24 |
+
|
| 25 |
# Create a no-op decorator if spaces is not available
|
| 26 |
class spaces:
|
| 27 |
@staticmethod
|
| 28 |
def GPU(fn):
|
| 29 |
return fn
|
| 30 |
|
| 31 |
+
|
| 32 |
from mosaic.inference import run_aeon, run_paladin
|
| 33 |
|
| 34 |
|
| 35 |
+
@spaces.GPU(duration=300)
|
| 36 |
def _run_gpu_inference(
|
| 37 |
coords,
|
| 38 |
slide_path,
|
|
|
|
| 44 |
progress,
|
| 45 |
):
|
| 46 |
"""Run GPU-intensive feature extraction and model inference.
|
| 47 |
+
|
| 48 |
This function is decorated with @spaces.GPU to allocate GPU resources only
|
| 49 |
when needed for GPU-intensive operations including:
|
| 50 |
- CTransPath feature extraction
|
|
|
|
| 52 |
- Optimus feature extraction
|
| 53 |
- Aeon cancer subtype inference
|
| 54 |
- Paladin biomarker prediction
|
| 55 |
+
|
| 56 |
Args:
|
| 57 |
coords: Tissue tile coordinates
|
| 58 |
slide_path: Path to the whole slide image file
|
|
|
|
| 62 |
cancer_subtype_name_map: Dictionary mapping cancer subtype names to codes
|
| 63 |
num_workers: Number of worker processes for feature extraction
|
| 64 |
progress: Gradio progress tracker for UI updates
|
| 65 |
+
|
| 66 |
Returns:
|
| 67 |
tuple: (aeon_results, paladin_results)
|
| 68 |
- aeon_results: DataFrame with cancer subtype predictions and confidence scores
|
|
|
|
| 217 |
progress=gr.Progress(track_tqdm=True),
|
| 218 |
):
|
| 219 |
"""Analyze a whole slide image for cancer subtype and biomarker prediction.
|
| 220 |
+
|
| 221 |
This function performs a complete analysis pipeline including:
|
| 222 |
1. Tissue segmentation (CPU-only, no GPU required)
|
| 223 |
2. GPU-intensive feature extraction and model inference
|
| 224 |
+
|
| 225 |
The GPU-intensive operations are handled by a separate function decorated
|
| 226 |
with @spaces.GPU to efficiently manage GPU resources on Hugging Face Spaces.
|
| 227 |
Tissue segmentation runs on CPU and is not included in the GPU allocation.
|
| 228 |
+
|
| 229 |
Args:
|
| 230 |
slide_path: Path to the whole slide image file
|
| 231 |
seg_config: Segmentation configuration, one of "Biopsy", "Resection", or "TCGA"
|
|
|
|
| 235 |
ihc_subtype: IHC subtype for breast cancer (optional)
|
| 236 |
num_workers: Number of worker processes for feature extraction
|
| 237 |
progress: Gradio progress tracker for UI updates
|
| 238 |
+
|
| 239 |
Returns:
|
| 240 |
tuple: (slide_mask, aeon_results, paladin_results)
|
| 241 |
- slide_mask: PIL Image of tissue segmentation visualization
|
| 242 |
- aeon_results: DataFrame with cancer subtype predictions and confidence scores
|
| 243 |
- paladin_results: DataFrame with biomarker predictions
|
| 244 |
+
|
| 245 |
Raises:
|
| 246 |
gr.Error: If no slide is provided
|
| 247 |
gr.Warning: If no tissue is detected in the slide
|
|
|
|
| 249 |
"""
|
| 250 |
if slide_path is None:
|
| 251 |
raise gr.Error("Please upload a slide.")
|
| 252 |
+
|
| 253 |
# Step 1: Segment tissue (CPU-only, not GPU-intensive)
|
| 254 |
start_time = pd.Timestamp.now()
|
| 255 |
|