ml_model_id - Infernet ML
Description: Infernet ML Documentation

Skip to content

# Module: `ml_model_id`

## `MlModelId`

Bases: `BaseModel`

Attributes:

Name | Type | Description
--- | --- | ---
`ml_type` | `Optional[MLType]` | MLType - The type of machine learning model
`repo_id` | `RitualRepoId` | RitualRepoId - The repository id of the model
`files` | `List[str]` | List[str] - The list of files that make up the model

Properties
Source code in `src/infernet_ml/utils/specs/ml_model_id.py`

     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    class MlModelId(BaseModel):
        """
        ModelId: Base class for all models within Ritual's services.

        Attributes:
            ml_type: MLType - The type of machine learning model
            repo_id: RitualRepoId - The repository id of the model
            files: List[str] - The list of files that make up the model

        Properties:
            repo_id: str - The repository id of the model
            unique_id: str - The unique identifier of the model
            version: str - The version of the model

        """

        repo_id: RitualRepoId
        files: List[str] = []
        ml_type: Optional[MLType] = None

        @property
        def version(self) -> str | None:
            """
            Get the version of the model, as denoted by the version of the model's
            repository.

            Returns:
                str - The version of the model
            """
            return self.repo_id.version

        @classmethod
        def from_any(
            cls, model: MlModelId | str, ml_type: Optional[MLType] = None
        ) -> MlModelId:
            """
            Utility function to convert a string or MlModelId to an MlModelId.

            Args:
                model: MlModelId | str - The model id or unique identifier
                ml_type: MLType - The type of machine learning model

            Returns:

            """
            if isinstance(model, str):
                return cls.from_unique_id(model, ml_type)
            elif isinstance(model, MlModelId):
                if ml_type:
                    model.ml_type = ml_type
                return model
            else:
                raise ValueError(f"Cannot convert data type: {type(model)} to MlModelId")

        @property
        def hf_repo_id(self) -> str:
            """
            Get the huggingface repository id of the model, if it is a huggingface model.

            Returns:
                str - The huggingface repository id of the model
            """
            return self.repo_id.hf_id

        @property
        def arweave_repo_id(self) -> str:
            """
            Get the arweave repository id of the model, as implemented in the
            RitualRepoId class.

            Returns:
                str - The arweave repository id of the model
            """
            return self.repo_id.ar_id

        @property
        def to_web3(self) -> bytes:
            """
            Get the web3 encoding of the model id.

            Returns:
                bytes - The web3 encoding of the model id
            """
            return encode(
                ["bytes", "string"],
                [
                    self.repo_id.to_web3(),
                    ",".join(self.files),
                ],
            )

        def to_local_dir(self, cache: Path = DEFAULT_CACHE_DIR) -> Path:
            return self.repo_id.to_local_dir(cache)

        @classmethod
        def from_web3(cls, encoding: bytes) -> MlModelId:
            """
            Create a ModelId from a web3 encoding.

            Args:
                encoding: bytes - The web3 encoding of the model id

            Returns:
                MlModelId - The model id
            """
            repo_b, files = decode(["bytes", "string"], encoding)
            repo = RitualRepoId.from_web3(repo_b)
            return cls(
                repo_id=repo,
                files=files.split(","),
            )

        @property
        def unique_id(self) -> str:
            """
            Get the unique identifier of the model. This has the format:
            <repo_id>:<files> where the repo_id is the unique identifier of the repository
            as implemented in the RitualRepoId class and the files are a comma-separated
            list of the files that make up the model.

            Returns:
                str - The unique identifier of the model
            """
            base = self.repo_id.to_unique_id()
            files_str = ",".join(self.files) if self.files else ""
            base = f"{base}:{files_str}" if files_str else base
            return base

        @classmethod
        def from_unique_id(
            cls, unique_id: str, ml_type: Optional[MLType] = None
        ) -> MlModelId:
            """
            Create a ModelId from a unique identifier.

            Args:
                unique_id: str - The unique identifier of the model
                ml_type: MLType - The type of machine learning model

            Returns:
                MlModelId - The model id
            """
            parts = unique_id.split(":")
            base = parts[0]
            repo_id = RitualRepoId.from_unique_id(base)
            files = parts[1].split(",") if len(parts) > 1 else []
            return MlModelId(
                ml_type=ml_type,
                repo_id=repo_id,
                files=files,
            )

        @classmethod
        def __hash__(self) -> int:
            return hash(self.unique_id)

        def __eq__(self, other: object) -> bool:
            if not isinstance(other, MlModelId):
                return False
            return self.unique_id == other.unique_id

### `arweave_repo_id: str` `property`

Get the arweave repository id of the model, as implemented in the RitualRepoId class.

Returns:

Type | Description
--- | ---
`str` | str - The arweave repository id of the model

### `hf_repo_id: str` `property`

Get the huggingface repository id of the model, if it is a huggingface model.

Returns:

Type | Description
--- | ---
`str` | str - The huggingface repository id of the model

### `to_web3: bytes` `property`

Get the web3 encoding of the model id.

Returns:

Type | Description
--- | ---
`bytes` | bytes - The web3 encoding of the model id

### `unique_id: str` `property`

Get the unique identifier of the model. This has the format: : where the repo_id is the unique identifier of the repository as implemented in the RitualRepoId class and the files are a comma-separated list of the files that make up the model.

Returns:

Type | Description
--- | ---
`str` | str - The unique identifier of the model

### `version: str | None` `property`

Get the version of the model, as denoted by the version of the model's repository.

Returns:

Type | Description
--- | ---
`str | None` | str - The version of the model

### `from_any(model, ml_type=None)` `classmethod`

Utility function to convert a string or MlModelId to an MlModelId.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`model` | `MlModelId | str` | MlModelId | str - The model id or unique identifier | required
`ml_type` | `Optional[MLType]` | MLType - The type of machine learning model | `None`

Source code in `src/infernet_ml/utils/specs/ml_model_id.py`

    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    @classmethod
    def from_any(
        cls, model: MlModelId | str, ml_type: Optional[MLType] = None
    ) -> MlModelId:
        """
        Utility function to convert a string or MlModelId to an MlModelId.

        Args:
            model: MlModelId | str - The model id or unique identifier
            ml_type: MLType - The type of machine learning model

        Returns:

        """
        if isinstance(model, str):
            return cls.from_unique_id(model, ml_type)
        elif isinstance(model, MlModelId):
            if ml_type:
                model.ml_type = ml_type
            return model
        else:
            raise ValueError(f"Cannot convert data type: {type(model)} to MlModelId")

### `from_unique_id(unique_id, ml_type=None)` `classmethod`

Create a ModelId from a unique identifier.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`unique_id` | `str` | str - The unique identifier of the model | required
`ml_type` | `Optional[MLType]` | MLType - The type of machine learning model | `None`

Returns:

Type | Description
--- | ---
`MlModelId` | MlModelId - The model id

Source code in `src/infernet_ml/utils/specs/ml_model_id.py`

    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    @classmethod
    def from_unique_id(
        cls, unique_id: str, ml_type: Optional[MLType] = None
    ) -> MlModelId:
        """
        Create a ModelId from a unique identifier.

        Args:
            unique_id: str - The unique identifier of the model
            ml_type: MLType - The type of machine learning model

        Returns:
            MlModelId - The model id
        """
        parts = unique_id.split(":")
        base = parts[0]
        repo_id = RitualRepoId.from_unique_id(base)
        files = parts[1].split(",") if len(parts) > 1 else []
        return MlModelId(
            ml_type=ml_type,
            repo_id=repo_id,
            files=files,
        )

### `from_web3(encoding)` `classmethod`

Create a ModelId from a web3 encoding.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`encoding` | `bytes` | bytes - The web3 encoding of the model id | required

Returns:

Type | Description
--- | ---
`MlModelId` | MlModelId - The model id

Source code in `src/infernet_ml/utils/specs/ml_model_id.py`

    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    @classmethod
    def from_web3(cls, encoding: bytes) -> MlModelId:
        """
        Create a ModelId from a web3 encoding.

        Args:
            encoding: bytes - The web3 encoding of the model id

        Returns:
            MlModelId - The model id
        """
        repo_b, files = decode(["bytes", "string"], encoding)
        repo = RitualRepoId.from_web3(repo_b)
        return cls(
            repo_id=repo,
            files=files.split(","),
        )