RuGlyphs / zero_shot /eval /eval_ocr.sh
ruglyphs's picture
RuGlyphs
6a4aac7 verified
Raw History Blame Contribute Delete
3.22 kB
#!/bin/bash
# Zero-shot benchmark for specialized OCR models.
#
# Unlike eval_vl.sh, each model gets its authors' recipe: prompt and its position
# in the request (olmOCR: text before the image; LightOnOCR: no text), image size
# (olmOCR 1288 px, LightOnOCR 1540 px), extra_body (DeepSeek-OCR ngram,
# HunyuanOCR repetition_penalty) and the answer format to strip before comparing
# with the plain-text reference (YAML front matter, HTML, DocTags, markdown).
# All of it is in ocr_prompts.json, with the source of each value.
#
# ./eval_ocr.sh # all models from ocr_prompts.json
# ./eval_ocr.sh GLM-OCR dots.ocr
# LIMIT=20 TAG=smoke ./eval_ocr.sh GLM-OCR
# DRY=1 ./eval_ocr.sh
set -uo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/common.sh"
TAG="${TAG:-ocr_$(date +%Y%m%d)}"
DRY="${DRY:-0}"
JOBS="${JOBS:-1}"
[ -f "$OCR_PROMPTS" ] || { echo "$OCR_PROMPTS not found" >&2; exit 1; }
[ -f "$REGISTRY" ] || { echo "registry $REGISTRY not found" >&2; exit 1; }
if [ $# -gt 0 ]; then
MODELS=("$@")
else
MODELS=()
while IFS= read -r m; do MODELS+=("$m"); done < <(
python3 -c "
import json
d=json.load(open('$OCR_PROMPTS'))
print('\n'.join(k for k in d if not k.startswith('_')))")
fi
ok=0; skip=0; fail=0; started=""
for m in "${MODELS[@]}"; do
out="$RESULTS_ROOT/$m/$TAG"
if has_results "$out"; then
printf '%-28s %s\n' "$m" "already done -> $out"; skip=$((skip+1)); continue
fi
# task and backend settings from ocr_prompts.json by model name: the task
# reads RUGLYPHS_* (passed via --export=ALL), the backend reads model_args
# (collected into EXTRA_MODEL_ARGS).
eval "$(python3 "$EVAL_DIR/ocr_env.py" "$OCR_PROMPTS" "$m")"
url="$(registry_url "$m")"
if [ -z "$url" ]; then
printf '%-28s %s\n' "$m" "not in registry (not served)"; fail=$((fail+1)); continue
fi
if ! curl -sf --max-time 15 "${url%/v1}/health" >/dev/null; then
printf '%-28s %s\n' "$m" "registered but /health does not respond, skipped"; fail=$((fail+1)); continue
fi
printf '%-28s %s\n' "$m" "$OCR_SUMMARY"
[ "$DRY" = "1" ] && continue
export EXTRA_MODEL_ARGS="${OCR_MODEL_ARGS:-}"
if [ "$JOBS" -le 1 ]; then
if "$EVAL_DIR/eval_endpoint.sh" "$m" "$TASKS" "$TAG"; then ok=$((ok+1)); else
fail=$((fail+1)); echo " ERROR, log: $out/eval.log" >&2
fi
continue
fi
mkdir -p "$out"
while [ "$(jobs -rp | wc -l)" -ge "$JOBS" ]; do sleep 5; done
echo " -> background, log: $out/runner.log"
"$EVAL_DIR/eval_endpoint.sh" "$m" "$TASKS" "$TAG" > "$out/runner.log" 2>&1 &
started="$started $m"
done
if [ "$JOBS" -gt 1 ]; then
echo; echo "waiting for jobs..."; wait
for m in $started; do
out="$RESULTS_ROOT/$m/$TAG"
if has_results "$out"; then ok=$((ok+1)); else
fail=$((fail+1)); echo " no metrics: $m, see $out/runner.log" >&2
fi
done
fi
echo; echo "done: $ok, skipped: $skip, failed: $fail"
[ "$DRY" = "1" ] && exit 0
echo; printf '%-28s %s\n' MODEL "LIMIT from eval.log"
for m in "${MODELS[@]}"; do
lim="$(limit_line "$RESULTS_ROOT/$m/$TAG")"
[ -n "$lim" ] && printf '%-28s %s\n' "$m" "$lim"
done