Blame view

scripts/setup_translator_venv.sh 2.13 KB
0fd2f875   tangwang   translate
1
2
3
4
5
6
7
8
9
10
  #!/bin/bash
  #
  # Create isolated venv for translator service (.venv-translator).
  #
  set -euo pipefail
  
  PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
  cd "${PROJECT_ROOT}"
  
  VENV_DIR="${PROJECT_ROOT}/.venv-translator"
0fd2f875   tangwang   translate
11
  TMP_DIR="${TRANSLATOR_PIP_TMPDIR:-${PROJECT_ROOT}/.tmp/translator-pip}"
f07947a5   tangwang   Improve portabili...
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
  MIN_PYTHON_MAJOR=3
  MIN_PYTHON_MINOR=10
  
  python_meets_minimum() {
    local bin="$1"
    "${bin}" - <<'PY' "${MIN_PYTHON_MAJOR}" "${MIN_PYTHON_MINOR}"
  import sys
  
  required = tuple(int(value) for value in sys.argv[1:])
  sys.exit(0 if sys.version_info[:2] >= required else 1)
  PY
  }
  
  discover_python_bin() {
    local candidates=()
  
    if [[ -n "${PYTHON_BIN:-}" ]]; then
      candidates+=("${PYTHON_BIN}")
    fi
    candidates+=("python3.12" "python3.11" "python3.10" "python3")
  
    local candidate
    for candidate in "${candidates[@]}"; do
      if ! command -v "${candidate}" >/dev/null 2>&1; then
        continue
      fi
      if python_meets_minimum "${candidate}"; then
        echo "${candidate}"
        return 0
      fi
    done
  
    return 1
  }
  
  if ! PYTHON_BIN="$(discover_python_bin)"; then
    echo "ERROR: unable to find Python >= ${MIN_PYTHON_MAJOR}.${MIN_PYTHON_MINOR}." >&2
    echo "Set PYTHON_BIN to a compatible interpreter and rerun." >&2
    exit 1
  fi
0fd2f875   tangwang   translate
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  
  if ! command -v "${PYTHON_BIN}" >/dev/null 2>&1; then
    echo "ERROR: python not found: ${PYTHON_BIN}" >&2
    exit 1
  fi
  
  if [[ -d "${VENV_DIR}" && ! -f "${VENV_DIR}/bin/activate" ]]; then
    echo "Found broken venv at ${VENV_DIR}, recreating..."
    rm -rf "${VENV_DIR}"
  fi
  
  if [[ ! -d "${VENV_DIR}" ]]; then
    echo "Creating ${VENV_DIR}"
    "${PYTHON_BIN}" -m venv "${VENV_DIR}"
  else
    echo "Reusing ${VENV_DIR}"
  fi
  
  mkdir -p "${TMP_DIR}"
  export TMPDIR="${TMP_DIR}"
  PIP_ARGS=(--no-cache-dir)
  
f07947a5   tangwang   Improve portabili...
74
  echo "Using Python=${PYTHON_BIN}"
0fd2f875   tangwang   translate
75
76
77
78
79
80
81
  echo "Using TMPDIR=${TMPDIR}"
  "${VENV_DIR}/bin/python" -m pip install "${PIP_ARGS[@]}" --upgrade pip wheel
  "${VENV_DIR}/bin/python" -m pip install "${PIP_ARGS[@]}" -r requirements_translator_service.txt
  
  echo
  echo "Done."
  echo "Translator venv: ${VENV_DIR}"
32e9b30c   tangwang   scripts/ 根目录主要保留启...
82
  echo "Download local models: ./.venv-translator/bin/python scripts/translation/download_translation_models.py --all-local"
0fd2f875   tangwang   translate
83
  echo "Start service: ./scripts/start_translator.sh"