sanchit-gandhi commited on
Commit
19020ff
·
1 Parent(s): 6ee69d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -7
app.py CHANGED
@@ -1,21 +1,38 @@
1
  import gradio as gr
2
  import numpy as np
3
- from bark import SAMPLE_RATE, generate_audio
 
4
 
5
- def predict(text_prompt):
6
  if len(text_prompt.strip()) == 0:
7
  return (16000, np.zeros(0).astype(np.int16))
8
 
9
- audio_array = audio_array = generate_audio(text_prompt)
10
  audio_array = (audio_array * 32767).astype(np.int16)
11
  return (SAMPLE_RATE, audio_array)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  title = "🐶 Bark"
15
 
16
  description = """
17
  Bark is a transformer-based text-to-audio model created by [Suno](https://suno.ai/). 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.
18
- """
 
19
 
20
  article = """
21
 
@@ -64,11 +81,18 @@ Try the prompt:
64
  You can provide certain speaker prompts such as NARRATOR, MAN, WOMAN, etc. Please note that these are not always respected, especially if a conflicting audio history prompt is given.
65
 
66
  Try the prompt:
 
67
  ```
68
  WOMAN: I would like an oatmilk latte please.
69
  MAN: Wow, that's expensive!
70
  ```
71
 
 
 
 
 
 
 
72
  ## Details
73
 
74
  Bark model by [Suno](https://suno.ai/), including official [code](https://github.com/suno-ai/bark/tree/main) and model weights. Gradio demo by 🤗 Hugging Face. Bark is licensed under a non-commercial license: CC-BY 4.0 NC.
@@ -82,8 +106,16 @@ examples = [
82
  ["WOMAN: I would like an oatmilk latte please. MAN: Wow, that's expensive!"],
83
  ]
84
 
85
- gr.Interface(
86
- fn=predict,
 
 
 
 
 
 
 
 
87
  inputs=[
88
  gr.Text(label="Input Text"),
89
  ],
@@ -94,4 +126,27 @@ gr.Interface(
94
  description=description,
95
  article=article,
96
  examples=examples,
97
- ).launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
+ import os
4
+ from bark import SAMPLE_RATE, generate_audio, semantic_to_waveform
5
 
6
+ def predict_without_prompt(text_prompt):
7
  if len(text_prompt.strip()) == 0:
8
  return (16000, np.zeros(0).astype(np.int16))
9
 
10
+ audio_array = generate_audio(text_prompt)
11
  audio_array = (audio_array * 32767).astype(np.int16)
12
  return (SAMPLE_RATE, audio_array)
13
 
14
+ def predict_with_prompt(text_prompt, speaker_prompt):
15
+ if len(text_prompt.strip()) == 0:
16
+ return (16000, np.zeros(0).astype(np.int16))
17
+
18
+ prompt_path = os.path.join(os.getcwd(), "bark", "assets", "prompts", f"speech_{speaker_prompt}.npz")
19
+ semantic_history = np.load(prompt_path)["semantic_prompt"]
20
+
21
+ prompt_array = semantic_to_waveform(semantic_history)
22
+ prompt_array = (prompt_array * 32767).astype(np.int16)
23
+
24
+ audio_array = generate_audio(text_prompt, history_prompt=f"speech_{speaker_prompt}")
25
+ audio_array = (audio_array * 32767).astype(np.int16)
26
+
27
+ return (SAMPLE_RATE, prompt_array), (SAMPLE_RATE, audio_array)
28
+
29
 
30
  title = "🐶 Bark"
31
 
32
  description = """
33
  Bark is a transformer-based text-to-audio model created by [Suno](https://suno.ai/). 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.
34
+
35
+ Bark has the capability to fully clone voices - including tone, pitch, emotion and prosody. The model also attempts to preserve music, ambient noise, etc. from the input audio prompt. However, to mitigate misuse of this technology, we limit the audio history prompts to a limited set of Suno-provided, fully synthetic options."""
36
 
37
  article = """
38
 
 
81
  You can provide certain speaker prompts such as NARRATOR, MAN, WOMAN, etc. Please note that these are not always respected, especially if a conflicting audio history prompt is given.
82
 
83
  Try the prompt:
84
+
85
  ```
86
  WOMAN: I would like an oatmilk latte please.
87
  MAN: Wow, that's expensive!
88
  ```
89
 
90
+ ## 🧬 Voice Cloning
91
+
92
+ Bark has the capability to fully clone voices - including tone, pitch, emotion and prosody. The model also attempts to preserve music, ambient noise, etc. from input audio. 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.
93
+
94
+ Voice cloning can be trialled using the demo tab "Text Prompt + Voice Clone". The slider bar is used to select the speaker prompt index, which ranges from 0-7 (8 possible prompts).
95
+
96
  ## Details
97
 
98
  Bark model by [Suno](https://suno.ai/), including official [code](https://github.com/suno-ai/bark/tree/main) and model weights. Gradio demo by 🤗 Hugging Face. Bark is licensed under a non-commercial license: CC-BY 4.0 NC.
 
106
  ["WOMAN: I would like an oatmilk latte please. MAN: Wow, that's expensive!"],
107
  ]
108
 
109
+ examples_with_speaker_prompt = [
110
+ ["Hello, my name is Suno. And, uh — and I like pizza. [laughs] But I also have other interests such as playing tic tac toe.", 0],
111
+ ["Hello, my name is Suno. And, uh — and I like pizza. [laughs] But I also have other interests such as playing tic tac toe.", 1],
112
+ ["Buenos días Miguel. Tu colega piensa que tu alemán es extremadamente malo. But I suppose your english isn't terrible.", 2],
113
+ ["Buenos días Miguel. Tu colega piensa que tu alemán es extremadamente malo. But I suppose your english isn't terrible.", 3],
114
+ ]
115
+
116
+
117
+ unprompted = gr.Interface(
118
+ fn=predict_without_prompt,
119
  inputs=[
120
  gr.Text(label="Input Text"),
121
  ],
 
126
  description=description,
127
  article=article,
128
  examples=examples,
129
+ )
130
+
131
+ prompted = gr.Interface(
132
+ fn=predict_with_prompt,
133
+ inputs=[
134
+ gr.Text(label="Input Text"),
135
+ gr.Slider(0, 7, value=0, step=1, label="Speaker Prompt Index"),
136
+ ],
137
+ outputs=[
138
+ gr.Audio(label="Speaker Prompt", type="numpy"),
139
+ gr.Audio(label="Generated Speech", type="numpy"),
140
+ ],
141
+ title=title,
142
+ description=description,
143
+ article=article,
144
+ examples=examples_with_speaker_prompt,
145
+ )
146
+
147
+ demo = gr.Blocks()
148
+
149
+ with demo:
150
+ gr.TabbedInterface([unprompted, prompted], ["Text Prompt", "Text Prompt + Voice Clone"])
151
+
152
+ demo.launch(enable_queue=True)