Skip to content

Text to speech task

TextToSpeechTask

Bases: BaseAudioGenerationTask

Source code in griptape/tasks/text_to_speech_task.py
@define
class TextToSpeechTask(BaseAudioGenerationTask):
    DEFAULT_INPUT_TEMPLATE = "{{ args[0] }}"

    _input: str | TextArtifact | Callable[[BaseTask], TextArtifact] = field(default=DEFAULT_INPUT_TEMPLATE)
    _text_to_speech_engine: TextToSpeechEngine = field(default=None, kw_only=True, alias="text_to_speech_engine")

    @property
    def input(self) -> TextArtifact:
        if isinstance(self._input, TextArtifact):
            return self._input
        elif isinstance(self._input, Callable):
            return self._input(self)
        else:
            return TextArtifact(J2().render_from_string(self._input, **self.full_context))

    @input.setter
    def input(self, value: TextArtifact) -> None:
        self._input = value

    @property
    def text_to_speech_engine(self) -> TextToSpeechEngine:
        if self._text_to_speech_engine is None:
            if self.structure is not None:
                self._text_to_speech_engine = TextToSpeechEngine(
                    text_to_speech_driver=self.structure.config.text_to_speech_driver
                )
            else:
                raise ValueError("Audio Generation Engine is not set.")
        return self._text_to_speech_engine

    @text_to_speech_engine.setter
    def text_to_speech_engine(self, value: TextToSpeechEngine) -> None:
        self._text_to_speech_engine = value

    def run(self) -> AudioArtifact:
        audio_artifact = self.text_to_speech_engine.run(prompts=[self.input.to_text()], rulesets=self.all_rulesets)

        if self.output_dir or self.output_file:
            self._write_to_file(audio_artifact)

        return audio_artifact

DEFAULT_INPUT_TEMPLATE = '{{ args[0] }}' class-attribute instance-attribute

input: TextArtifact property writable

text_to_speech_engine: TextToSpeechEngine property writable

run()

Source code in griptape/tasks/text_to_speech_task.py
def run(self) -> AudioArtifact:
    audio_artifact = self.text_to_speech_engine.run(prompts=[self.input.to_text()], rulesets=self.all_rulesets)

    if self.output_dir or self.output_file:
        self._write_to_file(audio_artifact)

    return audio_artifact