-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (98 loc) · 3.2 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
import tkinter
from tkinter import *
from tkinter.messagebox import showerror, showwarning, showinfo
import openai
import pyperclip
import os
if os.path.exists("key.txt"):
print("The file 'key.txt' exists")
with open('key.txt', 'r') as f:
key = f.read()
f.close()
else:
print("The file 'key.txt' does not exist")
#open the file "key.txt" in write mode
with open("key.txt", "w") as f:
print("The file key.txt was created (but is empty)")
key = ""
#close the file
f.close()
root = tkinter.Tk()
root.geometry("400x525")
label = tkinter.Label(root, text="PromptGUI", font=("Arial", 70))
label.pack()
space = tkinter.Label(root, text=" ")
space.pack()
input_label = tkinter.Label(root, text="Input:")
input_label.pack()
text_field = tkinter.Entry(root)
text_field.pack()
tkinter.Label(root, text="(character limit: 100)").pack()
def generate():
try:
text_fieldInput = text_field.get()
if len(text_fieldInput) > 100:
text_fieldInput = text_fieldInput[:100]
showwarning(title='Warning', message=f"More than 100 Characters in text field. I've cut it to 100. 👍")
# Doing 100% API key stuff :)
with open('key.txt', 'r') as f:
openai.api_key_path = "key.txt"
f.close()
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"""Human: {text_fieldInput}
Assistant:""",
max_tokens=100,
temperature=0.5,
stop=["Human:", "Assistant:"]
)
output_text.config(text=response.choices[0].text)
except:
showwarning(title='Warning', message=f"Something went wrong. Is the API Key correct?")
button = tkinter.Button(root, text="Generate!", command=generate)
button.pack()
space = tkinter.Label(root, text=" ")
space.pack()
output_label = tkinter.Label(root, text="Output:")
output_label.pack()
output_text = tkinter.Label(root, text="There is no output yet...", font="italic", wraplength=300, justify="center")
output_text.pack()
def copy_to_clipboard():
pyperclip.copy(str(output_text.cget("text")))
copyButton = tkinter.Button(root, text="Copy Output", command=copy_to_clipboard)
copyButton.pack()
# This is space between the actual interface and the few API-Key-Set buttons
space = tkinter.Label(root, text=" ")
space.pack()
space = tkinter.Label(root, text=" ")
space.pack()
space = tkinter.Label(root, text=" ")
space.pack()
space = tkinter.Label(root, text=" ")
space.pack()
space = tkinter.Label(root, text=" ")
space.pack()
space = tkinter.Label(root, text=" ")
space.pack()
apiKey_label = tkinter.Label(root, text="Set your API Key:")
apiKey_label.pack()
# Create input field
apiInputField = Entry(root)
apiInputField.pack()
# Check if key is not empty
if key != "":
apiInputField.delete(0, END)
apiInputField.insert(0, "Key was already set.")
# Create button
def apiSetButton():
print("API set!")
showinfo(title='Info', message=f"API Key was successfully set!")
key = str(apiInputField.get())
with open("key.txt", "w") as f:
f.truncate(0)
f.write(key)
#close the file
f.close()
apiSetButton = Button(root, text="Set Key", command=apiSetButton)
apiSetButton.pack()
root.mainloop()