mirror of
https://github.com/pyannote/pyannote-audio.git
synced 2026-11-20 13:07:51 +09:00
fix(hub): fix handling of revision with models in Pipeline subfolders (#1907, PYA-940)
This commit is contained in:
1 parent
36e5c8fd8c
commit
f75e659dd1
6 files changed
+185
-112
No files matched your search
@@ -42,6 +42,7 @@ With the optional telemetry feature in `pyannote.audio`, you can choose to send
|
||||
- BREAKING(io): remove support for `sox` and `soundfile` audio I/O backends (only `ffmpeg` or in-memory audio is supported)
|
||||
- BREAKING(setup): drop support to `Python` < 3.10
|
||||
- BREAKING(hub): rename `use_auth_token` to `token`
|
||||
- BREAKING(hub): drop support for `{pipeline_name}@{revision}` syntax in `Model.from_pretrained(...)` and `Pipeline.from_pretrained(...)` -- use new `revision` keyword argument instead
|
||||
- BREAKING(task): remove `OverlappedSpeechDetection` task (part of `SpeakerDiarization` task)
|
||||
- BREAKING(pipeline): remove `OverlappedSpeechDetection` and `Resegmentation` unmaintained pipelines (part of `SpeakerDiarization`)
|
||||
- BREAKING(cache): rely on `huggingface_hub` caching directory (`PYANNOTE_CACHE` is no longer used)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024- CNRS
|
||||
# Copyright (c) 2024-2025 CNRS
|
||||
# Copyright (c) 2025- pyannoteAI
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -23,16 +24,15 @@
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Text, Union
|
||||
from typing import Optional
|
||||
|
||||
import numpy as np
|
||||
import safetensors.numpy
|
||||
import scipy.interpolate
|
||||
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
|
||||
from sklearn.isotonic import IsotonicRegression
|
||||
from sklearn.utils.validation import NotFittedError, check_is_fitted
|
||||
|
||||
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
|
||||
|
||||
|
||||
class Calibration(IsotonicRegression):
|
||||
"""Logit/distance calibration"""
|
||||
@@ -68,7 +68,7 @@ class Calibration(IsotonicRegression):
|
||||
}
|
||||
|
||||
safetensors.numpy.save_file(tensor_dict, path)
|
||||
|
||||
|
||||
@classmethod
|
||||
def from_tensor_dict(cls, tensor_dict: dict) -> "Calibration":
|
||||
"""Load calibration from a dictionary of tensors
|
||||
@@ -129,10 +129,11 @@ class Calibration(IsotonicRegression):
|
||||
@classmethod
|
||||
def from_pretrained(
|
||||
cls,
|
||||
checkpoint: Union[Path, str],
|
||||
subfolder: Optional[str] = None,
|
||||
token: Optional[Text] = None,
|
||||
cache_dir: Optional[Union[str, Path]] = None,
|
||||
checkpoint: Path | str,
|
||||
subfolder: str | None = None,
|
||||
revision: str | None = None,
|
||||
token: str | bool | None = None,
|
||||
cache_dir: Path | str | None = None,
|
||||
**kwargs,
|
||||
) -> Optional["Calibration"]:
|
||||
"""Load calibration from disk or Huggingface Hub
|
||||
@@ -143,6 +144,8 @@ class Calibration(IsotonicRegression):
|
||||
Path to checkpoint or a model identifier from the hf.co model hub.
|
||||
subfolder : str, optional
|
||||
Folder inside the hf.co model repo.
|
||||
revision : str, optional
|
||||
Revision when loading from the huggingface.co model hub.
|
||||
token : str, optional
|
||||
When loading a private hf.co model, set `token`
|
||||
to True or to a string containing your hugginface.co authentication
|
||||
@@ -154,6 +157,9 @@ class Calibration(IsotonicRegression):
|
||||
# if checkpoint is a directory, look for the calibration checkpoint
|
||||
# inside this directory (or inside a subfolder if specified)
|
||||
if os.path.isdir(checkpoint):
|
||||
if revision is not None:
|
||||
raise ValueError("Revisions cannot be used with local checkpoints.")
|
||||
|
||||
if subfolder:
|
||||
path_to_calibration_checkpoint = (
|
||||
Path(checkpoint) / subfolder / AssetFileName.Calibration.value
|
||||
@@ -165,14 +171,24 @@ class Calibration(IsotonicRegression):
|
||||
|
||||
# if checkpoint is a file, use it as is
|
||||
elif os.path.isfile(checkpoint):
|
||||
if revision is not None:
|
||||
raise ValueError("Revisions cannot be used with local checkpoints.")
|
||||
|
||||
path_to_calibration_checkpoint = checkpoint
|
||||
|
||||
# otherwise, assume that the checkpoint is hosted on HF model hub
|
||||
else:
|
||||
_, _, path_to_calibration_checkpoint = download_from_hf_hub(
|
||||
str(checkpoint),
|
||||
checkpoint = str(checkpoint)
|
||||
if "@" in checkpoint:
|
||||
raise ValueError(
|
||||
"Revisions must be passed with `revision` keyword argument."
|
||||
)
|
||||
|
||||
path_to_calibration_checkpoint = download_from_hf_hub(
|
||||
checkpoint,
|
||||
AssetFileName.Calibration,
|
||||
subfolder=subfolder,
|
||||
revision=revision,
|
||||
cache_dir=cache_dir,
|
||||
token=token,
|
||||
)
|
||||
|
||||
@@ -23,14 +23,14 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import io
|
||||
import os
|
||||
import warnings
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property
|
||||
from importlib import import_module
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional, Text, Tuple, Union
|
||||
from typing import Any, Optional
|
||||
|
||||
import lightning
|
||||
import torch
|
||||
@@ -38,9 +38,6 @@ import torch.nn as nn
|
||||
import torch.optim
|
||||
from lightning.fabric.utilities.cloud_io import _load as pl_load
|
||||
from lightning.pytorch.utilities.model_summary.model_summary import ModelSummary
|
||||
from pyannote.core import SlidingWindow
|
||||
from torch.utils.data import DataLoader
|
||||
|
||||
from pyannote.audio import __version__
|
||||
from pyannote.audio.core.io import Audio
|
||||
from pyannote.audio.core.task import (
|
||||
@@ -49,10 +46,12 @@ from pyannote.audio.core.task import (
|
||||
Task,
|
||||
UnknownSpecificationsError,
|
||||
)
|
||||
from pyannote.audio.telemetry import track_model_init
|
||||
from pyannote.audio.utils.dependencies import check_dependencies
|
||||
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
|
||||
from pyannote.audio.utils.multi_task import map_with_specifications
|
||||
from pyannote.audio.utils.dependencies import check_dependencies
|
||||
from pyannote.audio.telemetry import track_model_init
|
||||
from pyannote.core import SlidingWindow
|
||||
from torch.utils.data import DataLoader
|
||||
|
||||
|
||||
# NOTE: needed to backward compatibility to load models trained before pyannote.audio 3.x
|
||||
@@ -84,13 +83,13 @@ class Model(lightning.LightningModule):
|
||||
self,
|
||||
sample_rate: int = 16000,
|
||||
num_channels: int = 1,
|
||||
task: Optional[Task] = None,
|
||||
task: Task | None = None,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
assert (
|
||||
num_channels == 1
|
||||
), "Only mono audio is supported for now (num_channels = 1)"
|
||||
assert num_channels == 1, (
|
||||
"Only mono audio is supported for now (num_channels = 1)"
|
||||
)
|
||||
|
||||
self.save_hyperparameters("sample_rate", "num_channels")
|
||||
|
||||
@@ -113,7 +112,7 @@ class Model(lightning.LightningModule):
|
||||
pass
|
||||
|
||||
@property
|
||||
def specifications(self) -> Union[Specifications, Tuple[Specifications]]:
|
||||
def specifications(self) -> Specifications | tuple[Specifications]:
|
||||
if self.task is None:
|
||||
try:
|
||||
specifications = self._specifications
|
||||
@@ -130,9 +129,7 @@ class Model(lightning.LightningModule):
|
||||
return specifications
|
||||
|
||||
@specifications.setter
|
||||
def specifications(
|
||||
self, specifications: Union[Specifications, Tuple[Specifications]]
|
||||
):
|
||||
def specifications(self, specifications: Specifications | tuple[Specifications]):
|
||||
if not isinstance(specifications, (Specifications, tuple)):
|
||||
raise ValueError(
|
||||
"Only regular specifications or tuple of specifications are supported."
|
||||
@@ -153,7 +150,7 @@ class Model(lightning.LightningModule):
|
||||
if hasattr(self, "_specifications"):
|
||||
del self._specifications
|
||||
|
||||
def __example_input_array(self, duration: Optional[float] = None) -> torch.Tensor:
|
||||
def __example_input_array(self, duration: float | None = None) -> torch.Tensor:
|
||||
duration = duration or next(iter(self.specifications)).duration
|
||||
return torch.randn(
|
||||
(
|
||||
@@ -258,8 +255,7 @@ class Model(lightning.LightningModule):
|
||||
"specifications": self.specifications,
|
||||
}
|
||||
|
||||
def on_load_checkpoint(self, checkpoint: Dict[str, Any]):
|
||||
|
||||
def on_load_checkpoint(self, checkpoint: dict[str, Any]):
|
||||
self.specifications = checkpoint["pyannote.audio"]["specifications"]
|
||||
|
||||
# add task-dependent (e.g. final classifier) layers
|
||||
@@ -267,12 +263,12 @@ class Model(lightning.LightningModule):
|
||||
|
||||
def forward(
|
||||
self, waveforms: torch.Tensor, **kwargs
|
||||
) -> Union[torch.Tensor, Tuple[torch.Tensor]]:
|
||||
) -> torch.Tensor | tuple[torch.Tensor]:
|
||||
msg = "Class {self.__class__.__name__} should define a `forward` method."
|
||||
raise NotImplementedError(msg)
|
||||
|
||||
# convenience function to automate the choice of the final activation function
|
||||
def default_activation(self) -> Union[nn.Module, Tuple[nn.Module]]:
|
||||
def default_activation(self) -> nn.Module | tuple[nn.Module]:
|
||||
"""Guess default activation function according to task specification
|
||||
|
||||
* sigmoid for binary classification
|
||||
@@ -286,7 +282,7 @@ class Model(lightning.LightningModule):
|
||||
"""
|
||||
|
||||
def __default_activation(
|
||||
specifications: Optional[Specifications] = None,
|
||||
specifications: Specifications | None = None,
|
||||
) -> nn.Module:
|
||||
if specifications.problem == Problem.BINARY_CLASSIFICATION:
|
||||
return nn.Sigmoid()
|
||||
@@ -326,7 +322,7 @@ class Model(lightning.LightningModule):
|
||||
def configure_optimizers(self):
|
||||
return torch.optim.Adam(self.parameters(), lr=1e-3)
|
||||
|
||||
def __up_to(self, module_name: Text, requires_grad: bool = False) -> List[Text]:
|
||||
def __up_to(self, module_name: str, requires_grad: bool = False) -> list[str]:
|
||||
"""Helper function for freeze_up_to and unfreeze_up_to"""
|
||||
|
||||
tokens = module_name.split(".")
|
||||
@@ -360,7 +356,7 @@ class Model(lightning.LightningModule):
|
||||
|
||||
return updated_modules
|
||||
|
||||
def freeze_up_to(self, module_name: Text) -> List[Text]:
|
||||
def freeze_up_to(self, module_name: str) -> list[str]:
|
||||
"""Freeze model up to specific module
|
||||
|
||||
Parameters
|
||||
@@ -385,7 +381,7 @@ class Model(lightning.LightningModule):
|
||||
"""
|
||||
return self.__up_to(module_name, requires_grad=False)
|
||||
|
||||
def unfreeze_up_to(self, module_name: Text) -> List[Text]:
|
||||
def unfreeze_up_to(self, module_name: str) -> list[str]:
|
||||
"""Unfreeze model up to specific module
|
||||
|
||||
Parameters
|
||||
@@ -412,10 +408,10 @@ class Model(lightning.LightningModule):
|
||||
|
||||
def __by_name(
|
||||
self,
|
||||
modules: Union[List[Text], Text],
|
||||
modules: list[str] | str,
|
||||
recurse: bool = True,
|
||||
requires_grad: bool = False,
|
||||
) -> List[Text]:
|
||||
) -> list[str]:
|
||||
"""Helper function for freeze_by_name and unfreeze_by_name"""
|
||||
|
||||
updated_modules = list()
|
||||
@@ -442,9 +438,9 @@ class Model(lightning.LightningModule):
|
||||
|
||||
def freeze_by_name(
|
||||
self,
|
||||
modules: Union[Text, List[Text]],
|
||||
modules: str | list[str],
|
||||
recurse: bool = True,
|
||||
) -> List[Text]:
|
||||
) -> list[str]:
|
||||
"""Freeze modules
|
||||
|
||||
Parameters
|
||||
@@ -473,9 +469,9 @@ class Model(lightning.LightningModule):
|
||||
|
||||
def unfreeze_by_name(
|
||||
self,
|
||||
modules: Union[List[Text], Text],
|
||||
modules: list[str] | str,
|
||||
recurse: bool = True,
|
||||
) -> List[Text]:
|
||||
) -> list[str]:
|
||||
"""Unfreeze modules
|
||||
|
||||
Parameters
|
||||
@@ -501,12 +497,13 @@ class Model(lightning.LightningModule):
|
||||
@classmethod
|
||||
def from_pretrained(
|
||||
cls,
|
||||
checkpoint: Union[Path, Text, io.BytesIO],
|
||||
checkpoint: Path | str | io.BytesIO,
|
||||
map_location=None,
|
||||
strict: bool = True,
|
||||
subfolder: Optional[str] = None,
|
||||
token: Union[str, bool, None] = None,
|
||||
cache_dir: Union[Path, str, None] = None,
|
||||
subfolder: str | None = None,
|
||||
revision: str | None = None,
|
||||
token: str | bool | None = None,
|
||||
cache_dir: Path | str | None = None,
|
||||
skip_dependencies: bool = False,
|
||||
**kwargs,
|
||||
) -> Optional["Model"]:
|
||||
@@ -528,6 +525,8 @@ class Model(lightning.LightningModule):
|
||||
the keys returned by this module’s state dict. Defaults to True.
|
||||
subfolder : str, optional
|
||||
Folder inside the hf.co model repo.
|
||||
revision : str, optional
|
||||
Revision when loading from the huggingface.co model hub.
|
||||
token : str or bool, optional
|
||||
Token to be used for the download.
|
||||
cache_dir: Path or str, optional
|
||||
@@ -550,14 +549,19 @@ class Model(lightning.LightningModule):
|
||||
|
||||
"""
|
||||
if isinstance(checkpoint, io.BytesIO) or os.path.isfile(checkpoint):
|
||||
if revision is not None:
|
||||
raise ValueError("Revisions cannot be used with local checkpoints.")
|
||||
|
||||
# if checkpoint is a BytesIO object or a file path, use it as is
|
||||
path_to_model_checkpoint = checkpoint
|
||||
otel_origin: str = "local"
|
||||
|
||||
|
||||
# if checkpoint is a directory, look for the model checkpoint
|
||||
# inside this directory (or inside a subfolder if specified)
|
||||
elif os.path.isdir(checkpoint):
|
||||
if revision is not None:
|
||||
raise ValueError("Revisions cannot be used with local checkpoints.")
|
||||
|
||||
if subfolder:
|
||||
path_to_model_checkpoint = (
|
||||
Path(checkpoint) / subfolder / AssetFileName.Model.value
|
||||
@@ -569,23 +573,28 @@ class Model(lightning.LightningModule):
|
||||
# otherwise, assume that the checkpoint is hosted on HF model hub
|
||||
else:
|
||||
checkpoint = str(checkpoint)
|
||||
_, _, path_to_model_checkpoint = download_from_hf_hub(
|
||||
if "@" in checkpoint:
|
||||
raise ValueError(
|
||||
"Revisions must be passed with `revision` keyword argument."
|
||||
)
|
||||
|
||||
path_to_model_checkpoint = download_from_hf_hub(
|
||||
checkpoint,
|
||||
AssetFileName.Model,
|
||||
subfolder=subfolder,
|
||||
revision=revision,
|
||||
cache_dir=cache_dir,
|
||||
token=token,
|
||||
)
|
||||
if path_to_model_checkpoint is None:
|
||||
return None
|
||||
|
||||
|
||||
otel_origin: str = (
|
||||
checkpoint
|
||||
if checkpoint.lower().startswith(("pyannote/", "pyannoteai/"))
|
||||
else "huggingface"
|
||||
)
|
||||
|
||||
|
||||
if map_location is None:
|
||||
|
||||
def default_map_location(storage, loc):
|
||||
@@ -644,5 +653,4 @@ class Model(lightning.LightningModule):
|
||||
model._otel_origin = otel_origin
|
||||
track_model_init(model)
|
||||
|
||||
|
||||
return model
|
||||
@@ -27,30 +27,30 @@ from collections import OrderedDict
|
||||
from collections.abc import Iterator
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from typing import Callable, Dict, List, Optional, Text, Union
|
||||
from typing import Callable, Dict, List, Optional
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import yaml
|
||||
from pyannote.core.utils.helper import get_class_by_name
|
||||
from pyannote.database import FileFinder, ProtocolFile
|
||||
from pyannote.pipeline import Pipeline as _Pipeline
|
||||
|
||||
from pyannote.audio import Audio
|
||||
from pyannote.audio.core.inference import BaseInference
|
||||
from pyannote.audio.core.io import AudioFile
|
||||
from pyannote.audio.core.model import Model
|
||||
from pyannote.audio.telemetry import track_pipeline_apply, track_pipeline_init
|
||||
from pyannote.audio.utils.dependencies import check_dependencies
|
||||
from pyannote.audio.utils.hf_hub import AssetFileName, download_from_hf_hub
|
||||
from pyannote.audio.utils.reproducibility import fix_reproducibility
|
||||
from pyannote.audio.utils.dependencies import check_dependencies
|
||||
from pyannote.audio.telemetry import track_pipeline_init, track_pipeline_apply
|
||||
from pyannote.core.utils.helper import get_class_by_name
|
||||
from pyannote.database import FileFinder, ProtocolFile
|
||||
from pyannote.pipeline import Pipeline as _Pipeline
|
||||
|
||||
|
||||
def expand_subfolders(
|
||||
config,
|
||||
model_id: Optional[Text] = None,
|
||||
revision: Optional[Text] = None,
|
||||
cache_dir: Union[Path, str, None] = None,
|
||||
token: Optional[Text] = None,
|
||||
model_id: str | None = None,
|
||||
parent_revision: str | None = None,
|
||||
cache_dir: Path | str | None = None,
|
||||
token: str | None = None,
|
||||
skip_dependencies: bool = False,
|
||||
) -> None:
|
||||
"""Expand $model subfolders in config
|
||||
@@ -65,7 +65,7 @@ def expand_subfolders(
|
||||
config : dict
|
||||
model_id : str, optional
|
||||
Model identifier when loading from the huggingface.co model hub.
|
||||
revision : str, optional
|
||||
parent_revision : str, optional
|
||||
Revision when loading from the huggingface.co model hub.
|
||||
token : str or bool, optional
|
||||
Huggingface token to be used for downloading from Huggingface hub.
|
||||
@@ -79,7 +79,16 @@ def expand_subfolders(
|
||||
if isinstance(config, dict):
|
||||
for key, value in config.items():
|
||||
if isinstance(value, str) and value.startswith("$model/"):
|
||||
|
||||
subfolder = "/".join(value.split("/")[1:])
|
||||
|
||||
# if subfolder contains '@', split it to get revision
|
||||
if "@" in subfolder:
|
||||
subfolder, revision = subfolder.split("@")
|
||||
# otherwise, use parent revision if any
|
||||
else:
|
||||
revision = parent_revision
|
||||
|
||||
config[key] = {
|
||||
"checkpoint": model_id,
|
||||
"revision": revision,
|
||||
@@ -92,7 +101,7 @@ def expand_subfolders(
|
||||
expand_subfolders(
|
||||
value,
|
||||
model_id,
|
||||
revision=revision,
|
||||
parent_revision=parent_revision,
|
||||
token=token,
|
||||
cache_dir=cache_dir,
|
||||
skip_dependencies=skip_dependencies,
|
||||
@@ -101,7 +110,16 @@ def expand_subfolders(
|
||||
elif isinstance(config, list):
|
||||
for idx, value in enumerate(config):
|
||||
if isinstance(value, str) and value.startswith("$model/"):
|
||||
|
||||
subfolder = "/".join(value.split("/")[1:])
|
||||
|
||||
# if subfolder contains '@', split it to get revision
|
||||
if "@" in subfolder:
|
||||
subfolder, revision = subfolder.split("@")
|
||||
# otherwise, use parent revision if any
|
||||
else:
|
||||
revision = parent_revision
|
||||
|
||||
config[idx] = {
|
||||
"checkpoint": model_id,
|
||||
"revision": revision,
|
||||
@@ -110,11 +128,12 @@ def expand_subfolders(
|
||||
"cache_dir": cache_dir,
|
||||
"skip_dependencies": skip_dependencies,
|
||||
}
|
||||
|
||||
else:
|
||||
expand_subfolders(
|
||||
value,
|
||||
model_id,
|
||||
revision=revision,
|
||||
parent_revision=parent_revision,
|
||||
token=token,
|
||||
cache_dir=cache_dir,
|
||||
skip_dependencies=skip_dependencies,
|
||||
@@ -125,10 +144,11 @@ class Pipeline(_Pipeline):
|
||||
@classmethod
|
||||
def from_pretrained(
|
||||
cls,
|
||||
checkpoint: Union[Text, Path],
|
||||
hparams_file: Union[Text, Path] = None,
|
||||
token: Union[Text, None] = None,
|
||||
cache_dir: Union[Path, Text, None] = None,
|
||||
checkpoint: str | Path,
|
||||
revision: str | None = None,
|
||||
hparams_file: str | Path | None = None,
|
||||
token: str | bool | None = None,
|
||||
cache_dir: Path | str | None = None,
|
||||
skip_dependencies: bool = False,
|
||||
) -> Optional["Pipeline"]:
|
||||
"""Load pretrained pipeline
|
||||
@@ -140,6 +160,8 @@ class Pipeline(_Pipeline):
|
||||
* path to a local `config.yaml` pipeline checkpoint
|
||||
* path to a local directory containing such a file
|
||||
* identifier of a pipeline on huggingface.co model hub
|
||||
revision : str, optional
|
||||
Revision when loading from the huggingface.co model hub.
|
||||
hparams_file: Path or str, optional
|
||||
token : str or bool, optional
|
||||
Token to be used for the download.
|
||||
@@ -153,6 +175,9 @@ class Pipeline(_Pipeline):
|
||||
# if checkpoint is a directory, look for the pipeline checkpoint
|
||||
# inside this directory
|
||||
if os.path.isdir(checkpoint):
|
||||
if revision is not None:
|
||||
raise ValueError("Revisions cannot be used with local checkpoints.")
|
||||
|
||||
model_id = Path(checkpoint)
|
||||
config_yml = model_id / AssetFileName.Pipeline.value
|
||||
revision = None
|
||||
@@ -160,6 +185,9 @@ class Pipeline(_Pipeline):
|
||||
|
||||
# if checkpoint is a file, assume it is the pipeline checkpoint
|
||||
elif os.path.isfile(checkpoint):
|
||||
if revision is not None:
|
||||
raise ValueError("Revisions cannot be used with local checkpoints.")
|
||||
|
||||
model_id = Path(checkpoint).parent
|
||||
config_yml = checkpoint
|
||||
revision = None
|
||||
@@ -167,10 +195,17 @@ class Pipeline(_Pipeline):
|
||||
|
||||
# otherwise, assume that the checkpoint is hosted on HF model hub
|
||||
else:
|
||||
checkpoint = str(checkpoint)
|
||||
model_id, revision, config_yml = download_from_hf_hub(
|
||||
checkpoint,
|
||||
model_id = str(checkpoint)
|
||||
|
||||
if "@" in model_id:
|
||||
raise ValueError(
|
||||
"Revisions must be passed with `revision` keyword argument."
|
||||
)
|
||||
|
||||
config_yml = download_from_hf_hub(
|
||||
model_id,
|
||||
AssetFileName.Pipeline,
|
||||
revision=revision,
|
||||
cache_dir=cache_dir,
|
||||
token=token,
|
||||
)
|
||||
@@ -178,8 +213,8 @@ class Pipeline(_Pipeline):
|
||||
return None
|
||||
|
||||
otel_origin: str = (
|
||||
checkpoint
|
||||
if checkpoint.lower().startswith(("pyannote/", "pyannoteai/"))
|
||||
model_id
|
||||
if model_id.lower().startswith(("pyannote/", "pyannoteai/"))
|
||||
else "huggingface"
|
||||
)
|
||||
|
||||
@@ -190,7 +225,7 @@ class Pipeline(_Pipeline):
|
||||
expand_subfolders(
|
||||
config,
|
||||
model_id,
|
||||
revision=revision,
|
||||
parent_revision=revision,
|
||||
token=token,
|
||||
cache_dir=cache_dir,
|
||||
skip_dependencies=skip_dependencies,
|
||||
@@ -350,7 +385,7 @@ class Pipeline(_Pipeline):
|
||||
super().__delattr__(name)
|
||||
|
||||
@staticmethod
|
||||
def setup_hook(file: AudioFile, hook: Optional[Callable] = None) -> Callable:
|
||||
def setup_hook(file: AudioFile, hook: Callable | None = None) -> Callable:
|
||||
def noop(*args, **kwargs):
|
||||
return
|
||||
|
||||
@@ -359,7 +394,7 @@ class Pipeline(_Pipeline):
|
||||
def default_parameters(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
def classes(self) -> Union[List, Iterator]:
|
||||
def classes(self) -> List | Iterator:
|
||||
"""Classes returned by the pipeline
|
||||
|
||||
Returns
|
||||
@@ -409,7 +444,7 @@ class Pipeline(_Pipeline):
|
||||
if hasattr(self, "preprocessors"):
|
||||
file = ProtocolFile(file, lazy=self.preprocessors)
|
||||
|
||||
# send file duration to telemetry as well as
|
||||
# send file duration to telemetry as well as
|
||||
# requested number of speakers in case of diarization
|
||||
track_pipeline_apply(self, file, **kwargs)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024- CNRS
|
||||
# Copyright (c) 2024-2025 CNRS
|
||||
# Copyright (c) 2025- pyannoteAI
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -66,6 +67,7 @@ class PLDA:
|
||||
cls,
|
||||
checkpoint: Path | str,
|
||||
subfolder: str | None = None,
|
||||
revision: str | None = None,
|
||||
token: str | None = None,
|
||||
cache_dir: str | Path | None = None,
|
||||
**kwargs,
|
||||
@@ -78,6 +80,8 @@ class PLDA:
|
||||
Path to checkpoint or a model identifier from the hf.co model hub.
|
||||
subfolder : str, optional
|
||||
Folder inside the hf.co model repo.
|
||||
revision : str, optional
|
||||
Revision when loading from the huggingface.co model hub.
|
||||
token : str, optional
|
||||
When loading a private hf.co model, set `token`
|
||||
to True or to a string containing your hugginface.co authentication
|
||||
@@ -89,6 +93,9 @@ class PLDA:
|
||||
# if checkpoint is a directory, look for the PLDA checkpoint
|
||||
# inside this directory (or inside a subfolder if specified)
|
||||
if os.path.isdir(checkpoint):
|
||||
if revision is not None:
|
||||
raise ValueError("Revisions cannot be used with local checkpoints.")
|
||||
|
||||
if subfolder:
|
||||
path_to_transform = Path(checkpoint) / subfolder / "xvec_transform.npz"
|
||||
path_to_plda = Path(checkpoint) / subfolder / "plda.npz"
|
||||
@@ -98,18 +105,26 @@ class PLDA:
|
||||
|
||||
# otherwise, assume that the checkpoint is hosted on HF model hub
|
||||
else:
|
||||
_, _, path_to_transform = download_from_hf_hub(
|
||||
str(checkpoint),
|
||||
checkpoint = str(checkpoint)
|
||||
if "@" in checkpoint:
|
||||
raise ValueError(
|
||||
"Revisions must be passed with `revision` keyword argument."
|
||||
)
|
||||
|
||||
path_to_transform = download_from_hf_hub(
|
||||
checkpoint,
|
||||
"xvec_transform.npz",
|
||||
subfolder=subfolder,
|
||||
revision=revision,
|
||||
cache_dir=cache_dir,
|
||||
token=token,
|
||||
)
|
||||
|
||||
_, _, path_to_plda = download_from_hf_hub(
|
||||
str(checkpoint),
|
||||
path_to_plda = download_from_hf_hub(
|
||||
checkpoint,
|
||||
"plda.npz",
|
||||
subfolder=subfolder,
|
||||
revision=revision,
|
||||
cache_dir=cache_dir,
|
||||
token=token,
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
# MIT License
|
||||
#
|
||||
# Copyright (c) 2024- CNRS
|
||||
# Copyright (c) 2024-2025 CNRS
|
||||
# Copyright (c) 2025- pyannoteAI
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -22,7 +23,6 @@
|
||||
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Optional, Tuple, Union
|
||||
|
||||
from huggingface_hub import hf_hub_download
|
||||
from huggingface_hub.utils import HfHubHTTPError
|
||||
@@ -37,24 +37,30 @@ class AssetFileName(Enum):
|
||||
Model = "pytorch_model.bin"
|
||||
Pipeline = "config.yaml"
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
|
||||
|
||||
def download_from_hf_hub(
|
||||
checkpoint: str,
|
||||
model_id: str,
|
||||
asset_file: AssetFileName | str,
|
||||
subfolder: Optional[str] = None,
|
||||
cache_dir: Optional[Union[str, Path]] = None,
|
||||
token: Union[bool, str, None] = None,
|
||||
) -> Tuple[str, Optional[str], Optional[str]]:
|
||||
subfolder: str | None = None,
|
||||
revision: str | None = None,
|
||||
cache_dir: str | Path | None = None,
|
||||
token: bool | str | None = None,
|
||||
) -> str | None:
|
||||
"""Download file from Huggingface Hub
|
||||
|
||||
Parameters
|
||||
----------
|
||||
checkpoint : Path or str
|
||||
model_id : str
|
||||
Model identifier from the hf.co model hub.
|
||||
asset_file : AssetFileName
|
||||
Type of asset file to download.
|
||||
subfolder : str, optional
|
||||
Folder inside the model repo.
|
||||
revision : str, optional
|
||||
Revision when loading from the huggingface.co model hub.
|
||||
token : str or bool, optional
|
||||
Huggingface token to be used for downloading from Huggingface hub.
|
||||
cache_dir: Path or str, optional
|
||||
@@ -65,42 +71,34 @@ def download_from_hf_hub(
|
||||
`huggingface_hub.hf_hub_download`
|
||||
"""
|
||||
|
||||
if "@" in checkpoint:
|
||||
model_id, revision = checkpoint.split("@")
|
||||
else:
|
||||
model_id, revision = checkpoint, None
|
||||
|
||||
# if provided token does not start with 'hf_', it is likely a pyannoteAI API key
|
||||
# and therefore should not be passed to Huggingface Hub.
|
||||
if token and not token.startswith("hf_"):
|
||||
if isinstance(token, str) and not token.startswith("hf_"):
|
||||
token = None
|
||||
|
||||
try:
|
||||
return (
|
||||
return hf_hub_download(
|
||||
model_id,
|
||||
revision,
|
||||
hf_hub_download(
|
||||
model_id,
|
||||
asset_file.value if isinstance(asset_file, AssetFileName) else asset_file,
|
||||
subfolder=subfolder,
|
||||
repo_type="model",
|
||||
revision=revision,
|
||||
library_name="pyannote",
|
||||
library_version=__version__,
|
||||
cache_dir=cache_dir,
|
||||
token=token,
|
||||
),
|
||||
asset_file.value if isinstance(asset_file, AssetFileName) else asset_file,
|
||||
subfolder=subfolder,
|
||||
repo_type="model",
|
||||
revision=revision,
|
||||
library_name="pyannote",
|
||||
library_version=__version__,
|
||||
cache_dir=cache_dir,
|
||||
token=token,
|
||||
)
|
||||
except HfHubHTTPError:
|
||||
asset_file_name: str = asset_file.name if isinstance(asset_file, AssetFileName) else asset_file
|
||||
print(
|
||||
f"""
|
||||
Could not download {asset_file.name.lower()} from {model_id}.
|
||||
Could not download {asset_file_name} from {model_id}.
|
||||
It might be because the repository is private or gated:
|
||||
|
||||
* visit https://hf.co/{model_id} to accept user conditions
|
||||
* visit https://hf.co/settings/tokens to create an authentication token
|
||||
* load the {asset_file.name.lower()} with the `token` argument:
|
||||
>>> {asset_file.name}.from_pretrained('{model_id}', token='hf_....')
|
||||
* load the {asset_file_name} with the `token` argument:
|
||||
>>> {asset_file_name}.from_pretrained('{model_id}', token='hf_....')
|
||||
"""
|
||||
)
|
||||
raise
|
||||
Reference in new issue
Block a user