Plana-Archive commited on
Commit
e1cd73f
·
verified ·
1 Parent(s): 513c784

Clean up previous raw files

Browse files
Files changed (1) hide show
  1. Zero-CPU/text/japanese_bert.py +0 -67
Zero-CPU/text/japanese_bert.py DELETED
@@ -1,67 +0,0 @@
1
- import sys
2
-
3
- import torch
4
- from transformers import AutoModelForMaskedLM, AutoTokenizer
5
-
6
- from config import config
7
- from text.japanese import text2sep_kata
8
-
9
- LOCAL_PATH = "./bert/deberta-v2-large-japanese-char-wwm"
10
-
11
- tokenizer = AutoTokenizer.from_pretrained(LOCAL_PATH)
12
-
13
- models = dict()
14
-
15
-
16
- def get_bert_feature(
17
- text,
18
- word2ph,
19
- device=config.bert_gen_config.device,
20
- assist_text=None,
21
- assist_text_weight=0.7,
22
- ):
23
- text = "".join(text2sep_kata(text)[0])
24
- if assist_text:
25
- assist_text = "".join(text2sep_kata(assist_text)[0])
26
- if (
27
- sys.platform == "darwin"
28
- and torch.backends.mps.is_available()
29
- and device == "cpu"
30
- ):
31
- device = "mps"
32
- if not device:
33
- device = "cuda"
34
- if device == "cuda" and not torch.cuda.is_available():
35
- device = "cpu"
36
- if device not in models.keys():
37
- models[device] = AutoModelForMaskedLM.from_pretrained(LOCAL_PATH).to(device)
38
- with torch.no_grad():
39
- inputs = tokenizer(text, return_tensors="pt")
40
- for i in inputs:
41
- inputs[i] = inputs[i].to(device)
42
- res = models[device](**inputs, output_hidden_states=True)
43
- res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu()
44
- if assist_text:
45
- style_inputs = tokenizer(assist_text, return_tensors="pt")
46
- for i in style_inputs:
47
- style_inputs[i] = style_inputs[i].to(device)
48
- style_res = models[device](**style_inputs, output_hidden_states=True)
49
- style_res = torch.cat(style_res["hidden_states"][-3:-2], -1)[0].cpu()
50
- style_res_mean = style_res.mean(0)
51
-
52
- assert len(word2ph) == len(text) + 2, text
53
- word2phone = word2ph
54
- phone_level_feature = []
55
- for i in range(len(word2phone)):
56
- if assist_text:
57
- repeat_feature = (
58
- res[i].repeat(word2phone[i], 1) * (1 - assist_text_weight)
59
- + style_res_mean.repeat(word2phone[i], 1) * assist_text_weight
60
- )
61
- else:
62
- repeat_feature = res[i].repeat(word2phone[i], 1)
63
- phone_level_feature.append(repeat_feature)
64
-
65
- phone_level_feature = torch.cat(phone_level_feature, dim=0)
66
-
67
- return phone_level_feature.T