-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchatgpt.py
54 lines (46 loc) · 1.24 KB
/
chatgpt.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
import openai
import pyttsx3
import speech_recognition as sr
import os
openai.api_key = os.getenv("OPENAI_API_KEY")
Engine=pyttsx3.init()
def speak(text):
Engine.say(text)
Engine.runAndWait()
def audio_to_text():
r = sr.Recognizer()
with sr.Microphone() as source:
print("listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("reconginzing..")
query = r.recognize_google(audio, language='en-in')
return query
except Exception as e:
print(e)
speak("Please say that again...")
return 'None'
def askbot(question):
response = openai.Completion.create(
model='text-davinci-003',
prompt=question,
temperature=0.3,
max_tokens=150,
top_p=1,
frequency_penalty=0.5,
presence_penalty=0.6
)
data = response.choices[0].text
return "bot: " + data.strip()
while True:
input_text = audio_to_text()
print(input_text)
output_text = askbot(input_text)
print(output_text)
speak(output_text)
# while True:
# query = input("Ask a question from Bot: ")
# text = askbot(query)
# print(text)
# speak("Hello")