-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
254 lines (218 loc) · 8.86 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
244
245
246
247
248
249
250
251
252
253
254
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QPushButton, QTextEdit, QScrollArea, QFrame, QSplitter)
from PyQt5.QtGui import QFont, QPainter, QColor, QLinearGradient, QPen, QIcon
from PyQt5.QtCore import Qt, QRect
from moral_compass import MoralCompass
from models import Decision, MoralDimension
from response_generator import ResponseGenerator
from input_analysis import ConversationAnalyzer
class MedievalScrollArea(QScrollArea):
def __init__(self, parent=None):
super().__init__(parent)
self.setWidgetResizable(True)
self.setStyleSheet("""
QScrollArea {
border: 2px solid #8B4513;
border-radius: 10px;
background-color: #FFF8DC;
}
QScrollBar:vertical {
border: none;
background: #D2B48C;
width: 14px;
margin: 15px 0 15px 0;
border-radius: 7px;
}
QScrollBar::handle:vertical {
background-color: #8B4513;
min-height: 30px;
border-radius: 7px;
}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {
border: none;
background: none;
}
""")
class MedievalButton(QPushButton):
def __init__(self, text, parent=None):
super().__init__(text, parent)
self.setFont(QFont("Times New Roman", 12, QFont.Bold))
self.setFixedSize(200, 50)
self.setCursor(Qt.PointingHandCursor)
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# Button shape
rect = self.rect().adjusted(2, 2, -2, -2)
gradient = QLinearGradient(rect.topLeft(), rect.bottomRight())
gradient.setColorAt(0, QColor("#D2B48C"))
gradient.setColorAt(1, QColor("#8B4513"))
painter.setBrush(gradient)
painter.setPen(QPen(QColor("#4A2601"), 2))
painter.drawRoundedRect(rect, 10, 10)
# Text
painter.setPen(QColor("#FFF8DC"))
painter.drawText(rect, Qt.AlignCenter, self.text())
# Highlight effect
if self.underMouse():
painter.setBrush(QColor(255, 255, 255, 30))
painter.drawRoundedRect(rect, 10, 10)
class GUI(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Ye Olde Decision Analysis Toole")
self.setGeometry(100, 100, 1000, 700)
self.setStyleSheet("""
QMainWindow {
background-color: qlineargradient(x1:0, y1:0, x2:1, y2:1,
stop:0 #F4A460, stop:1 #D2691E);
}
""")
self.response_generator = ResponseGenerator()
self.conversation_analyzer = ConversationAnalyzer()
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
main_layout.addWidget(self.create_header())
# Create a splitter for the main content
content_splitter = QSplitter(Qt.Horizontal)
main_layout.addWidget(content_splitter)
# Left side: Input, buttons, and output
left_widget = QWidget()
left_layout = QVBoxLayout(left_widget)
left_layout.addWidget(self.create_input_section())
left_layout.addWidget(self.create_button_section())
left_layout.addWidget(self.create_output_section())
content_splitter.addWidget(left_widget)
# Right side: Moral Compass
# Create an initial Decision with neutral moral scores
initial_moral_scores = {dim: 0 for dim in MoralDimension}
initial_decision = Decision("Initial Decision", "No decision made yet", initial_moral_scores)
self.moral_compass = MoralCompass(initial_decision)
content_splitter.addWidget(self.moral_compass)
# Set the initial sizes of the splitter
content_splitter.setSizes([600, 400])
def create_header(self):
header = QLabel("Ye Olde Decision Analysis Toole")
header.setStyleSheet("""
font-size: 36px;
color: #FFF8DC;
font-family: 'Times New Roman';
font-weight: bold;
padding: 20px;
background-color: rgba(139, 69, 19, 0.7);
border-radius: 15px;
""")
header.setAlignment(Qt.AlignCenter)
return header
def create_input_section(self):
input_frame = QFrame()
input_frame.setStyleSheet("""
QFrame {
background-color: rgba(255, 248, 220, 0.7);
border-radius: 15px;
padding: 15px;
}
""")
input_layout = QVBoxLayout(input_frame)
input_label = QLabel("Enter your query:")
input_label.setStyleSheet("""
font-size: 18px;
color: #8B4513;
font-family: 'Times New Roman';
font-weight: bold;
margin-bottom: 10px;
""")
input_layout.addWidget(input_label)
self.input_text = QTextEdit()
self.input_text.setStyleSheet("""
QTextEdit {
background-color: #FFF8DC;
color: #8B4513;
border: 2px solid #DAA520;
border-radius: 10px;
padding: 10px;
font-family: 'Times New Roman';
font-size: 14px;
}
""")
self.input_text.setFixedHeight(100)
input_layout.addWidget(self.input_text)
return input_frame
def create_button_section(self):
button_frame = QFrame()
button_layout = QHBoxLayout(button_frame)
button_layout.setSpacing(20)
analyze_button = MedievalButton("Analyze")
analyze_button.clicked.connect(self.analyze_input)
button_layout.addWidget(analyze_button)
clear_button = MedievalButton("Clear")
clear_button.clicked.connect(self.clear_input)
button_layout.addWidget(clear_button)
return button_frame
def create_output_section(self):
output_frame = QFrame()
output_frame.setStyleSheet("""
QFrame {
background-color: rgba(255, 248, 220, 0.7);
border-radius: 15px;
padding: 15px;
}
""")
output_layout = QVBoxLayout(output_frame)
output_label = QLabel("Analysis Results:")
output_label.setStyleSheet("""
font-size: 18px;
color: #8B4513;
font-family: 'Times New Roman';
font-weight: bold;
margin-bottom: 10px;
""")
output_layout.addWidget(output_label)
self.output_text = QTextEdit()
self.output_text.setReadOnly(True)
self.output_text.setStyleSheet("""
QTextEdit {
background-color: #FFF8DC;
color: #8B4513;
border: 2px solid #DAA520;
border-radius: 10px;
padding: 10px;
font-family: 'Times New Roman';
font-size: 14px;
}
""")
scroll_area = MedievalScrollArea()
scroll_area.setWidget(self.output_text)
scroll_area.setFixedHeight(200)
output_layout.addWidget(scroll_area)
return output_frame
def analyze_input(self):
input_text = self.input_text.toPlainText()
# Use ConversationAnalyzer to get detailed analysis
analysis_result = self.conversation_analyzer.analyze_input(input_text)
# Generate response using ResponseGenerator
response = self.response_generator.generate_response(input_text)
# Display the response
self.output_text.setPlainText(response)
# Update the Moral Compass
new_decision = analysis_result['decision']
self.moral_compass.decision = new_decision
self.moral_compass.update()
# Display additional analysis information
additional_info = f"""
Sentiment: {analysis_result['sentiment']['compound']:.2f}
Dominant Emotion: {max(analysis_result['emotions'], key=analysis_result['emotions'].get)}
Key Phrases: {', '.join(analysis_result['key_phrases'][:5])} # Display top 5 key phrases
Entities: {', '.join([e['text'] for e in analysis_result['entities'][:5]])} # Display top 5 entities
"""
self.output_text.append("\n\nAdditional Analysis:")
self.output_text.append(additional_info)
def clear_input(self):
self.input_text.clear()
self.output_text.clear()
# Reset the Moral Compass to its initial state
initial_moral_scores = {dim: 0 for dim in MoralDimension}
self.moral_compass.decision = Decision("Initial Decision", "No decision made yet", initial_moral_scores)
self.moral_compass.update()