Skip to content

Commit 86bd7b8

Browse files
committed
Added Stop function (and icon)
1 parent 0d8c567 commit 86bd7b8

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

ClipAI.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,19 @@ def __init__(self, root):
191191
self.model_menu.grid(row=0, column=4, padx=5)
192192

193193
# 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+
195197
self.send_button = ttk.Button(
196198
self.button_frame,
197-
image=imageSend,
198-
command=self.start_qa_llm,
199+
image=self.imageSend,
200+
command=self.handle_send_click,
199201
width=15
200202
)
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
202207
self.send_button.grid(row=0, column=5, padx=2)
203208

204209
# Configure grid columns
@@ -210,6 +215,16 @@ def __init__(self, root):
210215
# Initialize with current clipboard content
211216
self.update_clipboard_content()
212217

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+
213228
def fetch_models(self):
214229
"""Fetch available models from Ollama API"""
215230
try:
@@ -225,6 +240,7 @@ def fetch_models(self):
225240
except Exception as e:
226241
self.status_var.set(f"Error fetching models: {str(e)}")
227242

243+
228244
def update_clipboard_content(self):
229245
"""Update the text box with current clipboard content"""
230246
try:
@@ -239,12 +255,14 @@ def update_clipboard_content(self):
239255
except Exception as e:
240256
self.status_var.set(f"Error: {str(e)}")
241257

258+
242259
def clear_content(self):
243260
"""Clear the text box"""
244261
self.text_box.delete(1.0, tk.END)
245262
self.clear_outbox()
246263
self.status_var.set("Cleared")
247264

265+
248266
def toggle_auto_refresh(self):
249267
"""Toggle automatic clipboard monitoring"""
250268
self.auto_refresh = not self.auto_refresh
@@ -259,6 +277,7 @@ def toggle_auto_refresh(self):
259277
self.status_var.set("Auto-refresh disabled")
260278
self.monitor_thread = None
261279

280+
262281
def monitor_clipboard(self):
263282
"""Continuously monitor clipboard for changes"""
264283
last_content = self.text_box.get('1.0', tk.END)
@@ -274,6 +293,7 @@ def monitor_clipboard(self):
274293
time.sleep(0.5) # Check every half second
275294

276295
def send_to_llm(self):
296+
277297
"""Send clipboard text to an Ollama LLM model"""
278298
clipboard_text = self.text_box.get('1.0', tk.END)
279299
if not clipboard_text.strip():
@@ -294,9 +314,17 @@ def send_to_llm(self):
294314
json = {"model": model, "prompt": formatted_prompt, "keep_alive": "5m", "stream": True},
295315
stream = True
296316
)
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+
297323
if response.status_code == 200:
298324
self.out_text_box.delete(1.0, tk.END)
299325
for line in response.iter_lines():
326+
if not self.llm_active:
327+
break
300328
if line:
301329
data = json.loads(line.decode())
302330
token = data.get("response", "")
@@ -313,13 +341,20 @@ def send_to_llm(self):
313341
else:
314342
self.status_var.set(f"Error {response.status_code}: LLM request failed for {model}")
315343
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+
317351

318352
def start_qa_llm(self):
319353
self.clear_outbox()
320354
thread = threading.Thread(target=self.send_to_llm, daemon=True)
321355
thread.start()
322356

357+
323358
def clear_outbox(self):
324359
self.out_text_box.configure(state="normal")
325360
self.out_text_box.delete(1.0, tk.END)

images/iconStop.png

1.97 KB
Loading

0 commit comments

Comments
 (0)