Remove colab.py workarounds that broke transformers/hf-hub compatibility

PR #4601 added _pip_install_backend_deps(), _bootstrap_studio_venv(),
and _is_colab() to colab.py as workarounds for install_python_stack
being skipped on Colab. These workarounds:
- Stripped version constraints from studio.txt and installed into system Python
- Upgraded huggingface-hub to >=1.0, breaking Colab's pre-installed
  transformers which requires huggingface-hub<1.0

With install_python_stack now running on Colab (previous commit), these
workarounds are unnecessary — all deps are properly installed by setup.sh.
Restore colab.py to its original PR #4237 structure: just get_colab_url(),
show_link(), and start().
This commit is contained in:
Roland Tannous 2026-03-26 16:12:42 +00:00
commit c4a3323dae

View file

@ -18,90 +18,6 @@ if _backend_dir not in sys.path:
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
logger = get_logger(__name__)