autogpt 还原,开新分支编写

This commit is contained in:
w_xiaolizu
2023-05-30 16:38:01 +08:00
parent 548532e522
commit 519df19f43
65 changed files with 3352 additions and 1868 deletions

View File

@ -2,7 +2,7 @@
import abc
from threading import Lock
from autogpt.singleton import AbstractSingleton
from autogpt.config import AbstractSingleton
class VoiceBase(AbstractSingleton):
@ -37,6 +37,7 @@ class VoiceBase(AbstractSingleton):
"""
Setup the voices, API key, etc.
"""
pass
@abc.abstractmethod
def _speech(self, text: str, voice_index: int = 0) -> bool:
@ -46,3 +47,4 @@ class VoiceBase(AbstractSingleton):
Args:
text (str): The text to play.
"""
pass

View File

@ -12,6 +12,7 @@ class BrianSpeech(VoiceBase):
def _setup(self) -> None:
"""Setup the voices, API key, etc."""
pass
def _speech(self, text: str, _: int = 0) -> bool:
"""Speak text using Brian with the streamelements API

View File

@ -4,7 +4,7 @@ import os
import requests
from playsound import playsound
from autogpt.config.config import Config
from autogpt.config import Config
from autogpt.speech.base import VoiceBase
PLACEHOLDERS = {"your-voice-id"}
@ -69,8 +69,6 @@ class ElevenLabsSpeech(VoiceBase):
Returns:
bool: True if the request was successful, False otherwise
"""
from autogpt.logs import logger
tts_url = (
f"https://api.elevenlabs.io/v1/text-to-speech/{self._voices[voice_index]}"
)
@ -83,6 +81,6 @@ class ElevenLabsSpeech(VoiceBase):
os.remove("speech.mpeg")
return True
else:
logger.warn("Request failed with status code:", response.status_code)
logger.info("Response content:", response.content)
print("Request failed with status code:", response.status_code)
print("Response content:", response.content)
return False

View File

@ -20,3 +20,4 @@ class GTTSVoice(VoiceBase):
playsound("speech.mp3", True)
os.remove("speech.mp3")
return True

View File

@ -2,45 +2,45 @@
import threading
from threading import Semaphore
from autogpt.config.config import Config
from autogpt.speech.base import VoiceBase
from autogpt.config import Config
from autogpt.speech.brian import BrianSpeech
from autogpt.speech.eleven_labs import ElevenLabsSpeech
from autogpt.speech.gtts import GTTSVoice
from autogpt.speech.macos_tts import MacOSTTS
_QUEUE_SEMAPHORE = Semaphore(
CFG = Config()
DEFAULT_VOICE_ENGINE = GTTSVoice()
VOICE_ENGINE = None
if CFG.elevenlabs_api_key:
VOICE_ENGINE = ElevenLabsSpeech()
elif CFG.use_mac_os_tts == "True":
VOICE_ENGINE = MacOSTTS()
elif CFG.use_brian_tts == "True":
VOICE_ENGINE = BrianSpeech()
else:
VOICE_ENGINE = GTTSVoice()
QUEUE_SEMAPHORE = Semaphore(
1
) # The amount of sounds to queue before blocking the main thread
def say_text(text: str, voice_index: int = 0) -> None:
"""Speak the given text using the given voice index"""
cfg = Config()
default_voice_engine, voice_engine = _get_voice_engine(cfg)
def speak() -> None:
success = voice_engine.say(text, voice_index)
success = VOICE_ENGINE.say(text, voice_index)
if not success:
default_voice_engine.say(text)
DEFAULT_VOICE_ENGINE.say(text)
_QUEUE_SEMAPHORE.release()
QUEUE_SEMAPHORE.release()
_QUEUE_SEMAPHORE.acquire(True)
QUEUE_SEMAPHORE.acquire(True)
thread = threading.Thread(target=speak)
thread.start()
def _get_voice_engine(config: Config) -> tuple[VoiceBase, VoiceBase]:
"""Get the voice engine to use for the given configuration"""
default_voice_engine = GTTSVoice()
if config.elevenlabs_api_key:
voice_engine = ElevenLabsSpeech()
elif config.use_mac_os_tts == "True":
voice_engine = MacOSTTS()
elif config.use_brian_tts == "True":
voice_engine = BrianSpeech()
else:
voice_engine = GTTSVoice()
return default_voice_engine, voice_engine
if __name__ == '__main__':
say_text('你好呀')