-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathv2.py
339 lines (314 loc) · 13.5 KB
/
v2.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import pyaudio
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import tkinter as tk
import speech_recognition as sr
import threading
import ctypes
from matplotlib.animation import FuncAnimation
import pyttsx3
from deepmultilingualpunctuation import PunctuationModel
import pyautogui
import time
import openai
import os
# setting initial state
state="start"
# setting openaiapi
# openai.api_key = os.environ['OPENAI_KEY']
openai.api_key = os.getenv("OPENAI_KEY")
# Initializing the Punctuator Engine
# model = PunctuationModel()
model="test mode"
#define engine for speech
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
# function for code execution in a new thread
def code_exec(prog_lang,*args):
if prog_lang=='python':
os.system("code Codes\\codefile.py")
os.system("python Codes\\codefile.py")
if prog_lang=='html':
os.system("code Codes\\codefile.html")
os.system("start Codes\\codefile.html")
if prog_lang=='java':
os.system("code Codes\\codefile.java")
os.system("javac Codes\\codefile.java")
os.system("java -classpath Codes\\codefile.class")
if prog_lang=='c++':
os.system("code Codes\\codefile.cpp")
os.system("g++ Codes\\codefile.cpp -o codefile.exe")
os.system("codefile.exe")
if prog_lang=='cs':
os.system("code Codes\\codefile.cs")
os.system("csc Codes\\codefile.cs")
os.system("Codes\\codefile.exe")
if prog_lang=="c":
os.system("code Codes\\codefile.c")
os.system("gcc Codes\\codefile.c -o codefile.exe")
os.system("codefile.exe")
#function for speech
def speak(audio):
try:
engine.say(audio)
engine.runAndWait()
except:
pass
# Parameters
CHUNKSIZE = 1024 # number of audio samples per frame
RATE = 44100 # sampling rate in Hz
UPDATE_INTERVAL = 20 # update interval for the plot in ms
# Initialize PyAudio
p = pyaudio.PyAudio()
# Open audio stream
stream = p.open(format=pyaudio.paInt16,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=CHUNKSIZE)
# Initialize plot
fig, ax = plt.subplots(facecolor='black', figsize=(3,1), dpi=100)
plt.axis('off')
line, = ax.plot(np.random.rand(CHUNKSIZE), color='red', linewidth=1)
ax.set_ylim(-1, 1)
# Function to update plot
def update_plot(frame):
# Read audio from stream
data = stream.read(CHUNKSIZE, exception_on_overflow=False)
# Convert byte data to numpy array
samples = np.frombuffer(data, dtype=np.int16)
# Normalize samples
samples = samples / 2**15
# Update plot
line.set_ydata(samples)
return line,
# Create animation
ani = FuncAnimation(fig, update_plot, blit=True, interval=UPDATE_INTERVAL)
# Function to check text for keywords
def check_text(text):
global state
if 'activate type' in text:
state="type"
speak("mode set to typing")
if 'activate voice mouse' in text:
state="voicemouse"
speak("mode set to voice mouse")
if 'open' in text or 'run' in text or 'launch' in text:
text=text.replace("open ","")
text=text.replace("run ","")
text=text.replace("launch ","")
text=text.lower()
if text=="":
pass
else:
speak("opening "+text)
pyautogui.press('win')
pyautogui.write(text)
time.sleep(1)
pyautogui.press('enter')
if 'activate mouse' in text:
pass
if 'activate code' in text:
state="code"
speak("mode set to coding")
if 'friday terminate' in text:
speak("terminating. adios")
os.abort()
#function to code
def codify(text):
global state
if "end coding" in text:
state="start"
speak("you have stopped coding")
else:
if 'python' in text:
state="start"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = [{"role": "system", "content" : "Answer as concisely as possible. I will be giving you a prompt on what will a code do. Give me the code for it but don't explain how the code works. The code should come as a single output, i.e don't output the code in various parts. If creating functions, always include code for main as well"},
{"role": "user", "content" : text}]
)
print(completion['choices'][0]['message']['content'])
output=completion['choices'][0]['message']['content']
output=output.replace("```python","```")
output=(output.split("```"))[1].split("```")[0]
with open("Codes\\codefile.py", "w") as f:
f.write(output)
threading.Thread(target=code_exec, args=(['python'])).start()
speak("the code is opened in vscode and running. coding mode ended")
elif 'html' in text:
state="start"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = [{"role": "system", "content" : "Answer as concisely as possible. I will be giving you a prompt on how a webpage should look like and what will its function be. Give me the code for it but don't explain how the code works. The code should contain css and javscript code so the page is responsive. Ise the <script> and <style> tags instead of creating separate files"},
{"role": "user", "content" : text}]
)
print(completion['choices'][0]['message']['content'])
html=completion['choices'][0]['message']['content']
html=html.replace("```html","```")
html=(html.split("```"))[1].split("```")[0]
with open("Codes\\codefile.html", "w") as f:
f.write(output)
threading.Thread(target=code_exec, args=(['html'])).start()
speak("the code is opened in vscode and running. coding mode ended")
elif 'java' in text:
state="start"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = [{"role": "system", "content" : "Answer as concisely as possible. I will be giving you a prompt on what will a code do. Give me the code for it but don't explain how the code works. The code should come as a single output, i.e don't output the code in various parts. If creating functions, always include code for main as well"},
{"role": "user", "content" : text}]
)
print(completion['choices'][0]['message']['content'])
output=completion['choices'][0]['message']['content']
output=output.replace("```cpp","```")
output=(output.split("```"))[1].split("```")[0]
with open("Codes\\codefile.java", "w") as f:
f.write(output)
threading.Thread(target=code_exec, args=(['java'])).start()
speak("the code is opened in vscode and running. coding mode ended")
elif 'c plus plus' in text:
state="start"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = [{"role": "system", "content" : "Answer as concisely as possible. I will be giving you a prompt on what will a code do. Give me the code for it but don't explain how the code works. The code should come as a single output, i.e don't output the code in various parts. If creating functions, always include code for main as well"},
{"role": "user", "content" : text}]
)
print(completion['choices'][0]['message']['content'])
output=completion['choices'][0]['message']['content']
output=output.replace("```cpp","```")
output=(output.split("```"))[1].split("```")[0]
with open("Codes\\codefile.cpp", "w") as f:
f.write(output)
threading.Thread(target=code_exec, args=(['c++'])).start()
speak("the code is opened in vscode and running. coding mode ended")
elif 'c sharp' in text:
state="start"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = [{"role": "system", "content" : "Answer as concisely as possible. I will be giving you a prompt on what will a code do. Give me the code for it but don't explain how the code works. The code should come as a single output, i.e don't output the code in various parts. If creating functions, always include code for main as well"},
{"role": "user", "content" : text}]
)
print(completion['choices'][0]['message']['content'])
output=completion['choices'][0]['message']['content']
output=output.replace("```cs","```")
output=(output.split("```"))[1].split("```")[0]
with open("Codes\\codefile.cs", "w") as f:
f.write(output)
threading.Thread(target=code_exec, args=(['cs'])).start()
speak("the code is opened in vscode and running. coding mode ended")
elif ' c ' in text:
state="start"
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages = [{"role": "system", "content" : "Answer as concisely as possible. I will be giving you a prompt on what will a code do. Give me the code for it but don't explain how the code works. The code should come as a single output, i.e don't output the code in various parts. If creating functions, always include code for main as well"},
{"role": "user", "content" : text}]
)
print(completion['choices'][0]['message']['content'])
output=completion['choices'][0]['message']['content']
output=output.replace("```c","```")
output=(output.split("```"))[1].split("```")[0]
with open("Codes\\codefile.c", "w") as f:
f.write(output)
threading.Thread(target=code_exec, args=(['c'])).start()
speak("the code is opened in vscode and running. coding mode ended")
else:
speak("i'm sorry, i couldn't understand what you meant. please specify the language you want the code in.")
#function for voice mouse
def voice_mouse(text):
global state
if "mode reset" in text:
state="start"
speak("you have stopped voice mouse")
if "left click" in text:
count=text.count("left click")
for i in range(count):
pyautogui.click()
if "right click" in text:
count=text.count("right click")
for i in range(count):
pyautogui.click(button='right')
if "scroll up" in text:
count=text.count("scroll up")
pyautogui.scroll(count*100)
if "scroll down" in text:
count=text.count("scroll down")
pyautogui.scroll(count*-100)
if "up" in text:
count=text.count("up")
pyautogui.moveRel(0, count*-100, duration=0.2)
if "down" in text:
count=text.count("down")
pyautogui.moveRel(0, count*100, duration=0.2)
if "left" in text:
count=text.count("left")
pyautogui.moveRel(count*-100, 0, duration=0.2)
if "right" in text:
count=text.count("right")
pyautogui.moveRel(count*100, 0, duration=0.2)
# function to type
def type_text(text):
global state
if "end typing code confirm" in text:
text=text.replace("end typing code confirm","")
state="start"
speak("you have stopped typing")
if text=="":
pass
else:
punctuated_text = model.restore_punctuation(text)
pyautogui.typewrite(punctuated_text,0.1)
# Define a function to recognize speech
def recognize_speech():
global state
r = sr.Recognizer()
while True:
with sr.Microphone() as source:
print("Speak now...")
audio = r.listen(source)
print("Processing...")
text=""
try:
text = r.recognize_google(audio)
text=text.lower()
except sr.UnknownValueError:
print("Sorry, could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
finally:
if state=="type":
threading.Thread(target=type_text, args=(text,)).start()
print("You said: " + text)
elif state=="voicemouse":
threading.Thread(target=voice_mouse, args=(text,)).start()
print("You said: " + text)
elif state=="code":
threading.Thread(target=codify, args=(text,)).start()
print("You said: " + text)
else:
threading.Thread(target=check_text, args=(text,)).start()
print("You said: " + text)
# Start a new thread for speech recognition
speech_thread = threading.Thread(target=recognize_speech)
speech_thread.start()
# Create tkinter window
root = tk.Tk()
root.overrideredirect(True)
root.geometry("300x100+{}+{}".format(ctypes.windll.user32.GetSystemMetrics(0) - 320, 20))
root.resizable(False, False)
root.attributes("-alpha", 0.6)
root.attributes("-topmost", True)
# Create canvas for plot
canvas = tk.Canvas(root, width=300, height=100, highlightthickness=0)
canvas.pack()
# Embed plot in canvas
plot_widget = FigureCanvasTkAgg(fig, master=canvas)
plot_widget.draw()
plot_widget.get_tk_widget().place(relx=0.5, rely=0.5, anchor="center")
# Start tkinter event loop
root.mainloop()
# Stop and close audio stream
stream.stop_stream()
stream.close()
p.terminate()