k-l-lambda commited on
Commit
7b97a14
·
1 Parent(s): 197a18e

fix: run model download and ML predictors in background so services start immediately

Browse files
Files changed (1) hide show
  1. docker-entrypoint.sh +82 -87
docker-entrypoint.sh CHANGED
@@ -118,107 +118,102 @@ hf_download() {
118
  fi
119
  }
120
 
121
- if [ -n "$HF_TOKEN" ] && [ -f "$MODELS_YAML" ]; then
122
- echo "Downloading model files from HuggingFace..."
123
-
124
- # Download bdtopo ONNX models
125
- if [ -n "$BDTOPO_MODEL_PATH" ] && [ ! -f "$BDTOPO_MODEL_PATH" ]; then
126
- _bdtopo_path=$(yaml_get bdtopo path)
127
- _bdtopo_dir="$MODELS_BASE/starry-dist/$_bdtopo_path"
128
- mkdir -p "$_bdtopo_dir"
 
129
 
130
- sed -n '/^ files:/,/^[^ ]/{ /^ *- /p }' "$MODELS_YAML" | sed 's/^ *- *//' | while read -r FILE; do
131
- hf_download "starry-dist" "$_bdtopo_path/$FILE"
132
- done
133
- fi
 
 
 
 
 
 
 
 
 
 
 
 
134
 
135
- # Download PyTorch model directories (starry-dist)
136
- for model_var in LAYOUT_MODEL_PATH MASK_MODEL_PATH SEMANTIC_MODEL_PATH GAUGE_MODEL_PATH; do
137
- eval model_path=\$$model_var
138
- if [ -n "$model_path" ]; then
139
- # Download .state.yaml which lists the actual model files
140
- hf_download "starry-dist" "$model_path/.state.yaml"
141
 
142
- state_yaml="$MODELS_BASE/starry-dist/$model_path/.state.yaml"
143
- if [ -f "$state_yaml" ]; then
144
- # Extract model filename from .state.yaml
145
- model_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)
146
- if [ -n "$model_file" ]; then
147
- hf_download "starry-dist" "$model_path/$model_file"
148
- fi
149
- # Extract sub-model files if present (semantic clusters)
150
- sed -n '/^subs:/,/^[a-z]/{ /^ - /p }' "$state_yaml" | sed 's/^ *- *//' | while read -r sub; do
151
- if [ -n "$sub" ]; then
152
- hf_download "starry-dist" "$model_path/$sub/.state.yaml"
153
- sub_state="$MODELS_BASE/starry-dist/$model_path/$sub/.state.yaml"
154
- if [ -f "$sub_state" ]; then
155
- sub_file=$(sed -n 's/^file: *//p' "$sub_state" | head -1)
156
- if [ -n "$sub_file" ]; then
157
- hf_download "starry-dist" "$model_path/$sub/$sub_file"
158
  fi
159
  fi
160
- fi
161
- done
162
  fi
163
- fi
164
- done
165
 
166
- # Download OCR/TF models (ocr-dist) — these use config YAML files
167
- for config_var in LOC_MODEL_PATH OCR_CONFIG BRACKETS_CONFIG; do
168
- eval config_path=\$$config_var
169
- if [ -n "$config_path" ]; then
170
- # For loc, the path is a directory; for ocr/brackets, it's a YAML config
171
- if echo "$config_path" | grep -q '\.yaml$\|\.yml$'; then
172
- # Download the config YAML
173
- hf_download "ocr-dist" "$config_path"
174
- config_file="$MODELS_BASE/ocr-dist/$config_path"
175
- if [ -f "$config_file" ]; then
176
- # Extract model paths from the config
177
- sed -n 's/^ *model_path: *//p' "$config_file" | while read -r mpath; do
178
- if [ -n "$mpath" ]; then
179
- # Download all files in the model directory
180
- _dir=$(dirname "$config_path")
181
- hf_download "ocr-dist" "$_dir/$mpath"
 
 
 
 
 
 
182
  fi
183
- done
184
- fi
185
- else
186
- # Directory-based model (loc) — download .state.yaml and model file
187
- hf_download "ocr-dist" "$config_path/.state.yaml"
188
- state_yaml="$MODELS_BASE/ocr-dist/$config_path/.state.yaml"
189
- if [ -f "$state_yaml" ]; then
190
- model_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)
191
- if [ -n "$model_file" ]; then
192
- hf_download "ocr-dist" "$config_path/$model_file"
193
  fi
194
  fi
195
  fi
196
- fi
197
- done
198
-
199
- echo "Model download complete."
200
- fi
201
 
202
- # ── Set predictor addresses (internal ZMQ) ──
203
- export PREDICTOR_LAYOUT="${PREDICTOR_LAYOUT:-tcp://127.0.0.1:12022}"
204
- export PREDICTOR_GAUGE="${PREDICTOR_GAUGE:-tcp://127.0.0.1:12023}"
205
- export PREDICTOR_GAUGE_RENDERER="${PREDICTOR_GAUGE_RENDERER:-tcp://127.0.0.1:15656}"
206
- export PREDICTOR_MASK="${PREDICTOR_MASK:-tcp://127.0.0.1:12024}"
207
- export PREDICTOR_SEMANTIC="${PREDICTOR_SEMANTIC:-tcp://127.0.0.1:12025}"
208
- export PREDICTOR_LOC="${PREDICTOR_LOC:-tcp://127.0.0.1:12026}"
209
- export PREDICTOR_OCR="${PREDICTOR_OCR:-tcp://127.0.0.1:12027}"
210
- export PREDICTOR_BRACKETS="${PREDICTOR_BRACKETS:-tcp://127.0.0.1:12028}"
211
 
212
- # ── Start Python ML predictors via supervisord ──
213
- if [ -f /home/node/app/supervisord.conf ] && [ -d /home/node/app/backend/python-services ]; then
214
- if [ -n "$LAYOUT_MODEL_PATH" ]; then
215
- echo "Starting Python ML predictors via supervisord..."
216
- supervisord -c /home/node/app/supervisord.conf &
217
- sleep 2
218
- else
219
- echo "Skipping ML predictors (no model paths configured)."
220
  fi
221
- fi
222
 
223
  # ── Run database migrations ──
224
  echo 'Running database migrations...'
 
118
  fi
119
  }
120
 
121
+ # ── Set predictor addresses (internal ZMQ) ──
122
+ export PREDICTOR_LAYOUT="${PREDICTOR_LAYOUT:-tcp://127.0.0.1:12022}"
123
+ export PREDICTOR_GAUGE="${PREDICTOR_GAUGE:-tcp://127.0.0.1:12023}"
124
+ export PREDICTOR_GAUGE_RENDERER="${PREDICTOR_GAUGE_RENDERER:-tcp://127.0.0.1:15656}"
125
+ export PREDICTOR_MASK="${PREDICTOR_MASK:-tcp://127.0.0.1:12024}"
126
+ export PREDICTOR_SEMANTIC="${PREDICTOR_SEMANTIC:-tcp://127.0.0.1:12025}"
127
+ export PREDICTOR_LOC="${PREDICTOR_LOC:-tcp://127.0.0.1:12026}"
128
+ export PREDICTOR_OCR="${PREDICTOR_OCR:-tcp://127.0.0.1:12027}"
129
+ export PREDICTOR_BRACKETS="${PREDICTOR_BRACKETS:-tcp://127.0.0.1:12028}"
130
 
131
+ # ── Download models & start ML predictors (background) ──
132
+ # Runs in background so Node services and nginx can start immediately.
133
+ (
134
+ if [ -n "$HF_TOKEN" ] && [ -f "$MODELS_YAML" ]; then
135
+ echo "Downloading model files from HuggingFace (background)..."
136
+
137
+ # Download bdtopo ONNX models
138
+ if [ -n "$BDTOPO_MODEL_PATH" ] && [ ! -f "$BDTOPO_MODEL_PATH" ]; then
139
+ _bdtopo_path=$(yaml_get bdtopo path)
140
+ _bdtopo_dir="$MODELS_BASE/starry-dist/$_bdtopo_path"
141
+ mkdir -p "$_bdtopo_dir"
142
+
143
+ sed -n '/^ files:/,/^[^ ]/{ /^ *- /p }' "$MODELS_YAML" | sed 's/^ *- *//' | while read -r FILE; do
144
+ hf_download "starry-dist" "$_bdtopo_path/$FILE"
145
+ done
146
+ fi
147
 
148
+ # Download PyTorch model directories (starry-dist)
149
+ for model_var in LAYOUT_MODEL_PATH MASK_MODEL_PATH SEMANTIC_MODEL_PATH GAUGE_MODEL_PATH; do
150
+ eval model_path=\$$model_var
151
+ if [ -n "$model_path" ]; then
152
+ hf_download "starry-dist" "$model_path/.state.yaml"
 
153
 
154
+ state_yaml="$MODELS_BASE/starry-dist/$model_path/.state.yaml"
155
+ if [ -f "$state_yaml" ]; then
156
+ model_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)
157
+ if [ -n "$model_file" ]; then
158
+ hf_download "starry-dist" "$model_path/$model_file"
159
+ fi
160
+ sed -n '/^subs:/,/^[a-z]/{ /^ - /p }' "$state_yaml" | sed 's/^ *- *//' | while read -r sub; do
161
+ if [ -n "$sub" ]; then
162
+ hf_download "starry-dist" "$model_path/$sub/.state.yaml"
163
+ sub_state="$MODELS_BASE/starry-dist/$model_path/$sub/.state.yaml"
164
+ if [ -f "$sub_state" ]; then
165
+ sub_file=$(sed -n 's/^file: *//p' "$sub_state" | head -1)
166
+ if [ -n "$sub_file" ]; then
167
+ hf_download "starry-dist" "$model_path/$sub/$sub_file"
168
+ fi
 
169
  fi
170
  fi
171
+ done
172
+ fi
173
  fi
174
+ done
 
175
 
176
+ # Download OCR/TF models (ocr-dist)
177
+ for config_var in LOC_MODEL_PATH OCR_CONFIG BRACKETS_CONFIG; do
178
+ eval config_path=\$$config_var
179
+ if [ -n "$config_path" ]; then
180
+ if echo "$config_path" | grep -q '\.yaml$\|\.yml$'; then
181
+ hf_download "ocr-dist" "$config_path"
182
+ config_file="$MODELS_BASE/ocr-dist/$config_path"
183
+ if [ -f "$config_file" ]; then
184
+ sed -n 's/^ *model_path: *//p' "$config_file" | while read -r mpath; do
185
+ if [ -n "$mpath" ]; then
186
+ _dir=$(dirname "$config_path")
187
+ hf_download "ocr-dist" "$_dir/$mpath"
188
+ fi
189
+ done
190
+ fi
191
+ else
192
+ hf_download "ocr-dist" "$config_path/.state.yaml"
193
+ state_yaml="$MODELS_BASE/ocr-dist/$config_path/.state.yaml"
194
+ if [ -f "$state_yaml" ]; then
195
+ model_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)
196
+ if [ -n "$model_file" ]; then
197
+ hf_download "ocr-dist" "$config_path/$model_file"
198
  fi
 
 
 
 
 
 
 
 
 
 
199
  fi
200
  fi
201
  fi
202
+ done
 
 
 
 
203
 
204
+ echo "Model download complete."
205
+ fi
 
 
 
 
 
 
 
206
 
207
+ # Start ML predictors after models are downloaded
208
+ if [ -f /home/node/app/supervisord.conf ] && [ -d /home/node/app/backend/python-services ]; then
209
+ if [ -n "$LAYOUT_MODEL_PATH" ]; then
210
+ echo "Starting Python ML predictors via supervisord..."
211
+ supervisord -c /home/node/app/supervisord.conf
212
+ else
213
+ echo "Skipping ML predictors (no model paths configured)."
214
+ fi
215
  fi
216
+ ) &
217
 
218
  # ── Run database migrations ──
219
  echo 'Running database migrations...'