File size: 4,316 Bytes
ae0617f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3cd0dfd
 
 
24ebea4
 
 
3cd0dfd
ae0617f
0ca1335
ae0617f
 
 
 
ac877ac
 
 
 
 
 
 
 
 
ae0617f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bef4266
 
 
 
 
 
 
 
 
99febd9
 
 
bef4266
 
 
 
 
 
 
 
861914a
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
from __future__ import annotations

import gradio as gr
try:
    import spaces
except ImportError:  # 'spaces' is only provided by Hugging Face Spaces
    import types as _types

    def _gpu(*args, **kwargs):
        if len(args) == 1 and callable(args[0]) and not kwargs:
            return args[0]

        def _decorator(func):
            return func

        return _decorator

    spaces = _types.SimpleNamespace(GPU=_gpu)

from pyharp import *


try:  # torch>=2.6 flipped torch.load(weights_only) to True; legacy ckpts need False
    import torch as _torch

    if getattr(_torch.load, "__harp_compat__", False) is False:
        _torch_load_orig = _torch.load

        def _torch_load_compat(*args, **kwargs):
            kwargs.setdefault("weights_only", False)
            return _torch_load_orig(*args, **kwargs)

        _torch_load_compat.__harp_compat__ = True
        _torch.load = _torch_load_compat
except Exception:  # torch not installed / unexpected API -- nothing to patch
    pass


from bark import generate_audio, preload_models, SAMPLE_RATE
from scipy.io.wavfile import write as write_wav
import tempfile
import os

# Download and load all models once
preload_models()


model_card = ModelCard(
    name="Bark",
    description=(
        "Bark is a transformer-based text-to-audio model created by Suno. Bark "
        "can generate highly realistic, multilingual speech as well as other "
        "audio - including music, background noise and simple sound effects. "
        "The model can also produce nonverbal communications like laughing, "
        "sighing and crying. Repository: https://github.com/suno-ai/bark"
    ),
    author="suno-ai",
    tags=[Subcategory.TEXT_TO_SPEECH, 'multilingual'],
)


def process_fn(text_prompt, history_prompt, text_temp, waveform_temp):
    # Checked before the GPU is reserved, so a request the model cannot take spends none of the
    # caller's ZeroGPU allowance
    if not (text_prompt or "").strip():
        raise gr.Error("Enter a text prompt.")
    return generate(text_prompt, history_prompt, text_temp, waveform_temp)


@spaces.GPU
def generate(text_prompt, history_prompt, text_temp, waveform_temp):
    history_prompt_val = history_prompt if history_prompt != "None" else None

    # Generate audio from text
    audio_array = generate_audio(
        text_prompt,
        history_prompt=history_prompt_val,
        text_temp=text_temp,
        waveform_temp=waveform_temp,
        silent=True, # Disable progress bar for Gradio app
        output_full=False # Only return the audio array
    )

    # Save audio to a temporary WAV file
    output_file = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
    output_file.close()
    write_wav(output_file.name, SAMPLE_RATE, audio_array)

    return output_file.name


if __name__ == "__main__":
    with gr.Blocks() as demo:
        input_components = [
            gr.Textbox(label="Text Prompt", info="The text to be converted into audio. Bark supports multilingual speech, music, and nonverbal communications.").harp_required(True),
            gr.Dropdown(choices=["None", "announcer", "en_speaker_0", "en_speaker_1", "en_speaker_2", "en_speaker_3", "en_speaker_4", "en_speaker_5", "en_speaker_6", "en_speaker_7", "en_speaker_8", "en_speaker_9"], value="en_speaker_0", label="Voice Prompt (Speaker)", info="Choose a speaker to use as a voice prompt for audio cloning. 'None' will use a default voice."),
            gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.7, label="Text Generation Temperature", info="Controls the diversity of text token generation (1.0 more diverse, 0.0 more conservative)."),
            gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.7, label="Waveform Generation Temperature", info="Controls the diversity of waveform token generation (1.0 more diverse, 0.0 more conservative)."),
        ]
        output_components = [
            gr.Audio(type="filepath", label="Generated Audio").set_info((
                "The spoken or sung result, at 24 kHz."
            )),
        ]
        build_endpoint(
            model_card=model_card,
            input_components=input_components,
            output_components=output_components,
            process_fn=process_fn,
        )

    demo.queue().launch(show_error=True, pwa=True)