JackismyShephard's picture
some refactoring in application file
54811b2
raw
history blame
3.42 kB
import gradio as gr
import numpy as np
import torch
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
checkpoint_base = "microsoft/speecht5_tts"
checkpoint_finetuned = "JackismyShephard/speecht5_tts-finetuned-nst-da"
processor = SpeechT5Processor.from_pretrained(checkpoint_base)
model = SpeechT5ForTextToSpeech.from_pretrained(checkpoint_finetuned)
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
speaker_embeddings = {
"F23": "embeddings/female_23_vestjylland.npy",
"F24": "embeddings/female_24_storkoebenhavn.npy",
"F49": "embeddings/female_49_nordjylland.npy",
"M51": "embeddings/male_51_vest_sudsjaelland.npy",
"M18": "embeddings/male_18_vest_sydsjaelland.npy",
"M31": "embeddings/male_31_fyn.npy",
}
def predict(text, speaker):
if len(text.strip()) == 0:
return (16000, np.zeros(0))
text = replace_danish_letters(text)
inputs = processor(text=text, return_tensors="pt")
# limit input length
input_ids = inputs["input_ids"]
input_ids = input_ids[..., : model.config.max_text_positions]
speaker_id = speaker[:3]
speaker_embedding_path = speaker_embeddings[speaker_id]
speaker_embedding = np.load(speaker_embedding_path)
speaker_embedding = torch.tensor(speaker_embedding).unsqueeze(0)
speech = model.generate_speech(input_ids, speaker_embedding, vocoder=vocoder)
speech = speech.numpy()
return (16000, speech)
def replace_danish_letters(text):
for src, dst in replacements:
text = text.replace(src, dst)
return text
replacements = [
("&", "og"),
("\r", " "),
("´", ""),
("\\", ""),
("¨", " "),
("Å", "AA"),
("Æ", "AE"),
("É", "E"),
("Ö", "OE"),
("Ø", "OE"),
("á", "a"),
("ä", "ae"),
("å", "aa"),
("è", "e"),
("î", "i"),
("ô", "oe"),
("ö", "oe"),
("ø", "oe"),
("ü", "y"),
]
title = "Danish Speech Synthesis"
description = """
synthesize long-form danish speech from text with the click of a button! Demo uses the"
f" checkpoint [{checkpoint_finetuned}](https://huggingface.co/{checkpoint_finetuned}) and 🤗 Transformers to synthesize speech.
"""
examples = [
[
"I sin oprindelige før-kristne form blev alferne sandsynligvis opfattet som en personificering af det land og den natur, der omgav menneskene, dvs. den opdyrkede jord, gården og de naturressourcer, som hørte dertil. De var guddommelige eller delvis guddommelige væsener, der besad magiske kræfter, som de brugte både til fordel og ulempe for menneskene.",
"F23 (Female, 23, Vestjylland)",
],
]
demo = gr.Interface(
fn=predict,
inputs=[
gr.Textbox(label="Input Text"),
gr.Radio(
label="Speaker",
choices=[
"F23 (Female, 23, Vestjylland)",
"F24 (Female, 24, Storkoebenhavn)",
"F49 (Female, 49 Nordjylland)",
"M51 (Male. 51. Vest-sydsjaelland)",
"M18 (Male, 18, Vest-sysjaelland)",
"M31 (Male, 31, Fyn)",
],
value="F23 (Female, 23, Vestjylland)",
),
],
outputs=[
gr.Audio(label="Generated Speech", type="numpy"),
],
title=title,
description=description,
examples=examples,
cache_examples=True,
allow_flagging="never",
)
demo.launch()