💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Update TextToSpeech pipeline to support speaker parameter, closes #787
davidmezzetti05fe040e6814
diff --git a/src/python/txtai/pipeline/audio/texttospeech.py b/src/python/txtai/pipeline/audio/texttospeech.pyindex e85dffa0..b7e73caf 100644--- a/src/python/txtai/pipeline/audio/texttospeech.py+++ b/src/python/txtai/pipeline/audio/texttospeech.py@@ -63,7 +63,10 @@ class TextToSpeech(Pipeline):# Get model input name, typically "text"self.input = self.model.get_inputs()[0].name- def __call__(self, text, stream=False):+ # Get parameter names+ self.params = set(x.name for x in self.model.get_inputs())++ def __call__(self, text, stream=False, speaker=1):"""Generates speech from text. Text longer than maxtokens will be batched and returnedas a single waveform per text input.@@ -74,6 +77,7 @@ class TextToSpeech(Pipeline):Args:text: text|liststream: stream response if True, defaults to False+ speaker: speaker id, defaults to 1Returns:list of speech as NumPy array waveforms@@ -84,10 +88,10 @@ class TextToSpeech(Pipeline):# Streaming responseif stream:- return self.stream(texts)+ return self.stream(texts, speaker)# Transform text to speech- outputs = [self.execute(x) for x in texts]+ outputs = [self.execute(x, speaker) for x in texts]# Return resultsreturn outputs[0] if isinstance(text, str) else outputs@@ -108,13 +112,14 @@ class TextToSpeech(Pipeline):# Default when CUDA provider isn't availablereturn ["CPUExecutionProvider"]- def stream(self, texts):+ def stream(self, texts, speaker):"""Iterates over texts, splits into segments and yields snippets of audio.This method is designed to integrate with streaming LLM generation.Args:texts: list of input texts+ speaker: speaker idReturns:snippets of audio as NumPy arrays@@ -126,19 +131,20 @@ class TextToSpeech(Pipeline):if x == "\n" or x.strip().endswith("."):data, buffer = "".join(buffer), []- yield self.execute(data)+ yield self.execute(data, speaker)if buffer:data = "".join(buffer)- yield self.execute(data)+ yield self.execute(data, speaker)- def execute(self, text):+ def execute(self, text, speaker):"""Executes model run for an input array of tokens. This method will build batchesof tokens when len(tokens) > maxtokens.Args:text: text to tokenize and pass to model+ speaker: speaker idReturns:waveform as NumPy array@@ -150,8 +156,12 @@ class TextToSpeech(Pipeline):# Split into batches and processresults = []for x in self.batch(tokens, self.maxtokens):+ # Format input parameters+ params = {self.input: x}+ params = {**params, **{"sids": np.array([speaker])}} if "sids" in self.params else params+# Run text through TTS model and save waveform- output = self.model.run(None, {self.input: x})+ output = self.model.run(None, params)results.append(output[0])# Concatenate results and return