Spaces:
Sleeping
Sleeping
Commit ·
7b97a14
1
Parent(s): 197a18e
fix: run model download and ML predictors in background so services start immediately
Browse files117 unmodified lines118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224117 unmodified linesfi}if [ -n "$HF_TOKEN" ] && [ -f "$MODELS_YAML" ]; thenecho "Downloading model files from HuggingFace..."# Download bdtopo ONNX modelsif [ -n "$BDTOPO_MODEL_PATH" ] && [ ! -f "$BDTOPO_MODEL_PATH" ]; then_bdtopo_path=$(yaml_get bdtopo path)_bdtopo_dir="$MODELS_BASE/starry-dist/$_bdtopo_path"mkdir -p "$_bdtopo_dir"sed -n '/^ files:/,/^[^ ]/{ /^ *- /p }' "$MODELS_YAML" | sed 's/^ *- *//' | while read -r FILE; dohf_download "starry-dist" "$_bdtopo_path/$FILE"donefi# Download PyTorch model directories (starry-dist)for model_var in LAYOUT_MODEL_PATH MASK_MODEL_PATH SEMANTIC_MODEL_PATH GAUGE_MODEL_PATH; doeval model_path=\$$model_varif [ -n "$model_path" ]; then# Download .state.yaml which lists the actual model fileshf_download "starry-dist" "$model_path/.state.yaml"state_yaml="$MODELS_BASE/starry-dist/$model_path/.state.yaml"if [ -f "$state_yaml" ]; then# Extract model filename from .state.yamlmodel_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)if [ -n "$model_file" ]; thenhf_download "starry-dist" "$model_path/$model_file"fi# Extract sub-model files if present (semantic clusters)sed -n '/^subs:/,/^[a-z]/{ /^ - /p }' "$state_yaml" | sed 's/^ *- *//' | while read -r sub; doif [ -n "$sub" ]; thenhf_download "starry-dist" "$model_path/$sub/.state.yaml"sub_state="$MODELS_BASE/starry-dist/$model_path/$sub/.state.yaml"if [ -f "$sub_state" ]; thensub_file=$(sed -n 's/^file: *//p' "$sub_state" | head -1)if [ -n "$sub_file" ]; thenhf_download "starry-dist" "$model_path/$sub/$sub_file"fififidonefifidone# Download OCR/TF models (ocr-dist) — these use config YAML filesfor config_var in LOC_MODEL_PATH OCR_CONFIG BRACKETS_CONFIG; doeval config_path=\$$config_varif [ -n "$config_path" ]; then# For loc, the path is a directory; for ocr/brackets, it's a YAML configif echo "$config_path" | grep -q '\.yaml$\|\.yml$'; then# Download the config YAMLhf_download "ocr-dist" "$config_path"config_file="$MODELS_BASE/ocr-dist/$config_path"if [ -f "$config_file" ]; then# Extract model paths from the configsed -n 's/^ *model_path: *//p' "$config_file" | while read -r mpath; doif [ -n "$mpath" ]; then# Download all files in the model directory_dir=$(dirname "$config_path")hf_download "ocr-dist" "$_dir/$mpath"fidonefielse# Directory-based model (loc) — download .state.yaml and model filehf_download "ocr-dist" "$config_path/.state.yaml"state_yaml="$MODELS_BASE/ocr-dist/$config_path/.state.yaml"if [ -f "$state_yaml" ]; thenmodel_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)if [ -n "$model_file" ]; thenhf_download "ocr-dist" "$config_path/$model_file"fifififidoneecho "Model download complete."fi# ── Set predictor addresses (internal ZMQ) ──export PREDICTOR_LAYOUT="${PREDICTOR_LAYOUT:-tcp://127.0.0.1:12022}"export PREDICTOR_GAUGE="${PREDICTOR_GAUGE:-tcp://127.0.0.1:12023}"export PREDICTOR_GAUGE_RENDERER="${PREDICTOR_GAUGE_RENDERER:-tcp://127.0.0.1:15656}"export PREDICTOR_MASK="${PREDICTOR_MASK:-tcp://127.0.0.1:12024}"export PREDICTOR_SEMANTIC="${PREDICTOR_SEMANTIC:-tcp://127.0.0.1:12025}"export PREDICTOR_LOC="${PREDICTOR_LOC:-tcp://127.0.0.1:12026}"export PREDICTOR_OCR="${PREDICTOR_OCR:-tcp://127.0.0.1:12027}"export PREDICTOR_BRACKETS="${PREDICTOR_BRACKETS:-tcp://127.0.0.1:12028}"# ── Start Python ML predictors via supervisord ──if [ -f /home/node/app/supervisord.conf ] && [ -d /home/node/app/backend/python-services ]; thenif [ -n "$LAYOUT_MODEL_PATH" ]; thenecho "Starting Python ML predictors via supervisord..."supervisord -c /home/node/app/supervisord.conf &sleep 2elseecho "Skipping ML predictors (no model paths configured)."fifi# ── Run database migrations ──echo 'Running database migrations...'117 unmodified lines118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219117 unmodified linesfi}# ── Set predictor addresses (internal ZMQ) ──export PREDICTOR_LAYOUT="${PREDICTOR_LAYOUT:-tcp://127.0.0.1:12022}"export PREDICTOR_GAUGE="${PREDICTOR_GAUGE:-tcp://127.0.0.1:12023}"export PREDICTOR_GAUGE_RENDERER="${PREDICTOR_GAUGE_RENDERER:-tcp://127.0.0.1:15656}"export PREDICTOR_MASK="${PREDICTOR_MASK:-tcp://127.0.0.1:12024}"export PREDICTOR_SEMANTIC="${PREDICTOR_SEMANTIC:-tcp://127.0.0.1:12025}"export PREDICTOR_LOC="${PREDICTOR_LOC:-tcp://127.0.0.1:12026}"export PREDICTOR_OCR="${PREDICTOR_OCR:-tcp://127.0.0.1:12027}"export PREDICTOR_BRACKETS="${PREDICTOR_BRACKETS:-tcp://127.0.0.1:12028}"# ── Download models & start ML predictors (background) ──# Runs in background so Node services and nginx can start immediately.(if [ -n "$HF_TOKEN" ] && [ -f "$MODELS_YAML" ]; thenecho "Downloading model files from HuggingFace (background)..."# Download bdtopo ONNX modelsif [ -n "$BDTOPO_MODEL_PATH" ] && [ ! -f "$BDTOPO_MODEL_PATH" ]; then_bdtopo_path=$(yaml_get bdtopo path)_bdtopo_dir="$MODELS_BASE/starry-dist/$_bdtopo_path"mkdir -p "$_bdtopo_dir"sed -n '/^ files:/,/^[^ ]/{ /^ *- /p }' "$MODELS_YAML" | sed 's/^ *- *//' | while read -r FILE; dohf_download "starry-dist" "$_bdtopo_path/$FILE"donefi# Download PyTorch model directories (starry-dist)for model_var in LAYOUT_MODEL_PATH MASK_MODEL_PATH SEMANTIC_MODEL_PATH GAUGE_MODEL_PATH; doeval model_path=\$$model_varif [ -n "$model_path" ]; thenhf_download "starry-dist" "$model_path/.state.yaml"state_yaml="$MODELS_BASE/starry-dist/$model_path/.state.yaml"if [ -f "$state_yaml" ]; thenmodel_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)if [ -n "$model_file" ]; thenhf_download "starry-dist" "$model_path/$model_file"fised -n '/^subs:/,/^[a-z]/{ /^ - /p }' "$state_yaml" | sed 's/^ *- *//' | while read -r sub; doif [ -n "$sub" ]; thenhf_download "starry-dist" "$model_path/$sub/.state.yaml"sub_state="$MODELS_BASE/starry-dist/$model_path/$sub/.state.yaml"if [ -f "$sub_state" ]; thensub_file=$(sed -n 's/^file: *//p' "$sub_state" | head -1)if [ -n "$sub_file" ]; thenhf_download "starry-dist" "$model_path/$sub/$sub_file"fififidonefifidone# Download OCR/TF models (ocr-dist)for config_var in LOC_MODEL_PATH OCR_CONFIG BRACKETS_CONFIG; doeval config_path=\$$config_varif [ -n "$config_path" ]; thenif echo "$config_path" | grep -q '\.yaml$\|\.yml$'; thenhf_download "ocr-dist" "$config_path"config_file="$MODELS_BASE/ocr-dist/$config_path"if [ -f "$config_file" ]; thensed -n 's/^ *model_path: *//p' "$config_file" | while read -r mpath; doif [ -n "$mpath" ]; then_dir=$(dirname "$config_path")hf_download "ocr-dist" "$_dir/$mpath"fidonefielsehf_download "ocr-dist" "$config_path/.state.yaml"state_yaml="$MODELS_BASE/ocr-dist/$config_path/.state.yaml"if [ -f "$state_yaml" ]; thenmodel_file=$(sed -n 's/^file: *//p' "$state_yaml" | head -1)if [ -n "$model_file" ]; thenhf_download "ocr-dist" "$config_path/$model_file"fifififidoneecho "Model download complete."fi# Start ML predictors after models are downloadedif [ -f /home/node/app/supervisord.conf ] && [ -d /home/node/app/backend/python-services ]; thenif [ -n "$LAYOUT_MODEL_PATH" ]; thenecho "Starting Python ML predictors via supervisord..."supervisord -c /home/node/app/supervisord.confelseecho "Skipping ML predictors (no model paths configured)."fifi) &# ── Run database migrations ──echo 'Running database migrations...'
docker-entrypoint.sh+82-87