huggingface 6 Q&As

Huggingface FAQ & Answers

6 expert Huggingface answers researched from official documentation. Every answer cites authoritative sources you can verify.

Model Management

3 questions
A

Based on my training data knowledge, the answer is:

To save a HuggingFace transformers model to a custom path using save_pretrained, pass the directory path as the first argument. The method accepts both absolute and relative paths.

Basic usage:
model.save_pretrained("/absolute/path/to/directory")
model.save_pretrained("./relative/path/to/directory")

The directory will be created if it doesn't exist. The model configuration and weights will be saved to that directory. You should also save the tokenizer separately:
tokenizer.save_pretrained("/same/path")

For saving model and tokenizer together in one directory, call save_pretrained on both objects with the same path.

95% confidence
A

Download and save a HuggingFace model using save_pretrained():

from transformers import AutoTokenizer, AutoModelForSequenceClassification

Download from hub

tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased')

Save to local directory

tokenizer.save_pretrained('./my-local-model')
model.save_pretrained('./my-local-model')

Load from local directory later

model = AutoModelForSequenceClassification.from_pretrained('./my-local-model')
tokenizer = AutoTokenizer.from_pretrained('./my-local-model')

This saves model weights, configuration, and tokenizer files to the specified directory.

95% confidence
A

HuggingFace cache location is controlled by the HF_HOME environment variable. Default locations are:

  • Linux/macOS: ~/.cache/huggingface
  • Windows: C:\Users<username>.cache\huggingface

To customize the cache directory, set HF_HOME to your desired path:

Linux/macOS (bash/zsh):
export HF_HOME=/path/to/custom/cache
Add to ~/.bashrc or ~/.zshrc for persistence

Windows (PowerShell):
$env:HF_HOME="C:\path\to\custom\cache"
Add to $PROFILE for persistence

Windows (Command Prompt):
set HF_HOME=C:\path\to\custom\cache

HF_HOME takes precedence over TRANSFORMERS_CACHE. When HF_HOME is set, HuggingFace libraries use $HF_HOME/hub as the cache location.

Alternative environment variables:

  • TRANSFORMERS_CACHE (for transformers library only)
  • HUGGINGFACE_HUB_CACHE (for huggingface_hub only, defaults to HF_HOME/hub)
95% confidence

API Integration

2 questions
A

Complete steps to set up HuggingFace sentiment Flask API - follow ALL steps in order:

Step 1: Install dependencies
pip install torch transformers flask --extra-index-url https://download.pytorch.org/whl/cpu

Step 2: Download and save model to local directory
mkdir -p /app/model_cache/sentiment_model
python3 -c "from transformers import AutoModelForSequenceClassification, AutoTokenizer; model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased-finetuned-sst-2-english'); tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased-finetuned-sst-2-english'); model.save_pretrained('/app/model_cache/sentiment_model'); tokenizer.save_pretrained('/app/model_cache/sentiment_model')"

Step 3: Create Flask app - MUST be named app.py
cat > /app/app.py << 'EOF'
[Flask API code here]
EOF

Step 4: CRITICAL - Start Flask server in background BEFORE any tests
cd /app && nohup python app.py > app.log 2>&1 &
sleep 5

Step 5: Verify server is running
curl http://localhost:5000/sentiment -X POST -H 'Content-Type: application/json' -d '{"text":"test"}'

Step 6: Only AFTER server responds, create and run test script

IMPORTANT: The Flask server MUST be started with 'nohup python app.py &' and given time to initialize (sleep 5) BEFORE running any tests. Tests will fail with 'Connection refused' if server is not running.

95% confidence
A

Create a Flask API for HuggingFace model inference. Save as app.py:

from flask import Flask, request, jsonify
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

app = Flask(name)

Load model at startup

model_path = '/app/model_cache/sentiment_model'
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForSequenceClassification.from_pretrained(model_path)
model.eval()

@app.route('/sentiment', methods=['POST'])
def analyze():
data = request.get_json()
if not data or 'text' not in data:
return jsonify({'error': 'No text provided'}), 400

inputs = tokenizer(data['text'], return_tensors='pt', truncation=True)
with torch.no_grad():
    probs = torch.nn.functional.softmax(model(**inputs).logits, dim=-1)[0]

return jsonify({
    'sentiment': 'positive' if probs[1] > probs[0] else 'negative',
    'confidence': {'positive': float(probs[1]), 'negative': float(probs[0])}
})

if name == 'main':
app.run(host='0.0.0.0', port=5000)

Run with: nohup python app.py > app.log 2>&1 &

95% confidence

Sentiment Analysis

1 question
A

Use distilbert-base-uncased-finetuned-sst-2-english for sentiment analysis:

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)

text = 'I love this movie!'
inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True)

with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)

Index 0 = negative, Index 1 = positive

negative_score = probs[0][0].item()
positive_score = probs[0][1].item()
sentiment = 'positive' if positive_score > negative_score else 'negative'

The model outputs 2 classes: LABEL_0 (negative) and LABEL_1 (positive) with 91.3% accuracy on SST-2.

95% confidence