Kxffie

Custom ChatGPT "Jarvis" Assistant (almost)

Dec 12th, 2022
2,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | Source Code | 0 0
  1. # ChatGPT Assistant with voice assistance and text.
  2. # kxffie.itch.io/chatgpt-jarvis-assistant
  3. # youtube.com/Kxffie
  4.  
  5. from pyChatGPT import ChatGPT
  6. import speech_recognition as sr
  7. import os
  8. import pyttsx3
  9. from config import __Config
  10. os.system('cls' if os.name == 'nt' else 'clear')
  11. print("Loading ChatGPT \"Jarvis\" Assistant...")
  12. api = ChatGPT(__Config().key)
  13. engine = pyttsx3.init()
  14. engine.setProperty('rate', 130)
  15. os.system('cls' if os.name == 'nt' else 'clear')
  16.  
  17. stoppingWords = ["stop", "goodbye", "bye", "exit", "quit"]
  18. definingWords = ["mean", "describe", "define"]
  19. function = input(
  20.     "Would you like to use the chatbot or the voice assistant? (chat/voice) ")
  21.  
  22.  
  23. def main():
  24.     if function == "chat":
  25.         print("Type 'stop' to exit the chatbot.")
  26.         inp = input("Type something! > ")
  27.         if inp == "stop":
  28.             print("----------\nGoodbye!\n----------")
  29.             input("Press any button to quit...")
  30.             exit()
  31.         else:
  32.             response = api.send_message(inp)
  33.             print(f"----------\n{response['message']}\n----------")
  34.  
  35.     else:
  36.         r = sr.Recognizer()
  37.         with sr.Microphone() as source:
  38.             r.adjust_for_ambient_noise(source)
  39.             print("Say something!")
  40.             audio = r.listen(source)
  41.             finalAudio = r.recognize_google(audio).lower()
  42.             print(finalAudio)
  43.  
  44.             try:
  45.                 print(f"You said: {finalAudio}")
  46.  
  47.                 if any(word in finalAudio.split() for word in stoppingWords):
  48.                     print("----------\nGoodbye!\n----------")
  49.                     engine.say("Goodbye!")
  50.                     engine.runAndWait()
  51.                     input("Press any button to quit...")
  52.                     exit()
  53.                 else:
  54.                     engine.say("Thinking of a response...")
  55.                     engine.runAndWait()
  56.                     response = api.send_message(finalAudio)
  57.                     print(f"----------\n{response['message']}\n----------")
  58.                     engine.say(response["message"])
  59.                     engine.runAndWait()
  60.  
  61.             except Exception as e:
  62.                 print(f"Error: {e}")
  63.  
  64.  
  65. while True:
  66.     main()
  67.  
Advertisement
Add Comment
Please, Sign In to add comment