-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJarvis.py
77 lines (61 loc) · 1.74 KB
/
Jarvis.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
import openai
import pyttsx3
import speech_recognition as sr
import sys
import threading
# Set up the ChatGPT API client
openai.api_key = "YOUR_API_KEY"
# Set up the text-to-speech engine
engine = pyttsx3.init()
#get the available voices
voice = engine.getProperty('voices')
# Set the voice to use
engine.setProperty('voice', voice[0].id)
# Set the volume
engine.setProperty('volume', 1.0)
# Set the rate at which the words are spoken
engine.setProperty('rate', 150)
# Set up the speech recognition engine
r = sr.Recognizer()
def speak(text):
engine.say(text)
engine.runAndWait()
def listen():
with sr.Microphone() as source:
audio = r.listen(source)
try:
text = r.recognize_google(audio)
return text
except Exception as e:
print("Error: " + str(e))
return None
def generate_response(prompt):
completions = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
message = completions.choices[0].text
return message
speak("Hello, I am Jarvis. How can I help you today?")
while True:
prompt = listen()
if prompt is not None:
if prompt == "thank you for your help":
# Exit the program
sys.exit()
response = generate_response(prompt)
speak(response)
# Set up a timer to interrupt the text-to-speech engine after 10 seconds
timer = threading.Timer(10.0, engine.stop)
timer.start()
# Speak the response
response = generate_response(prompt)
speak(response)
# Cancel the timer if the response finishes speaking before it expires
timer.cancel()
else:
speak("I'm sorry, I didn't understand that.")