File size: 2,158 Bytes
1e1d0ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUNDLE_DIR="${BUNDLE_DIR:-$ROOT_DIR/bundles/qwen3_reranker_ane_bundle_4b}"
HOST="${HOST:-127.0.0.1}"
PORT="${PORT:-8000}"
COMPUTE_UNITS="${COMPUTE_UNITS:-cpu_and_ne}"
MODEL_ID="${MODEL_ID:-qwen3-reranker-4b-ane}"
if [[ -n "${PYTHON_BIN:-}" ]]; then
PY_BIN="$PYTHON_BIN"
elif [[ -x "$ROOT_DIR/.venv/bin/python" ]]; then
PY_BIN="$ROOT_DIR/.venv/bin/python"
else
PY_BIN="python3"
fi
if ! command -v "$PY_BIN" >/dev/null 2>&1; then
echo "[ERROR] Python 不可用: $PY_BIN"
echo "请先执行: python3.11 -m venv .venv && source .venv/bin/activate && python -m pip install -r requirements-service.txt"
exit 1
fi
PY_MM="$($PY_BIN -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
case "$PY_MM" in
3.10|3.11|3.12) ;;
*)
echo "[ERROR] 当前 Python 版本为 ${PY_MM},不受支持。"
echo "coremltools 在该版本下无法加载本地运行库(典型报错: libcoremlpython / libmilstoragepython)。"
echo "请改用 Python 3.11:"
echo " python3.11 -m venv .venv"
echo " source .venv/bin/activate"
echo " python -m pip install -r requirements-service.txt"
exit 1
;;
esac
if [[ ! -f "$BUNDLE_DIR/manifest.json" ]]; then
echo "[ERROR] 未找到 bundle manifest: $BUNDLE_DIR/manifest.json"
exit 1
fi
$PY_BIN - <<'PY'
import sys
errors = []
try:
import coremltools # noqa: F401
except Exception as e:
errors.append(f"import coremltools 失败: {e}")
for mod in ("coremltools.libcoremlpython", "coremltools.libmilstoragepython"):
try:
__import__(mod)
except Exception as e:
errors.append(f"{mod} 加载失败: {e}")
if errors:
print("[ERROR] Core ML Python 运行库不可用:")
for item in errors:
print(" -", item)
print("请确认你在 Python 3.11 的虚拟环境中安装依赖。")
sys.exit(1)
PY
cd "$ROOT_DIR"
exec "$PY_BIN" -m qwen3_ane_rerank serve \
--bundle-dir "$BUNDLE_DIR" \
--no-auto-build \
--compute-units "$COMPUTE_UNITS" \
--model-id "$MODEL_ID" \
--host "$HOST" \
--port "$PORT"
|