copilot-swe-agent[bot] raylim commited on
Commit
c0cda2e
·
1 Parent(s): 7dc4181

Add CLI wrapper script for Docker usage

Browse files

Co-authored-by: raylim <3074310+raylim@users.noreply.github.com>

Files changed (2) hide show
  1. README-docker.md +47 -0
  2. mosaic +121 -0
README-docker.md CHANGED
@@ -51,6 +51,8 @@ export HF_TOKEN="TOKEN-FROM-HUGGINGFACE"
51
 
52
  ## Usage
53
 
 
 
54
  1. Start up the web app using the command
55
 
56
  ```bash
@@ -79,6 +81,51 @@ or
79
  ./run_mosaic_docker.sh --port 7863
80
  ```
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  ### Notes
83
 
84
  - After you start up the application, it will download the necessary models from
 
51
 
52
  ## Usage
53
 
54
+ ### Web Application
55
+
56
  1. Start up the web app using the command
57
 
58
  ```bash
 
81
  ./run_mosaic_docker.sh --port 7863
82
  ```
83
 
84
+ ### Command Line Interface (CLI)
85
+
86
+ For seamless CLI usage via Docker, use the provided `mosaic` wrapper script. This script
87
+ automatically handles volume mounting and passes all arguments to the containerized
88
+ Mosaic CLI.
89
+
90
+ #### Basic Usage
91
+
92
+ ```bash
93
+ # Show help
94
+ ./mosaic --help
95
+
96
+ # Process a single slide
97
+ ./mosaic --slide-path /path/to/slide.svs \
98
+ --output-dir /path/to/output \
99
+ --site-type Primary \
100
+ --cancer-subtype Unknown \
101
+ --segmentation-config Resection
102
+
103
+ # Process multiple slides from a CSV file
104
+ ./mosaic --slide-csv /path/to/slides.csv \
105
+ --output-dir /path/to/output
106
+
107
+ # Process a breast cancer slide with IHC subtype
108
+ ./mosaic --slide-path /path/to/breast_slide.svs \
109
+ --output-dir /path/to/output \
110
+ --site-type Primary \
111
+ --cancer-subtype BRCA \
112
+ --ihc-subtype "HR+/HER2-"
113
+ ```
114
+
115
+ #### How it works
116
+
117
+ The `mosaic` wrapper script:
118
+ - Automatically mounts input slide directories and output directories into the container
119
+ - Passes through all Mosaic CLI arguments
120
+ - Handles the HF_TOKEN environment variable
121
+ - Detects and uses GPU support if available (falls back to CPU if not)
122
+
123
+ #### Requirements for CLI usage
124
+
125
+ - Docker installed and running
126
+ - HF_TOKEN environment variable set (same as web app)
127
+ - NVIDIA Docker runtime for GPU support (optional, will run on CPU if not available)
128
+
129
  ### Notes
130
 
131
  - After you start up the application, it will download the necessary models from
mosaic ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Mosaic CLI wrapper script for Docker
4
+ # This script enables seamless usage of the Mosaic CLI through Docker
5
+ #
6
+ # Usage: ./mosaic [mosaic-arguments]
7
+ #
8
+ # Examples:
9
+ # ./mosaic --help
10
+ # ./mosaic --slide-path /path/to/slide.svs --output-dir /path/to/output
11
+ # ./mosaic --slide-csv /path/to/slides.csv --output-dir /path/to/output
12
+ #
13
+ # Requirements:
14
+ # - Docker installed and running
15
+ # - NVIDIA Docker runtime (for GPU support)
16
+ # - HF_TOKEN environment variable set
17
+ # - Docker image pulled: docker pull tomp/mosaic-gradio
18
+
19
+ DOCKER_IMAGE="tomp/mosaic-gradio"
20
+ USE_GPU=true
21
+
22
+ die () { echo "FATAL: $*" >&2; exit 1; }
23
+
24
+ # Check if Docker is available
25
+ command -v docker >/dev/null 2>&1 || die "Docker is not installed or not in PATH"
26
+
27
+ # Check if HF_TOKEN is set
28
+ [[ -n $HF_TOKEN ]] || die "HF_TOKEN environment variable is not set. Please set it to your HuggingFace access token."
29
+
30
+ # Check if nvidia runtime is available
31
+ if docker info 2>/dev/null | grep -q nvidia; then
32
+ USE_GPU=true
33
+ else
34
+ echo "Warning: NVIDIA Docker runtime not detected. Running without GPU support." >&2
35
+ echo " To enable GPU support, install the NVIDIA Container Toolkit." >&2
36
+ USE_GPU=false
37
+ fi
38
+
39
+ # Parse arguments to identify paths that need to be mounted
40
+ VOLUME_MOUNTS=()
41
+ ARGS=()
42
+
43
+ while [[ $# -gt 0 ]]; do
44
+ ARG="$1"
45
+ case "$ARG" in
46
+ --slide-path)
47
+ shift
48
+ SLIDE_PATH="$1"
49
+ if [[ -n "$SLIDE_PATH" ]]; then
50
+ # Convert to absolute path
51
+ SLIDE_PATH=$(readlink -f "$SLIDE_PATH")
52
+ SLIDE_DIR=$(dirname "$SLIDE_PATH")
53
+ SLIDE_FILE=$(basename "$SLIDE_PATH")
54
+ # Mount the directory containing the slide
55
+ VOLUME_MOUNTS+=("-v" "$SLIDE_DIR:/mnt/slides:ro")
56
+ # Pass the container path to mosaic
57
+ ARGS+=("--slide-path" "/mnt/slides/$SLIDE_FILE")
58
+ fi
59
+ shift
60
+ ;;
61
+ --slide-csv)
62
+ shift
63
+ CSV_PATH="$1"
64
+ if [[ -n "$CSV_PATH" ]]; then
65
+ # Convert to absolute path
66
+ CSV_PATH=$(readlink -f "$CSV_PATH")
67
+ CSV_DIR=$(dirname "$CSV_PATH")
68
+ CSV_FILE=$(basename "$CSV_PATH")
69
+ # Mount the directory containing the CSV
70
+ VOLUME_MOUNTS+=("-v" "$CSV_DIR:/mnt/csv:ro")
71
+ # Pass the container path to mosaic
72
+ ARGS+=("--slide-csv" "/mnt/csv/$CSV_FILE")
73
+ # Note: CSV may reference slide paths that also need to be mounted
74
+ # For simplicity, we assume slides are in same dir or subdirs
75
+ fi
76
+ shift
77
+ ;;
78
+ --output-dir)
79
+ shift
80
+ OUTPUT_DIR="$1"
81
+ if [[ -n "$OUTPUT_DIR" ]]; then
82
+ # Convert to absolute path and create if it doesn't exist
83
+ mkdir -p "$OUTPUT_DIR"
84
+ OUTPUT_DIR=$(readlink -f "$OUTPUT_DIR")
85
+ # Mount the output directory
86
+ VOLUME_MOUNTS+=("-v" "$OUTPUT_DIR:/mnt/output")
87
+ # Pass the container path to mosaic
88
+ ARGS+=("--output-dir" "/mnt/output")
89
+ fi
90
+ shift
91
+ ;;
92
+ *)
93
+ # Pass through all other arguments
94
+ ARGS+=("$ARG")
95
+ shift
96
+ ;;
97
+ esac
98
+ done
99
+
100
+ # Remove duplicate volume mounts
101
+ UNIQUE_MOUNTS=$(printf '%s\n' "${VOLUME_MOUNTS[@]}" | awk '!seen[$0]++')
102
+ VOLUME_ARGS=()
103
+ while IFS= read -r mount; do
104
+ [[ -n "$mount" ]] && VOLUME_ARGS+=("$mount")
105
+ done <<< "$UNIQUE_MOUNTS"
106
+
107
+ # Run the Docker container with the mosaic CLI
108
+ # Override the entrypoint to run mosaic command instead of gradio_app
109
+ GPU_ARGS=()
110
+ if [ "$USE_GPU" = true ]; then
111
+ GPU_ARGS+=("--gpus=all" "--runtime=nvidia")
112
+ fi
113
+
114
+ docker run --rm \
115
+ "${GPU_ARGS[@]}" \
116
+ --env HF_TOKEN="${HF_TOKEN}" \
117
+ --shm-size=500m \
118
+ "${VOLUME_ARGS[@]}" \
119
+ --entrypoint mosaic \
120
+ "$DOCKER_IMAGE" \
121
+ "${ARGS[@]}"