-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_window.py
194 lines (153 loc) · 7.88 KB
/
chat_window.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
from PyQt6.QtWidgets import QWidget, QVBoxLayout, QSpacerItem, QSizePolicy, QApplication, QLabel
from PyQt6.QtGui import QPixmap
from input_widget import Ui_Form as Input_Form
from output_widget import Ui_Form as Output_Form
from markdown import markdown
class InputWidget(QWidget):
def __init__(self, parent=None, chat_obj=None):
super().__init__(parent)
self.input_ui = Input_Form()
self.input_ui.setupUi(self)
self.chat_obj = chat_obj
self.input_label = self.input_ui.input_label
def set_input_text(self, input_str):
self.input_label.setText(input_str)
class OutWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.output_ui = Output_Form()
self.output_ui.setupUi(self)
self.output_label = self.output_ui.output_lable
# Set QSizePolicy to Expanding for the label_2 widget
self.output_label.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
def set_output_text(self, out_str):
self.output_label.setText(out_str)
class ChatWindow(QWidget):
def __init__(self, parent=None, chat_obj=None, chat_data=None):
super().__init__(parent)
self.chat_object = chat_obj
self.chat_data = chat_data
self.main_verticalLayout = QVBoxLayout(self)
self.main_verticalLayout.setContentsMargins(0, 0, 0, 0)
self.main_verticalLayout.setSpacing(0)
self.main_verticalLayout.setObjectName("main_verticalLayout")
self.style_str = """
QPushButton,
QLabel {
border: none;
padding: 5px;
}
QWidget {
background: transparent;
}
"""
self.setStyleSheet(self.style_str)
self.chats_data = {
"title": "",
"chatlist": []
}
if self.chat_data:
self.chats_data["title"] = self.chat_data["title"]
self.chats_data["chatlist"] += self.chat_data["chatlist"]
self.show_chats()
def show_chats(self):
chat_list = self.chats_data.get("chatlist")
for chat in chat_list:
input_str = chat.get("input_str")
input_widget = InputWidget(chat_obj=self.chat_object)
input_widget.set_input_text(input_str)
self.main_verticalLayout.addWidget(input_widget)
out_str = chat.get("output_str")
out_widget = OutWidget()
markdown_text = markdown(out_str, extensions=['markdown.extensions.extra', 'markdown.extensions.attr_list'])
markdown_text = markdown_text.replace('<table>', '<table style="border: 1px solid white; padding: 10px; border-collapse: collapse;">')
markdown_text = markdown_text.replace('<th>', '<th style="border: 1px solid white; padding: 10px;">')
markdown_text = markdown_text.replace('<td>', '<td style="border: 1px solid white; padding: 10px;">')
out_widget.set_output_text(markdown_text)
self.main_verticalLayout.addWidget(out_widget)
attached_pdf_info = chat.get("pdf_info")
if attached_pdf_info:
pdf_widget = OutWidget()
pdf_widget.set_output_text(attached_pdf_info)
self.main_verticalLayout.addWidget(pdf_widget)
spacerItem = QSpacerItem(20, 300, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
self.main_verticalLayout.addItem(spacerItem)
self.setLayout(self.main_verticalLayout)
# class ChatWindow(QWidget):
# def __init__(self, parent=None, chat_obj=None, chat_data=None):
# super().__init__(parent)
# self.chat_object = chat_obj
# self.chat_data = chat_data
# self.main_verticalLayout = QVBoxLayout(self)
# self.main_verticalLayout.setContentsMargins(0, 0, 0, 0)
# self.main_verticalLayout.setSpacing(0)
# self.main_verticalLayout.setObjectName("main_verticalLayout")
# self.style_str = """
# QPushButton,
# QLabel {
# border: none;
# padding: 5px;
# }
# QWidget {
# background: transparent;
# }
# """
# self.setStyleSheet(self.style_str)
# self.chats_data = { # Initialize chats_data as an empty dictionary
# "title": "",
# "chatlist": []
# }
# if self.chat_data:
# self.chats_data["title"] = self.chat_data["title"]
# self.chats_data["chatlist"] += self.chat_data["chatlist"]
# self.show_chats()
# def show_chats(self):
# # chat_title = self.chats_data.get("title")
# chat_list = self.chats_data.get("chatlist")
# for chat in chat_list:
# input_str = chat.get("input_str")
# input_widget = InputWidget(chat_obj=self.chat_object)
# input_widget.set_input_text(input_str)
# self.main_verticalLayout.addWidget(input_widget)
# out_str = chat.get("output_str")
# out_widget = OutWidget()
# # markdown_text = markdown(out_str, extensions=['markdown.extensions.extra', 'markdown.extensions.tables'])
# markdown_text = markdown(out_str, extensions=['markdown.extensions.extra', 'markdown.extensions.attr_list'])
# # Add the custom attributes for the table, th, and td elements
# markdown_text = markdown_text.replace('<table>', '<table style="border: 1px solid white; padding: 10px; border-collapse: collapse;">')
# markdown_text = markdown_text.replace('<th>', '<th style="border: 1px solid white; padding: 10px;">')
# markdown_text = markdown_text.replace('<td>', '<td style="border: 1px solid white; padding: 10px;">')
# out_widget.set_output_text(markdown_text)
# self.main_verticalLayout.addWidget(out_widget)
# # out_str = chat.get("output_str")
# # out_widget = OutWidget()
# # out_widget.set_output_text(out_str)
# # self.main_verticalLayout.addWidget(out_widget)
# spacerItem = QSpacerItem(20, 300, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
# self.main_verticalLayout.addItem(spacerItem)
# self.setLayout(self.main_verticalLayout)
# def show_chats(self):
# chat_list = self.chats_data.get("chatlist")
# for chat in chat_list:
# input_str = chat.get("input_str")
# input_widget = InputWidget(chat_obj=self.chat_object)
# input_widget.set_input_text(input_str)
# self.main_verticalLayout.addWidget(input_widget)
# out_str = chat.get("output_str")
# out_widget = OutWidget()
# # Check if the output is an image
# if out_str.startswith("[Image "):
# image_path = out_str[len("[Image "):-1] # Extract the image path from the output string
# image_widget = QLabel()
# pixmap = QPixmap(image_path)
# if not pixmap.isNull():
# image_widget.setPixmap(pixmap.scaledToWidth(400)) # Adjust the width as needed
# self.main_verticalLayout.addWidget(image_widget)
# else:
# # If it's not an image, treat it as regular text and convert any markdown in the output
# markdown_text = markdown(out_str, extensions=['markdown.extensions.extra', 'markdown.extensions.tables'])
# out_widget.set_output_text(markdown_text)
# self.main_verticalLayout.addWidget(out_widget)
# spacerItem = QSpacerItem(20, 300, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
# self.main_verticalLayout.addItem(spacerItem)
# self.setLayout(self.main_verticalLayout)