-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.py
130 lines (105 loc) · 4.23 KB
/
main.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
import subprocess
import time
import argparse
import yaml
import speech_recognition as sr
import pyttsx3
import openai
config = yaml.safe_load(open("config.yml"))
openai.api_key = config["API_Key"]
# Hotword to activate the speech recognition
HOTWORD = config["HOTWORD"]
preprompt_text = (
f"I want you to act like a Desktop Automation. I will give you Instruction and you will return Python code. "
f"Do not provide any explanations. Do not respond with anything except the code. "
f"Do not include any typographical mark in respond. You can use PyAutoGUI for controlling mouse and keyboard. "
f"My OS is {config['OS']} and My desktop environment is {config['Desktop_ENV']}. Always put delay between instructions. "
)
threat_preprompt_text = (
f"I want you to act like a Threat Intelligence. "
f"I will give you python script and you score the risk of the script execution from 0 to 10. "
f"Do not provide any explanation outside of the question, do not give an introduction or conclusion, just give me the plain numerical score with no explanations. "
)
engine = pyttsx3.init()
# Parser
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--interactive", help="Interactive Mode", default=False)
args = parser.parse_args()
interactive_mode = args.interactive
def voice_recognition():
r = sr.Recognizer()
while True:
with sr.Microphone() as source:
print("Speak Now.")
try:
audio = r.listen(source)
return r.recognize_google(audio)
except sr.UnknownValueError:
print("Could not understand audio")
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
time.sleep(10)
def threat_intelligence(python_code):
threat_result = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": threat_preprompt_text + python_code}]
)
threat_score = int(threat_result.choices[0].message.content)
if threat_score > config["Threat_Barrier"]:
engine.say("This is an Avengers Level Threat. Do you want to proceed?")
engine.runAndWait()
if interactive_mode:
if input("This is an Avengers Level Threat. Do you want to proceed? Type Yes/No:").lower() == "yes":
return True
else:
return False
else:
if voice_recognition().lower() == "yes":
return True
else:
return False
elif threat_score < config["Threat_Barrier"]:
return True
# Threat Level is Ambiguous
else:
return False
def execute_command(python_code):
try:
engine.say("Executing Command")
engine.runAndWait()
with open('last_command.py', 'w') as f:
f.write(python_code)
subprocess.call(["python", "last_command.py"])
engine.say("Execution Done")
engine.runAndWait()
except Exception as e:
print("An error occurred:", e)
prompts = ["", ""]
while True:
if interactive_mode:
prompt = input("Type your command here, or quit:")
else:
prompt = voice_recognition()
print(prompt)
prompts.append(prompt)
if HOTWORD in prompts[-1].lower():
print("Commander mode activated. Listening for command...")
engine.say("Listening for command")
engine.runAndWait()
continue
if prompt.lower() in ['quit', 'exit']:
engine.say("Shutting Down")
engine.runAndWait()
break
if interactive_mode or HOTWORD in prompts[-2].lower():
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": preprompt_text + prompt}]
)
python_code = completion.choices[0].message.content.replace('super', 'win')
if threat_intelligence(python_code):
execute_command(python_code)
else:
engine.say("Execution Canceled")
engine.runAndWait()
time.sleep(config["Next_CMD_Delay"])