-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatBrowser.py
143 lines (118 loc) · 4.44 KB
/
chatBrowser.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
from PyQt6.QtCore import Qt, pyqtSignal
from PyQt6.QtWidgets import QScrollArea, QVBoxLayout, QWidget, QLabel, QHBoxLayout, QTextEdit
class ChatBrowser(QScrollArea):
def __init__(self):
super().__init__()
self.__initUi()
def __initUi(self):
lay = QVBoxLayout()
lay.setAlignment(Qt.AlignmentFlag.AlignTop)
lay.setSpacing(0)
lay.setContentsMargins(0, 0, 0, 0)
widget = QWidget()
widget.setLayout(lay)
self.setWidget(widget)
self.setWidgetResizable(True)
def setMessages(self, messages):
for message in messages:
self.addMessage(message)
def __setAndGetLabel(self, text):
chatLbl = QLabel(text)
chatLbl.setWordWrap(True)
chatLbl.setTextInteractionFlags(Qt.TextInteractionFlag.TextSelectableByMouse)
return chatLbl
def addChunk(self, chunk):
"""
For streaming messages (by AI)
:param chunk:
:return:
"""
# For temporary measure to add messages in chunks
# Currently i don't want to make this application anymore complex
if self.widget().layout().count() % 2 == 1:
chatLbl = self.__setAndGetLabel(chunk)
chatLbl.setStyleSheet('QLabel { background-color: #AAA; padding: 1em }')
self.widget().layout().addWidget(chatLbl)
else:
# The unit is AI
chatLbl = self.__getLastUnit()
if chatLbl:
chatLbl.setText(chatLbl.text() + chunk)
def __getLastUnit(self) -> QLabel | None:
item = self.widget().layout().itemAt(self.widget().layout().count() - 1)
if item:
return item.widget()
else:
return None
def addMessage(self, message):
"""
For none-streaming messages (by user)
:param message:
:return:
"""
content = message['content']
role = message['role']
chatLbl = self.__setAndGetLabel(content)
if role == 'user':
chatLbl.setStyleSheet('QLabel { padding: 1em }')
else:
chatLbl.setStyleSheet('QLabel { background-color: #AAA; padding: 1em }')
self.widget().layout().addWidget(chatLbl)
def event(self, e):
if e.type() == 43:
self.verticalScrollBar().setSliderPosition(self.verticalScrollBar().maximum())
return super().event(e)
def getAllText(self):
all_text_lst = []
lay = self.widget().layout()
if lay:
for i in range(lay.count()):
if lay.itemAt(i) and lay.itemAt(i).widget():
widget = lay.itemAt(i).widget()
if isinstance(widget, QLabel):
all_text_lst.append(widget.text())
return '\n'.join(all_text_lst)
def clearMessages(self):
lay = self.widget().layout()
if lay:
for i in range(lay.count()-1, -1, -1):
lay.removeWidget(lay.itemAt(i).widget())
class TextEditPrompt(QTextEdit):
returnPressed = pyqtSignal(str)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__initUi()
def __initUi(self):
self.setStyleSheet('QTextEdit { border: 1px solid #AAA; } ')
self.setAcceptRichText(False)
def keyPressEvent(self, e):
if e.key() == Qt.Key.Key_Return or e.key() == Qt.Key.Key_Enter:
if e.modifiers() == Qt.KeyboardModifier.ShiftModifier:
return super().keyPressEvent(e)
else:
self.returnPressed.emit(self.toPlainText())
else:
return super().keyPressEvent(e)
class PromptWidget(QWidget):
sendPrompt = pyqtSignal(str)
def __init__(self):
super().__init__()
self.__initUi()
def __initUi(self):
self.__textEdit = TextEditPrompt()
self.__textEdit.textChanged.connect(self.updateHeight)
self.__textEdit.returnPressed.connect(self.__sendPrompt)
lay = QHBoxLayout()
lay.addWidget(self.__textEdit)
lay.setContentsMargins(0, 0, 0, 0)
self.setLayout(lay)
self.updateHeight()
def __sendPrompt(self, text):
self.sendPrompt.emit(text)
self.__textEdit.clear()
def updateHeight(self):
document = self.__textEdit.document()
height = document.size().height()
self.setMaximumHeight(int(height + document.documentMargin()))
def getTextEdit(self):
return self.__textEdit