💡 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
Add encoding parameter to TextToSpeech pipeline, closes #885
davidmezzetti679e0cef8921
diff --git a/src/python/txtai/pipeline/audio/texttospeech.py b/src/python/txtai/pipeline/audio/texttospeech.pyindex 0aaba5ef..98c84713 100644--- a/src/python/txtai/pipeline/audio/texttospeech.py+++ b/src/python/txtai/pipeline/audio/texttospeech.py@@ -2,11 +2,10 @@TextToSpeech module"""-import logging-# Conditional importtry:import onnxruntime as ort+ import soundfile as sffrom ttstokenizer import IPATokenizer, TTSTokenizer@@ -17,6 +16,10 @@ except ImportError:TTS = Falseimport json+import logging++from io import BytesIO+import torchimport yaml@@ -64,7 +67,7 @@ class TextToSpeech(Pipeline):else:self.pipeline = SpeechT5(path, maxtokens, self.providers())- def __call__(self, text, stream=False, speaker=1, **kwargs):+ def __call__(self, text, stream=False, speaker=1, encoding=None, **kwargs):"""Generates speech from text. Text longer than maxtokens will be batched and returnedas a single waveform per text input.@@ -76,10 +79,11 @@ class TextToSpeech(Pipeline):text: text|liststream: stream response if True, defaults to Falsespeaker: speaker id, defaults to 1+ encoding: optional audio encoding formatkwargs: additional keyword argsReturns:- list of (audio, sample rate)+ list of (audio, sample rate) or list of audio depending on encoding parameter"""# Convert results to a list if necessary@@ -87,10 +91,10 @@ class TextToSpeech(Pipeline):# Streaming responseif stream:- return self.stream(texts, speaker)+ return self.stream(texts, speaker, encoding)# Transform text to speech- results = [self.execute(x, speaker, **kwargs) for x in texts]+ results = [self.execute(x, speaker, encoding, **kwargs) for x in texts]# Return resultsreturn results[0] if isinstance(text, str) else results@@ -132,7 +136,7 @@ class TextToSpeech(Pipeline):return exists- def stream(self, texts, speaker):+ def stream(self, texts, speaker, encoding):"""Iterates over texts, splits into segments and yields snippets of audio.This method is designed to integrate with streaming LLM generation.@@ -140,9 +144,10 @@ class TextToSpeech(Pipeline):Args:texts: list of input textsspeaker: speaker id+ encoding: audio encoding formatReturns:- snippets of audio as NumPy arrays+ snippets of audio as NumPy arrays or audio bytes depending on fmt parameter"""buffer = []@@ -151,13 +156,13 @@ class TextToSpeech(Pipeline):if x == "\n" or (x.strip().endswith(".") and len([y for y in buffer if y]) > 2):data, buffer = "".join(buffer), []- yield self.execute(data, speaker)+ yield self.execute(data, speaker, encoding)if buffer:data = "".join(buffer)- yield self.execute(data, speaker)+ yield self.execute(data, speaker, encoding)- def execute(self, text, speaker, **kwargs):+ def execute(self, text, speaker, encoding, **kwargs):"""Executes model run for an input array of tokens. This method will build batchesof tokens when len(tokens) > maxtokens.@@ -165,17 +170,27 @@ class TextToSpeech(Pipeline):Args:text: text to tokenize and pass to modelspeaker: speaker id+ encoding: audio encoding formatkwargs: additional keyword argsReturns:- (audio, sample rate)+ (audio, sample rate) or audio bytes depending on fmt parameter"""# Run pipeline modelaudio, rate = self.pipeline(text, speaker, **kwargs)# Resample, if necessary and return- return (Signal.resample(audio, rate, self.rate), self.rate) if self.rate else (audio, rate)+ audio, rate = (Signal.resample(audio, rate, self.rate), self.rate) if self.rate else (audio, rate)++ # Encoding audio data+ if encoding:+ data = BytesIO()+ sf.write(data, audio, rate, format=encoding)+ return data.getvalue()++ # Default to (audio, rate) tuple+ return (audio, rate)class SpeechPipeline(Pipeline):