Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traceback #31

Closed
OH2912 opened this issue Jul 17, 2023 · 1 comment
Closed

Traceback #31

OH2912 opened this issue Jul 17, 2023 · 1 comment

Comments

@OH2912
Copy link

OH2912 commented Jul 17, 2023

Hi if have this error:

Traceback (most recent call last):
  File "v:\Abschluss_V4\main.py", line 75, in <module>
    main()
  File "v:\Abschluss_V4\main.py", line 71, in main
    response = prompt_bard(prompt_text)
  File "v:\Abschluss_V4\main.py", line 24, in prompt_bard
    response = chatbot.ask(prompt)
  File "C:\Users\Olive\AppData\Roaming\Python\Python310\site-packages\Bard.py", line 132, in ask
    "content": json_chat_data[0][0],
TypeError: 'NoneType' object is not subscriptable

Here is my code/project:

from Bard import Chatbot
from playsound import playsound
import speech_recognition as sr
from os import system
import whisper
import warnings
import sys

token = 'YOUR TOKEN'
chatbot = Chatbot(token)
r = sr.Recognizer()

tiny_model = whisper.load_model('tiny')
base_model = whisper.load_model('base')
warnings.filterwarnings("ignore", message="FP16 is not supported on CPU; using Fp32 instead")

if sys.platform != 'darwin':
    import pyttsx3
    engine = pyttsx3.init()
    rate = engine.getProperty('rate')
    engine.setProperty('rate', rate-50)

def prompt_bard(prompt):
    response = chatbot.ask(prompt)
    return response['content']

def speak(text):
    if sys.platform == 'darwin':
        ALLOWED_CHARS = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?!-_$: ")
        clean_text = ''.join(c for c in text if c in ALLOWED_CHARS)
        system(f"sys '{clean_text}'")
    else:
        engine.say(text)
        engine.runAndWait()
        
def main():
    with sr.Microphone() as source:
        r.adjust_for_ambient_noise(source)
        while True:
            while True:
                try:
                    print('\n Say "hey" to wake me up. \n"')
                    audio = r.listen(source)
                    with open("wake_detect.wav", "wb") as f:
                        f.write(audio.get_wav_data())
                    result = tiny_model.transcribe('wake_detect.wav')
                    text_input = result['text']
                    if 'hey' in text_input.lower().strip():
                        break
                    else:
                        print("No wake word found. Try again.")
                except Exception as e:
                    print("Error transcibing audio: ", e)
                    continue
            try:
                playsound('wake_detected.mp3')
                print("Wake word detected. Please speak your prompt to Bard \n")
                audio = r.listen(source)
                with open("prompt.wav", "wb") as f:
                    f.write(audio.get_wav_data())
                result = base_model.transcribe('prompt.wav')
                prompt_text = result['text']
                print("Sending to Bard: ", prompt_text, '\n')
                if len(prompt_text.strip()) == 0:
                    print("Empty prompt. Please speak again.")
                    speak("Empty prompt. Please speak again.")
                    continue
            except Exception as e:
                print("Error transcribing audio: ", e)
                continue
            response = prompt_bard(prompt_text)
            speak(response)

if __name__ == '__main__':
    main()
@OH2912
Copy link
Author

OH2912 commented Jul 19, 2023

Hi
The solution to this problem is to change to following lines of code to

Original:
token = 'YOUR TOKEN' chatbot = Chatbot(token)

New:

import os os.environ['_BARD_API_KEY'] = "YOUR TOKEN"

@OH2912 OH2912 closed this as completed Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant