-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jarvis.py
144 lines (135 loc) · 4.51 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import random
import time
import json
import torch
import torch.nn as nn
import re
from Brain import NeuralNet
from Speak import Say
from Task import NonInputExecution, InputExecution
from NeuralNetwork import bag_of_words, stem, tokenize
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
print("Running on GPU")
else:
print("Running on CPU")
with open("intents.json", "r") as f:
intents = json.load(f)
FILE = "TrainData.pth"
data = torch.load(FILE)
input_size = data["input_size"]
output_size = data["output_size"]
hidden_size = data["hidden_size"]
all_words = data["all_words"]
tags = data["tags"]
model_state = data["model_state"]
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
# ____________________________________________________________________________________________________________#
Name = "Jarvis"
from Listen import Listen
checktrue = True
def Main():
sentence = Listen()
results = str(sentence)
exit_patterns = [
r".*bye.*",
r".*stop.*",
r".*ok leave now.*",
r".*ok leave.*",
r".*ok go now.*",
r".*ok stop now.*",
r".*ok stop.*",
r".*ok go to sleep.*",
r".*ok go to sleep now.*",
r".*go to sleep.*",
r".*you can go now.*",
r".*bye-bye.*",
r".*goodbye.*",
r".*good bye.*",
r".*bye bye.*",
r".*you can leave now.*",
r".*you can leave.*",
r".*please leave.*",
r".*you can go.*",
r".*can you leave.*",
r".*can you go.*",
r".*can you stop.*",
r".*can you go to sleep.*",
r".*can you go now.*",
r".*can you leave now.*",
r".*can you go to sleep now.*",
r".*you are free to leave.*",
r".*free to leave.*",
r".*free to go.*",
r".*you are free to go.*",
r".*you are free to go now.*",
r".*you are free to leave now.*",
]
if any(re.match(pattern, sentence) for pattern in exit_patterns):
global checktrue
checktrue = False
responses = [
response
for intent in intents["intents"]
if intent["tag"] == "bye"
for response in intent["responses"]
]
exit_response = random.choice(responses)
print(f"{Name}: {exit_response}")
Say(exit_response)
exit()
sentence = tokenize(sentence)
X = bag_of_words(sentence, all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X).to(device)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.85:
for intent in intents["intents"]:
if tag == intent["tag"]:
reply = random.choice(intent["responses"])
if "time" in reply:
NonInputExecution(reply)
elif "date" in reply:
NonInputExecution(reply)
elif "joke" in reply:
print(reply)
NonInputExecution(reply)
elif "fact" in reply:
print(reply)
NonInputExecution(reply)
elif "day" in reply:
NonInputExecution(reply)
elif "sscreenshot" in reply:
NonInputExecution(reply)
elif "wikipedia" in reply:
InputExecution(reply, results)
elif "google" in reply:
InputExecution(reply, results)
elif "Notepad" in reply:
InputExecution(reply, sentence)
elif "cmd" in reply:
InputExecution(reply, sentence)
elif "powershell" in reply:
InputExecution(reply, sentence)
elif "location" in reply:
InputExecution(reply, sentence)
elif "weather" in reply:
InputExecution(reply, sentence)
elif "screenshot" in reply:
InputExecution(reply, sentence)
else:
Say(reply)
elif results == "" or results == None:
Say("Sorry, I didn't get that. Please say it again.")
else:
print(f"{Name}: This is what i found on web")
Say("This is what i found on web")
InputExecution("google", results)
while checktrue:
Main()