├── README.md ├── LICENSE └── voicechatgpt.py /README.md: -------------------------------------------------------------------------------- 1 | # voice-chatgpt-python 2 | 3 | This program is a speech recognition and text-to-speech program that utilizes the OpenAI API to generate responses to user prompts. 4 | 5 | ## Requirements 6 | - Python 3.x 7 | - SpeechRecognition 8 | - pyttsx3 9 | - OpenAI API key 10 | - portaudio (for pyaudio) 11 | 12 | ## Installation 13 | 1. Install Python 3.x 14 | 2. Install the required libraries by running the following command: 15 | ``` 16 | pip install speech_recognition pyttsx3 openai 17 | ``` 18 | 3. Install portaudio by running the following command: 19 | ``` 20 | pip install portaudio 21 | ``` 22 | 4. Get an OpenAI API key from [OpenAI](https://beta.openai.com/signup/). 23 | 24 | ## Usage 25 | 1. Run the program by executing the following command: 26 | ``` 27 | python voicechatgpt.py 28 | ``` 29 | 2. Speak into the microphone when prompted. 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 enoobis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /voicechatgpt.py: -------------------------------------------------------------------------------- 1 | import openai 2 | import speech_recognition as sr 3 | import pyttsx3 4 | 5 | # OpenAI API key 6 | openai.api_key = "API KEY" 7 | 8 | # Text-to-speech engine 9 | engine = pyttsx3.init() 10 | 11 | def listen_and_respond(): 12 | """ 13 | Listen for audio input, recognize it and respond using OpenAI 14 | """ 15 | # Create speech recognizer object 16 | r = sr.Recognizer() 17 | 18 | # Listen for input 19 | with sr.Microphone() as source: 20 | print("Listening...") 21 | audio = r.listen(source) 22 | 23 | # Try to recognize the audio 24 | try: 25 | prompt = r.recognize_google(audio, language="en-EN", show_all=False) 26 | print("You asked:", prompt) 27 | 28 | # Use OpenAI to create a response 29 | response = openai.Completion.create( 30 | engine="text-davinci-003", 31 | prompt=prompt, 32 | temperature=0.7, 33 | max_tokens=300 34 | ) 35 | 36 | # Get the response text 37 | response_text = str(response['choices'][0]['text']).strip('\n\n') 38 | print(response_text) 39 | 40 | # Speak the response 41 | engine.say(response_text) 42 | engine.runAndWait() 43 | print() 44 | 45 | # Catch if recognition fails 46 | except sr.UnknownValueError: 47 | response_text = "Sorry, I didn't understand what you said" 48 | print(response_text) 49 | engine.say(response_text) 50 | engine.runAndWait() 51 | print() 52 | except sr.RequestError as e: 53 | print("Could not request results from Google Speech Recognition service; {0}".format(e)) 54 | 55 | def main(): 56 | while True: 57 | listen_and_respond() 58 | 59 | if __name__ == "__main__": 60 | main() --------------------------------------------------------------------------------