#!/usr/bin/env bash set -euo pipefail if [[ $# -ne 2 ]]; then echo "Usage: $0 INPUT_PDF_DIR OUTPUT_DIR" >&2 exit 2 fi input_dir=$(realpath "$1") output_dir=$(realpath -m "$2") env_dir=/data/chenxing/code/docs/tools/.envs/mineru cache_dir=/data/chenxing/code/docs/tools/model_cache mkdir -p "$output_dir" "$output_dir/logs" work_dir=$(mktemp -d "$output_dir/.shards.XXXXXXXX") trap 'rm -rf -- "$work_dir"' EXIT mapfile -d '' pdfs < <(find "$input_dir" -maxdepth 1 -type f -iname '*.pdf' -print0 | sort -z) if [[ ${#pdfs[@]} -eq 0 ]]; then echo "No PDFs found in $input_dir" >&2 exit 1 fi for gpu in {0..7}; do mkdir -p "$work_dir/$gpu" done for i in "${!pdfs[@]}"; do gpu=$((i % 8)) ln -s "${pdfs[$i]}" "$work_dir/$gpu/$(basename "${pdfs[$i]}")" done pids=() for gpu in {0..7}; do if find "$work_dir/$gpu" -type l -print -quit | grep -q .; then env CUDA_VISIBLE_DEVICES="$gpu" \ HF_HOME="$cache_dir" \ MODELSCOPE_CACHE="$cache_dir/modelscope" \ "$env_dir/bin/mineru" \ -p "$work_dir/$gpu" \ -o "$output_dir" \ -b hybrid-engine \ --effort high \ --image-analysis true \ --formula true \ --table true \ >"$output_dir/logs/gpu-$gpu.log" 2>&1 & pids+=("$!") fi done status=0 for pid in "${pids[@]}"; do if ! wait "$pid"; then status=1 fi done if [[ $status -ne 0 ]]; then echo "At least one shard failed; inspect $output_dir/logs/gpu-*.log" >&2 exit "$status" fi echo "Converted ${#pdfs[@]} PDFs into $output_dir"