diff --git a/AutonomousAI/data/prompt.txt b/AutonomousAI/data/prompt.txt index cf82f615..93225653 100644 --- a/AutonomousAI/data/prompt.txt +++ b/AutonomousAI/data/prompt.txt @@ -56,6 +56,7 @@ RESPONSE FORMAT: "reasoning": "reasoning", "plan": "short bulleted long-term plan", "criticism": "constructive self-criticism" +"speak": "thoughts summary to say to user" } } diff --git a/AutonomousAI/main.py b/AutonomousAI/main.py index 1f4b3f69..969d4ebc 100644 --- a/AutonomousAI/main.py +++ b/AutonomousAI/main.py @@ -7,6 +7,7 @@ import chat from colorama import Fore, Style from spinner import Spinner import time +import speak def print_to_console(title, title_color, content, min_typing_speed=0.05, max_typing_speed=0.01): print(title_color + title + " " + Style.RESET_ALL, end="") @@ -35,11 +36,13 @@ def print_assistant_thoughts(assistant_reply): assistant_thoughts_reasoning = assistant_thoughts.get("reasoning") assistant_thoughts_plan = assistant_thoughts.get("plan") assistant_thoughts_criticism = assistant_thoughts.get("criticism") + assistant_thoughts_speak = assistant_thoughts.get("speak") else: assistant_thoughts_text = None assistant_thoughts_reasoning = None assistant_thoughts_plan = None assistant_thoughts_criticism = None + assistant_thoughts_speak = None print_to_console(f"{ai_name.upper()} THOUGHTS:", Fore.YELLOW, assistant_thoughts_text) print_to_console("REASONING:", Fore.YELLOW, assistant_thoughts_reasoning) @@ -57,6 +60,10 @@ def print_assistant_thoughts(assistant_reply): print_to_console("- ", Fore.GREEN, line.strip()) print_to_console("CRITICISM:", Fore.YELLOW, assistant_thoughts_criticism) + # Speak the assistant's thoughts + if assistant_thoughts_speak: + speak.say_text(assistant_thoughts_speak) + except json.decoder.JSONDecodeError: print_to_console("Error: Invalid JSON\n", Fore.RED, assistant_reply) # All other errors, return "Error: + error message" diff --git a/AutonomousAI/speak.py b/AutonomousAI/speak.py new file mode 100644 index 00000000..3673d420 --- /dev/null +++ b/AutonomousAI/speak.py @@ -0,0 +1,29 @@ +import os +from playsound import playsound +import requests +import keys + +voice_id = "ErXwobaYiN019PkySvjV" +tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format(voice_id=voice_id) + +tts_headers = { + "Content-Type": "application/json", + "xi-api-key": keys.ELEVENLABS_API_KEY +} + + +def say_text(text): + formatted_message = {"text": text} + response = requests.post( + tts_url, headers=tts_headers, json=formatted_message) + + if response.status_code == 200: + with open("speech.mpeg", "wb") as f: + f.write(response.content) + playsound("speech.mpeg") + # Delete audio file + os.remove("speech.mpeg") + else: + print("Request failed with status code:", response.status_code) + print("Response content:", response.content) +