Python / Python Modern Generative AI and Agents Interview Questions
What is the Auto-class pattern in Hugging Face and how do you run inference with a raw model?
The Auto* classes (AutoTokenizer, AutoModel, AutoModelForSequenceClassification, etc.) are factory classes that read a model's config.json from the Hub and automatically instantiate the correct tokenizer or model architecture without you needing to know which specific class to use. This makes code model-agnostic — you can swap a BERT model for a RoBERTa or DistilBERT model by changing only the model name string.
For custom inference beyond what pipeline() provides, you load the tokenizer and model separately, tokenize the input, run the forward pass, and post-process the logits. Understanding this lower-level workflow is essential for fine-tuning, batched inference at scale, and extracting intermediate representations (embeddings).
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_name = 'distilbert-base-uncased-finetuned-sst-2-english'
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
model.eval() # disable dropout
texts = ['I love this movie!', 'This was a terrible waste of time.']
inputs = tokenizer(texts, return_tensors='pt', padding=True, truncation=True)
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits # (batch, num_labels)
probs = torch.softmax(logits, dim=-1) # convert to probabilities
preds = torch.argmax(probs, dim=-1) # class index
labels = [model.config.id2label[p.item()] for p in preds]
print(labels) # ['POSITIVE', 'NEGATIVE']
# ── Extracting text embeddings (for semantic search / RAG)
from transformers import AutoModel
embed_model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
inputs2 = tokenizer(['Hello world', 'Hi earth'], return_tensors='pt',
padding=True, truncation=True)
with torch.no_grad():
hidden = embed_model(**inputs2).last_hidden_state # (2, seq_len, 384)
# Mean-pool over token dimension
mask = inputs2['attention_mask'].unsqueeze(-1).float()
embeds = (hidden * mask).sum(1) / mask.sum(1) # (2, 384)
print('Embedding shape:', embeds.shape)
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
