forked from StarkWizard/askAi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
askAi.py
executable file
·286 lines (235 loc) · 9.83 KB
/
askAi.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
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
import sys
from pathlib import Path
import pyperclip
import requests
import json
import threading
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton, QScrollArea, QTextEdit,QHBoxLayout,QStatusBar,QProgressBar,QMainWindow
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt,QSize,QSettings,QEvent
import openai
from openai import OpenAI
import os
from os import path
# Define the shared style
button_style = """
QPushButton {
border: 2px solid rgb(250,250,250);
border-radius: 10px;
background-color: black;
color: white;
padding: 5px;
}
QPushButton:hover {
background-color: #505050;
}
"""
status_style = """
QStatusBar {
background-color: #404040;
color: white;
font-weight: bold;
border-top: 1px solid #606060;
}
"""
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.answer = ""
self.clipboard_content=""
def initUI(self):
self.setWindowTitle("askAi")
self.setMinimumSize(500, 300)
self.settings = QSettings("aiTalks", "askAI")
geometry = self.settings.value("geometry")
if geometry:
self.restoreGeometry(geometry)
state = self.settings.value("windowState")
if state:
self.restoreState(state)
self.setStyleSheet("background-color: rgb(31, 31, 31);")
# Set the window to be frameless
#self.setWindowFlags(Qt.FramelessWindowHint)
# Set the window's opacity
#self.setWindowOpacity(0.9) # Adjust opacity from 0.0 (fully transparent) to 1.0 (fully opaque)
if getattr( sys, 'frozen', False ) :
# runs in a pyinstaller bundle
here_dir = sys._MEIPASS
icon_path = path.join(here_dir, 'askAi.png')
else :
here_dir = path.dirname(__file__)
icon_path = path.join(here_dir, 'askAi.png')
if os.path.exists(icon_path):
self.setWindowIcon(QIcon(icon_path))
else:
print(f"Icon file not found: {icon_path}")
# Layout
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
layout = QVBoxLayout(central_widget)
self.status_bar = QStatusBar()
self.setStatusBar(self.status_bar)
self.status_bar.setStyleSheet(status_style)
# Progress Bar (Spinner)
self.spinner = QProgressBar()
self.spinner.setRange(0, 0) # Indeterminate mode
self.spinner.setVisible(False) # Hidden by default
self.status_bar.addPermanentWidget(self.spinner)
# Label above prompt area
label = QLabel("Prompt:", self)
label.setStyleSheet("background-color: rgb(31, 31, 31); color: white;")
layout.addWidget(label)
# Prompt Area
self.prompt_area = QTextEdit(self)
self.prompt_area.setStyleSheet("background-color: white; color: black;")
self.prompt_area.setFixedHeight(30) # Set a fixed height for prompt area
# Load content from QSettings or set default
self.prompt_area.setText(self.settings.value("promptText", "sum-up the following text"))
layout.addWidget(self.prompt_area)
# Scroll Area
self.scroll_area = QScrollArea(self) # Create Scroll Area
self.scroll_area.setWidgetResizable(True) # Allow the contained widget to resize
# Label above message area
label = QLabel("Answer:", self)
label.setStyleSheet("background-color: rgb(31, 31, 31); color: white;")
layout.addWidget(label)
# Message area
self.message_area = QLabel("", self)
self.message_area.setStyleSheet("""background-color: white;
color: black;""")
self.message_area.setWordWrap(True)
self.message_area.setAlignment(Qt.AlignLeft | Qt.AlignTop) # Align text to the left and top
# Add the message area to the scroll area
self.scroll_area.setWidget(self.message_area)
# Add the scroll area to the layout
layout.addWidget(self.scroll_area)
button_layout = QHBoxLayout()
# Text Button Before
self.regenerate_button = QPushButton("Regenerate", self)
self.regenerate_button.clicked.connect(self.on_generate_click) # Define this method
self.regenerate_button.setStyleSheet(button_style)
button_layout.addWidget(self.regenerate_button)
# Icon Button (Existing Button)
self.copy_clipboard = QPushButton(self)
if getattr( sys, 'frozen', False ) :
# runs in a pyinstaller bundle
here_dir = sys._MEIPASS
icon_path = path.join(here_dir, 'askAi.png')
else :
here_dir = path.dirname(__file__)
icon_path = path.join(here_dir, 'askAi.png')
self.copy_clipboard .setIcon(QIcon(icon_path)) # Set the icon here
self.copy_clipboard .setIconSize(QSize(64, 64))
self.copy_clipboard .clicked.connect(self.on_copy_to_clipboard_click) # Define this method
button_layout.addWidget(self.copy_clipboard )
# Text Button After
self.close_button = QPushButton("Close", self)
self.close_button.setStyleSheet(button_style)
self.close_button.clicked.connect(QApplication.instance().quit) # Define this method
button_layout.addWidget(self.close_button)
# Add the horizontal layout to the main vertical layout
layout.addLayout(button_layout)
def enableUI(self,val):
self.copy_clipboard.setEnabled(val)
self.regenerate_button.setEnabled(val)
self.prompt_area
def closeEvent(self, event):
# Save the current text of the prompt area to QSettings
self.settings.setValue("promptText", self.prompt_area.toPlainText())
self.settings.setValue("geometry", self.saveGeometry())
self.settings.setValue("windowState", self.saveState())
super().closeEvent(event)
def on_copy_to_clipboard_click(self):
if self.answer=="":
self.status_bar.showMessage("Nothing to copy to clipboard")
return
pyperclip.copy(self.answer)
QApplication.instance().quit()
def update_text_box(self, text):
self.message_area.setText(text)
def sprint(self, text):
self.status_bar.showMessage(text)
def stream_from_lm_server(self, text):
self.update_text_box("...")
self.enableUI(False)
self.spinner.setVisible(True)
try:
stream = client.chat.completions.create(
messages=[
{
"role": "user",
"content": text,
}
],
model="gpt-3.5-turbo",
stream=True,
)
self.answer = ""
try:
for chunk in stream:
if chunk.choices[0].delta.content is not None:
content = chunk.choices[0].delta.content
if content is not None:
self.answer += content
self.update_text_box(self.answer)
except Exception as e:
self.update_text_box(f"Error: {e}")
except Exception as e:
if isinstance(e, openai.Timeout):
self.sprint(f"OpenAI API request timed out: {str(e)}")
elif isinstance(e, openai.APIError):
self.sprint(f"OpenAI APIError: {str(e)}")
elif isinstance(e, openai.APIConnectionError):
self.sprint(f"OpenAI API request failed to connect: {str(e)}")
elif isinstance(e, openai.InvalidRequestError):
self.sprint(f"OpenAI API request was invalid: {str(e)}")
elif isinstance(e, openai.APICoAuthenticationErrornnectionError):
self.sprint(f"OpenAI API request was not authorized: {str(e)}")
elif isinstance(e, openai.PermissionError):
self.sprint(f"OpenAI API request was not permitted: {str(e)}")
elif isinstance(e, openai.RateLimitError):
self.sprint(f"OpenAI API request exceeded rate limit: {str(e)}")
else:
self.sprint(f"Error: {str(e)}")
self.enableUI(True)
self.spinner.setVisible(False)
def on_generate_click(self):
self.clipboard_content = get_clipboard_content()
if self.clipboard_content:
threading.Thread(target=self.stream_from_lm_server, args=(self.clipboard_content,), daemon=True).start()
else :
self.status_bar.showMessage("No text in clipboard - or contains images")
def event(self, event):
if event.type() == QEvent.ActivationChange:
if self.isActiveWindow():
self.onFocus()
return super(MainWindow, self).event(event)
def onFocus(self):
# Votre code ici
clp =get_clipboard_content()
if clp == self.clipboard_content:
return
self.clipboard_content = clp
if clp:
threading.Thread(target=self.stream_from_lm_server, args=(clp,), daemon=True).start()
def get_clipboard_content():
try:
clipboard_content = pyperclip.paste()
print(clipboard_content)
if isinstance(clipboard_content, str):
return clipboard_content
else:
return None
except Exception as e:
return None
key = os.environ.get("OPENAI_API_KEY")
client = OpenAI(
api_key="key",
base_url="http://localhost:1234/v1"
)
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
mainWindow.on_generate_click()
sys.exit(app.exec_())