Instructions to use labhamlet/AudioSphere with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use labhamlet/AudioSphere with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="labhamlet/AudioSphere", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("labhamlet/AudioSphere", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download Patcher.py from labhamlet/AudioSphere: direct link, hf CLI and curl.
- Browser
- Download file 2.99 kB
-
https://huggingface.co/labhamlet/AudioSphere/resolve/main/Patcher.py
- Command line
-
hf download hf://labhamlet/AudioSphere/Patcher.py
-
curl -L -o Patcher.py https://huggingface.co/labhamlet/AudioSphere/resolve/main/Patcher.py
2.99 kB
| from abc import ABC | |
| from .patching_utils import combine_patches, generate_patches, get_shape | |
| class PatchStrategy(ABC): | |
| def __init__(self, tstride, tshape, fstride, fshape, input_fdim, input_tdim): | |
| self.tstride = tstride | |
| self.tshape = tshape | |
| self.fstride = fstride | |
| self.fshape = fshape | |
| self.input_fdim = input_fdim | |
| self.input_tdim = input_tdim | |
| def _patch(self, x): | |
| patches = generate_patches( | |
| input=x, | |
| fstride=self.fstride, | |
| tstride=self.tstride, | |
| fshape=self.fshape, | |
| tshape=self.tshape, | |
| ) | |
| return patches | |
| def patch(self, x): | |
| return self._patch(x) | |
| def embed(self, x, patch_embedder): | |
| return patch_embedder(x) | |
| def patch_and_embed(self, x, patch_embedder): | |
| """ | |
| Generate patches from the input spectrogram and embed them. | |
| This method creates patches based on the frequency and temporal stride/shape | |
| parameters, and then applies the given patch embedding function. | |
| Parameters | |
| ---------- | |
| x : torch.Tensor | |
| The input spectrogram tensor to be patched and embedded. | |
| patch_embedder : Callable | |
| A function that applies embedding to the patches. | |
| Returns | |
| ------- | |
| Tuple[torch.Tensor, torch.Tensor] | |
| The generated patches and their embeddings. | |
| """ | |
| # Generate patches for knowing the input. | |
| patches = generate_patches( | |
| input=x, | |
| fstride=self.fstride, | |
| tstride=self.tstride, | |
| fshape=self.fshape, | |
| tshape=self.tshape, | |
| ) | |
| x = patch_embedder(x) | |
| return patches, x | |
| def get_patch_size(self): | |
| p_f_dim, p_t_dim = get_shape( | |
| fstride=self.fstride, | |
| tstride=self.tstride, | |
| input_fdim=self.input_fdim, | |
| input_tdim=self.input_tdim, | |
| fshape=self.fshape, | |
| tshape=self.tshape, | |
| ) | |
| return p_f_dim, p_t_dim | |
| def combine_patches(self, patches, original_size): | |
| return combine_patches( | |
| patches, original_size, self.fstride, self.tstride, self.fshape, self.tshape | |
| ) | |
| class TimePatching(PatchStrategy): | |
| def __init__( | |
| self, input_tdim, tstride=2, tshape=2, fstride=128, fshape=128, input_fdim=128 | |
| ): | |
| super().__init__( | |
| tstride=tstride, | |
| tshape=tshape, | |
| fstride=fstride, | |
| fshape=fshape, | |
| input_fdim=input_fdim, | |
| input_tdim=input_tdim, | |
| ) | |
| class FramePatching(PatchStrategy): | |
| def __init__( | |
| self, input_tdim, tstride=16, tshape=16, fstride=16, fshape=16, input_fdim=128 | |
| ): | |
| super().__init__( | |
| tstride=tstride, | |
| tshape=tshape, | |
| fstride=fstride, | |
| fshape=fshape, | |
| input_fdim=input_fdim, | |
| input_tdim=input_tdim, | |
| ) | |