-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
243 lines (209 loc) · 8.26 KB
/
gui.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
# Imports.
from asyncio.windows_events import NULL
from PyQt5.QtWidgets import *
from PyQt5 import QtGui
from styles import styles
from googletrans import Translator
from PyQt5.QtCore import Qt
import sys
import os
import yake
import requests
import cv2
# Initializing translator and keyword searching system.
translator = Translator()
kw_extractor = yake.KeywordExtractor()
language = "en"
max_ngram_size = 1
deduplication_threshold = 0.9
numOfKeywords = 1
image = NULL
reso_image = NULL
custom_kw_extractor = yake.KeywordExtractor(
lan=language,
n=max_ngram_size,
dedupLim=deduplication_threshold,
top=numOfKeywords,
features=None,
)
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.init()
def init(self):
self.layout = QHBoxLayout()
self.layout1 = QVBoxLayout()
self.image = QLabel(self)
self.image.setStyleSheet(styles.text_style)
self.image.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.label = QLabel("Preparing...", self)
self.label.setAlignment(
Qt.AlignmentFlag.AlignCenter | Qt.AlignmentFlag.AlignTop
)
self.label.setStyleSheet(styles.text_style)
self.label.setVisible(False)
self.environment = os.environ["HOMEPATH"]
self.dir = os.getcwd()
self.textInput = QTextEdit()
self.textInput.setStyleSheet(styles.text_edit_style)
self.textInput.setPlaceholderText("Enter text here (280 character limit)")
self.resolution_list = ["Resolution", "256", "512", "736"]
self.numIterations_list = ["NumIterations", "400", "600", "800"]
self.resolution_combobox = QComboBox()
self.resolution_combobox.setStyleSheet(styles.combobox_style)
self.resolution_combobox.addItems(self.resolution_list)
self.resolution_combobox.setPlaceholderText("Resolution")
self.numIterations_combobox = QComboBox()
self.numIterations_combobox.setStyleSheet(styles.combobox_style)
self.numIterations_combobox.addItems(self.numIterations_list)
self.output_path_button = QPushButton("Output Path")
self.output_path_button.setStyleSheet(styles.button_style)
self.output_path_button.clicked.connect(self.modify_output_path)
self.create_button = QPushButton("Save")
self.create_button.setStyleSheet(styles.button_style)
self.create_button.clicked.connect(self.timeout)
self.reset_button = QPushButton("Reset")
self.reset_button.setStyleSheet(styles.button_style)
self.reset_button.clicked.connect(self.reset)
self.placement()
self.setLayout(self.layout)
def reset(self):
# This function sets all fields to default.
self.textInput.setText("")
self.image.setPixmap(QtGui.QPixmap(""))
self.resolution_combobox.setCurrentIndex(0)
self.numIterations_combobox.setCurrentIndex(0)
self.label.setVisible(False)
def placement(self):
# Layout and widget management.
h_box = QHBoxLayout()
h_box.addWidget(self.output_path_button)
h_box.addWidget(self.create_button)
h_box.addWidget(self.reset_button)
self.layout1.addWidget(self.textInput)
self.layout1.addWidget(self.resolution_combobox)
self.layout1.addWidget(self.numIterations_combobox)
self.layout1.addLayout(h_box)
self.layout1.addWidget(self.label)
self.layout1.addWidget(self.image)
self.layout1.addStretch()
self.layout.addStretch()
self.layout.addLayout(self.layout1)
self.layout.addStretch()
def timeout(self):
# After searching for keywords, using some conditional statements so we could know if there is something missing. Yn case everything is alright, we can trigger our timeout_subfunc function.
global keywords
keywords = custom_kw_extractor.extract_keywords(self.textInput.toPlainText())
# print(keywords)
if keywords == []:
self.message_box(
QMessageBox.Warning,
QMessageBox.Ok,
"Warning",
"No keywords found. Please enter some text.",
)
return
if self.textInput.toPlainText() == "":
self.message_box(
QMessageBox.Icon.Warning,
QMessageBox.Ok,
"Warning",
"Please enter a valid text input.",
)
elif len(self.textInput.toPlainText()) > 280:
self.message_box(
QMessageBox.Icon.Warning,
QMessageBox.Ok,
"Warning",
"There is a 280 character limit on the text input.",
)
elif self.resolution_combobox.currentText() == "Resolution":
self.message_box(
QMessageBox.Icon.Warning,
QMessageBox.Ok,
"Warning",
"Please select a valid resolution.",
)
elif self.numIterations_combobox.currentText() == "NumIterations":
self.message_box(
QMessageBox.Icon.Warning,
QMessageBox.Ok,
"Warning",
"Please select a valid number of iterations.",
)
else:
self.timeout_subfunc()
def timeout_subfunc(self):
# Triggering create_image function and then resizing the image if desired.
self.label.setVisible(True)
self.message_box(
QMessageBox.Icon.Information,
QMessageBox.Ok,
"Information",
"Creating image process has been started.",
)
self.create_image()
image = cv2.imread(os.path.join(self.dir, "artymind_output.png"))
if self.resolution_combobox.currentText() == "256":
reso_image = cv2.resize(image, (256, 256))
if self.resolution_combobox.currentText() == "512":
reso_image = cv2.resize(image, (512, 512))
if self.resolution_combobox.currentText() == "736":
reso_image = cv2.resize(image, (736, 736))
cv2.imwrite(os.path.join(self.dir, "artymind_output.png"), reso_image)
def create_image(self):
# Gathering the image using the DeepAI Api.
word = keywords[0][0]
translated_word = translator.translate(word)
print(translated_word.text)
r = requests.post(
"https://api.deepai.org/api/text2img",
data={
"text": translated_word.text,
},
headers={"api-key": "Enter your api-key here."},
)
# print(r.json())
response = requests.get(r.json()["output_url"])
with open(f"{self.dir}/artymind_output.png", "wb") as file:
file.write(response.content)
pixmap = QtGui.QPixmap(f"{self.dir}/artymind_output.png")
self.image.setPixmap(pixmap)
self.label.setVisible(False)
self.message_box(
QMessageBox.Information,
QMessageBox.Ok,
"Success",
f"Image created as {self.dir}/artymind_output.png.",
)
def message_box(self, icon, buttons, title, text):
# Just a function to simply create message dialogs.
msg = QMessageBox()
msg.setIcon(icon)
msg.setText(text)
msg.setStandardButtons(buttons)
msg.setWindowTitle(title)
return msg.exec()
def modify_output_path(self):
# Modifying output path if needed. The output image will appear in that path.
self.temp_path = self.dir
self.dir = QFileDialog.getExistingDirectory(
None, "Select project folder:", self.environment, QFileDialog.ShowDirsOnly
)
if self.dir == "":
self.dir = self.temp_path
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setStyleSheet("background-color: rgba(255, 255, 255, .1);")
self.setGeometry(400, 200, 1200, 600)
self.startMainMenu()
def startMainMenu(self):
self.window = Window(self)
self.setWindowTitle("Artymind")
self.setCentralWidget(self.window)
self.showMaximized()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec())