Feat: Add support for local Hugging Face text classifiers#1612
Open
RobGeada wants to merge 3 commits intoNVIDIA-NeMo:developfrom
Open
Feat: Add support for local Hugging Face text classifiers#1612RobGeada wants to merge 3 commits intoNVIDIA-NeMo:developfrom
RobGeada wants to merge 3 commits intoNVIDIA-NeMo:developfrom
Conversation
Contributor
Greptile OverviewGreptile SummaryAdds integration for local HuggingFace text classification models as guardrails. The implementation allows users to run any HuggingFace text classifier locally to detect harmful content, prompt injections, or other policy violations in user inputs, bot outputs, and tool messages. Key Features
Critical Issues
Minor Issues
ArchitectureThe implementation follows NeMo Guardrails patterns with configuration schemas in
|
| Filename | Overview |
|---|---|
| pyproject.toml | Added dependency for HuggingFace detector, but wrong package specified (sentence-transformers instead of transformers) |
| nemoguardrails/library/huggingface_detector/actions.py | Core implementation for HuggingFace text classifier integration with proper error handling, caching, and device management |
| examples/configs/huggingface_detector/README.md | Usage documentation with examples and configuration guide (minor typo on line 62) |
Sequence Diagram
sequenceDiagram
participant User
participant NeMoGuardrails
participant HFDetectorFlow as HuggingFace Detector Flow
participant HFAction as huggingface_detector_check
participant ModelLoader as _load_model_and_tokenizer
participant HFHub as HuggingFace Hub
participant Model as Classification Model
User->>NeMoGuardrails: Send message
NeMoGuardrails->>HFDetectorFlow: Execute input flow with $hf_model param
HFDetectorFlow->>HFAction: Call HuggingfaceDetectorCheckAction(context_key, model_repo)
HFAction->>HFAction: Extract text from context
HFAction->>HFAction: Find model config by model_repo
HFAction->>ModelLoader: Load model and tokenizer
alt Model not in cache
ModelLoader->>HFHub: Download model and tokenizer
HFHub-->>ModelLoader: Return model files
ModelLoader->>ModelLoader: Load AutoModelForSequenceClassification
ModelLoader->>ModelLoader: Move to device (if specified)
ModelLoader->>ModelLoader: Cache model
else Model in cache
ModelLoader->>ModelLoader: Return cached model
end
ModelLoader-->>HFAction: Return (model, tokenizer)
HFAction->>HFAction: Convert blocked_classes to indices
HFAction->>Model: Tokenize and classify text
Model-->>HFAction: Return logits and predictions
HFAction->>HFAction: Calculate probabilities (softmax)
HFAction->>HFAction: Check if predicted class in blocked_classes
HFAction-->>HFDetectorFlow: Return {allowed, detected_class, score, all_scores}
alt Content is blocked (not allowed)
HFDetectorFlow->>NeMoGuardrails: Send exception or refuse to respond
NeMoGuardrails->>User: Block message
else Content is allowed
HFDetectorFlow-->>NeMoGuardrails: Continue processing
NeMoGuardrails->>User: Process message normally
end
Signed-off-by: Rob Geada <[email protected]>
adcad48 to
ab4b708
Compare
Contributor
Documentation preview |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds support to run local Hugging Face text classifiers as rails. The models are run inside the NeMo-Guardrails process, and as such, this feature is best suited for smaller predictive text models in the sub-100m parameter range.
Features:
Example config:
Checklist