File size: 1,838 Bytes
e95bc25
 
 
 
2c32692
 
 
e95bc25
 
 
 
 
 
 
884068b
 
 
 
e95bc25
 
 
 
f194fff
e95bc25
 
 
 
f194fff
e95bc25
 
 
 
 
 
 
2c32692
e95bc25
 
f194fff
 
 
 
 
 
e95bc25
f194fff
e95bc25
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tempfile
from TTS.utils.synthesizer import Synthesizer
from huggingface_hub import hf_hub_download
import torch

CUDA = torch.cuda.is_available()

REPO_ID = "ayymen/Coqui-TTS-Vits-shi"

my_title = "Tamazight Text-to-Speech"
my_description = "This model is based on [VITS](https://github.com/jaywalnut310/vits), thanks to 🐸 [Coqui.ai](https://coqui.ai/)." 

my_examples = [
  ["ⴰⵣⵓⵍ. ⵎⴰⵏⵣⴰⴽⵉⵏ?"],
  ["ⵡⴰ ⵜⴰⵎⵖⴰⵔⵜ ⵎⴰ ⴷ ⵓⴽⴰⵏ ⵜⵙⴽⵔⵜ?"],
  ["ⴳⵏ! ⴰⴷ ⴰⴽ ⵉⵙⵙⴳⵏ ⵕⴱⴱⵉ ⵉⵜⵜⵓ ⴽ."],
  ["ⴰⵔⵔⴰⵡ ⵏ ⵍⵀⵎⵎ ⵢⵓⴽⵔ ⴰⵖ ⵉⵀⴷⵓⵎⵏ ⵏⵏⵖ!"]
]

my_inputs = [
  gr.Textbox(lines=5, label="Input Text"),
  gr.Checkbox(label="Split Sentences", value=True)
]

my_outputs = gr.Audio(type="filepath", label="Output Audio")

def tts(text: str, split_sentences: bool = True):
    best_model_path = hf_hub_download(repo_id=REPO_ID, filename="best_model.pth") 
    config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
    
    # init synthesizer  
    synthesizer = Synthesizer(
        best_model_path,
        config_path,
        use_cuda=CUDA
    )

    # replace oov characters
    text = text.replace("\n", ". ")
    text = text.replace("(", ",")
    text = text.replace(")", ",")
    text = text.replace(";", ",")

    # create audio file
    wavs = synthesizer.tts(text, split_sentences=split_sentences)
    with tempfile.NamedTemporaryFile(suffix = ".wav", delete = False) as fp:
        synthesizer.save_wav(wavs, fp)                      
    return fp.name 
 
iface = gr.Interface(
    fn=tts, 
    inputs=my_inputs, 
    outputs=my_outputs, 
    title=my_title, 
    description = my_description, 
    examples = my_examples,
    cache_examples=True
)
iface.launch()