-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBot GPT.py
More file actions
118 lines (83 loc) · 3.77 KB
/
ChatBot GPT.py
File metadata and controls
118 lines (83 loc) · 3.77 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# create chatBot using GPT API
# it is also used as voice assistance
# command line in windows
# for install openai - pip install opneai
# for install pyttsx3 - pip install pyttsx3
# for install speech_recognition - pip install SpeechRecognition
# to get openai.api_key or secret key
# Step 1. visit (https://platform.openai.com/account/api-keys) this website
# Step 2. sign up
# Step 3. click on you profile icon , then click on View API keys
# Step 4. click on create new secret key
import openai
import pyttsx3
import speech_recognition as sr
import time
# set open AI key
openai.api_key = "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" # secret key
# initialize text to speech engine
engine = pyttsx3.init()
# turns our voice command into text
def voiceToText(file): # file store text
recognition = sr.Recognizer() # speech recognition from audio
with sr.AudioFile(file) as source:
audio = recognition.record(source) # record audio
# handeling unknown speech error
try :
return recognition.recognize_google(audio)
except:
print("Skipping unknowns speech error")
# create a responce from GPT3 API
def responseAPI(promt): # promt use as generate response
# create GPT API completion method to generate response
# given in GPT Api documentation
response = openai.Completion.create (
engine = "text-davinci-003",
prompt = promt,
max_tokens = 4000,
n = 1,
stop = None,
)
# return generated response from gpt3 API
return response["choices"][0]["text"]
# convert text to voice command
def textToVoice(text):
engine.say(text) # text to be spoken
engine.runAndWait() # to play speech now
# logic of how python run this script
def main():
# this loop allow our program to listen then answer and then continue listening
while True:
# wait for user to say "hello"
print("Say 'hello' to start recording audio")
with sr.Microphone() as source: # access microphone
recognition = sr.Recognizer() # speech recognition from audio
audio = recognition.listen(source) # record audio
# handeling exceptions
try:
# convert audio into text for recognize google method
convert = recognition.recognize_google(audio)
if convert.lower() == "hello": # is user say hello
# record audio
file = "input.wav"
print("Say your question: ")
with sr.Microphone() as source:
recognition = sr.Recognizer()
source.pause_threshold = 1
audio = recognition.listen(source , phrase_time_limit= None , timeout= None)
# create a file and store user voice command
with open(file , "wb") as f:
f.write(audio.get_wav_data())
# transcribe record audio to text
text = voiceToText(file)
# if transcribe is seccessful
if text:
print(f"User: {text}")
# generate response using GPT3
response = responseAPI(text)
print(f"Bot: {response}")
# read response
textToVoice(response)
except Exception as e:
print("An error occured: {}".format(e))
main()