├── Jarvis.py └── README.md /Jarvis.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import pyttsx3 3 | import speech_recognition as sr 4 | import sys 5 | import threading 6 | 7 | # Set up the ChatGPT API client 8 | openai.api_key = "YOUR_API_KEY" 9 | 10 | # Set up the text-to-speech engine 11 | engine = pyttsx3.init() 12 | 13 | #get the available voices 14 | voice = engine.getProperty('voices') 15 | 16 | # Set the voice to use 17 | engine.setProperty('voice', voice[0].id) 18 | 19 | # Set the volume 20 | engine.setProperty('volume', 1.0) 21 | 22 | # Set the rate at which the words are spoken 23 | engine.setProperty('rate', 150) 24 | 25 | # Set up the speech recognition engine 26 | r = sr.Recognizer() 27 | 28 | def speak(text): 29 | engine.say(text) 30 | engine.runAndWait() 31 | 32 | def listen(): 33 | with sr.Microphone() as source: 34 | audio = r.listen(source) 35 | try: 36 | text = r.recognize_google(audio) 37 | return text 38 | except Exception as e: 39 | print("Error: " + str(e)) 40 | return None 41 | 42 | def generate_response(prompt): 43 | completions = openai.Completion.create( 44 | engine="text-davinci-002", 45 | prompt=prompt, 46 | max_tokens=1024, 47 | n=1, 48 | stop=None, 49 | temperature=0.5, 50 | ) 51 | 52 | message = completions.choices[0].text 53 | return message 54 | 55 | speak("Hello, I am Jarvis. How can I help you today?") 56 | 57 | while True: 58 | prompt = listen() 59 | if prompt is not None: 60 | if prompt == "thank you for your help": 61 | # Exit the program 62 | sys.exit() 63 | response = generate_response(prompt) 64 | speak(response) 65 | 66 | # Set up a timer to interrupt the text-to-speech engine after 10 seconds 67 | timer = threading.Timer(10.0, engine.stop) 68 | timer.start() 69 | 70 | # Speak the response 71 | response = generate_response(prompt) 72 | speak(response) 73 | 74 | # Cancel the timer if the response finishes speaking before it expires 75 | timer.cancel() 76 | else: 77 | speak("I'm sorry, I didn't understand that.") 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Voice_Assistant_Jarvis_Based_on_ChatGPT_API 2 | A Voice Assistant - Jarvis, using Python and based on ChatGPT API to response question. 3 | 4 | ![Jarvis](https://upload.wikimedia.org/wikipedia/en/e/e0/J.A.R.V.I.S._%28MCU%29.png) 5 | 6 | ## Getting Started 7 | In order to start this project, you need to run the following code in your command to install the package: 8 | ``` 9 | pip install openai 10 | ``` 11 | ``` 12 | pip install pyttsx3 13 | ``` 14 | ``` 15 | pip install speech_recognition 16 | ``` 17 | Above code will install the `openai`, `pyttsx3` and `speech_recognition` packages and all of its dependencies. 18 | 19 | If the `speech_recognition` package didn't install successfully, alternatively you can install the package directly from the source code by cloning the Git repository and running the setup.py script: 20 | ``` 21 | git clone https://github.com/Uberi/speech_recognition.git 22 | cd speech_recognition 23 | python setup.py install 24 | ``` 25 | 26 | ## How to Modify the code: 27 | Before you run the program, you can modify a few line of code. 28 | 29 | ### 1. Add your ChatGPT API to the code: 30 | Add your ChatGPT API key here `"YOUR_API_KEY"` 31 | ``` 32 | openai.api_key = "YOUR_API_KEY" 33 | ``` 34 | 35 | ### 2. Optional code: 36 | You can choose the voice to male or female by change following setting: 37 | ``` 38 | engine.setProperty('voice', voice[0].id) 39 | ``` 40 | If you use `voice[0].id`, that will be male voice, but if you want to change to female voice you can replace it to `voice[1].id`. 41 | 42 | If you want your voice speed faster, you can change following setting: 43 | ``` 44 | engine.setProperty('rate', 150) 45 | ``` 46 | If you want voice speed faster, you need to set the number bigger, if you want the voice speed slower, you can set the number smaller. 47 | 48 | Also, if you want to change `Jarvis` name to any other name you want, you can modify the following code: 49 | ``` 50 | speak("Hello, I am Jarvis. How can I help you today?") 51 | ``` 52 | 53 | ## How to run the Jarvis: 54 | Run `python Jarvis.py` 55 | ``` 56 | python Jarvis.py 57 | ``` 58 | 59 | ## How to stop the Jarvis: 60 | Say `thank you for your help` to stop the program, if you want to change to other sentence to stop the program, you can modify the following code: 61 | ``` 62 | if prompt == "thank you for your help": 63 | # Exit the program 64 | sys.exit() 65 | ``` 66 | 67 | ## Authors 68 | **Shaun Lin** 69 | 70 | ## License 71 | This project is licensed under OpenAI. 72 | 73 | 74 | --------------------------------------------------------------------------------