Fix: support infer 2.2 models (#244)
* Fix: support infer 2.2 models * Fix imports * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
49
oldVersion/V220/clap_wrapper.py
Normal file
49
oldVersion/V220/clap_wrapper.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import sys
|
||||
|
||||
import torch
|
||||
from transformers import ClapModel, ClapProcessor
|
||||
|
||||
from config import config
|
||||
|
||||
models = dict()
|
||||
processor = ClapProcessor.from_pretrained("./emotional/clap-htsat-fused")
|
||||
|
||||
|
||||
def get_clap_audio_feature(audio_data, device=config.bert_gen_config.device):
|
||||
if (
|
||||
sys.platform == "darwin"
|
||||
and torch.backends.mps.is_available()
|
||||
and device == "cpu"
|
||||
):
|
||||
device = "mps"
|
||||
if not device:
|
||||
device = "cuda"
|
||||
if device not in models.keys():
|
||||
models[device] = ClapModel.from_pretrained("./emotional/clap-htsat-fused").to(
|
||||
device
|
||||
)
|
||||
with torch.no_grad():
|
||||
inputs = processor(
|
||||
audios=audio_data, return_tensors="pt", sampling_rate=48000
|
||||
).to(device)
|
||||
emb = models[device].get_audio_features(**inputs)
|
||||
return emb.T
|
||||
|
||||
|
||||
def get_clap_text_feature(text, device=config.bert_gen_config.device):
|
||||
if (
|
||||
sys.platform == "darwin"
|
||||
and torch.backends.mps.is_available()
|
||||
and device == "cpu"
|
||||
):
|
||||
device = "mps"
|
||||
if not device:
|
||||
device = "cuda"
|
||||
if device not in models.keys():
|
||||
models[device] = ClapModel.from_pretrained("./emotional/clap-htsat-fused").to(
|
||||
device
|
||||
)
|
||||
with torch.no_grad():
|
||||
inputs = processor(text=text, return_tensors="pt").to(device)
|
||||
emb = models[device].get_text_features(**inputs)
|
||||
return emb.T
|
||||
Reference in New Issue
Block a user