forked from LazyAGI/LazyLLM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
101 additions
and
7 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
Empty file.
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,70 @@ | ||
import os | ||
import json | ||
|
||
import lazyllm | ||
from lazyllm import LOG | ||
from lazyllm.thirdparty import torch | ||
from lazyllm.thirdparty import transformers as tf | ||
from ..utils.downloader import ModelManager | ||
|
||
class Bark(object): | ||
|
||
def __init__(self, base_sd, source=None, trust_remote_code=True, init=False): | ||
source = lazyllm.config['model_source'] if not source else source | ||
self.base_sd = ModelManager(source).download(base_sd) | ||
self.trust_remote_code = trust_remote_code | ||
self.processor, self.bark = None, None | ||
self.init_flag = lazyllm.once_flag() | ||
self.device = 'cpu' | ||
if init: | ||
lazyllm.call_once(self.init_flag, self.load_bark) | ||
|
||
def load_bark(self): | ||
self.device = "cuda" if torch.cuda.is_available() else "cpu" | ||
self.processor = tf.AutoProcessor.from_pretrained(self.base_sd) | ||
self.processor.speaker_embeddings['repo_or_path'] = self.base_sd | ||
self.bark = tf.BarkModel.from_pretrained(self.base_sd, | ||
torch_dtype=torch.float16, | ||
attn_implementation="flash_attention_2").to(self.device) | ||
|
||
def __call__(self, string): | ||
lazyllm.call_once(self.init_flag, self.load_bark) | ||
if isinstance(string, str): | ||
query = string | ||
voice_preset = "v2/zh_speaker_9" | ||
elif isinstance(string, dict): | ||
query = string['inputs'] | ||
voice_preset = string['voice_preset'] | ||
else: | ||
raise TypeError(f"Not support input type:{type(string)}, requires str or dict.") | ||
inputs = self.processor(query, voice_preset=voice_preset).to(self.device) | ||
speech = self.bark.generate(**inputs) * 32767 | ||
res = {'sounds': ( | ||
self.bark.generation_config.sample_rate, | ||
speech.cpu().numpy().squeeze().tolist() | ||
)} | ||
return json.dumps(res) | ||
|
||
|
||
class BarkDeploy(object): | ||
keys_name_handle = { | ||
'inputs': 'inputs', | ||
} | ||
message_format = { | ||
'inputs': 'Who are you ?', | ||
'voice_preset': None, | ||
} | ||
default_headers = {'Content-Type': 'application/json'} | ||
|
||
def __init__(self, launcher=None): | ||
self.launcher = launcher | ||
|
||
def __call__(self, finetuned_model=None, base_model=None): | ||
if not os.path.exists(finetuned_model) or \ | ||
not any(filename.endswith('.bin') or filename.endswith('.safetensors') | ||
for _, _, filename in os.walk(finetuned_model) if filename): | ||
if not finetuned_model: | ||
LOG.warning(f"Note! That finetuned_model({finetuned_model}) is an invalid path, " | ||
f"base_model({base_model}) will be used") | ||
finetuned_model = base_model | ||
return lazyllm.deploy.RelayServer(func=Bark(finetuned_model), launcher=self.launcher)() |
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