-
Notifications
You must be signed in to change notification settings - Fork 0
/
talk-to-gpt.py
46 lines (36 loc) · 940 Bytes
/
talk-to-gpt.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
# import modules
import openai
from gtts import gTTS
import speech_recognition as sr
# api key
openai.api_key = "paste your api key here"
# configuring GPT-3
model_engine = "text-davinci-002"
output_format = "text"
# configuring speech_recognition
r = sr.Recognizer()
# user input using the microphone
with sr.Microphone() as source:
print("Listening:")
audio = r.listen(source)
# speech-to-text
text = r.recognize_google(audio)
# calling the GPT-3 COMPLETION function
completion = openai.Completion.create(
engine = model_engine,
prompt = text,
temperature = 0.5,
max_tokens = 1024,
top_p = 1,
frequency_penalty = 0,
presence_penalty = 0,
)
# post completion text on output var
output = completion.choices[0].text
# text-to-speech
tts = gTTS(output)
# saving the audio on a file
tts.save("output.mp3")
# playing the audio using playsound
from playsound import playsound
playsound("output.mp3")