πŸ€—
HF-Mirror Mirror Gateway
🌐 δΈ­ζ–‡
πŸ“– Complete Developer Guide

Hugging Face Mirror Acceleration Quick Start Guide

End-to-end acceleration manual covering basic environment variables, official CLI resume downloads, Python SDK integration, and enterprise Docker setups.

πŸ“‘ Table of Contents

1. Acceleration Principles & Mechanism

Downloading large AI models (such as DeepSeek-R1, Qwen2.5, LLaMA-3.3) directly from huggingface.co frequently suffers from DNS pollution, high packet loss, and frequent connection resets.

HF-Mirror (hf-mirror.net) deploys high-bandwidth Anycast edge PoPs. Unlike traditional mirror sources that issue 302 Found redirects bouncing downloads back to overseas object stores, this platform delivers large binary chunks directly with HTTP 200 OK / 206 Partial Content streaming, ensuring stable, uninterrupted transfers.

2. Environment Configuration (Linux / macOS / Windows)

Direct your system's HF_ENDPOINT environment variable to HF-Mirror:

Linux / macOS (Bash or Zsh) Terminal

Temporary configuration (active shell only):

export HF_ENDPOINT="https://hf-mirror.net"

Permanent configuration (append to shell profile):

echo 'export HF_ENDPOINT="https://hf-mirror.net"' >> ~/.bashrc && source ~/.bashrc
Windows (PowerShell) PowerShell

Temporary configuration:

$env:HF_ENDPOINT = "https://hf-mirror.net"

Permanent configuration (current user environment):

[System.Environment]::SetEnvironmentVariable('HF_ENDPOINT', 'https://hf-mirror.net', [System.EnvironmentVariableTarget]::User)

3. huggingface-cli CLI Download (Recommended)

The official huggingface-cli utility provides automatic resume capability, chunk integrity verification, and precise file filtering.

1. Install official hub package:
pip install -U huggingface_hub
2. Download model (always specify --resume-download):
# Example: Download Qwen2.5-7B-Instruct to a local directory
huggingface-cli download Qwen/Qwen2.5-7B-Instruct --resume-download --local-dir Qwen2.5-7B
3. Advanced filtering (save bandwidth and disk):
# Download only specific GGUF quantizations
huggingface-cli download bartowski/DeepSeek-R1-Distill-Qwen-7B-GGUF --include "*Q4_K_M.gguf" --local-dir ./

# Download only Safetensors weights, skipping .bin and .onnx
huggingface-cli download meta-llama/Llama-3.3-70B-Instruct --include "*.safetensors" "*.json" --local-dir ./

4. Gated Repository Authentication & Downloads

Gated models (e.g. meta-llama/Llama-3.3, google/gemma-2) require user authorization:

  1. Sign in to Hugging Face and request license access on the model page;
  2. Generate a Read Token in your Account Settings: https://huggingface.co/settings/tokens;
  3. Run huggingface-cli login and paste your Token, or pass --token hf_*** to the download command.
πŸ”’ Security Guarantee: HF-Mirror uses end-to-end TLS passthrough. Tokens are transmitted directly to official Hugging Face servers for verification and are never stored on this mirror.

5. Python Script & Library Integration

Inject the environment variable at the very top of your Python file (before importing transformers or diffusers):

import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.net"

# Subsequent imports will automatically route requests through HF-Mirror
from transformers import AutoTokenizer, AutoModelForCausalLM

model_id = "Qwen/Qwen2.5-7B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")

6. Git LFS Large File Downloads

If you prefer cloning complete code and weight repositories using Git:

# 1. Ensure git-lfs is installed and initialized
git lfs install

# 2. Replace official domain with hf-mirror.net
git clone https://hf-mirror.net/gpt2

* Note: For models larger than several gigabytes, huggingface-cli is strongly recommended over Git clone to avoid downloading bloated Git history commits.

7. Docker / Cloud Server Persistence Best Practices

When running GPU container instances, mount host cache volumes to persist models across restarts:

# Docker command: Inject mirror endpoint and mount host cache volume
docker run -it --gpus all \
  -e HF_ENDPOINT="https://hf-mirror.net" \
  -v /data/hf_cache:/root/.cache/huggingface \
  pytorch/pytorch:latest

8. Ollama Fast GGUF Import & Modelfile Tuning (Recommended)

Pulling large models directly using ollama run frequently suffers from throttled throughput or TLS handshake timeout errors because the default registry routes across transoceanic CDNs.

Production Engineering Best Practice: Use huggingface-cli via HF-Mirror to pull target quantized GGUF weights at full line-speed, then import them into local Ollama via a custom Modelfile. This completes model registration in 2 seconds and avoids the silent 2048 context truncation trap:

Step 1: Download target GGUF weights via HF-Mirror
# Inject mirror endpoint and pull Qwen2.5 or DeepSeek-R1 GGUF
export HF_ENDPOINT="https://hf-mirror.net"
huggingface-cli download Qwen/Qwen2.5-7B-Instruct-GGUF \
  --include "*q4_k_m.gguf" \
  --local-dir ./qwen-7b-gguf
Step 2: Create custom Modelfile with explicit context window
# Write Modelfile (Critical: explicitly define num_ctx; avoid default 2048 cut-off!)
cat <<'EOF' > Modelfile
FROM ./qwen-7b-gguf/qwen2.5-7b-instruct-q4_k_m.gguf

PARAMETER num_ctx 16384
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER stop "<|im_end|>"
EOF
Step 3: Build and launch local inference in seconds
# Register model into Ollama local manifest (near-instantaneous)
ollama create my-qwen2.5 -f Modelfile

# Start interactive session
ollama run my-qwen2.5
⚠️ Engineering Pitfall Memo:
  • Linux Systemd Trap: If Ollama runs as a background systemd service, adding export OLLAMA_HOST="0.0.0.0" to ~/.bashrc has NO EFFECT. You must run sudo systemctl edit ollama.service and set Environment="OLLAMA_HOST=0.0.0.0" under [Service] to allow Open WebUI or LAN access.
  • Deep-Dive Guide: Want to master DeepSeek-R1 thinking tokens, VRAM formulas across RTX 3060/4060Ti/4090 and Apple Silicon? Read our exhaustive engineering report: Ollama + HF-Mirror GGUF Local Deployment and Production Pitfalls Guide →

9. Troubleshooting Quick Check

Q: Ollama pull fails with TLS handshake timeout or slow speed?

Official registries suffer high transoceanic latency. Follow Section 8 above to download GGUF files directly via HF-Mirror and register with ollama create in seconds with robust resume support.

Q: Download drops at 90% with Connection Reset?

Always include --resume-download. HF-Mirror fully supports HTTP Range sharding and will resume from the exact byte offset.

Q: Error 401 Client Error: Unauthorized?

The model is gated (e.g. LLaMA 3.3). Accept the license on the model page and run huggingface-cli login with your Access Token.

Q: Cloned weights are small 1KB pointer files?

Git LFS was not initialized. Run git lfs install and execute git lfs pull inside the repository directory.

Need more technical assistance?
Explore our FAQ guide or reach out to our engineering support team.