-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
383 lines (325 loc) · 14.3 KB
/
Copy pathGUI.py
File metadata and controls
383 lines (325 loc) · 14.3 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QStackedWidget, QWidget, QLineEdit, QGridLayout, QVBoxLayout, QHBoxLayout, QPushButton, QFrame, QLabel, QSizePolicy
from PyQt5.QtGui import QIcon, QPainter, QMovie, QColor, QTextCharFormat, QFont, QPixmap
from PyQt5.QtCore import Qt, QSize, QTimer
from dotenv import dotenv_values
import sys
import os
from PyQt5.QtGui import QTextCursor
env_vars = dotenv_values(".env")
Assistantname = env_vars.get("Assistantname")
current_dir = os.getcwd()
old_chat_message = ""
TempDirPath = rf"{current_dir}\Frontend\Files"
GraphicsDirPath = rf"{current_dir}\Frontend\Graphics"
def AnswerModifier(Answer):
lines = Answer.split('\n')
non_empty_lines = [line for line in lines if line.strip()]
modified_answer = '\n'.join(non_empty_lines)
return modified_answer
def QueryModifier(Query):
new_query = Query.lower().strip()
query_words = ["how", "what", "who", "where", "when", "why", "which", "whose", "whom", "can you" ,"what's", "where's" , "how' s" ]
if any(word + " " in new_query for word in query_words):
if query_words[-1][-1] in ['.', '?', '!']:
new_query = new_query[:-1] + "?"
else:
new_query += "?"
else:
if query_words[-1][-1] in ['.', '?', '!']:
new_query = new_query[:-1] + "."
else:
new_query += ""
return new_query.capitalize()
def SetMicrophoneStatus(Command):
with open(rf'{TempDirPath}\Mic.data', "w", encoding='utf-8') as file:
file.write(Command)
def GetMicrophoneStatus():
with open(rf'{TempDirPath}\Mic.data', "r", encoding='utf-8') as file:
Status = file.read()
return Status
def SetAssistantStatus(Status):
with open(rf'{TempDirPath}\Status.data', "w", encoding='utf-8') as file:
file.write(Status)
def GetAssistantStatus():
with open(rf'{TempDirPath}\Status.data', "r", encoding='utf-8') as file:
Status = file.read()
return Status
def MicButtonInitialed():
SetMicrophoneStatus("False")
def MicButtonClosed():
SetMicrophoneStatus("True")
def GraphicsDirectoryPath(Filename):
Path = rf'{GraphicsDirPath}\{Filename}'
return Path
def TempDirectoryPath(Filename):
Path = rf'{TempDirPath}\{Filename}'
return Path
def ShowTextToScreen(Text):
with open(rf'{TempDirPath}\Responses.data', "w", encoding='utf-8') as file:
file.write(Text)
# Define the InitialScreen class
class InitialScreen(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
layout = QVBoxLayout(self)
layout.setAlignment(Qt.AlignCenter)
# Create the label for the GIF
self.gif_label = QLabel()
self.gif_label.setStyleSheet("border: none;")
movie = QMovie(GraphicsDirectoryPath('jarvis.gif'))
max_gif_size_W = 1080 # Larger width for the GIF
max_gif_size_H = 608 # Larger height for the GIF
movie.setScaledSize(QSize(max_gif_size_W, max_gif_size_H))
self.gif_label.setMovie(movie)
movie.start()
# Add the GIF label to the layout
layout.addWidget(self.gif_label)
# Create the label for the JARVIS text
self.jarvis_label = QLabel("JARVIS")
self.jarvis_label.setStyleSheet("color: white; font-size: 36px; font-weight: bold;")
self.jarvis_label.setAlignment(Qt.AlignCenter)
layout.addWidget(self.jarvis_label)
# Toggle button for microphone
self.toggle_button = QPushButton("Toggle Microphone")
self.toggle_button.setStyleSheet("background-color: white; color: black")
self.toggle_button.clicked.connect(self.toggle_icon)
layout.addWidget(self.toggle_button)
self.toggled = False
self.setLayout(layout)
self.setStyleSheet("background-color: black;")
def toggle_icon(self, event=None):
if self.toggled:
self.load_icon(GraphicsDirectoryPath('Mic_off.png'), 60, 60)
MicButtonInitialed()
else:
self.load_icon(GraphicsDirectoryPath('Mic_on.png'), 60, 60)
MicButtonClosed()
self.toggled = not self.toggled
def load_icon(self, path, width=60, height=60):
pixmap = QPixmap(path)
new_pixmap = pixmap.scaled(width, height)
self.toggle_button.setIcon(QIcon(new_pixmap))
from PyQt5.QtGui import QTextCursor, QIcon, QPixmap
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QTextEdit, QLabel, QPushButton, QSizePolicy, QFrame
class ChatSection(QWidget):
def __init__(self):
super(ChatSection, self).__init__()
layout = QVBoxLayout(self)
layout.setContentsMargins(-10, 40, 40, 100)
layout.setSpacing(-100)
self.chat_text_edit = QTextEdit()
self.chat_text_edit.setReadOnly(True)
self.chat_text_edit.setTextInteractionFlags(Qt.NoTextInteraction)
self.chat_text_edit.setFrameStyle(QFrame.NoFrame)
layout.addWidget(self.chat_text_edit)
self.setStyleSheet("background-color: black;")
layout.setSizeConstraint(QVBoxLayout.SetDefaultConstraint)
layout.setStretch(1, 1)
self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))
text_color = QColor(Qt.white)
text_color_text = QTextCharFormat()
text_color_text.setForeground(text_color)
self.chat_text_edit.setCurrentCharFormat(text_color_text)
self.gif_label = QLabel()
self.gif_label.setStyleSheet("border: none;")
movie = QMovie(GraphicsDirectoryPath('Jarvis.gif'))
max_gif_size_W = 720
max_gif_size_H = 405
movie.setScaledSize(QSize(max_gif_size_W, max_gif_size_H))
self.gif_label.setAlignment(Qt.AlignRight | Qt.AlignBottom)
self.gif_label.setMovie(movie)
movie.start()
layout.addWidget(self.gif_label)
self.label = QLabel("")
self.label.setStyleSheet("color: white; font-size:16px; margin-right: 195px; border: none; margin-top: -30px;")
self.label.setAlignment(Qt.AlignRight)
layout.addWidget(self.label)
layout.setSpacing(-10)
layout.addWidget(self.gif_label)
font = QFont()
font.setPointSize(13)
self.chat_text_edit.setFont(font)
self.timer = QTimer(self)
self.timer.timeout.connect(self.loadMessages)
self.timer.timeout.connect(self.SpeechRecogText)
self.timer.start(5)
self.chat_text_edit.viewport().installEventFilter(self)
# Toggle button for microphone
self.toggle_button = QPushButton("Start Microphone")
self.toggle_button.setStyleSheet("background-color: white; color: black")
self.toggle_button.clicked.connect(self.toggle_icon) # Connect the button to the toggle_icon method
layout.addWidget(self.toggle_button)
self.toggled = False # Track microphone state
def loadMessages(self):
global old_chat_message
with open(TempDirectoryPath('Responses.data'), "r", encoding='utf-8') as file:
messages = file.read()
if None == messages:
pass
elif len(messages) <= 1:
pass
elif str(old_chat_message) == str(messages):
pass
else:
self.addMessage(message=messages, color='White')
old_chat_message = messages
def SpeechRecogText(self):
with open(TempDirectoryPath('Status.data'), "r", encoding='utf-8') as file:
messages = file.read()
self.label.setText(messages)
def addMessage(self, message, color):
"""This method adds a message to the QTextEdit."""
current_text = self.chat_text_edit.toPlainText()
new_text = f"{message}\n"
self.chat_text_edit.setText(current_text + new_text)
self.chat_text_edit.moveCursor(QTextCursor.End) # Use QTextCursor instead of Qt.TextCursor
def toggle_icon(self):
if not self.toggled: # If the microphone is not already on
self.toggle_button.setText("Stop Microphone") # Change button text to "Stop Microphone"
MicButtonClosed()
else: # If the microphone is already on
self.toggle_button.setText("Start Microphone") # Change button text back to "Start Microphone"
MicButtonInitialed()
self.toggled = not self.toggled
def load_icon(self, path, width=60, height=60):
pixmap = QPixmap(path)
new_pixmap = pixmap.scaled(width, height)
self.toggle_button.setIcon(QIcon(new_pixmap))
class MessageScreen(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
desktop = QApplication.desktop()
screen_width = desktop.screenGeometry().width()
screen_height = desktop.screenGeometry().height()
layout = QVBoxLayout ()
label = QLabel("")
layout.addWidget(label)
chat_section = ChatSection()
layout.addWidget(chat_section)
self.setLayout(layout)
self.setStyleSheet("background-color: black;")
self.setFixedHeight(screen_height)
self.setFixedWidth(screen_width)
class CustomTopBar(QWidget):
def __init__(self, parent, stacked_widget):
super().__init__(parent)
self.initUI()
self.current_screen = None
self.stacked_widget = stacked_widget
def initUI(self):
self.setFixedHeight(50)
layout = QHBoxLayout(self)
layout.setAlignment(Qt.AlignRight)
home_button = QPushButton()
home_icon = QIcon(GraphicsDirectoryPath("Home.png"))
home_button.setIcon(home_icon)
home_button.setText(" Home")
home_button.setStyleSheet("height:40px; line-height:40px ; background-color:white ; color: black")
message_button = QPushButton()
message_icon = QIcon(GraphicsDirectoryPath("Chats.png"))
message_button.setIcon(message_icon)
message_button.setText(" Chat")
message_button.setStyleSheet("height:40px; line-height:40px; background-color:white ; color: black")
minimize_button = QPushButton()
minimize_icon = QIcon(GraphicsDirectoryPath('Minimize2.png'))
minimize_button.setIcon(minimize_icon)
minimize_button.setStyleSheet("background-color:white")
minimize_button.clicked.connect(self.minimizeWindow)
self.maximize_button = QPushButton()
self.maximize_icon = QIcon(GraphicsDirectoryPath('Maximize.png'))
self.restore_icon = QIcon(GraphicsDirectoryPath('Minimize.png'))
self.maximize_button.setIcon(self.maximize_icon)
self.maximize_button.setFlat(True)
self.maximize_button.setStyleSheet("background-color:white")
self.maximize_button.clicked.connect(self.maximizeWindow)
close_button = QPushButton()
close_icon = QIcon(GraphicsDirectoryPath('Close.png'))
close_button.setIcon(close_icon)
close_button.setStyleSheet("background-color:white")
close_button.clicked.connect(self.closeWindow)
line_frame = QFrame()
line_frame.setFixedHeight(1)
line_frame.setFrameShape(QFrame.HLine)
line_frame.setFrameShadow(QFrame.Sunken)
line_frame.setStyleSheet("border-color: black;")
title_label = QLabel(f" {str(Assistantname).capitalize()} AI ")
title_label.setStyleSheet("color: black; font-size: 18px ;; background-color:white")
home_button.clicked.connect(lambda: self.stacked_widget.setCurrentIndex(0))
message_button.clicked.connect(lambda: self.stacked_widget.setCurrentIndex(1))
layout.addWidget(title_label)
layout.addStretch(1)
layout.addWidget(home_button)
layout.addWidget(message_button)
layout.addStretch(1)
layout.addWidget(minimize_button)
layout.addWidget(self.maximize_button)
layout.addWidget(close_button)
layout.addWidget(line_frame)
self.draggable = True
self.offset = None
def paintEvent(self, event):
painter = QPainter(self)
painter.fillRect(self.rect(), Qt.white)
super().paintEvent(event)
def minimizeWindow(self):
self.parent().showMinimized()
def maximizeWindow(self):
if self.parent().isMaximized():
self.parent().showNormal()
self.maximize_button.setIcon(self.maximize_icon)
else:
self.parent().showMaximized()
self.maximize_button.setIcon(self.restore_icon)
def closeWindow(self):
self.parent().close()
def mousePressEvent(self, event):
if self.draggable:
self.offset = event.pos()
def mouseMoveEvent(self, event):
if self.draggable and self.offset:
new_pos = event.globalPos() - self.offset
self.parent().move(new_pos)
def showMessageScreen(self):
if self.current_screen is not None:
self.current_screen.hide()
message_screen = MessageScreen(self)
layout = self.parent().layout()
if layout is not None:
layout.addWidget(message_screen)
self.current_screen = message_screen
def showInitialScreen(self):
if self.current_screen is not None:
self.current_screen.hide()
initial_screen = InitialScreen(self)
layout = self.parent().layout()
if layout is not None:
layout.addWidget(initial_screen)
self.current_screen = initial_screen
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowFlags(Qt.FramelessWindowHint)
self.initUI()
def initUI(self):
desktop = QApplication.desktop()
screen_width = desktop.screenGeometry().width()
screen_height = desktop.screenGeometry().height()
stacked_widget = QStackedWidget(self)
initial_screen = InitialScreen()
message_screen = MessageScreen()
stacked_widget.addWidget(initial_screen)
stacked_widget.addWidget(message_screen)
self.setGeometry(0, 0, screen_width, screen_height)
self.setStyleSheet("background-color: black;")
top_bar = CustomTopBar(self, stacked_widget)
self.setMenuWidget(top_bar)
self.setCentralWidget(stacked_widget)
self.show()
def GraphicalUserInterface():
app = QApplication(sys.argv)
app.setStyle('Fusion')
window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
GraphicalUserInterface()