File size: 2,204 Bytes
6eb1048
 
 
 
 
 
 
27e87f7
 
 
 
 
6eb1048
27e87f7
 
 
 
 
 
 
 
 
6eb1048
27e87f7
 
6eb1048
27e87f7
6eb1048
 
 
 
 
1d3c496
 
27e87f7
 
6eb1048
27e87f7
6eb1048
 
 
9ee1d23
6eb1048
 
 
 
 
27e87f7
 
 
 
 
 
 
 
 
 
 
6eb1048
 
 
 
 
27e87f7
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from TTS.utils.download import download_url
from TTS.utils.synthesizer import Synthesizer
import gradio as gr
import tempfile

MAX_TXT_LEN = 800
BASE_DIR = "kbd-vits-tts-{}"
MALE_MODEL_URL = "https://huggingface.co/anzorq/kbd-vits-tts-male/resolve/main/checkpoint_56000.pth"
MALE_CONFIG_URL = "https://huggingface.co/anzorq/kbd-vits-tts-male/resolve/main/config_35000.json"
FEMALE_MODEL_URL = "https://huggingface.co/anzorq/kbd-vits-tts-female/resolve/main/best_model_56351.pth"
FEMALE_CONFIG_URL = "https://huggingface.co/anzorq/kbd-vits-tts-female/resolve/main/config.json"

def download_model_and_config(gender):
    dir_path = BASE_DIR.format(gender)
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
    model_url = MALE_MODEL_URL if gender == "male" else FEMALE_MODEL_URL
    config_url = MALE_CONFIG_URL if gender == "male" else FEMALE_CONFIG_URL
    download_url(model_url, dir_path, "model.pth")
    download_url(config_url, dir_path, "config.json")
    return dir_path

download_model_and_config("male")
download_model_and_config("female")

def tts(text: str, voice: str="Male"):
    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

    model_dir = BASE_DIR.format("male" if voice == "Male" else "female")

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

    # return output
    with tempfile.NamedTemporaryFile(dir="sound/", prefix=f"-", suffix=".wav", delete=False) as fp:
        synthesizer.save_wav(wavs, fp)
        return fp.name

iface = gr.Interface(
    fn=tts,
    inputs=[
        gr.Textbox(
            label="Text",
            value="Default text here if you need it.",
        ),
        gr.Radio(
            choices=["Male", "Female"],
            value="Male",  # Set Male as the default choice
            label="Voice"
        )
    ],
    outputs=gr.Audio(label="Output", type='filepath'),
    title="KBD TTS",
    live=False
)

iface.launch(share=False)