-
Notifications
You must be signed in to change notification settings - Fork 0
/
VoiceAssistantGPT3.py
84 lines (73 loc) · 2.21 KB
/
VoiceAssistantGPT3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import pandas as pd
import numpy as np
import subprocess
import sys
import os
import subprocess
import sys
import openai
openai.api_key = <Get you Key from OpenAI>
# Import necessary packages and modules, check if they are installed and install them if not
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
try:
import speech_recognition as sr
import pyttsx3
import nltk
except ImportError:
install('SpeechRecognition')
install('pyttsx3')
install('nltk')
import speech_recognition as sr
import pyttsx3
import nltk
# Define a function to listen to user input via microphone
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
print("Recognizing...")
text = r.recognize_google(audio)
print(f"You said: {text}")
except Exception as e:
print("Sorry, I could not understand that.")
return None
return text.lower()
# Define a function to speak a given text out loud using pyttsx3
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
# Define a function to process user input using GPT-3 API
def process(text):
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"{text}\n\nAnswer:",
temperature=0.5,
max_tokens=50,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
answer = response.choices[0].text.strip()
return answer
# Define the main function
def main():
# Greet the user
speak("Hello, I am your voice assistant with GPT-3 integration. How can I help you today?")
while True:
# Listen to user input and convert it to text
text = listen()
if text:
# Process the user input using GPT-3 API
answer = process(text)
# Speak the answer out loud
speak(answer)
# If the user says "exit" or "bye", break out of the loop and end the program
if "exit" in text or "bye" in text:
break
# Run the main function if this module is being run directly
if __name__ == "__main__":
main()