Merge remote-tracking branch 'origin/main'
Browse files
app.py
CHANGED
@@ -1,177 +1,68 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import numpy as np
|
3 |
-
import gradio as gr
|
4 |
-
from bark import SAMPLE_RATE, generate_audio, preload_models
|
5 |
-
from bark.generation import SUPPORTED_LANGS
|
6 |
-
from share_btn import community_icon_html, loading_icon_html, share_js
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
AVAILABLE_PROMPTS.append(label)
|
19 |
-
PROMPT_LOOKUP[label] = f"{lang}_speaker_{n}"
|
20 |
-
PROMPT_LOOKUP["Unconditional"] = None
|
21 |
-
PROMPT_LOOKUP["Announcer"] = "announcer"
|
22 |
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
</div>
|
33 |
-
Bark is a universal text-to-audio model created by [Suno](www.suno.ai), with code publicly available [here](https://github.com/suno-ai/bark). \
|
34 |
-
Bark can generate highly realistic, multilingual speech as well as other audio - including music, background noise and simple sound effects. \
|
35 |
-
This demo should be used for research purposes only. Commercial use is strictly prohibited. \
|
36 |
-
The model output is not censored and the authors do not endorse the opinions in the generated content. \
|
37 |
-
Use at your own risk.
|
38 |
-
"""
|
39 |
-
|
40 |
-
article = """
|
41 |
-
## 🌎 Foreign Language
|
42 |
-
Bark supports various languages out-of-the-box and automatically determines language from input text. \
|
43 |
-
When prompted with code-switched text, Bark will even attempt to employ the native accent for the respective languages in the same voice.
|
44 |
-
Try the prompt:
|
45 |
-
```
|
46 |
-
Buenos días Miguel. Tu colega piensa que tu alemán es extremadamente malo. But I suppose your english isn't terrible.
|
47 |
-
```
|
48 |
-
## 🤭 Non-Speech Sounds
|
49 |
-
Below is a list of some known non-speech sounds, but we are finding more every day. \
|
50 |
-
Please let us know if you find patterns that work particularly well on Discord!
|
51 |
-
* [laughter]
|
52 |
-
* [laughs]
|
53 |
-
* [sighs]
|
54 |
-
* [music]
|
55 |
-
* [gasps]
|
56 |
-
* [clears throat]
|
57 |
-
* — or ... for hesitations
|
58 |
-
* ♪ for song lyrics
|
59 |
-
* capitalization for emphasis of a word
|
60 |
-
* MAN/WOMAN: for bias towards speaker
|
61 |
-
Try the prompt:
|
62 |
-
```
|
63 |
-
" [clears throat] Hello, my name is Suno. And, uh — and I like pizza. [laughs] But I also have other interests such as... ♪ singing ♪."
|
64 |
-
```
|
65 |
-
## 🎶 Music
|
66 |
-
Bark can generate all types of audio, and, in principle, doesn't see a difference between speech and music. \
|
67 |
-
Sometimes Bark chooses to generate text as music, but you can help it out by adding music notes around your lyrics.
|
68 |
-
Try the prompt:
|
69 |
-
```
|
70 |
-
♪ In the jungle, the mighty jungle, the lion barks tonight ♪
|
71 |
-
```
|
72 |
-
## 🧬 Voice Cloning
|
73 |
-
Bark has the capability to fully clone voices - including tone, pitch, emotion and prosody. \
|
74 |
-
The model also attempts to preserve music, ambient noise, etc. from input audio. \
|
75 |
-
However, to mitigate misuse of this technology, we limit the audio history prompts to a limited set of Suno-provided, fully synthetic options to choose from.
|
76 |
-
## 👥 Speaker Prompts
|
77 |
-
You can provide certain speaker prompts such as NARRATOR, MAN, WOMAN, etc. \
|
78 |
-
Please note that these are not always respected, especially if a conflicting audio history prompt is given.
|
79 |
-
Try the prompt:
|
80 |
-
```
|
81 |
-
WOMAN: I would like an oatmilk latte please.
|
82 |
-
MAN: Wow, that's expensive!
|
83 |
-
```
|
84 |
-
## Details
|
85 |
-
Bark model by [Suno](https://suno.ai/), including official [code](https://github.com/suno-ai/bark) and model weights. \
|
86 |
-
Gradio demo supported by 🤗 Hugging Face. Bark is licensed under a non-commercial license: CC-BY 4.0 NC, see details on [GitHub](https://github.com/suno-ai/bark).
|
87 |
-
"""
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
["Buenos días Miguel. Tu colega piensa que tu alemán es extremadamente malo. But I suppose your english isn't terrible.",
|
95 |
-
"Speaker 0 (es)"], # , 0.7, 0.7],
|
96 |
-
]
|
97 |
-
|
98 |
-
|
99 |
-
def gen_tts(text, history_prompt): # , temp_semantic, temp_waveform):
|
100 |
-
history_prompt = PROMPT_LOOKUP[history_prompt]
|
101 |
-
if DEBUG_MODE:
|
102 |
-
audio_arr = np.zeros(SAMPLE_RATE)
|
103 |
-
else:
|
104 |
-
# , text_temp=temp_semantic, waveform_temp=temp_waveform)
|
105 |
-
audio_arr = generate_audio(text, history_prompt=history_prompt)
|
106 |
-
audio_arr = (audio_arr * 32767).astype(np.int16)
|
107 |
-
return (SAMPLE_RATE, audio_arr)
|
108 |
-
|
109 |
-
|
110 |
-
css = """
|
111 |
-
#share-btn-container {
|
112 |
-
display: flex;
|
113 |
-
padding-left: 0.5rem !important;
|
114 |
-
padding-right: 0.5rem !important;
|
115 |
-
background-color: #000000;
|
116 |
-
justify-content: center;
|
117 |
-
align-items: center;
|
118 |
-
border-radius: 9999px !important;
|
119 |
-
width: 13rem;
|
120 |
-
margin-top: 10px;
|
121 |
-
margin-left: auto;
|
122 |
-
flex: unset !important;
|
123 |
-
}
|
124 |
-
#share-btn {
|
125 |
-
all: initial;
|
126 |
-
color: #ffffff;
|
127 |
-
font-weight: 600;
|
128 |
-
cursor: pointer;
|
129 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
130 |
-
margin-left: 0.5rem !important;
|
131 |
-
padding-top: 0.25rem !important;
|
132 |
-
padding-bottom: 0.25rem !important;
|
133 |
-
right:0;
|
134 |
-
}
|
135 |
-
#share-btn * {
|
136 |
-
all: unset !important;
|
137 |
-
}
|
138 |
-
#share-btn-container div:nth-child(-n+2){
|
139 |
-
width: auto !important;
|
140 |
-
min-height: 0px !important;
|
141 |
-
}
|
142 |
-
#share-btn-container .wrap {
|
143 |
-
display: none !important;
|
144 |
-
}
|
145 |
-
"""
|
146 |
-
with gr.Blocks(css=css) as block:
|
147 |
-
gr.Markdown(title)
|
148 |
-
gr.Markdown(description)
|
149 |
-
with gr.Row():
|
150 |
-
with gr.Column():
|
151 |
-
input_text = gr.Textbox(
|
152 |
-
label="Input Text", lines=2, value=default_text, elem_id="input_text")
|
153 |
-
options = gr.Dropdown(
|
154 |
-
AVAILABLE_PROMPTS, value="Speaker 1 (en)", label="Acoustic Prompt", elem_id="speaker_option")
|
155 |
-
run_button = gr.Button(text="Generate Audio", type="button")
|
156 |
-
with gr.Column():
|
157 |
-
audio_out = gr.Audio(label="Generated Audio",
|
158 |
-
type="numpy", elem_id="audio_out")
|
159 |
-
with gr.Row(visible=False) as share_row:
|
160 |
-
with gr.Group(elem_id="share-btn-container"):
|
161 |
-
community_icon = gr.HTML(community_icon_html)
|
162 |
-
loading_icon = gr.HTML(loading_icon_html)
|
163 |
-
share_button = gr.Button(
|
164 |
-
"Share to community", elem_id="share-btn")
|
165 |
-
share_button.click(None, [], [], _js=share_js)
|
166 |
-
inputs = [input_text, options]
|
167 |
-
outputs = [audio_out]
|
168 |
-
gr.Examples(examples=examples, fn=gen_tts, inputs=inputs,
|
169 |
-
outputs=outputs, cache_examples=True)
|
170 |
-
gr.Markdown(article)
|
171 |
-
run_button.click(fn=lambda: gr.update(visible=False), inputs=None, outputs=share_row, queue=False).then(
|
172 |
-
fn=gen_tts, inputs=inputs, outputs=outputs, queue=True).then(
|
173 |
-
fn=lambda: gr.update(visible=True), inputs=None, outputs=share_row, queue=False)
|
174 |
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Prepare the Shakespeare dataset for character-level language modeling.
|
3 |
+
So instead of encoding with GPT-2 BPE tokens, we just map characters to ints.
|
4 |
+
Will save train.bin, val.bin containing the ids, and meta.pkl containing the
|
5 |
+
encoder and decoder and some other related info.
|
6 |
+
"""
|
7 |
+
import os
|
8 |
+
import pickle
|
9 |
+
import requests
|
10 |
import numpy as np
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# download the tiny shakespeare dataset
|
13 |
+
input_file_path = os.path.join(os.path.dirname(__file__), 'input.txt')
|
14 |
+
if not os.path.exists(input_file_path):
|
15 |
+
data_url = 'https://raw.githubusercontent.com/karpathy/char-rnn/master/data/tinyshakespeare/input.txt'
|
16 |
+
with open(input_file_path, 'w') as f:
|
17 |
+
f.write(requests.get(data_url).text)
|
18 |
|
19 |
+
with open(input_file_path, 'r') as f:
|
20 |
+
data = f.read()
|
21 |
+
print(f"length of dataset in characters: {len(data):,}")
|
22 |
|
23 |
+
# get all the unique characters that occur in this text
|
24 |
+
chars = sorted(list(set(data)))
|
25 |
+
vocab_size = len(chars)
|
26 |
+
print("all the unique characters:", ''.join(chars))
|
27 |
+
print(f"vocab size: {vocab_size:,}")
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# create a mapping from characters to integers
|
30 |
+
stoi = { ch:i for i,ch in enumerate(chars) }
|
31 |
+
itos = { i:ch for i,ch in enumerate(chars) }
|
32 |
+
def encode(s):
|
33 |
+
return [stoi[c] for c in s] # encoder: take a string, output a list of integers
|
34 |
+
def decode(l):
|
35 |
+
return ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string
|
36 |
|
37 |
+
# create the train and test splits
|
38 |
+
n = len(data)
|
39 |
+
train_data = data[:int(n*0.9)]
|
40 |
+
val_data = data[int(n*0.9):]
|
41 |
|
42 |
+
# encode both to integers
|
43 |
+
train_ids = encode(train_data)
|
44 |
+
val_ids = encode(val_data)
|
45 |
+
print(f"train has {len(train_ids):,} tokens")
|
46 |
+
print(f"val has {len(val_ids):,} tokens")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
+
# export to bin files
|
49 |
+
train_ids = np.array(train_ids, dtype=np.uint16)
|
50 |
+
val_ids = np.array(val_ids, dtype=np.uint16)
|
51 |
+
train_ids.tofile(os.path.join(os.path.dirname(__file__), 'train.bin'))
|
52 |
+
val_ids.tofile(os.path.join(os.path.dirname(__file__), 'val.bin'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
# save the meta information as well, to help us encode/decode later
|
55 |
+
meta = {
|
56 |
+
'vocab_size': vocab_size,
|
57 |
+
'itos': itos,
|
58 |
+
'stoi': stoi,
|
59 |
+
}
|
60 |
+
with open(os.path.join(os.path.dirname(__file__), 'meta.pkl'), 'wb') as f:
|
61 |
+
pickle.dump(meta, f)
|
62 |
|
63 |
+
# length of dataset in characters: 1115394
|
64 |
+
# all the unique characters:
|
65 |
+
# !$&',-.3:;?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
|
66 |
+
# vocab size: 65
|
67 |
+
# train has 1003854 tokens
|
68 |
+
# val has 111540 tokens
|