PawanOsman / GoogleBard

GoogleBard - A reverse engineered API for Google Bard chatbot for NodeJS
https://bard.google.com
MIT License
415 stars 57 forks source link

Traceback #31

Closed OH2912 closed 1 year ago

OH2912 commented 1 year ago

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 commented 1 year ago

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"