@@ -191,14 +191,19 @@ def __init__(self, root):
191
191
self .model_menu .grid (row = 0 , column = 4 , padx = 5 )
192
192
193
193
# Send button
194
- imageSend = tk .PhotoImage (file = r'images/iconSend.png' )
194
+ self .imageSend = tk .PhotoImage (file = r'images/iconSend.png' )
195
+ self .imageStop = tk .PhotoImage (file = r'images/iconStop.png' )
196
+
195
197
self .send_button = ttk .Button (
196
198
self .button_frame ,
197
- image = imageSend ,
198
- command = self .start_qa_llm ,
199
+ image = self . imageSend ,
200
+ command = self .handle_send_click ,
199
201
width = 15
200
202
)
201
- self .send_button .image = imageSend
203
+ self .send_button .config (image = self .imageSend )
204
+ self .send_button .image = self .imageSend
205
+ self .llm_active = False
206
+ self .llm_response = None
202
207
self .send_button .grid (row = 0 , column = 5 , padx = 2 )
203
208
204
209
# Configure grid columns
@@ -210,6 +215,16 @@ def __init__(self, root):
210
215
# Initialize with current clipboard content
211
216
self .update_clipboard_content ()
212
217
218
+
219
+ def handle_send_click (self ):
220
+ if self .llm_active and self .llm_response :
221
+ self .llm_active = False
222
+ self .status_var .set ("LLM response stopped by user." )
223
+ self .llm_response .close ()
224
+ return
225
+ self .start_qa_llm ()
226
+
227
+
213
228
def fetch_models (self ):
214
229
"""Fetch available models from Ollama API"""
215
230
try :
@@ -225,6 +240,7 @@ def fetch_models(self):
225
240
except Exception as e :
226
241
self .status_var .set (f"Error fetching models: { str (e )} " )
227
242
243
+
228
244
def update_clipboard_content (self ):
229
245
"""Update the text box with current clipboard content"""
230
246
try :
@@ -239,12 +255,14 @@ def update_clipboard_content(self):
239
255
except Exception as e :
240
256
self .status_var .set (f"Error: { str (e )} " )
241
257
258
+
242
259
def clear_content (self ):
243
260
"""Clear the text box"""
244
261
self .text_box .delete (1.0 , tk .END )
245
262
self .clear_outbox ()
246
263
self .status_var .set ("Cleared" )
247
264
265
+
248
266
def toggle_auto_refresh (self ):
249
267
"""Toggle automatic clipboard monitoring"""
250
268
self .auto_refresh = not self .auto_refresh
@@ -259,6 +277,7 @@ def toggle_auto_refresh(self):
259
277
self .status_var .set ("Auto-refresh disabled" )
260
278
self .monitor_thread = None
261
279
280
+
262
281
def monitor_clipboard (self ):
263
282
"""Continuously monitor clipboard for changes"""
264
283
last_content = self .text_box .get ('1.0' , tk .END )
@@ -274,6 +293,7 @@ def monitor_clipboard(self):
274
293
time .sleep (0.5 ) # Check every half second
275
294
276
295
def send_to_llm (self ):
296
+
277
297
"""Send clipboard text to an Ollama LLM model"""
278
298
clipboard_text = self .text_box .get ('1.0' , tk .END )
279
299
if not clipboard_text .strip ():
@@ -294,9 +314,17 @@ def send_to_llm(self):
294
314
json = {"model" : model , "prompt" : formatted_prompt , "keep_alive" : "5m" , "stream" : True },
295
315
stream = True
296
316
)
317
+
318
+ self .llm_active = True
319
+ self .llm_response = response
320
+ self .send_button .config (image = self .imageStop )
321
+ self .send_button .image = self .imageStop
322
+
297
323
if response .status_code == 200 :
298
324
self .out_text_box .delete (1.0 , tk .END )
299
325
for line in response .iter_lines ():
326
+ if not self .llm_active :
327
+ break
300
328
if line :
301
329
data = json .loads (line .decode ())
302
330
token = data .get ("response" , "" )
@@ -313,13 +341,20 @@ def send_to_llm(self):
313
341
else :
314
342
self .status_var .set (f"Error { response .status_code } : LLM request failed for { model } " )
315
343
except Exception as e :
316
- self .status_var .set (f"Error: { str (e )} " )
344
+ if self .llm_active :
345
+ self .status_var .set (f"Error: { str (e )} " )
346
+ finally :
347
+ self .llm_response = None
348
+ self .send_button .config (image = self .imageSend )
349
+ self .send_button .image = self .imageSend
350
+
317
351
318
352
def start_qa_llm (self ):
319
353
self .clear_outbox ()
320
354
thread = threading .Thread (target = self .send_to_llm , daemon = True )
321
355
thread .start ()
322
356
357
+
323
358
def clear_outbox (self ):
324
359
self .out_text_box .configure (state = "normal" )
325
360
self .out_text_box .delete (1.0 , tk .END )
0 commit comments