Spaces:
Runtime error
Runtime error
File size: 8,767 Bytes
bcc6ddf |
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import os
import scipy
import nltk
import tempfile
import numpy as np
from bark.generation import preload_models, SAMPLE_RATE
from bark import generate_audio
from scipy.io import wavfile
import gradio as gr
nltk.download('punkt')
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
preload_models()
def generate_audio_from_text(text,language_prompt,speaker_prompt):
if language_prompt == "english":
if speaker_prompt=="speaker 1":
history_prompt = "v2/en_speaker_0"
elif speaker_prompt=="speaker 2":
history_prompt = "v2/en_speaker_1"
elif speaker_prompt=="speaker 3":
history_prompt = "v2/en_speaker_2"
elif speaker_prompt=="speaker 4":
history_prompt = "v2/en_speaker_3"
elif speaker_prompt=="speaker 5":
history_prompt = "v2/en_speaker_4"
elif speaker_prompt=="speaker 6":
history_prompt = "v2/en_speaker_5"
elif speaker_prompt=="speaker 7":
history_prompt = "v2/en_speaker_6"
elif speaker_prompt=="speaker 8":
history_prompt = "v2/en_speaker_7"
elif speaker_prompt=="speaker 9":
history_prompt = "v2/en_speaker_8"
elif speaker_prompt=="speaker 10":
history_prompt = "v2/en_speaker_9"
else:
history_prompt = "v2/en_speaker_9"
elif language_prompt == "french":
if speaker_prompt=="speaker 1":
history_prompt = "v2/fr_speaker_0"
elif speaker_prompt=="speaker 2":
history_prompt = "v2/fr_speaker_1"
elif speaker_prompt=="speaker 3":
history_prompt = "v2/fr_speaker_2"
elif speaker_prompt=="speaker 4":
history_prompt = "v2/fr_speaker_3"
elif speaker_prompt=="speaker 5":
history_prompt = "v2/fr_speaker_4"
elif speaker_prompt=="speaker 6":
history_prompt = "v2/fr_speaker_5"
elif speaker_prompt=="speaker 7":
history_prompt = "v2/fr_speaker_6"
elif speaker_prompt=="speaker 8":
history_prompt = "v2/fr_speaker_7"
elif speaker_prompt=="speaker 9":
history_prompt = "v2/fr_speaker_8"
elif speaker_prompt=="speaker 10":
history_prompt = "v2/fr_speaker_9"
else:
history_prompt = "v2/fr_speaker_9"
elif language_prompt =="german":
if speaker_prompt=="speaker 1":
history_prompt = "v2/de_speaker_0"
elif speaker_prompt=="speaker 2":
history_prompt = "v2/de_speaker_1"
elif speaker_prompt=="speaker 3":
history_prompt = "v2/de_speaker_2"
elif speaker_prompt=="speaker 4":
history_prompt = "v2/de_speaker_3"
elif speaker_prompt=="speaker 5":
history_prompt = "v2/de_speaker_4"
elif speaker_prompt=="speaker 6":
history_prompt = "v2/de_speaker_5"
elif speaker_prompt=="speaker 7":
history_prompt = "v2/de_speaker_6"
elif speaker_prompt=="speaker 8":
history_prompt = "v2/de_speaker_7"
elif speaker_prompt=="speaker 9":
history_prompt = "v2/de_speaker_8"
elif speaker_prompt=="speaker 10":
history_prompt = "v2/de_speaker_9"
else:
history_prompt = "v2/de_speaker_9"
elif language_prompt =="hindi":
if speaker_prompt=="speaker 1":
history_prompt = "v2/hi_speaker_0"
elif speaker_prompt=="speaker 2":
history_prompt = "v2/hi_speaker_1"
elif speaker_prompt=="speaker 3":
history_prompt = "v2/hi_speaker_2"
elif speaker_prompt=="speaker 4":
history_prompt = "v2/hi_speaker_3"
elif speaker_prompt=="speaker 5":
history_prompt = "v2/hi_speaker_4"
elif speaker_prompt=="speaker 6":
history_prompt = "v2/hi_speaker_5"
elif speaker_prompt=="speaker 7":
history_prompt = "v2/hi_speaker_6"
elif speaker_prompt=="speaker 8":
history_prompt = "v2/hi_speaker_7"
elif speaker_prompt=="speaker 9":
history_prompt = "v2/hi_speaker_8"
elif speaker_prompt=="speaker 10":
history_prompt = "v2/hi_speaker_9"
else:
history_prompt = "v2/hi_speaker_9"
elif language_prompt =="chinese":
if speaker_prompt=="speaker 1":
history_prompt = "v2/zh_speaker_0"
elif speaker_prompt=="speaker 2":
history_prompt = "v2/zh_speaker_1"
elif speaker_prompt=="speaker 3":
history_prompt = "v2/zh_speaker_2"
elif speaker_prompt=="speaker 4":
history_prompt = "v2/zh_speaker_3"
elif speaker_prompt=="speaker 5":
history_prompt = "v2/zh_speaker_4"
elif speaker_prompt=="speaker 6":
history_prompt = "v2/zh_speaker_5"
elif speaker_prompt=="speaker 7":
history_prompt = "v2/zh_speaker_6"
elif speaker_prompt=="speaker 8":
history_prompt = "v2/zh_speaker_7"
elif speaker_prompt=="speaker 9":
history_prompt = "v2/zh_speaker_8"
elif speaker_prompt=="speaker 10":
history_prompt = "v2/zh_speaker_9"
else:
history_prompt = "v2/zh_speaker_9"
elif language_prompt =="italian":
if speaker_prompt=="speaker 1":
history_prompt = "v2/it_speaker_0"
elif speaker_prompt=="speaker 2":
history_prompt = "v2/it_speaker_1"
elif speaker_prompt=="speaker 3":
history_prompt = "v2/it_speaker_2"
elif speaker_prompt=="speaker 4":
history_prompt = "v2/it_speaker_3"
elif speaker_prompt=="speaker 5":
history_prompt = "v2/it_speaker_4"
elif speaker_prompt=="speaker 6":
history_prompt = "v2/it_speaker_5"
elif speaker_prompt=="speaker 7":
history_prompt = "v2/it_speaker_6"
elif speaker_prompt=="speaker 8":
history_prompt = "v2/it_speaker_7"
elif speaker_prompt=="speaker 9":
history_prompt = "v2/it_speaker_8"
elif speaker_prompt=="speaker 10":
history_prompt = "v2/it_speaker_9"
else:
history_prompt = "v2/it_speaker_9"
elif language_prompt =="japanese":
if speaker_prompt=="speaker 1":
history_prompt = "v2/ja_speaker_0"
elif speaker_prompt=="speaker 2":
history_prompt = "v2/ja_speaker_1"
elif speaker_prompt=="speaker 3":
history_prompt = "v2/ja_speaker_2"
elif speaker_prompt=="speaker 4":
history_prompt = "v2/ja_speaker_3"
elif speaker_prompt=="speaker 5":
history_prompt = "v2/ja_speaker_4"
elif speaker_prompt=="speaker 6":
history_prompt = "v2/ja_speaker_5"
elif speaker_prompt=="speaker 7":
history_prompt = "v2/ja_speaker_6"
elif speaker_prompt=="speaker 8":
history_prompt = "v2/ja_speaker_7"
elif speaker_prompt=="speaker 9":
history_prompt = "v2/ja_speaker_8"
elif speaker_prompt=="speaker 10":
history_prompt = "v2/ja_speaker_9"
else:
history_prompt = "v2/ja_speaker_9"
else:
raise ValueError("Invalid language or gender selection")
sentences = nltk.sent_tokenize(text)
silence = np.zeros(int(0.25 * SAMPLE_RATE)) # quarter second of silence
pieces = []
for sentence in sentences:
audio_array = generate_audio(sentence, history_prompt=history_prompt)
pieces += [audio_array]
# Concatenate the audio pieces
final_audio = np.concatenate(pieces)
# Save the audio to a WAV file
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
wavfile.write(temp_wav.name, SAMPLE_RATE, final_audio)
# Return the saved audio file
return temp_wav.name
# Define lists of language and gender options
language_options = [
"english",
"french",
"german",
"hindi",
"chinese",
"italian",
"japanese",
]
speaker_options=[
"speaker 1",
"speaker 2",
"speaker 3",
"speaker 4",
"speaker 5",
"speaker 6",
"speaker 7",
"speaker 8",
"speaker 9",
"speaker 10",
]
# Create a Gradio interface with text input and dropdown menus for language and gender
iface = gr.Interface(
fn=generate_audio_from_text,
inputs=[
gr.Textbox(text="Enter text to convert to speech:"),
gr.Dropdown(choices=language_options, label="Select language:"),
gr.Dropdown(choices=speaker_options, label="Select speaker:"),
],
outputs=gr.outputs.File(label="Download WAV File"),
title="Text-to-Speech App Vertical Solution",
timeout=300,
)
# Launch the Gradio app with sharing enabled
iface.launch(debug=True, enable_queue=True)
|