imgutils.generic.attachment

This module provides functionality for handling attachments in machine learning models, particularly those hosted on Hugging Face’s model hub. It includes tools for loading, managing and making predictions with ONNX models for classification, tagging and regression tasks.

The module provides a caching mechanism for model loading and thread-safe operations for concurrent access to models and their metadata.

An example of attachment models is deepghs/eattach_monochrome_experiments.

Note

If you want to train a custom attachment model for taggers, take a look at our framework deepghs/emb_attachments.

Attachment

class imgutils.generic.attachment.Attachment(repo_id: str, model_name: str, hf_token: str | None = None)[source]

A class to manage machine learning model attachments from Hugging Face.

This class handles model loading, caching, and prediction for various types of problems including classification, tagging, and regression.

Parameters:
  • repo_id (str) – The Hugging Face repository ID

  • model_name (str) – Name of the model

  • hf_token (Optional[str]) – Optional Hugging Face authentication token

__init__(repo_id: str, model_name: str, hf_token: str | None = None)[source]

Initialize the Attachment instance with repository and model information.

property encoder_model: str

Get the encoder model name from metadata.

Returns:

Name of the encoder model

Return type:

str

predict(embedding: ndarray, **kwargs)[source]

Make predictions based on the problem type (classification, tagging, or regression).

Parameters:
  • embedding (np.ndarray) – Input embedding array

  • kwargs – Additional arguments passed to specific prediction methods

Returns:

Prediction results in specified format

Raises:

ValueError – If embedding shape is invalid or problem type is unknown

open_attachment

imgutils.generic.attachment.open_attachment(repo_id: str, model_name: str, hf_token: str | None = None) Attachment[source]

Create and cache an Attachment instance.

This function creates a new Attachment instance or returns a cached one if it was previously created with the same parameters.

Parameters:
  • repo_id (str) – The Hugging Face repository ID

  • model_name (str) – Name of the model

  • hf_token (Optional[str]) – Optional Hugging Face authentication token

Returns:

An Attachment instance

Return type:

Attachment