aboutsummaryrefslogtreecommitdiff
path: root/autogpts/autogpt/autogpt/speech/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'autogpts/autogpt/autogpt/speech/base.py')
-rw-r--r--autogpts/autogpt/autogpt/speech/base.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/autogpts/autogpt/autogpt/speech/base.py b/autogpts/autogpt/autogpt/speech/base.py
new file mode 100644
index 000000000..fd9fda60f
--- /dev/null
+++ b/autogpts/autogpt/autogpt/speech/base.py
@@ -0,0 +1,54 @@
+"""Base class for all voice classes."""
+from __future__ import annotations
+
+import abc
+import re
+from threading import Lock
+
+
+class VoiceBase:
+ """
+ Base class for all voice classes.
+ """
+
+ def __init__(self, *args, **kwargs):
+ """
+ Initialize the voice class.
+ """
+ self._url = None
+ self._headers = None
+ self._api_key = None
+ self._voices = []
+ self._mutex = Lock()
+ self._setup(*args, **kwargs)
+
+ def say(self, text: str, voice_index: int = 0) -> bool:
+ """
+ Say the given text.
+
+ Args:
+ text (str): The text to say.
+ voice_index (int): The index of the voice to use.
+ """
+ text = re.sub(
+ r"\b(?:https?://[-\w_.]+/?\w[-\w_.]*\.(?:[-\w_.]+/?\w[-\w_.]*\.)?[a-z]+(?:/[-\w_.%]+)*\b(?!\.))", # noqa: E501
+ "",
+ text,
+ )
+ with self._mutex:
+ return self._speech(text, voice_index)
+
+ @abc.abstractmethod
+ def _setup(self, *args, **kwargs) -> None:
+ """
+ Setup the voices, API key, etc.
+ """
+
+ @abc.abstractmethod
+ def _speech(self, text: str, voice_index: int = 0) -> bool:
+ """
+ Play the given text.
+
+ Args:
+ text (str): The text to play.
+ """