Compare commits
3 commits
main
...
fix/colab-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dcb1534657 | ||
|
|
c4a3323dae | ||
|
|
2e156691e2 |
3 changed files with 6 additions and 88 deletions
|
|
@ -64,7 +64,7 @@
|
||||||
"id": "27e68f91"
|
"id": "27e68f91"
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": "!git clone --depth 1 --branch main https://github.com/unslothai/unsloth.git\n%cd /content/unsloth\n!chmod +x studio/setup.sh && ./studio/setup.sh --local"
|
"source": "!git clone --depth 1 --branch main https://github.com/unslothai/unsloth.git\n%cd /content/unsloth\n!chmod +x studio/setup.sh && ./studio/setup.sh"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
|
|
||||||
|
|
@ -18,90 +18,6 @@ if _backend_dir not in sys.path:
|
||||||
import _platform_compat # noqa: F401
|
import _platform_compat # noqa: F401
|
||||||
|
|
||||||
|
|
||||||
def _is_colab() -> bool:
|
|
||||||
"""Detect Google Colab by checking for COLAB_ prefixed env vars."""
|
|
||||||
import os
|
|
||||||
|
|
||||||
return any(k.startswith("COLAB_") for k in os.environ)
|
|
||||||
|
|
||||||
|
|
||||||
def _pip_install_backend_deps() -> None:
|
|
||||||
"""Install Studio backend dependencies directly into the current Python.
|
|
||||||
|
|
||||||
Used on Colab when the Studio venv does not exist (install.sh was not
|
|
||||||
run). Reads the requirements from studio.txt next to this file.
|
|
||||||
|
|
||||||
Version constraints are stripped entirely so pip keeps whatever Colab
|
|
||||||
already has installed (e.g. huggingface-hub, datasets, transformers)
|
|
||||||
and only installs genuinely missing packages like structlog, fastapi.
|
|
||||||
"""
|
|
||||||
import re
|
|
||||||
import subprocess
|
|
||||||
|
|
||||||
req_file = Path(__file__).parent / "requirements" / "studio.txt"
|
|
||||||
if not req_file.exists():
|
|
||||||
return
|
|
||||||
|
|
||||||
packages = []
|
|
||||||
for line in req_file.read_text().splitlines():
|
|
||||||
line = line.strip()
|
|
||||||
if not line or line.startswith("#"):
|
|
||||||
continue
|
|
||||||
# Strip all version constraints -- just keep the package name
|
|
||||||
pkg_name = re.split(r"[><=!~;\[]", line)[0].strip()
|
|
||||||
if pkg_name:
|
|
||||||
packages.append(pkg_name)
|
|
||||||
|
|
||||||
if not packages:
|
|
||||||
return
|
|
||||||
print("Installing Studio backend dependencies ...")
|
|
||||||
subprocess.check_call(
|
|
||||||
[sys.executable, "-m", "pip", "install", "-q"] + packages,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Colab ships huggingface-hub 0.36.x which removed is_offline_mode,
|
|
||||||
# breaking transformers. Upgrade to 1.0+ which restored it.
|
|
||||||
try:
|
|
||||||
from huggingface_hub import is_offline_mode # noqa: F401
|
|
||||||
except ImportError:
|
|
||||||
print("Upgrading huggingface-hub (is_offline_mode missing) ...")
|
|
||||||
subprocess.check_call(
|
|
||||||
[sys.executable, "-m", "pip", "install", "-q", "huggingface-hub>=1.0"],
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _bootstrap_studio_venv() -> None:
|
|
||||||
"""Expose the Studio venv's site-packages to the current interpreter.
|
|
||||||
|
|
||||||
On Colab, notebook cells run outside the venv subshell. Instead of
|
|
||||||
installing the full stack into system Python, we prepend the venv's
|
|
||||||
site-packages so that packages like structlog, fastapi, etc. are
|
|
||||||
importable from notebook cells and take priority over system copies.
|
|
||||||
|
|
||||||
If the venv does not exist and we are running on Colab, fall back to
|
|
||||||
pip-installing the backend dependencies into the current environment
|
|
||||||
so that imports like structlog and fastapi succeed.
|
|
||||||
"""
|
|
||||||
venv_lib = Path.home() / ".unsloth" / "studio" / "unsloth_studio" / "lib"
|
|
||||||
if not venv_lib.exists():
|
|
||||||
if _is_colab():
|
|
||||||
_pip_install_backend_deps()
|
|
||||||
return
|
|
||||||
import warnings
|
|
||||||
|
|
||||||
warnings.warn(
|
|
||||||
f"Studio venv not found at {venv_lib.parent} -- run 'unsloth studio setup' first",
|
|
||||||
stacklevel = 2,
|
|
||||||
)
|
|
||||||
return
|
|
||||||
for sp in venv_lib.glob("python*/site-packages"):
|
|
||||||
sp_str = str(sp)
|
|
||||||
if sp_str not in sys.path:
|
|
||||||
sys.path.insert(0, sp_str)
|
|
||||||
|
|
||||||
|
|
||||||
_bootstrap_studio_venv()
|
|
||||||
|
|
||||||
from loggers import get_logger
|
from loggers import get_logger
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
|
|
|
||||||
|
|
@ -346,13 +346,15 @@ fi
|
||||||
# ── Check if Python deps need updating ──
|
# ── Check if Python deps need updating ──
|
||||||
# Compare installed package version against PyPI latest.
|
# Compare installed package version against PyPI latest.
|
||||||
# Skip all Python dependency work if versions match (fast update path).
|
# Skip all Python dependency work if versions match (fast update path).
|
||||||
# On Colab (no venv), skip the entire venv-dependent Python deps section.
|
# On Colab (no venv), skip this version check (it needs $VENV_DIR/bin/python)
|
||||||
|
# but still run install_python_stack below (it uses sys.executable).
|
||||||
_SKIP_PYTHON_DEPS=false
|
_SKIP_PYTHON_DEPS=false
|
||||||
|
_SKIP_VERSION_CHECK=false
|
||||||
if [ "$_COLAB_NO_VENV" = true ]; then
|
if [ "$_COLAB_NO_VENV" = true ]; then
|
||||||
_SKIP_PYTHON_DEPS=true
|
_SKIP_VERSION_CHECK=true
|
||||||
fi
|
fi
|
||||||
_PKG_NAME="${STUDIO_PACKAGE_NAME:-unsloth}"
|
_PKG_NAME="${STUDIO_PACKAGE_NAME:-unsloth}"
|
||||||
if [ "$_SKIP_PYTHON_DEPS" != true ] && [ "${SKIP_STUDIO_BASE:-0}" != "1" ] && [ "${STUDIO_LOCAL_INSTALL:-0}" != "1" ]; then
|
if [ "$_SKIP_VERSION_CHECK" != true ] && [ "${SKIP_STUDIO_BASE:-0}" != "1" ] && [ "${STUDIO_LOCAL_INSTALL:-0}" != "1" ]; then
|
||||||
# Only check when NOT called from install.sh (which just installed the package)
|
# Only check when NOT called from install.sh (which just installed the package)
|
||||||
INSTALLED_VER=$("$VENV_DIR/bin/python" -c "
|
INSTALLED_VER=$("$VENV_DIR/bin/python" -c "
|
||||||
from importlib.metadata import version
|
from importlib.metadata import version
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue