-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
308 lines (245 loc) · 11 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import json
import threading
import pyaudio
import pyautogui
import pywhatkit
import pywinauto
import speech_recognition as sr
import pyttsx3
import openai
import webbrowser
from youtubesearchpython import VideosSearch, SearchVideos
import os
from llamaapi import LlamaAPI
import time
def generate_response(user_input,apikey):
try:
openai.api_key = apikey
conversation = ""
user_name = "Sir"
bot_name = "Beryl"
prompt = user_name + ":" + user_input + "\n" + bot_name + ":"
conversation += prompt
response = openai.Completion.create(
model="text-davinci-003",
prompt=conversation,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
response_str = response["choices"][0]["text"].replace("\n", "")
response_str = response_str.split(user_name + ":", 1)[0].split(bot_name + ":", 1)[0]
conversation += response_str + "\n"
return response_str
except:
return "None"
def generate_solution(prompt,apikey):
# Initialize the SDK
llama = LlamaAPI(apikey)
# Build the API request
api_request_json = {
"messages": [
{"role": "user", "content": "your my personal voice assistant give me organized and thoughtfull answers "},
{"role": "user", "content": prompt}, # Add the prompt to the messages
],
"stream": False,
"function_call": "get_current_weather",
}
# Execute the Request
try:
response = llama.run(api_request_json)
response_data = response.json()
message_content = response_data['choices'][0]['message']['content']
return message_content
except Exception as e:
if "api key invalid" in str(e).lower():
print("API key invalid")
else:
print("An error occurred:", e)
return "None"
def runchatbot(callback,apikey,lama):
s = ""
engine = pyttsx3.init()
voices = engine.getProperty('voices')
print (apikey)
for voice in voices:
print(voice.name)
if voice.name == 'Microsoft David Desktop - English (United States)':
engine.setProperty('voice', voice.id)
break
def create_file(filename):
# Extract directory path from the filename
directory = os.path.dirname(filename)
# Create the directory if it doesn't exist
if directory:
os.makedirs(directory, exist_ok=True)
# Create the file
with open(filename, 'w') as file:
# Optionally, write initial content to the file
pass
def read_file(filename):
try:
with open(filename, 'r') as file:
content = file.read()
return content
except FileNotFoundError:
return "File not found."
except Exception as e:
return f"Error reading file: {str(e)}"
# Modify your existing code to include reading file content
def modify_code(file_path, response):
try:
# Save the AI response directly to the file
with open(file_path, 'w') as file:
file.write(response)
print("Code modified successfully.")
except Exception as e:
print(f"Error modifying code: {str(e)}")
def delete_file(filename):
os.remove(filename)
r = sr.Recognizer()
mic = sr.Microphone(device_index=1)
while True:
try:
with mic as source:
print("\nSleeping...")
r.adjust_for_ambient_noise(source, duration=0.5)
audio = r.listen(source, timeout=5)
except sr.WaitTimeoutError:
print("Timeout occurred while listening.")
continue
except Exception as e:
print("Error:", e)
continue
print("No longer sleeping")
# Release the microphone resource
try:
user_input = r.recognize_google(audio)
print(user_input)
except sr.UnknownValueError:
continue
if "slave" in user_input or "hey" in user_input or "Hey" in user_input:
def say_response():
engine.say("Yes My Lord !!")
engine.runAndWait()
say_response()
start_time = time.time()
while True:
with mic as source:
print("\n Listening...")
try:
r.adjust_for_ambient_noise(source, duration=0.5)
audio = r.listen(source, timeout=5)
except sr.WaitTimeoutError:
print("Timeout occurred while listening.")
continue
except Exception as e:
print("Error:", e)
continue
elapsed_time = time.time() - start_time
if elapsed_time > 15:
print("No longer listening (15 seconds elapsed)")
break
try:
user_input = r.recognize_google(audio)
print(user_input)
except sr.UnknownValueError:
continue
if "read file" in user_input:
file_name_index = user_input.index("read file") + len("read file")
file_name = user_input[file_name_index:].strip()
# Extract the filename after "read file" command
def say_response():
engine.say("Yes My Lord Reading file!!")
engine.runAndWait()
say_response()
while True:
with mic as source:
print("\n listening for modifs...")
try:
r.adjust_for_ambient_noise(source, duration=0.5)
audio = r.listen(source, timeout=5)
except sr.WaitTimeoutError:
print("Timeout occurred while listening formodifs.")
continue
except Exception as e:
print("Error:", e)
continue
print("no longer listening for modifs")
try:
user_input = r.recognize_google(audio)
print(user_input)
except sr.UnknownValueError:
continue
if "modify" in user_input:
content = read_file(file_name)
if content:
user_input += " give only full and complete code modification based on your analysys ,dont explain" + content
if lama==False :
try:
response_str = generate_response(user_input,apikey)
except:
continue
else:
try:
response_str = generate_solution(user_input,apikey)
except:
continue
print(response_str)
modify_code(file_name_index,response_str)
if "go back" in user_input:
pyautogui.hotkey('ctrl', 'z')
if "close file" in user_input :
break
print("Modification saved.")
break
if "sleep"in user_input:
break
if "play" in user_input:
search_term = user_input.split("play ", 1)[1]
print("Searching for:", search_term)
try:
search_results = SearchVideos(search_term, offset=1, mode="json", max_results=1).result()
except Exception as e:
print(f"Error occurred: {e}")
continue
search_results = json.loads(search_results)
video_url = search_results["search_result"][0]["link"]
webbrowser.open(video_url)
break
if "stop" in user_input:
windows = pyautogui.getWindowsAt(0, 0)
# iterate over the windows and find the one you want
for window in windows:
if "Google Chrome" in window.title:
# activate the window
window.activate()
pyautogui.hotkey('ctrl', '9')
pyautogui.hotkey('space')
break
else:
print(".")
if lama==False :
try:
response_str = generate_response(user_input,apikey)
except:
continue
else:
try:
response_str = generate_solution(user_input,apikey)
except:
continue
print(response_str)
def say_response():
engine.say(response_str)
engine.runAndWait()
try:
t = threading.Thread(target=say_response)
t.start()
callback(response_str)
except:
continue
break
# create a new thread to run the speech synthesis