Downloads · 30 days
3
5% of all-time downloads
zijuncheng/word-to-phoneme
word-to-phoneme is a machine learning model from zijuncheng. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Download all the files to a local directory
Downloads · 30 days
3
5% of all-time downloads
All-time downloads
59
Public
Parameters
10.5M
42.1 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors42.1 MB · 100%
From the Hugging Face model README
Download all the files to a local directory model_dir
from transformers import BartForConditionalGeneration, BartConfig, BartTokenizer, PreTrainedTokenizerFast
tokenizer_w2p = PreTrainedTokenizerFast.from_pretrained(model_dir)
model_w2p = BartForConditionalGeneration.from_pretrained(model_dir)
Set seed and create inference function (batch)
import random
import numpy as np
import torch
def set_seed(seed=42):
torch.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def g2p_local(sentence, model, tokenizer):
set_seed(42)
model.eval()
batched_inputs = sentence.split()
# will produced a list of words that are space separated
batched_inputs = [" ".join(list(w)) for w in batched_inputs]
inputs = tokenizer(batched_inputs, return_tensors="pt", padding=True, truncation=True, max_length=32)
with torch.no_grad():
output_ids = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
max_length=32,
num_beams=5, # You can adjust decoding strategy
early_stopping=True
)
decoded_outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
decoded_outputs = [r.replace(" ", "") for r in decoded_outputs]
return " ".join(decoded_outputs)
The inference function:
text = "It's raining cats and dogs."
result = g2p_local(text, model_w2p, tokenizer_w2p)
This should return:
'ˌɪts ɹˈAnɪŋ kˈæts ænd dˈɔɡz.'