Spaces:
Sleeping
Sleeping
File size: 2,541 Bytes
09da05c | 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #!/bin/sh
set -eu
VENV_DIR="${ADSP_VENV_DIR:-/opt/venv}"
PYTHON_BIN="$VENV_DIR/bin/python"
PIP_BIN="$VENV_DIR/bin/pip"
is_truthy() {
case "${1:-}" in
1|true|TRUE|True|yes|YES|on|ON)
return 0
;;
*)
return 1
;;
esac
}
ensure_venv() {
if [ ! -x "$PYTHON_BIN" ]; then
python -m venv "$VENV_DIR"
fi
}
needs_requirements_install() {
"$PYTHON_BIN" - <<'PY'
import importlib.util
required = ("fastapi", "sentence_transformers", "streamlit")
missing = [name for name in required if importlib.util.find_spec(name) is None]
print("yes" if missing else "no")
PY
}
ensure_requirements() {
if [ "$(needs_requirements_install)" = "yes" ]; then
echo "Installing Python requirements into $VENV_DIR"
"$PIP_BIN" install --upgrade pip
"$PIP_BIN" install -r requirements.txt
fi
}
torch_install_state() {
"$PYTHON_BIN" - <<'PY'
import importlib.util
import os
want_cuda = os.environ.get("ADSP_TORCH_EXPECT_CUDA", "true").strip().lower() in {"1", "true", "yes", "on"}
spec = importlib.util.find_spec("torch")
if spec is None:
print("missing")
raise SystemExit
import torch
has_cuda_build = bool(getattr(torch.version, "cuda", None))
if want_cuda and not has_cuda_build:
print("needs_cuda")
else:
print("ok")
PY
}
ensure_torch_runtime() {
if ! is_truthy "${ADSP_TORCH_INSTALL_ENABLED:-true}"; then
return 0
fi
torch_state="$(torch_install_state)"
if is_truthy "${ADSP_TORCH_FORCE_REINSTALL:-false}"; then
torch_state="force"
fi
if [ "$torch_state" = "ok" ]; then
return 0
fi
torch_index_url="${ADSP_TORCH_INDEX_URL:-https://download.pytorch.org/whl/cu124}"
torch_packages="${ADSP_TORCH_PACKAGES:-torch torchvision torchaudio}"
echo "Installing PyTorch runtime from $torch_index_url"
echo "PyTorch packages: $torch_packages"
set -- "$PIP_BIN" install --index-url "$torch_index_url"
if is_truthy "${ADSP_TORCH_FORCE_REINSTALL:-false}"; then
set -- "$@" --force-reinstall
fi
for package in $torch_packages; do
set -- "$@" "$package"
done
"$@"
}
log_torch_runtime() {
"$PYTHON_BIN" - <<'PY'
import importlib.util
if importlib.util.find_spec("torch") is None:
print("PyTorch runtime check: torch is not installed")
raise SystemExit
import torch
print(
"PyTorch runtime check: "
f"version={torch.__version__} "
f"cuda_build={getattr(torch.version, 'cuda', None)} "
f"cuda_available={torch.cuda.is_available()}"
)
PY
}
ensure_venv
ensure_requirements
ensure_torch_runtime
log_torch_runtime
|