1
1
import tkinter as tk
2
- from tkinter import ttk
3
- from tkinter import scrolledtext
2
+ from tkinter import ttk , scrolledtext , messagebox
4
3
import pyperclip
5
4
import threading
6
5
import time
7
6
import requests
8
7
import json
8
+ import os
9
9
10
- OLLAMA_URL = "http://localhost:11434/api/generate"
11
- OLLAMA_URL_MODEL_LIST = "http://localhost:11434/api/tags" # API endpoint to get models list
12
- DEFAULT_MODEL = "aya-expanse:latest"
10
+ ollamaURL = "http://localhost:11434/api/"
11
+ defaultModel = "aya-expanse:latest"
12
+
13
+ transformationPrompts = None
14
+
15
+ def core ():
16
+ root = tk .Tk ()
17
+ app = ClipboardViewer (root )
18
+ root .mainloop ()
19
+
20
+
21
+ def main ():
22
+
23
+ global transformationPrompts
24
+ global ollamaURL
25
+ global defaultModel
26
+
27
+ if os .path .isfile ('prompts.json' ):
28
+ with open ("prompts.json" , "r" , encoding = "utf-8" ) as file :
29
+ transformationPrompts = json .load (file )
30
+ startCore = True
31
+ else :
32
+ show_popup ("ERROR: The prompt container file prompts.json does not exist. Please download it from the repo." , "Loading file error" )
33
+ startCore = False
34
+
35
+ if os .path .isfile ('config.json' ):
36
+ with open ("config.json" , "r" , encoding = "utf-8" ) as file :
37
+ CONFIGS_DATA = json .load (file )
38
+ ollamaURL = CONFIGS_DATA ["OLLAMA_URL" ]
39
+ defaultModel = CONFIGS_DATA ["DEFAULT_MODEL" ]
40
+ startCore = True
41
+ else :
42
+ show_popup ("WARNING: The configuration file config.json does not exist. Please download it from the repo for better use. Default parameters will be loaded." , "Loading file error" )
43
+ startCore = True
44
+
45
+ if startCore :
46
+ core ()
47
+
48
+
49
+ def show_popup (message , title = "Message" ):
50
+ # Create a hidden root window
51
+ root = tk .Tk ()
52
+ root .withdraw () # Hide the root window
53
+
54
+ # Show the message box
55
+ messagebox .showinfo (title , message )
56
+
57
+ # Close the root window after message is shown
58
+ root .destroy ()
13
59
14
- with open ("prompts.json" , "r" , encoding = "utf-8" ) as file :
15
- TRANSFORMATION_PROMPTS = json .load (file )
16
60
17
61
class ClipboardViewer :
18
62
def __init__ (self , root ):
63
+
19
64
self .root = root
20
65
21
66
# Configure the window
@@ -105,7 +150,7 @@ def __init__(self, root):
105
150
self .clear_button .grid (row = 0 , column = 2 , padx = 2 )
106
151
107
152
# Dropdown menu for selecting transformation type
108
- self .transformation_options = list (TRANSFORMATION_PROMPTS .keys ())
153
+ self .transformation_options = list (transformationPrompts .keys ())
109
154
self .selected_transformation = tk .StringVar ()
110
155
self .selected_transformation .set (self .transformation_options [0 ])
111
156
self .transformation_menu = ttk .Combobox (
@@ -131,11 +176,11 @@ def __init__(self, root):
131
176
# Fetch and set the model list in a dropdown menu
132
177
self .model_list = []
133
178
self .fetch_models ()
134
- defaultModel = DEFAULT_MODEL
135
- if defaultModel not in self .model_list :
136
- defaultModel = self .model_list [0 ]
179
+ defaultModelLocal = defaultModel
180
+ if defaultModelLocal not in self .model_list :
181
+ defaultModelLocal = self .model_list [0 ]
137
182
138
- self .selected_model = tk .StringVar (value = defaultModel )
183
+ self .selected_model = tk .StringVar (value = defaultModelLocal )
139
184
self .model_menu = ttk .Combobox (
140
185
self .button_frame ,
141
186
textvariable = self .selected_model ,
@@ -168,7 +213,7 @@ def __init__(self, root):
168
213
def fetch_models (self ):
169
214
"""Fetch available models from Ollama API"""
170
215
try :
171
- response = requests .get (OLLAMA_URL_MODEL_LIST )
216
+ response = requests .get (ollamaURL + "tags" )
172
217
response_tmp = response .json ()
173
218
# print(response_tmp['models'])
174
219
if response .status_code == 200 :
@@ -236,7 +281,7 @@ def send_to_llm(self):
236
281
return
237
282
238
283
selected_option = self .selected_transformation .get ()
239
- prompt_template = TRANSFORMATION_PROMPTS .get (selected_option , "{}" )
284
+ prompt_template = transformationPrompts .get (selected_option , "{}" )
240
285
formatted_prompt = prompt_template .format (clipboard_text )
241
286
242
287
model = self .selected_model .get ()
@@ -245,7 +290,7 @@ def send_to_llm(self):
245
290
246
291
try :
247
292
response = requests .post (
248
- OLLAMA_URL ,
293
+ ollamaURL + "generate" ,
249
294
json = {"model" : model , "prompt" : formatted_prompt , "keep_alive" : "5m" , "stream" : True },
250
295
stream = True
251
296
)
@@ -281,11 +326,5 @@ def clear_outbox(self):
281
326
self .out_text_box .configure (state = "disabled" )
282
327
283
328
284
-
285
- def main ():
286
- root = tk .Tk ()
287
- app = ClipboardViewer (root )
288
- root .mainloop ()
289
-
290
329
if __name__ == "__main__" :
291
330
main ()
0 commit comments