Module: artifact_manager
Ritual Artifact Manager
This module contains a base class that provides nice abstractions for managing artifacts across the Ritual ecosystem. Depending on the type of computation, various artifacts might be needed. In the context of AI/ML this could mean the machine learning models. In the context of ZKPs this could mean the circuit files. This class provides a way to easily manage these artifacts.
Usage
To define a new artifact type all you need to do is create a new Pydantic model. The Artifact Manager will handle the uploading/downloading of the artifact to/from different storage layers.
Pydantic Model's Structure
Generally, artifacts will be composed of: 1. A collection of files, and 2. Metadata about the artifact.
The Pydantic model should have the following structure:
* All the fields of type Path
or List[Path]
will be treated as artifact
files and are uploaded to the storage layer. In addition, the sha256
hash
of the file is also calculated & stored in the manifest file.
* All other fields are treated as metadata and are stored in the manifest
file.
Here's an example of what a ZK artifact & its integration with the
RitualArtifactManager
might look like:
class MyZkMlArtifact(BaseModel):
circuit_file: Path
model_file: Path
version: str
num_params: str
my_artifact = RitualArtifactManager(
artifact=MyZkMlArtifact(
circuit_file=Path("path/to/circuit_file"),
model_file=Path("path/to/model_file"),
version="v1.0",
num_params="1000"
)
)
# Upload artifact to HuggingFace
my_artifact.to_huggingface_hub(
repo_name="my-hf-username/my-repo-name",
token=os.getenv("HF_TOKEN")
)
# Upload artifact to Arweave
my_artifact.to_arweave(
repo_name="my-repo-name",
wallet_path="path/to/wallet.json"
)
# Downloading from Huggingface Hub
artifact = RitualArtifactManager.from_huggingface_hub(
artifact_class=MyZkMlArtifact,
repo_id="my-hf-username/my-repo-name",
token=os.getenv("HF_TOKEN")
)
# Downloading from Arweave
artifact = RitualArtifactManager.from_arweave(
artifact_class=MyZkMlArtifact,
repo_id="my-arweave-address/arweave-id"
)
Manifest File
The code blocks above will upload the files to arweave/huggingface. A manifest
file called ritual_manifest.json
will be created in the repository, which
contains the metadata about the artifact. This manifest file is used to
reconstruct the artifact when it is downloaded.
BroadcastedArtifact
Bases: BaseModel
Ritual's services will broadcast their artifacts to the network. This class represents a broadcasted artifact.
Properties
type (str): The type of the artifact manifest (Dict[str, Any]): Manifest data repo_id (str): ID of the repo
Source code in src/infernet_ml/resource/artifact_manager.py
CachedArtifact
Bases: BaseModel
A cached artifact is an artifact that has been downloaded from the storage layer and saved to the local cache.
Properties
type (str): The type of the artifact path (Path): Path to the manifest file files (List[Path]): List of paths to the files manifest (Dict[str, Any]): Manifest data repo_id (RitualRepoId): ID of the repo
Source code in src/infernet_ml/resource/artifact_manager.py
to_broadcasted_artifact()
Convert the cached artifact to a broadcasted artifact.
Returns:
Name | Type | Description |
---|---|---|
BroadcastedArtifact |
BroadcastedArtifact
|
Broadcasted artifact |
Source code in src/infernet_ml/resource/artifact_manager.py
RitualArtifactManager
Bases: BaseModel
, Generic[ArtifactType]
A RitualArtifact is a resource that is stored in a RitualRepo. It can be converted to and from a HuggingFace dataset, and can be stored on Arweave.
Source code in src/infernet_ml/resource/artifact_manager.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 |
|
from_arweave(artifact_class, repo_id, directory=None, repomanager_kwargs=None)
classmethod
Download the files from the Arweave network, and return an instance of RitualArtifactManager[ArtifactType].
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_class |
Type[ArtifactType]
|
Class to instantiate |
required |
repo_id |
str
|
ID of the repo |
required |
directory |
str | Path
|
Directory to save the files |
None
|
repomanager_kwargs |
Any
|
Keyword arguments for ritual-arweave's RepoManager class. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
RitualArtifactManager[ArtifactType]
|
RitualArtifactManager[ArtifactType]: Instance of the class |
Source code in src/infernet_ml/resource/artifact_manager.py
from_dir(artifact_class, directory, manifest_data=None)
classmethod
Create an instance of the artifact class from a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_class |
Type[ArtifactType]
|
Class to instantiate |
required |
directory |
Path
|
Directory containing the files |
required |
manifest_data |
Dict[str, Any]
|
Data to add to the manifest. |
None
|
Returns:
Type | Description |
---|---|
RitualArtifactManager[ArtifactType]
|
RitualArtifactManager[ArtifactType]: Instance of the class |
Source code in src/infernet_ml/resource/artifact_manager.py
from_huggingface_hub(artifact_class, repo_id, directory=None, token=None, repomanager_kwargs=None)
classmethod
Download the files from the Huggingface Hub, and return an instance of
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_class |
Type[ArtifactType]
|
Class to instantiate |
required |
repo_id |
str
|
ID of the repo |
required |
directory |
str | Path
|
Directory to save the files |
None
|
token |
str
|
Huggingface Hub token. Defaults to None. |
None
|
repomanager_kwargs |
Any
|
Keyword arguments for Huggingface's snapshot_download method. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
RitualArtifactManager[ArtifactType]
|
RitualArtifactManager[ArtifactType]: Instance of the class |
Source code in src/infernet_ml/resource/artifact_manager.py
from_repo(artifact_class, repo_id, directory=None, hf_token=None, repomanager_kwargs=None)
classmethod
Download the files from the repo, and return an instance of RitualArtifactManager[ArtifactType]. Depending on the repo_id, this may be either arweave or huggingface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_class |
Type[ArtifactType]
|
Class to instantiate |
required |
repo_id |
str
|
ID of the repo |
required |
directory |
str | Path
|
Directory to save the files |
None
|
repomanager_kwargs |
Any
|
Keyword arguments for RepoManager. Defaults to None. |
None
|
hf_token |
str
|
Huggingface Hub token. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
RitualArtifactManager[ArtifactType]
|
RitualArtifactManager[ArtifactType]: Instance of the class |
Source code in src/infernet_ml/resource/artifact_manager.py
get_broadcasted_artifacts_typed(artifact_class, cache_dir=DEFAULT_ARTIFACT_DIRECTORY)
classmethod
Get all the cached artifacts of a certain type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_class |
Type[ArtifactType]
|
Type of the artifact |
required |
cache_dir |
Path
|
Path to the cache directory |
DEFAULT_ARTIFACT_DIRECTORY
|
Returns:
Type | Description |
---|---|
List[BroadcastedArtifact]
|
List[BroadcastedArtifact]: List of broadcasted artifacts |
Source code in src/infernet_ml/resource/artifact_manager.py
get_cached_artifacts(cache_dir=DEFAULT_ARTIFACT_DIRECTORY)
classmethod
Get all the cached artifacts Args: cache_dir: Path to the cache directory
Returns:
Type | Description |
---|---|
List[CachedArtifact]
|
List[CachedArtifact]: List of cached artifacts |
Source code in src/infernet_ml/resource/artifact_manager.py
get_instance_from_manifest(artifact_class, manifest_file)
classmethod
Create an instance of the artifact class from the manifest file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
artifact_class |
Type[ArtifactType]
|
Class to instantiate |
required |
manifest_file |
Path
|
Path to the manifest file |
required |
Returns:
Name | Type | Description |
---|---|---|
ArtifactType |
ArtifactType
|
Instance of the artifact class |
Source code in src/infernet_ml/resource/artifact_manager.py
has_artifact(repo_id)
classmethod
Check if the artifact is in the repo.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
RitualRepoId
|
ID of the repo |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the artifact is in the repo |
Source code in src/infernet_ml/resource/artifact_manager.py
to_arweave(repo_name, wallet_path=None, repo_manager_kwargs=None, upload_kwargs=None)
Upload the files to the Arweave network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_name |
str
|
Name of the repo |
required |
wallet_path |
str | Path
|
Path to the arweave wallet. Defaults to None. |
None
|
repo_manager_kwargs |
Any
|
Keyword arguments for RepoManager. Defaults to None. |
None
|
upload_kwargs |
Any
|
Keyword arguments for
|
None
|
Returns:
Name | Type | Description |
---|---|---|
UploadArweaveRepoResult |
UploadArweaveRepoResult
|
Result of the upload |
Source code in src/infernet_ml/resource/artifact_manager.py
to_dir(directory, repo_id=None)
Save the artifact files to a directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory |
Path
|
Directory to save the files |
required |
repo_id |
RitualRepoId | str
|
ID of the repo. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in src/infernet_ml/resource/artifact_manager.py
to_huggingface_hub(repo_name, token=None, repo_manager_kwargs=None, upload_kwargs=None)
Upload the files to the Huggingface Hub.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_name |
str
|
Name of the repo, in the format "owner/repo_name" |
required |
token |
str
|
Huggingface Hub token. Defaults to None. |
None
|
repo_manager_kwargs |
Any
|
Keyword arguments for RepoManager. Defaults to None. |
None
|
upload_kwargs |
Any
|
Keyword arguments for upload_repo. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
HfUploadType |
HFUploadType
|
Result of the upload |
Source code in src/infernet_ml/resource/artifact_manager.py
to_repo(repo_id, hf_token=None, wallet_path=None, repo_manager_kwargs=None, upload_kwargs=None)
Upload the files to the repository. Depending on the repo_id, this may be either arweave or huggingface.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
repo_id |
str | RitualRepoId
|
ID of the repo |
required |
hf_token |
str
|
Huggingface Hub token. Defaults to None. |
None
|
wallet_path |
str | Path
|
Path to the arweave wallet, if uploading to Arweave. Defaults to None. |
None
|
repo_manager_kwargs |
Any
|
Keyword arguments for RepoManager. Defaults to None. |
None
|
upload_kwargs |
Any
|
Keyword arguments for upload_repo. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
UploadArweaveRepoResult |
ArtifactUploadType
|
Result of the upload |
Source code in src/infernet_ml/resource/artifact_manager.py
calculate_sha256(file)
Calculate the sha256 hash of a file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Path
|
Path to the file |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
sha256 hash |