Spaces:
Runtime error
Runtime error
File size: 2,290 Bytes
7001f7d 3e166ec 7001f7d 3e166ec 7001f7d 3e166ec 7001f7d 3e166ec 7001f7d 3e166ec 7001f7d 3e166ec 7001f7d |
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 |
import torch
from parler_tts import ParlerTTSForConditionalGeneration
from transformers import AutoTokenizer
import gradio as gr
import numpy as np
# Set device to GPU if available, else CPU
device = "cuda:0" if torch.cuda.is_available() else "cpu"
# Load the TTS model and tokenizers
model = ParlerTTSForConditionalGeneration.from_pretrained("ai4bharat/indic-parler-tts").to(device)
tokenizer = AutoTokenizer.from_pretrained("ai4bharat/indic-parler-tts")
description_tokenizer = AutoTokenizer.from_pretrained(model.config.text_encoder._name_or_path)
def generate_audio(text: str):
"""
Generate synthesized speech audio based on the input text.
Args:
text (str): The text prompt to be spoken.
Returns:
tuple: A tuple containing the audio numpy array and the sampling rate.
"""
# Set a default voice description
default_description = ("Divya's voice is monotone yet slightly fast in delivery, with a very close recording "
"that almost has no background noise.")
# Tokenize the default description and the input text
description_tokens = description_tokenizer(default_description, return_tensors="pt").to(device)
prompt_tokens = tokenizer(text, return_tensors="pt").to(device)
# Generate the audio tensor using the model
generation = model.generate(
input_ids=description_tokens.input_ids,
attention_mask=description_tokens.attention_mask,
prompt_input_ids=prompt_tokens.input_ids,
prompt_attention_mask=prompt_tokens.attention_mask
)
# Convert the generated tensor to a numpy array and remove extra dimensions
audio_arr = generation.cpu().numpy().squeeze()
# Retrieve the sampling rate from the model config
sampling_rate = model.config.sampling_rate
return (audio_arr, sampling_rate)
# Build the Gradio interface with a single text input
iface = gr.Interface(
fn=generate_audio,
inputs=gr.Textbox(label="Enter Text", value="เค
เคฐเฅ, เคคเฅเคฎ เคเค เคเฅเคธเฅ เคนเฅ?"),
outputs=gr.Audio(label="Generated Audio"),
title="Indic Parler TTS",
description="Generate synthesized speech using the Indic Parler TTS model from ai4bharat."
)
if __name__ == "__main__":
iface.launch()
|