-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from h-munakata/muna/amr_demo
Add inference API of AMR
- Loading branch information
Showing
14 changed files
with
315 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from typing import Optional | ||
|
||
import numpy as np | ||
import torch | ||
import torchaudio.transforms as T | ||
from msclap import CLAP | ||
|
||
|
||
class CLAPAudioConfig: | ||
def __init__(self, cfg: Optional[dict] = None): | ||
self.sample_rate: int = 44100 | ||
self.window_sec: float = 1.0 | ||
self.version: str = '2023' | ||
self.feature_time: float = 1.0 | ||
|
||
if cfg is not None: | ||
self.update(cfg) | ||
|
||
def update(self, cfg: dict): | ||
self.__dict__.update(cfg) | ||
|
||
|
||
class CLAPAudio(torch.nn.Module): | ||
def __init__(self, device: str, cfg: CLAPAudioConfig): | ||
super(CLAPAudio, self).__init__() | ||
use_cuda = True if device == 'cuda' else False | ||
self.clap = CLAP(use_cuda=use_cuda, version=cfg.version) | ||
self.sample_rate = cfg.sample_rate | ||
self.window_sec = cfg.window_sec | ||
self.feature_time = cfg.feature_time | ||
self._device = device | ||
|
||
def _preprocess(self, audio: np.ndarray, sr: int) -> torch.Tensor: | ||
audio_tensor = self._move_data_to_device(audio) | ||
audio_tensor = T.Resample(sr, self.sample_rate)(audio_tensor) # original implementation in msclap | ||
|
||
win_length = int(round(self.window_sec * self.sample_rate)) | ||
hop_length = int(round(self.feature_time * self.sample_rate)) | ||
|
||
time = audio_tensor.shape[-1] / self.sample_rate | ||
batches = int(time // self.feature_time) | ||
clip_sr = round(self.sample_rate * self.feature_time) | ||
audio_tensor = audio_tensor[:batches * clip_sr] # Truncate audio to fit the clip_sr | ||
|
||
audio_clip = audio_tensor.unfold(0, win_length, hop_length) | ||
|
||
return audio_clip | ||
|
||
def _move_data_to_device( | ||
self, | ||
x: np.ndarray) -> torch.Tensor: | ||
if 'float' in str(x.dtype): | ||
return torch.Tensor(x).to(self._device) | ||
elif 'int' in str(x.dtype): | ||
return torch.LongTensor(x).to(self._device) | ||
else: | ||
raise ValueError('The input x cannot be cast into float or int.') | ||
|
||
def forward(self, audio: np.ndarray, sr: int): | ||
audio_clip = self._preprocess(audio, sr) | ||
output_dict = self.clap.clap.audio_encoder.base(audio_clip) | ||
audio_mask = torch.ones(1, len(output_dict['embedding'])).to(self._device) | ||
x = output_dict['embedding'].unsqueeze(0) | ||
return x, audio_mask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Tuple | ||
|
||
import torch | ||
from msclap import CLAP | ||
|
||
""" | ||
Copyright $today.year LY Corporation | ||
LY Corporation licenses this file to you under the Apache License, | ||
version 2.0 (the "License"); you may not use this file except in compliance | ||
with the License. You may obtain a copy of the License at: | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
License for the specific language governing permissions and limitations | ||
under the License. | ||
""" | ||
|
||
|
||
class CLAPText: | ||
def __init__( | ||
self, | ||
device: str, | ||
model_path: str, | ||
) -> None: | ||
self._model_path: str = model_path | ||
self._device: str = device | ||
use_cuda = True if self._device == 'cuda' else False | ||
self._clap_extractor = CLAP(use_cuda=use_cuda, version=model_path) | ||
self._preprocessor = self._clap_extractor.preprocess_text | ||
self._text_encoder = self._clap_extractor.clap.caption_encoder | ||
|
||
def __call__(self, query: str) -> Tuple[torch.Tensor, torch.Tensor]: | ||
preprocessed = self._preprocessor([query]) | ||
mask = preprocessed['attention_mask'] | ||
|
||
out = self._text_encoder.base(**preprocessed) | ||
x = out[0] # out[1] is pooled output | ||
|
||
return x, mask |
Oops, something went wrong.