File size: 2,209 Bytes
ccb20f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3524910
ccb20f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
904b919
ccb20f4
 
 
 
 
 
 
 
3524910
ccb20f4
 
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import tempfile
from typing import Optional
from TTS.config import load_config
import gradio as gr
import numpy as np
import os
import json
from TTS.utils.manage import ModelManager
from TTS.utils.synthesizer import Synthesizer


MAX_TXT_LEN = 100

SPEAKERS = ['f_cen_05', 'f_cen_81', 'f_occ_31', 'f_occ_de', 'f_sep_31', 'm_cen_08', 'm_occ_44', 'm_val_89']

def tts(text, speaker_idx):
    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)

    model_path = os.getcwd() + "/best_model.pth"
    config_path = os.getcwd() + "/config.json"
    speakers_file_path = os.getcwd() + "/speakers.pth"
    speakers_maping_path = os.getcwd() + "/speaker_map.json"
    vocoder_path = None
    vocoder_config_path = None

    synthesizer = Synthesizer(
        model_path, config_path, speakers_file_path, None, vocoder_path, vocoder_config_path,
    )

    # Map speaker aliases to speaker ids
    with open(speakers_maping_path, 'r') as fp:
        maping = json.load(fp)

    speaker_idx = maping[speaker_idx]

    # synthesize
    if synthesizer is None:
        raise NameError("model not found")
    wavs = synthesizer.tts(text, speaker_idx)
    # return output
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp:
        synthesizer.save_wav(wavs, fp)
        return fp.name


description="""
1️⃣ Introdueix el text a sintetitzar.

2️⃣ Selecciona una veu en el desplegable.

3️⃣ Gaudeix!
"""
article= ""

iface = gr.Interface(
    fn=tts,
    inputs=[
        gr.inputs.Textbox(
            label="Text",
            default="L'Èlia i l'Alí a l'aula.  L'oli i l'ou.  Lulú olorava la lila.",
        ),
        gr.inputs.Dropdown(label="Selecciona un parlant", choices=SPEAKERS, default=None)
    ],
    outputs=gr.outputs.Audio(label="Output",type="filepath"),
    title="🗣️ TTS Català Multi Parlant - VITS 🗣️",
    theme="grass",
    description=description,
    article=article,
    allow_flagging="never",
    flagging_options=['error', 'bad-quality', 'wrong-pronounciation'],
    layout="vertical",
    live=False
)
iface.launch(share=False)