SYNAPSE
RU
Guides

HuggingFace for Beginners: How to Choose and Run a Model

Step-by-step guide: registration, finding a model, running via transformers, and checking the result. No unnecessary theory.

What You'll Need

  • A HuggingFace account (free).
  • Python 3.9 or newer.
  • The transformers and torch packages installed.
  • An access token (if the model requires authorization).
  • A basic understanding of what a model and weights are.

Step 1. Sign Up and Get a Token

Go to huggingface.co and create an account. Without an account, you can download many models, but some (e.g., Llama) require accepting a license.

The token is needed to access private or licensed models. Create it in settings: Settings → Access Tokens → New token. Choose the read role.

Save the token in an environment variable:

export HF_TOKEN=YOUR_TOKEN

Where to get it: on the token creation page. Copy it immediately—it won't be shown again.

Step 2. Install the Libraries

pip install transformers torch

If you have a GPU, install the torch version with CUDA support. The command depends on the CUDA version. Instructions are at pytorch.org.

Step 3. Find a Model

On the HuggingFace homepage there is a search. Enter a task, e.g., "text-classification" or "russian sentiment". On the left are filters: tasks, libraries, languages, licenses.

Pay attention to the model card. It states: which task, what data it was trained on, what metrics, how to run it. If there is no code example in the card, that's a bad sign.

Don't take the first model you come across. Check that it supports your language and task. For example, for Russian, look for models with the "ru" tag.

Step 4. Run the Model via pipeline

The simplest way is to use pipeline. It's a wrapper that loads the model and tokenizer (text-to-numbers converter) itself.

Шаг 4. Запустите модель через pipeline
from transformers import pipeline

classifier = pipeline("sentiment-analysis", model="blanchefort/rubert-base-cased-sentiment")
result = classifier("Я люблю HuggingFace")
print(result)

This code will download the model and run it. The first run may take time—weights are downloaded from the internet.

If the model requires a token, pipeline will pick it up from the HF_TOKEN variable.

Step 5. Check the Result

The output will be something like:

[{'label': 'POSITIVE', 'score': 0.99}]

The label and confidence depend on the model. If the result is strange, check that the model is trained on Russian. For example, a model for English on Russian text will produce garbage.

Step 6. Run the Model Manually (if pipeline doesn't fit)

Sometimes you need access to hidden states or a non-standard pipeline. Then load the model and tokenizer separately.

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

model_name = "blanchefort/rubert-base-cased-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

inputs = tokenizer("Я люблю HuggingFace", return_tensors="pt")
with torch.no_grad():
    outputs = model(**inputs)

probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
print(probs)

This code returns probabilities for each class. See the class labels in the model card (usually in the config.json file).

Step 7. Save the Model Locally

If the model is already downloaded, it's in the cache. Default path: ~/.cache/huggingface/hub. You can specify your own folder:

Шаг 6. Запустите модель вручную (если pipeline не подходит)
model.save_pretrained("./my_model")
tokenizer.save_pretrained("./my_model")

Then load from the folder:

model = AutoModelForSequenceClassification.from_pretrained("./my_model")
tokenizer = AutoTokenizer.from_pretrained("./my_model")

Common Errors

Error 1: OSError: Can't load tokenizer

The model is not found or there is no access. Check the model name on the website. If the model is private, make sure the token is passed.

Error 2: RuntimeError: CUDA out of memory

The model doesn't fit in GPU memory. Reduce the batch size or run on CPU. For CPU, use device=-1 in pipeline.

Error 3: The model produces nonsense

Most likely, the model is not for your language or task. Check the tags and description. Not all models are multilingual.

Error 4: Slow loading

The first time, weights are downloaded. This is normal. Subsequent runs will be faster. If speed is critical, use mirrors or a local cache.

Error 5: Mismatch of transformers and torch versions

Update both libraries. pip install --upgrade transformers torch.

What's Next

Once you've mastered pipeline, move on to fine-tuning. There's Trainer and examples in the documentation for that. But that's a separate story.

↗ Primary source: Habr