Skip to content

[RHOAIENG-28843] Create Huggingface detector package #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

AmberJBlue
Copy link
Member

@AmberJBlue AmberJBlue commented Jul 31, 2025

Summary by Sourcery

Create and package a shared guardrails-detector-common library and a new guardrails-detector-huggingface module, refactor existing detectors to use the common library, and add necessary packaging, documentation, and dependency updates

New Features:

  • Add guardrails-detector-common package with shared FastAPI base classes, schemas, and utilities
  • Add guardrails-detector-huggingface package for Huggingface model–based detection

Enhancements:

  • Refactor built-in and LLM judge detectors to import from guardrails-detector-common
  • Update dependencies to include guardrails-detector-common in Huggingface detector
  • Add packaging configuration (pyproject.toml) and metadata for common and Huggingface packages

Build:

  • Configure build system using hatchling and pyproject.toml for both common and Huggingface packages

Documentation:

  • Add README for guardrails-detector-common package

Chores:

  • Add LICENSE files and development requirements for detector packages

Copy link

sourcery-ai bot commented Jul 31, 2025

Reviewer's Guide

This PR modularizes detector logic by extracting shared APIs and models into a new common library, introduces a standalone Huggingface-based detector package built on that common module, and refactors existing detectors to consume the shared components, while setting up packaging and documentation for both new packages.

Entity relationship diagram for shared ContentAnalysis* models

erDiagram
    CONTENTANALYSISHTTPREQUEST ||--o{ CONTENTANALYSISRESPONSE : triggers
    CONTENTANALYSISRESPONSE ||--o{ EVIDENCE : contains
    CONTENTANALYSISRESPONSE ||--o{ ERROR : may_have
    CONTENTSANALYSISRESPONSE ||--o{ CONTENTANALYSISRESPONSE : aggregates
Loading

Class diagram for the new guardrails-detector-common package

classDiagram
    class DetectorBaseAPI
    class logger
    class RoleEnum
    class Message
    class TextDetectionHttpRequest
    class TextDetectionResponse
    class GenerationDetectionHttpRequest
    class GenerationDetectionResponse
    class ChatDetectionHttpRequest
    class ContextBasedDetectionHttpRequest
    class AttributionBasedDetectionResponse
    class DetectionHttpRequest
    class DetectionResponseSpan
    class DetectionResponse
    class Evidence
    class EvidenceType
    class EvidenceObj
    class ContentAnalysisHttpRequest
    class ContentAnalysisResponse
    class ContentsAnalysisResponse
    class Error

    DetectorBaseAPI <|-- logger
    DetectorBaseAPI <|-- ContentAnalysisHttpRequest
    DetectorBaseAPI <|-- ContentAnalysisResponse
    ContentAnalysisResponse o-- Evidence
    Evidence o-- EvidenceType
    DetectionResponse o-- DetectionResponseSpan
    DetectionResponse o-- EvidenceObj
    DetectionHttpRequest o-- Message
    TextDetectionHttpRequest <|-- DetectionHttpRequest
    GenerationDetectionHttpRequest <|-- DetectionHttpRequest
    ChatDetectionHttpRequest <|-- DetectionHttpRequest
    ContextBasedDetectionHttpRequest <|-- DetectionHttpRequest
    AttributionBasedDetectionResponse <|-- DetectionResponse
    ContentsAnalysisResponse o-- ContentAnalysisResponse
Loading

Class diagram for the new Huggingface detector package

classDiagram
    class Detector
    class app
    class ContentAnalysisHttpRequest
    class ContentAnalysisResponse
    class ContentsAnalysisResponse
    class Error

    Detector <|-- app
    app o-- ContentAnalysisHttpRequest
    app o-- ContentAnalysisResponse
    ContentsAnalysisResponse o-- ContentAnalysisResponse
Loading

File-Level Changes

Change Details Files
Introduce guardrails-detector-common package with shared base API, models, and utilities
  • Defined DetectorBaseAPI and logger in app.py
  • Created shared Pydantic schemas and response models in scheme.py
  • Exposed components via init.py with versioning
  • Added packaging via pyproject.toml with build targets and dev dependencies
  • Included README and LICENSE for documentation and licensing
detectors/common/pyproject.toml
detectors/common/src/guardrails_detector_common/app.py
detectors/common/src/guardrails_detector_common/__init__.py
detectors/common/src/guardrails_detector_common/scheme.py
detectors/common/README.md
detectors/common/LICENSE
Add guardrails-detector-huggingface package implementing a Huggingface-based detector
  • Scaffolded API app and Detector class under the new package
  • Defined request/response schemas and exposed via init.py
  • Configured dependency on common package and transformers in pyproject.toml
  • Added README and LICENSE for package documentation
  • Initialized requirements-dev for development tasks
detectors/huggingface/pyproject.toml
detectors/huggingface/src/guardrails_detector_huggingface/app.py
detectors/huggingface/src/guardrails_detector_huggingface/detector.py
detectors/huggingface/src/guardrails_detector_huggingface/__init__.py
detectors/huggingface/requirements.txt
detectors/huggingface/README.md
detectors/huggingface/LICENSE
detectors/huggingface/requirements-dev.txt
Refactor existing detectors to use the new common library
  • Replaced local imports from detectors.common with guardrails_detector_common
  • Updated built-in detector modules to consume shared ContentAnalysis types
  • Adjusted requirements to include the common package where needed
detectors/built_in/app.py
detectors/built_in/base_detector_registry.py
detectors/built_in/file_type_detectors.py
detectors/built_in/regex_detectors.py
detectors/built_in/requirements.txt
detectors/llm_judge/requirements.txt

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@AmberJBlue AmberJBlue marked this pull request as ready for review August 1, 2025 14:00
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @AmberJBlue - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@m-misiura
Copy link
Collaborator

thanks for your work @AmberJBlue ; any chance you could please rebase the PR? For more information, see e.g. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@m-misiura m-misiura self-requested a review August 7, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants