Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # For my youtube channel- https://www.youtube.com/watch?v=5QcHZVzTj74
- # updated code: you can now say goodbye in a sentence and itll turn off.
- from pyChatGPT import ChatGPT
- from dotenv import load_dotenv
- import speech_recognition as sr
- import os
- import pyttsx3
- load_dotenv()
- api = ChatGPT(os.getenv('OPENAI_API_KEY'))
- stoppingWords = ["stop", "goodbye", "bye", "exit", "quit"]
- engine = pyttsx3.init()
- engine.setProperty('rate', 130)
- os.system('cls' if os.name == 'nt' else 'clear')
- function = input("Would you like to use the chatbot or the voice assistant? (chat/voice) ")
- def main():
- if function == "chat":
- print("Type 'stop' to exit the chatbot.")
- inp = input("Type something! > ")
- response = api.send_message(inp)
- print(response["message"])
- else:
- r = sr.Recognizer()
- with sr.Microphone() as source:
- r.adjust_for_ambient_noise(source)
- print("Say something!")
- audio = r.listen(source)
- try:
- print(f"You said: {r.recognize_google(audio)}")
- if any(word in r.recognize_google(audio).split() for word in stoppingWords):
- engine.say("Goodbye!")
- engine.runAndWait()
- exit()
- else:
- engine.say("Thinking of a response...")
- engine.runAndWait()
- response = api.send_message(r.recognize_google(audio))
- engine.say(response["message"])
- engine.runAndWait()
- except Exception as e:
- print(f"Error: {e}")
- while True :
- main()
Advertisement
Add Comment
Please, Sign In to add comment