Skip to content

Commit 0d8c567

Browse files
committed
Added external configuration file and error/warning if the external files are missing
1 parent aab7abf commit 0d8c567

File tree

2 files changed

+64
-21
lines changed

2 files changed

+64
-21
lines changed

ClipAI.py

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,66 @@
11
import tkinter as tk
2-
from tkinter import ttk
3-
from tkinter import scrolledtext
2+
from tkinter import ttk, scrolledtext, messagebox
43
import pyperclip
54
import threading
65
import time
76
import requests
87
import json
8+
import os
99

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()
1359

14-
with open("prompts.json", "r", encoding="utf-8") as file:
15-
TRANSFORMATION_PROMPTS = json.load(file)
1660

1761
class ClipboardViewer:
1862
def __init__(self, root):
63+
1964
self.root = root
2065

2166
# Configure the window
@@ -105,7 +150,7 @@ def __init__(self, root):
105150
self.clear_button.grid(row=0, column=2, padx=2)
106151

107152
# Dropdown menu for selecting transformation type
108-
self.transformation_options = list(TRANSFORMATION_PROMPTS.keys())
153+
self.transformation_options = list(transformationPrompts.keys())
109154
self.selected_transformation = tk.StringVar()
110155
self.selected_transformation.set(self.transformation_options[0])
111156
self.transformation_menu = ttk.Combobox(
@@ -131,11 +176,11 @@ def __init__(self, root):
131176
# Fetch and set the model list in a dropdown menu
132177
self.model_list = []
133178
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]
137182

138-
self.selected_model = tk.StringVar(value=defaultModel)
183+
self.selected_model = tk.StringVar(value=defaultModelLocal)
139184
self.model_menu = ttk.Combobox(
140185
self.button_frame,
141186
textvariable=self.selected_model,
@@ -168,7 +213,7 @@ def __init__(self, root):
168213
def fetch_models(self):
169214
"""Fetch available models from Ollama API"""
170215
try:
171-
response = requests.get(OLLAMA_URL_MODEL_LIST)
216+
response = requests.get(ollamaURL + "tags")
172217
response_tmp = response.json()
173218
# print(response_tmp['models'])
174219
if response.status_code == 200:
@@ -236,7 +281,7 @@ def send_to_llm(self):
236281
return
237282

238283
selected_option = self.selected_transformation.get()
239-
prompt_template = TRANSFORMATION_PROMPTS.get(selected_option, "{}")
284+
prompt_template = transformationPrompts.get(selected_option, "{}")
240285
formatted_prompt = prompt_template.format(clipboard_text)
241286

242287
model = self.selected_model.get()
@@ -245,7 +290,7 @@ def send_to_llm(self):
245290

246291
try:
247292
response = requests.post(
248-
OLLAMA_URL,
293+
ollamaURL + "generate",
249294
json = {"model": model, "prompt": formatted_prompt, "keep_alive": "5m", "stream": True},
250295
stream = True
251296
)
@@ -281,11 +326,5 @@ def clear_outbox(self):
281326
self.out_text_box.configure(state="disabled")
282327

283328

284-
285-
def main():
286-
root = tk.Tk()
287-
app = ClipboardViewer(root)
288-
root.mainloop()
289-
290329
if __name__ == "__main__":
291330
main()

config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"OLLAMA_URL": "http://localhost:11434/api/",
3+
"DEFAULT_MODEL": "gemma3:1b-it-qat"
4+
}

0 commit comments

Comments
 (0)