-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
202 lines (164 loc) · 5.95 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
# AutoCode v0.1 By R.Hasaranga
# https://github.com/hasaranga/AutoCode
from tkinter import *
from tkinter.messagebox import *
import multiprocessing
import requests
from functools import partial
from threading import Thread
from tkinter.filedialog import *
from tkinter import simpledialog
import queue
# packages which are ChatGPT forget to import sometimes.
from urllib.request import urlopen
import base64
import math
import time
apiKey = ""
class CodeWindowState:
process = 0
codeTextArea = 0
def runTheCode(txtCode):
try:
exec(txtCode, globals())
except Exception as e:
showinfo("Error",str(e))
return
def onRunCodePress(state):
try:
state.process.terminate()
except Exception as e:
pass
state.process = multiprocessing.Process(target=runTheCode, args=(state.codeTextArea.get(1.0,END),))
state.process.start()
return
def onSaveCodePress(state):
file = asksaveasfilename(initialfile='code.py', defaultextension=".py", filetypes=[("Python Files","*.py")])
if file != "":
file = open(file,"w")
file.write(state.codeTextArea.get(1.0,END))
file.close()
return
def onWindowClose(window, state):
try:
state.process.terminate()
except Exception as e:
pass
window.destroy()
return
def setAPIKey():
global apiKey
txt = simpledialog.askstring(title="Set API Key",prompt="Your API Key:",show='*',initialvalue=apiKey)
if txt != None:
apiKey = txt.strip()
file = open("config.dat","w")
file.write(apiKey)
file.close()
return
def generateCode(prompt, taskMsgQueue):
generatedCode = ""
global apiKey
try:
headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer '+ apiKey}
body = '{"model": "text-davinci-003", "prompt": "python code to do ' + prompt + '", "temperature": 0.5, "max_tokens": 4000}'
response = requests.post('https://api.openai.com/v1/completions', data=body, headers=headers)
generatedCode = response.json()['choices'][0]['text']
except Exception as e:
pass
codeWindow = Tk()
codeWindow.title('Generated Code')
codeWindowWidth = 800
codeWindowHeight = 500
screenWidth = codeWindow.winfo_screenwidth()
screenHeight = codeWindow.winfo_screenheight()
left = (screenWidth - codeWindowWidth) / 2
top = (screenHeight - codeWindowHeight) /2
codeWindow.geometry('%dx%d+%d+%d' % (codeWindowWidth, codeWindowHeight, left, top))
codeWindow.grid_rowconfigure(0, weight=1)
codeWindow.grid_columnconfigure(0, weight=1)
codeTextArea = Text(codeWindow)
codeTextArea.grid(sticky = N + E + S + W)
codeTextArea.insert(1.0, generatedCode)
state = CodeWindowState()
state.codeTextArea = codeTextArea
codeMenuBar = Menu(codeWindow)
codeFileMenu = Menu(codeMenuBar, tearoff=0)
codeFileMenu.add_command(label="Run Code", command=partial(onRunCodePress, state))
codeFileMenu.add_separator()
codeFileMenu.add_command(label="Save Code...", command=partial(onSaveCodePress, state))
codeMenuBar.add_cascade(label="File", menu=codeFileMenu)
codeWindow.config(menu=codeMenuBar)
codeWindow.protocol("WM_DELETE_WINDOW", partial(onWindowClose, codeWindow, state))
taskMsgQueue.put("generated")
codeWindow.mainloop()
return
def onGenerateCodePress(textArea, fileMenu, taskQueue):
prompt = textArea.get(1.0,END).strip()
if prompt == "":
return
global apiKey
if apiKey == "":
showinfo("Error", "Please set your API Key")
return
fileMenu.entryconfig("Generate Code",state="disabled")
t = Thread(target=generateCode, args=(prompt,taskMsgQueue,))
t.start()
return
def onSaveTextPress(textArea):
prompt = textArea.get(1.0,END).strip()
if prompt == "":
return
file = asksaveasfilename(initialfile='prompt.txt', defaultextension=".txt", filetypes=[("Text Files","*.txt")])
if file != "":
file = open(file,"w")
file.write(prompt)
file.close()
return
def onAboutPress():
showinfo("About","AutoCode v0.1\nBy Ruchira Hasaranga.\nhttps://github.com/hasaranga/AutoCode")
return
def process_queue(window, fileMenu, taskQueue):
try:
msg = taskQueue.get_nowait()
if msg == "generated":
fileMenu.entryconfig("Generate Code",state="normal")
except queue.Empty:
pass
window.after(1000, process_queue, window, fileMenu, taskQueue)
return
if __name__ == "__main__":
try:
file = open("config.dat","r")
apiKey = file.read()
file.close()
except Exception as e:
pass
window = Tk()
window.title('AutoCode v0.1')
thisWidth = 600
thisHeight = 400
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()
left = (screenWidth - thisWidth) / 2
top = (screenHeight - thisHeight) /2
window.geometry('%dx%d+%d+%d' % (thisWidth, thisHeight, left, top))
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
textArea = Text(window)
textArea.grid(sticky = N + E + S + W)
menuBar = Menu(window)
taskMsgQueue = queue.Queue()
fileMenu = Menu(menuBar, tearoff=0)
fileMenu.add_command(label="Generate Code", command=partial(onGenerateCodePress, textArea, fileMenu, taskMsgQueue))
fileMenu.add_separator()
fileMenu.add_command(label="Save Text...", command=partial(onSaveTextPress, textArea))
menuBar.add_cascade(label="File", menu=fileMenu)
settingsMenu = Menu(menuBar, tearoff=0)
settingsMenu.add_command(label="Set API Key...", command=setAPIKey)
menuBar.add_cascade(label="Settings", menu=settingsMenu)
helpMenu = Menu(menuBar, tearoff=0)
helpMenu.add_command(label="About", command=onAboutPress)
menuBar.add_cascade(label="Help", menu=helpMenu)
window.config(menu=menuBar)
window.after(1000, process_queue, window, fileMenu, taskMsgQueue)
window.mainloop()