Skip to content

Base text to speech driver

BaseTextToSpeechDriver

Bases: SerializableMixin, ExponentialBackoffMixin, ABC

Source code in griptape/drivers/text_to_speech/base_text_to_speech_driver.py
@define
class BaseTextToSpeechDriver(SerializableMixin, ExponentialBackoffMixin, ABC):
    model: str = field(kw_only=True, metadata={"serializable": True})
    structure: Optional[Structure] = field(default=None, kw_only=True)

    def before_run(self, prompts: list[str]) -> None:
        if self.structure:
            self.structure.publish_event(StartTextToSpeechEvent(prompts=prompts))

    def after_run(self) -> None:
        if self.structure:
            self.structure.publish_event(FinishTextToSpeechEvent())

    def run_text_to_audio(self, prompts: list[str]) -> AudioArtifact:
        for attempt in self.retrying():
            with attempt:
                self.before_run(prompts)
                result = self.try_text_to_audio(prompts)
                self.after_run()

                return result

        else:
            raise Exception("Failed to run text to audio generation")

    @abstractmethod
    def try_text_to_audio(self, prompts: list[str]) -> AudioArtifact: ...

model: str = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute

structure: Optional[Structure] = field(default=None, kw_only=True) class-attribute instance-attribute

after_run()

Source code in griptape/drivers/text_to_speech/base_text_to_speech_driver.py
def after_run(self) -> None:
    if self.structure:
        self.structure.publish_event(FinishTextToSpeechEvent())

before_run(prompts)

Source code in griptape/drivers/text_to_speech/base_text_to_speech_driver.py
def before_run(self, prompts: list[str]) -> None:
    if self.structure:
        self.structure.publish_event(StartTextToSpeechEvent(prompts=prompts))

run_text_to_audio(prompts)

Source code in griptape/drivers/text_to_speech/base_text_to_speech_driver.py
def run_text_to_audio(self, prompts: list[str]) -> AudioArtifact:
    for attempt in self.retrying():
        with attempt:
            self.before_run(prompts)
            result = self.try_text_to_audio(prompts)
            self.after_run()

            return result

    else:
        raise Exception("Failed to run text to audio generation")

try_text_to_audio(prompts) abstractmethod

Source code in griptape/drivers/text_to_speech/base_text_to_speech_driver.py
@abstractmethod
def try_text_to_audio(self, prompts: list[str]) -> AudioArtifact: ...