Spaces:
Sleeping
Sleeping
File size: 2,722 Bytes
9e563bf 9638295 |
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 |
import gradio as gr
from bark import SAMPLE_RATE, generate_audio, preload_models
from scipy.io.wavfile import write as write_wav
import tempfile
import os
# Download and load all models
preload_models()
# Define a function to generate audio from text
def generate_audio_from_text(text_prompt, language_prompt, gender_prompt):
if language_prompt == "english":
if gender_prompt == "male":
history_prompt = "v2/en_speaker_6"
else:
history_prompt = "v2/en_speaker_9"
elif language_prompt == "french":
if gender_prompt == "male":
history_prompt = "v2/fr_speaker_0"
else:
history_prompt = "v2/fr_speaker_1"
elif language_prompt =="german":
if gender_prompt=="male":
history_prompt = "v2/de_speaker_2"
else:
history_prompt="v2/de_speaker_3"
elif language_prompt =="hindi":
if gender_prompt=="male":
history_prompt = "v2/hi_speaker_8"
else:
history_prompt="v2/hi_speaker_3"
elif language_prompt =="chinese":
if gender_prompt=="male":
history_prompt = "v2/zh_speaker_1"
else:
history_prompt="v2/zh_speaker_4"
elif language_prompt =="italian":
if gender_prompt=="male":
history_prompt = "v2/it_speaker_4"
else:
history_prompt="v2/it_speaker_7"
elif language_prompt =="japanese":
if gender_prompt=="male":
history_prompt = "v2/ja_speaker_2"
else:
history_prompt="v2/ja_speaker_0"
else:
raise ValueError("Invalid language or gender selection")
audio_array = generate_audio(text_prompt, history_prompt)
# Create a temporary WAV file
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
temp_wav_path = temp_wav.name
write_wav(temp_wav_path, SAMPLE_RATE, audio_array)
return temp_wav_path
# Define lists of language and gender options
language_options = [
"english",
"french",
"german",
"hindi",
"chinese",
"italian",
"japanese",
]
gender_options = [
"male",
"female",
]
# 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=gender_options, label="Select gender:"),
],
outputs=gr.outputs.File(label="Download WAV File"),
title="Text-to-Speech App Vertical Solution",
)
# Launch the Gradio app with sharing enabled
iface.launch(debug=True)
|