Resolved conflicts in config.py and speak.py

This commit is contained in:
meta-fx
2023-04-11 08:15:58 -05:00
parent 3ee62211db
commit 3cdde2d49c
2 changed files with 40 additions and 27 deletions

View File

@@ -33,7 +33,7 @@ class Config(metaclass=Singleton):
def __init__(self): def __init__(self):
"""Initialize the Config class""" """Initialize the Config class"""
self.debug = False self.debug_mode = False
self.continuous_mode = False self.continuous_mode = False
self.speak_mode = False self.speak_mode = False
@@ -92,9 +92,6 @@ class Config(metaclass=Singleton):
"""Set the speak mode value.""" """Set the speak mode value."""
self.speak_mode = value self.speak_mode = value
def set_debug_mode(self, value: bool):
self.debug_mode = value
def set_fast_llm_model(self, value: str): def set_fast_llm_model(self, value: str):
"""Set the fast LLM model value.""" """Set the fast LLM model value."""
self.fast_llm_model = value self.fast_llm_model = value
@@ -137,4 +134,4 @@ class Config(metaclass=Singleton):
def set_debug_mode(self, value: bool): def set_debug_mode(self, value: bool):
"""Set the debug mode value.""" """Set the debug mode value."""
self.debug = value self.debug_mode = value

View File

@@ -4,6 +4,8 @@ import requests
from config import Config from config import Config
cfg = Config() cfg = Config()
import gtts import gtts
import threading
from threading import Lock, Semaphore
# TODO: Nicer names for these ids # TODO: Nicer names for these ids
@@ -14,6 +16,9 @@ tts_headers = {
"xi-api-key": cfg.elevenlabs_api_key "xi-api-key": cfg.elevenlabs_api_key
} }
mutex_lock = Lock() # Ensure only one sound is played at a time
queue_semaphore = Semaphore(1) # The amount of sounds to queue before blocking the main thread
def eleven_labs_speech(text, voice_index=0): def eleven_labs_speech(text, voice_index=0):
"""Speak text using elevenlabs.io's API""" """Speak text using elevenlabs.io's API"""
tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format( tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format(
@@ -23,9 +28,10 @@ def eleven_labs_speech(text, voice_index=0):
tts_url, headers=tts_headers, json=formatted_message) tts_url, headers=tts_headers, json=formatted_message)
if response.status_code == 200: if response.status_code == 200:
with mutex_lock:
with open("speech.mpeg", "wb") as f: with open("speech.mpeg", "wb") as f:
f.write(response.content) f.write(response.content)
playsound("speech.mpeg") playsound("speech.mpeg", True)
os.remove("speech.mpeg") os.remove("speech.mpeg")
return True return True
else: else:
@@ -39,6 +45,7 @@ def brian_speech(text):
response = requests.get(tts_url) response = requests.get(tts_url)
if response.status_code == 200: if response.status_code == 200:
with mutex_lock:
with open("speech.mp3", "wb") as f: with open("speech.mp3", "wb") as f:
f.write(response.content) f.write(response.content)
playsound("speech.mp3") playsound("speech.mp3")
@@ -51,14 +58,17 @@ def brian_speech(text):
def gtts_speech(text): def gtts_speech(text):
tts = gtts.gTTS(text) tts = gtts.gTTS(text)
with mutex_lock:
tts.save("speech.mp3") tts.save("speech.mp3")
playsound("speech.mp3") playsound("speech.mp3", True)
os.remove("speech.mp3") os.remove("speech.mp3")
def macos_tts_speech(text): def macos_tts_speech(text):
os.system(f'say "{text}"') os.system(f'say "{text}"')
def say_text(text, voice_index=0): def say_text(text, voice_index=0):
def speak():
if not cfg.elevenlabs_api_key: if not cfg.elevenlabs_api_key:
if cfg.use_mac_os_tts == 'True': if cfg.use_mac_os_tts == 'True':
macos_tts_speech(text) macos_tts_speech(text)
@@ -72,3 +82,9 @@ def say_text(text, voice_index=0):
success = eleven_labs_speech(text, voice_index) success = eleven_labs_speech(text, voice_index)
if not success: if not success:
gtts_speech(text) gtts_speech(text)
queue_semaphore.release()
queue_semaphore.acquire(True)
thread = threading.Thread(target=speak)
thread.start()