File size: 1,426 Bytes
6eb1048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d3c496
 
6eb1048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
from TTS.utils.download import download_url
from TTS.utils.synthesizer import Synthesizer
import gradio as gr
import tempfile

# Variables
MAX_TXT_LEN = 800
MODEL_DIR = "kbd-vits-tts-male"
MODEL_URL = "https://huggingface.co/anzorq/kbd-vits-tts-male/resolve/main/checkpoint_56000.pth"
CONFIG_URL = "https://huggingface.co/anzorq/kbd-vits-tts-male/resolve/main/config_35000.json"

# Downloading model and config
if not os.path.exists(MODEL_DIR):
    os.makedirs(MODEL_DIR)
download_url(MODEL_URL, MODEL_DIR, "model.pth")
download_url(CONFIG_URL, MODEL_DIR, "config.json")


def tts(text: str):
    if len(text) > MAX_TXT_LEN:
        text = text[:MAX_TXT_LEN]
        print(f"Input text was cutoff since it went over the {MAX_TXT_LEN} character limit.")
    print(text)

    text = text.replace("I", "ӏ") #replace capital is with "Palochka" symbol

    # synthesize
    synthesizer = Synthesizer(f"{MODEL_DIR}/model.pth", f"{MODEL_DIR}/config.json")
    wavs = synthesizer.tts(text)

    # return output
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
        synthesizer.save_wav(wavs, fp)
        return fp.name

# Gradio interface
iface = gr.Interface(
    fn=tts,
    inputs=gr.Textbox(
        label="Text",
        value="Default text here if you need it.",
    ),
    outputs=gr.Audio(label="Output", type='filepath'),
    title="KBD TTS",
    live=False
)

iface.launch(share=False)