Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Environment:666ghj BettaFish HuggingFace Transformers

From Leeroopedia
Knowledge Sources
Domains NLP, Machine_Learning, Deep_Learning
Last Updated 2026-10-08 21:30 GMT

Overview

HuggingFace Transformers library for loading pre-trained NLP models (BERT, Qwen3) used in sentiment analysis and embedding generation.

Description

This environment provides the HuggingFace Transformers library, which is used to load pre-trained language models for sentiment classification. BettaFish uses it with the WeiboMultilingualSentimentAnalyzer (DistilBERT-based multilingual model), Qwen3 LoRA fine-tuning (Qwen3-0.6B/4B/8B causal LM), and BERT training (bert-base-chinese). The library handles tokenization, model loading from local cache or HuggingFace Hub, and generation.

Usage

Use this environment for sentiment model training, inference, and NLP model loading. It is required by WeiboMultilingualSentimentAnalyzer and Qwen3LoRAUniversal trainer.

System Requirements

Category Requirement Notes
OS Linux, macOS, Windows Model downloads work on all platforms
Hardware GPU recommended for training Inference works on CPU
Disk 1-10GB+ Per model: Qwen3-0.6B ~1.2GB, 4B ~8GB, 8B ~16GB
Network Outbound HTTPS Required for downloading models from HuggingFace Hub

Dependencies

System Packages

  • None beyond Python runtime

Python Packages

  • `transformers` >= 4.30.0 (main requirements.txt)
  • `transformers` >= 4.51.0 (SmallQwen requirements.txt)
  • `transformers` >= 4.6.1 (MachineLearning requirements.txt)
  • `sentence-transformers` >= 2.2.2 (main), >= 2.7.0 (SmallQwen)
  • `tokenizers` (bundled with transformers)

Credentials

  • `HF_TOKEN`: Optional HuggingFace API token for accessing gated models

Quick Install

# Main requirements
pip install transformers>=4.30.0 sentence-transformers>=2.2.2

# SmallQwen sentiment model requirements
pip install transformers>=4.51.0 sentence-transformers>=2.7.0

Code Evidence

Import guard with graceful degradation from `InsightEngine/tools/sentiment_analyzer.py:12-28`:

try:
    import torch
    TORCH_AVAILABLE = True
    torch.classes.__path__ = []
except ImportError:
    torch = None
    TORCH_AVAILABLE = False

try:
    from transformers import AutoTokenizer, AutoModelForSequenceClassification
    TRANSFORMERS_AVAILABLE = True
except ImportError:
    AutoTokenizer = None
    AutoModelForSequenceClassification = None
    TRANSFORMERS_AVAILABLE = False

Qwen3 model loading with fallback chain from `SentimentAnalysisModel/WeiboSentiment_SmallQwen/qwen3_lora_universal.py:50-130`:

# Step 1: Check local model directory
local_model_dir = f"./models/qwen3-{self.model_size.lower()}"
if os.path.exists(local_model_dir) and os.path.exists(os.path.join(local_model_dir, "config.json")):
    self.tokenizer = AutoTokenizer.from_pretrained(local_model_dir)
    self.base_model = AutoModelForCausalLM.from_pretrained(local_model_dir, ...)

# Step 2: Check HuggingFace cache
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name_hf)
self.base_model = AutoModelForCausalLM.from_pretrained(self.model_name_hf, ...)

# Step 3: Force download from HuggingFace
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name_hf, force_download=True)

Common Errors

Error Message Cause Solution
`OSError: We couldn't connect to HuggingFace Hub` No network access or blocked Download models manually and place in `./models/` directory
`RuntimeError: 无法加载{model_size}模型` All three loading fallbacks failed Check network, disk space, and model name
`ImportError: transformers not found` Package not installed `pip install transformers>=4.30.0`

Compatibility Notes

  • Local model loading: Qwen3 models are first loaded from `./models/qwen3-{size}/`, then HuggingFace cache, then forced download.
  • Version requirements: SmallQwen requires `transformers >= 4.51.0`; main system requires `>= 4.30.0`.
  • Graceful degradation: If `torch` or `transformers` is not installed, the sentiment analyzer disables itself rather than crashing.
  • Flash attention warning: The multilingual sentiment model may emit `UserWarning: Torch was not compiled with flash attention` — this is harmless.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment