File size: 6,966 Bytes
9659078
5f924a4
 
9659078
adf3e3f
f523090
 
adf3e3f
5a84593
 
 
bd64281
5a84593
 
 
9659078
cc07fe9
 
9659078
cc07fe9
 
 
 
 
5a82610
 
 
5f924a4
 
8302c0f
 
5f924a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a82610
e236784
9659078
8302c0f
9659078
a4b64d4
 
 
 
0435c60
 
 
 
ed4738c
 
0435c60
 
8e00ffa
9659078
5a82610
 
3c31edb
8302c0f
0435c60
 
 
 
 
5a82610
 
 
 
 
 
f523090
0c32eee
 
 
 
 
cc07fe9
 
 
0c32eee
b5682ef
8302c0f
0cdadc9
0bd14d2
9d546f2
cc07fe9
 
 
 
da5b19f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44eb4d8
 
b90ed12
44eb4d8
 
 
 
0bd14d2
 
 
 
 
cc07fe9
007b47b
7a3df69
 
ce514b9
 
7a3df69
cc07fe9
 
 
0bd14d2
b5682ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bd14d2
b5682ef
 
 
0bd14d2
b5682ef
da5b19f
 
b5682ef
 
 
 
 
 
0bd14d2
3d3d24d
e420073
9bb5aa9
 
 
 
 
 
 
 
 
 
e420073
9bb5aa9
 
 
 
e420073
9bb5aa9
 
 
 
 
 
 
 
 
 
 
007b47b
9bb5aa9
 
44eb4d8
 
 
b90ed12
 
44eb4d8
 
 
9bb5aa9
e420073
0bd14d2
 
 
 
 
 
 
 
cc07fe9
adf3e3f
d9dce8e
0bd14d2
 
b5682ef
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import gradio as gr
import os 
import shutil

#from huggingface_hub import snapshot_download
import numpy as np
from scipy.io import wavfile
"""
model_ids = [
    'suno/bark',
]

for model_id in model_ids:
    model_name = model_id.split('/')[-1]
    snapshot_download(model_id, local_dir=f'checkpoints/{model_name}')

from TTS.tts.configs.bark_config import BarkConfig
from TTS.tts.models.bark import Bark

#os.environ['CUDA_VISIBLE_DEVICES'] = '1'
config = BarkConfig()
model = Bark.init_from_config(config)
model.load_checkpoint(config, checkpoint_dir="checkpoints/bark", eval=True)
"""
from TTS.api import TTS
tts = TTS("tts_models/multilingual/multi-dataset/bark", gpu=True)

def infer(prompt, input_wav_file):

    print("SAVING THE AUDIO FILE TO WHERE IT BELONGS")

    # Path to your WAV file
    source_path = input_wav_file

    # Destination directory
    destination_directory = "bark_voices"

    # Extract the file name without the extension
    file_name = os.path.splitext(os.path.basename(source_path))[0]

    # Construct the full destination directory path
    destination_path = os.path.join(destination_directory, file_name)

    # Create the new directory
    os.makedirs(destination_path, exist_ok=True)

    # Move the WAV file to the new directory
    shutil.move(source_path, os.path.join(destination_path, f"{file_name}.wav"))

    """
    text = prompt

    print("SYNTHETIZING...")
    # with random speaker
    #output_dict = model.synthesize(text, config, speaker_id="random", voice_dirs=None)

    # cloning a speaker.
    # It assumes that you have a speaker file in `bark_voices/speaker_n/speaker.wav` or `bark_voices/speaker_n/speaker.npz`
    output_dict = model.synthesize(
        text, 
        config, 
        speaker_id=f"{file_name}", 
        voice_dirs="bark_voices/",
        gpu=True
    )
    
    print(output_dict)

    

    sample_rate = 24000  # Replace with the actual sample rate
    print("WRITING WAVE FILE")
    wavfile.write(
        'output.wav', 
        sample_rate, 
        output_dict['wav']
    )
    """
    
    tts.tts_to_file(text=prompt,
                file_path="output.wav",
                voice_dir="bark_voices/",
                speaker=f"{file_name}")

    # List all the files and subdirectories in the given directory
    contents = os.listdir(f"bark_voices/{file_name}")

    # Print the contents
    for item in contents:
        print(item)  

    tts_video = gr.make_waveform(audio="output.wav")
    
    return "output.wav", tts_video, gr.update(value=f"bark_voices/{file_name}/{contents[1]}", visible=True)


css = """
#col-container {max-width: 780px; margin-left: auto; margin-right: auto;}
img[src*='#center'] { 
    display: block;
    margin: auto;
}
.footer {
        margin-bottom: 45px;
        margin-top: 10px;
        text-align: center;
        border-bottom: 1px solid #e5e5e5;
    }
    .footer>p {
        font-size: .8rem;
        display: inline-block;
        padding: 0 10px;
        transform: translateY(10px);
        background: white;
    }
    .dark .footer {
        border-color: #303030;
    }
    .dark .footer>p {
        background: #0b0f19;
    }

.disclaimer {
    text-align: left;
}
.disclaimer > p {
    font-size: .8rem;
}
"""

with gr.Blocks(css=css) as demo:
    with gr.Column(elem_id="col-container"):
        
        gr.Markdown("""
        <h1 style="text-align: center;">Coqui Bark Voice Cloning</h1>
        <p style="text-align: center;">
        Clone any voice in less than 2 minutes with this <a href="https://tts.readthedocs.io/en/dev/models/bark.html" target="_blank">Coqui TSS + Bark</a> demo ! <br />
        Upload a clean 20 seconds WAV file of the voice you want to clone, <br />
        type your text-to-speech prompt and hit submit ! <br />
        </p>

        [![Duplicate this Space](https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-sm.svg#center)](https://huggingface.co/spaces/fffiloni/instant-TTS-Bark-cloning?duplicate=true)
            
        """)
        with gr.Row():
            with gr.Column():
                prompt = gr.Textbox(
                    label="Text to speech prompt"
                )
                
                audio_in = gr.Audio(
                    label="WAV voice to clone", 
                    type="filepath",
                    source="upload"
                )
                
                submit_btn = gr.Button("Submit")

            with gr.Column():
        
                cloned_out = gr.Audio(
                    label="Text to speech output"
                )
        
                video_out = gr.Video(
                    label = "Waveform video",
                    animate = True
                )
                
                npz_file = gr.File(
                    label = ".npz file",
                    visible = False
                )
    
    
    
        gr.Examples(
            examples = [
                [
                    "Once upon a time, in a cozy little shell, lived a friendly crab named Crabby. Crabby loved his cozy home, but he always felt like something was missing.",
                    "./examples/en_speaker_6.wav",
                ],
                [ 
                    "It was a typical afternoon in the bustling city, the sun shining brightly through the windows of the packed courtroom. Three people sat at the bar, their faces etched with worry and anxiety. ",
                    "./examples/en_speaker_9.wav",
                ],
            ],
            fn = infer,
            inputs = [
                prompt,
                audio_in
            ],
            outputs = [
                cloned_out, 
                video_out,
                npz_file
            ],
            cache_examples = True
        )
    
        gr.HTML("""
                <div class="footer">
                    <p>
                    Coqui + Bark Voice Cloning Demo by 🤗 <a href="https://twitter.com/fffiloni" target="_blank">Sylvain Filoni</a>
                    </p>
                </div>
                <div class="disclaimer">
                    <h3> * DISCLAIMER </h3>
                    <p>
                        I hold no responsibility for the utilization or outcomes of audio content produced using the semantic constructs generated by this model. <br />
                        Please ensure that any application of this technology remains within legal and ethical boundaries. <br />
                        It is important to utilize this technology for ethical and legal purposes, upholding the standards of creativity and innovation.
                    </p>
                </div>
            """)
    
    submit_btn.click(
        fn = infer,
        inputs = [
            prompt,
            audio_in
        ],
        outputs = [
            cloned_out, 
            video_out,
            npz_file
        ]
    )

demo.queue(max_size=20).launch()