Spaces:
Sleeping
Sleeping
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) | |