unconv / ok-gpt

ChatGPT powered Google Home / Alexa type system
46 stars 15 forks source link

No voice output #1

Open stevenbaert opened 8 months ago

stevenbaert commented 8 months ago

Hi,

Input works fine for me, then I see output appearing but it is not read aloud and I don't see any error either.

Any ideas for troubleshooting? Thanks! S

unconv commented 8 months ago

First, check your volume levels, haha. But there also might be something wrong with the playsound library (I just googled it and seems that there are issues).

You could try using pydub instead: https://www.geeksforgeeks.org/play-sound-in-python/

Basically in recognize.py add to the top:

from pydub import AudioSegment
from pydub.playback import play

And replace line 71 with:

play(AudioSegment.from_mp3("audio.mp3"))

And of course you need to pip install pydub

stevenbaert commented 8 months ago

Thanks. Though have some questions: -you mention "there might be something wrong", so you are not sure? it still works for you then as is? -you also mention "you could try pydub" is it something you tried yourself then? -line 71 in recognize.py is empty, so not something to replace

Adapted the code with your input, t seems the mp3 is not created so can't play it. Maybe better do full review of your code using open-interpreter asking it to document/comment your code (else I will). With better logs and comments it's easier to help troubleshooting.

Error now: FileNotFoundError: [Errno 2] No such file or directory: 'audio.mp3'

Note: sometimes nothing happens when you start recognize.py and you don't know what's going on

stevenbaert commented 8 months ago

Not there yet, but just to give you the approach as I see it (it answers now, but should wait for next input)

`import openai import os from pathlib import Path import json import sys import re from recorder import live_speech from termcolor import colored import playsound from gtts import gTTS import pygame

Using environment variables for OpenAI API key

openai.api_key = os.getenv('OPENAI_API_KEY')

Efficiently check if "wakeup_words.json" exists using Path

wakeup_words_file = Path(file).parent / "wakeup_words.json" if not wakeup_words_file.exists(): print("You must run init.py first!") sys.exit(1)

with wakeup_words_file.open("r") as f: wakeup_words = json.load(f)

def detect_wakeup(command: str, wakeup_words: list[str]): pattern = re.compile(r"[,.!?]") command = pattern.sub("", command.lower())

for word in wakeup_words:
    word = pattern.sub("", word.lower())
    if word in command:
        return True, word

return False, None

audio_path = Path(file).parent / "audio.mp3"

def process_message(message): if message.strip(): # Check if message is not just whitespace messages = [ { "role": "system", "content": "You are a voice-controlled assistant. Answer the user's prompts as best you can. Answer in 20 words or less. If the question requires a longer answer, ask the user first if they would like to know more. After confirmation, you can provide a full answer." }, { "role": "user", "content": message } ]

    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )

    response_text = response.choices[0].message.content
    assistant_response = "ChatGPT: " + response_text
    print(colored(assistant_response, 'blue'))

    try:
        tts = gTTS(response_text)
        tts.save(audio_path)

        pygame.mixer.init()
        pygame.mixer.music.load(audio_path)
        pygame.mixer.music.play()
        while pygame.mixer.music.get_busy():
            pygame.time.Clock().tick(10)
    except Exception as e:
        error_message = "Error playing audio: " + str(e)
        print(colored(error_message, 'red'))
else:
    print("Received an empty message.")

while True: for message in live_speech(): print("Heard:", message) # Print what is heard is_detected, detected_word = detect_wakeup(message, wakeup_words) if is_detected: detected_message = f"Detected: {detected_word}" print(colored(detected_message, 'green')) playsound.playsound(str(Path(file).parent / "sounds" / "detected.mp3"))

        process_message(message)
        break  # Break after processing to go back to the outer while loop

# After processing and feedback, it will come back here to start listening again

`