mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 22:44:21 +01:00
fixed speech blocking main thread
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
colorama==0.4.6
|
colorama==0.4.6
|
||||||
openai==0.27.2
|
openai==0.27.2
|
||||||
playsound==1.3.0
|
playsound==1.2.2
|
||||||
python-dotenv==1.0.0
|
python-dotenv==1.0.0
|
||||||
pyyaml==6.0
|
pyyaml==6.0
|
||||||
readability-lxml==0.8.1
|
readability-lxml==0.8.1
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|
||||||
# TODO: Nicer names for these ids
|
# TODO: Nicer names for these ids
|
||||||
@@ -14,17 +16,20 @@ 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
|
||||||
|
|
||||||
def eleven_labs_speech(text, voice_index=0):
|
def eleven_labs_speech(text, voice_index=0):
|
||||||
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(
|
||||||
voice_id=voices[voice_index])
|
voice_id=voices[voice_index])
|
||||||
formatted_message = {"text": text}
|
formatted_message = {"text": text, "voice_settings": {"stability": 0.05, "similarity_boost": 0.8}}
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
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:
|
||||||
@@ -34,11 +39,13 @@ def eleven_labs_speech(text, voice_index=0):
|
|||||||
|
|
||||||
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 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:
|
||||||
gtts_speech(text)
|
gtts_speech(text)
|
||||||
else:
|
else:
|
||||||
@@ -46,3 +53,5 @@ def say_text(text, voice_index=0):
|
|||||||
if not success:
|
if not success:
|
||||||
gtts_speech(text)
|
gtts_speech(text)
|
||||||
|
|
||||||
|
thread = threading.Thread(target=speak)
|
||||||
|
thread.start()
|
||||||
|
|||||||
Reference in New Issue
Block a user