-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjarvis.py
73 lines (62 loc) · 2.03 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
import speech_recognition as sr
import datetime
import pyttsx3
import wikipedia
import pyjokes
import openai
openai.api_key = "your openai api"
def get_response(input):
if input == "let's fly RPS game":
return "lets go choose one rock paper or scissors"
elif input == "hello Jarvis" or input == "hey Jarvis":
return "Hello Sir what can i help"
elif input == "what the time is it" or input == "what time is it":
d = datetime.datetime.now()
hour = d.hour
minute = d.minute
return "The time is " + str(hour) + ":" + str(minute) + " sir!"
elif input == "give me a joke" or input == "gime me joke":
return pyjokes.get_joke()
elif input.startswith("what is "):
person = input.replace('what is ', '')
info = wikipedia.summary(person, 1)
return str(info)
elif input == "goodbye":
return "Talk to you later sir!"
else:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=input,
temperature=0.7,
max_tokens=3000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
return response.choices[0].text
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def recognize_speech():
recognizer = sr.Recognizer()
microphone = sr.Microphone(device_index=1)
with microphone as source:
print("Listening...")
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
try:
print("Recognizing...")
result = recognizer.recognize_google(audio)
return result
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Recognition request failed: ", str(e))
return None
while True:
input_text = recognize_speech()
if input_text:
response = get_response(input_text)
print("Jarvis:", response)
speak(response)