# Setting Up Your Environment — Hugging Face Transformers

Source: https://www.skillbyai.com/en/hugging-face/hf-setup

> Install transformers with a backend, understand the model cache and log in to the Hub safely.

## What you install and where files go

`transformers` is a Python library that needs a deep-learning **backend** to run models; for current releases that means **PyTorch**. Install both in a virtual environment so project versions do not clash. The first time you call `from_pretrained` with a model id, the library downloads the config, tokenizer files and weights from the Hub and stores them in a **local cache**, by default under `~/.cache/huggingface/hub`. Later calls reuse the cache, so only the first run is slow. You can move the cache with the `HF_HOME` environment variable, which is useful on college lab machines with a small home directory. Public models download without an account, but **gated** models (whose authors ask you to accept a licence) and private repositories need an **access token**, which you create in your Hub settings and supply with the `hf auth login` command or the `HF_TOKEN` environment variable.

## Install into a virtual environment

Create an isolated environment, then install the library with PyTorch and the companion packages used in this course.

```bash
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install torch transformers datasets accelerate evaluate peft
# Optional: move the download cache to a bigger disk
export HF_HOME=/data/hf-cache
```

## Check the install and log in

Print the installed versions and whether a GPU is visible. `hf auth login` stores your token locally; on older huggingface_hub versions the command is `huggingface-cli login`.

```bash
python -c "import transformers, torch; print(transformers.__version__, torch.cuda.is_available())"
hf auth login        # paste a token from your Hub settings
hf auth whoami       # confirms which account is active
```

## Never hard-code your token

Do not paste a Hub token into a notebook you will share or a script you will push to GitHub. Read it from the `HF_TOKEN` environment variable or a secrets manager, and give it *read* scope unless you really need to upload.

**Quiz:** You run the same `from_pretrained("some-model")` line twice on one machine. Why is the second run much faster?

- [ ] The second run downloads a smaller version of the weights
- [ ] transformers compiles the model to C++ after the first run
- [ ] The Hub caches the model in your browser
- [x] The files are reused from the local Hugging Face cache

*Answer:* The files are reused from the local Hugging Face cache. Downloaded files are stored in the local cache (by default under ~/.cache/huggingface, or HF_HOME), so later loads read from disk.
