Spaces:
Running
Running
File size: 2,084 Bytes
a6bbc99 907a50a a6bbc99 907a50a a6bbc99 907a50a a6bbc99 |
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 |
import gradio as gr
from transformers import AutoProcessor, BarkModel
import torch
import scipy
# Limit CPU usage
torch.set_num_threads(1)
# Load the Bark model and processor
processor = AutoProcessor.from_pretrained("suno/bark-small")
model = BarkModel.from_pretrained("suno/bark-small")
# Function to generate speech
def generate_speech(text, voice_preset):
# Process the input text with the selected voice preset
inputs = processor(text, voice_preset=voice_preset)
# Generate audio and convert to float32 early to optimize memory usage
with torch.no_grad(): # Disable gradient calculations for faster inference
audio_array = model.generate(**inputs)
audio_array = audio_array.cpu().numpy().astype('float32').squeeze() # Converting early
# Return the audio with sample rate for Gradio's audio component
return (model.generation_config.sample_rate, audio_array)
# Gradio app setup
with gr.Blocks() as app:
gr.Markdown("# Turkish Text-to-Speech with Bark")
gr.Markdown("Enter text, select a Turkish voice preset, and click 'Generate Voice' to play the generated audio.")
# Input text box for user to type text
text_input = gr.Textbox(label="Enter Text in Turkish", placeholder="Merhaba, bugün bir yerlere gidelim mi?")
# Dropdown for selecting voice preset
voice_preset_input = gr.Dropdown(
["v2/tr_speaker_0", "v2/tr_speaker_1", "v2/tr_speaker_2", "v2/tr_speaker_3",
"v2/tr_speaker_4", "v2/tr_speaker_5", "v2/tr_speaker_6",
"v2/tr_speaker_7", "v2/tr_speaker_8", "v2/tr_speaker_9"],
label="Select Turkish Voice Preset"
)
# Audio output component for playing generated audio
audio_output = gr.Audio(label="Generated Voice", type="numpy")
# Button to trigger the generation
generate_button = gr.Button("Generate Voice")
# When the button is clicked, call the generate_speech function
generate_button.click(generate_speech, inputs=[text_input, voice_preset_input], outputs=audio_output)
# Launch the Gradio app
app.launch()
|