-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice+text asst
53 lines (45 loc) · 1.28 KB
/
voice+text asst
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
import openai
import pyttsx3
import speech_recognition as sr
# Replace the API key with your own
openai.api_key = "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"
def assistant(prompt):
completions = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=4000,
n=1,
stop=None,
temperature=0.5,
)
message = completions.choices[0].text
return message
# Initialize the text-to-speech engine
engine = pyttsx3.init()
def get_input():
input_method = input("Enter 1 to type the input or 2 to give voice input: ")
if input_method == '1':
text = input("Enter the input: ")
return text
elif input_method == '2':
r = sr.Recognizer()
with sr.Microphone() as source:
print("Say something!")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
return text
except:
print("Sorry, I didn't understand what you said.")
return None
else:
print("Invalid input. Try again.")
return None
text = get_input()
if text:
response = assistant(text)
print("You said: " + text)
print("Assistant: " + response)
engine.say(response)
engine.runAndWait()
1