ollama-gui 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 chyok
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,56 @@
1
+ Metadata-Version: 2.1
2
+ Name: ollama-gui
3
+ Version: 1.0.0
4
+ Summary: A very simple ollama GUI, implemented using the built-in Python Tkinter library, with no additional dependencies.
5
+ Home-page: https://github.com/chyok/ollama-gui
6
+ License: MIT
7
+ Author: chyok
8
+ Author-email: chyok@hotmail.com
9
+ Requires-Python: >=3.6,<4.0
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.6
13
+ Classifier: Programming Language :: Python :: 3.7
14
+ Classifier: Programming Language :: Python :: 3.8
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Project-URL: Repository, https://github.com/chyok/ollama-gui
20
+ Description-Content-Type: text/markdown
21
+
22
+ # Ollama-GUI
23
+
24
+ ![GitHub License](https://img.shields.io/github/license/chyok/ollama-gui)
25
+
26
+ A very simple ollama GUI, implemented using the built-in Python Tkinter library, with no additional dependencies.
27
+
28
+ ![ollama-gui](https://github.com/chyok/ollama-gui/assets/32629225/672ba3c1-f7eb-4ee2-bcff-e06a0a3f349f)
29
+
30
+ + one file project.
31
+ + no external dependencies.
32
+
33
+ ## Run
34
+ Choose any way you like:
35
+ ### source code
36
+
37
+ `python ollama_gui.py`
38
+
39
+ ### using pip
40
+
41
+ ```
42
+ pip install ollama-gui
43
+ ollama-gui
44
+ ```
45
+
46
+ ### binary file
47
+
48
+ | Platform | Download Link |
49
+ |----------|---------------|
50
+ | Windows | TBD |
51
+
52
+ ## License
53
+
54
+ This project is licensed under the [MIT License](LICENSE).
55
+
56
+
@@ -0,0 +1,34 @@
1
+ # Ollama-GUI
2
+
3
+ ![GitHub License](https://img.shields.io/github/license/chyok/ollama-gui)
4
+
5
+ A very simple ollama GUI, implemented using the built-in Python Tkinter library, with no additional dependencies.
6
+
7
+ ![ollama-gui](https://github.com/chyok/ollama-gui/assets/32629225/672ba3c1-f7eb-4ee2-bcff-e06a0a3f349f)
8
+
9
+ + one file project.
10
+ + no external dependencies.
11
+
12
+ ## Run
13
+ Choose any way you like:
14
+ ### source code
15
+
16
+ `python ollama_gui.py`
17
+
18
+ ### using pip
19
+
20
+ ```
21
+ pip install ollama-gui
22
+ ollama-gui
23
+ ```
24
+
25
+ ### binary file
26
+
27
+ | Platform | Download Link |
28
+ |----------|---------------|
29
+ | Windows | TBD |
30
+
31
+ ## License
32
+
33
+ This project is licensed under the [MIT License](LICENSE).
34
+
@@ -0,0 +1,206 @@
1
+ import json
2
+ import time
3
+
4
+ import urllib.parse
5
+ import urllib.request
6
+ import tkinter as tk
7
+
8
+ from tkinter import ttk, font
9
+ from contextlib import contextmanager
10
+ from typing import Union, Generator
11
+ from threading import Thread
12
+
13
+
14
+ @contextmanager
15
+ def widget_state_context(self: "AIChatInterface",
16
+ enable_sending: bool,
17
+ enable_typing: bool):
18
+ try:
19
+ if enable_typing:
20
+ self.chat.config(state=tk.NORMAL)
21
+ if not enable_sending:
22
+ self.send_button.state(['disabled'])
23
+ yield
24
+ except Exception as _:
25
+ self.chat.insert(tk.END, f"\nAI error!\n\n", ("error",))
26
+ finally:
27
+ self.chat.config(state=tk.DISABLED)
28
+ self.send_button.state(['!disabled'])
29
+
30
+
31
+ class AIChatInterface:
32
+ def __init__(self, root):
33
+ self.root = root
34
+ self.default_font = font.nametofont('TkTextFont').actual()["family"]
35
+ root.title("Ollama GUI")
36
+ root.geometry("800x600")
37
+
38
+ self.api_url = 'http://localhost:11434'
39
+ self.chat_history = []
40
+
41
+ root.grid_columnconfigure(0, weight=1)
42
+ root.grid_rowconfigure(1, weight=1)
43
+ root.grid_rowconfigure(2, weight=0)
44
+
45
+ # header
46
+ header_frame = ttk.Frame(root)
47
+ header_frame.grid(row=0, column=0, sticky="ew", padx=20, pady=20)
48
+ header_frame.grid_columnconfigure(2, weight=1)
49
+
50
+ self.model_select = ttk.Combobox(header_frame, state="readonly", width=30)
51
+ self.model_select.grid(row=0, column=0)
52
+
53
+ self.refresh_button = ttk.Button(header_frame, text="Refresh", command=self.refresh_models)
54
+ self.refresh_button.grid(row=0, column=1, padx=(10, 0))
55
+
56
+ self.error_label = ttk.Label(header_frame, text="", foreground="red")
57
+ self.error_label.grid(row=0, column=2, padx=(10, 0), sticky="w")
58
+
59
+ host_label = ttk.Label(header_frame, text="Host:")
60
+ host_label.grid(row=0, column=3, padx=(10, 0))
61
+
62
+ self.host_input = ttk.Entry(header_frame, width=30)
63
+ self.host_input.grid(row=0, column=4, padx=(5, 10))
64
+ self.host_input.insert(0, self.api_url)
65
+
66
+ clear_button = ttk.Button(header_frame, text="Clear Chat", command=self.clear_chat)
67
+ clear_button.grid(row=0, column=5)
68
+
69
+ # chat container
70
+ chat_frame = ttk.Frame(root)
71
+ chat_frame.grid(row=1, column=0, sticky="nsew", padx=20, pady=(0, 20))
72
+ chat_frame.grid_columnconfigure(0, weight=1)
73
+ chat_frame.grid_rowconfigure(0, weight=1)
74
+
75
+ self.chat = tk.Text(chat_frame, wrap=tk.WORD, state=tk.DISABLED, font=(self.default_font, 12))
76
+ self.chat.grid(row=0, column=0, sticky="nsew")
77
+
78
+ scrollbar = ttk.Scrollbar(chat_frame, orient="vertical", command=self.chat.yview)
79
+ scrollbar.grid(row=0, column=1, sticky="ns")
80
+
81
+ self.chat.configure(yscrollcommand=scrollbar.set)
82
+
83
+ # input area
84
+ input_frame = ttk.Frame(root)
85
+ input_frame.grid(row=2, column=0, sticky="ew", padx=20, pady=20)
86
+ input_frame.grid_columnconfigure(0, weight=1)
87
+
88
+ self.user_input = tk.Text(input_frame, font=(self.default_font, 12), height=3, wrap=tk.WORD)
89
+ self.user_input.grid(row=0, column=0, sticky="ew", padx=(0, 10))
90
+ self.user_input.bind("<Key>", self.handle_key_press)
91
+
92
+ self.send_button = ttk.Button(input_frame, text=" Send \n<Enter>", command=lambda: self.send_message(None))
93
+ self.send_button.grid(row=0, column=1)
94
+ self.send_button.state(['disabled'])
95
+ self.refresh_models()
96
+
97
+ def handle_key_press(self, event):
98
+ if event.keysym == "Return":
99
+ if event.state & 0x1 == 0x1: # Shift key is pressed
100
+ self.user_input.insert("end", "\n")
101
+ else:
102
+ if self.send_button.state() == ('disabled',):
103
+ return "break"
104
+ self.send_message(event)
105
+ return "break"
106
+ else:
107
+ return
108
+
109
+ def refresh_models(self):
110
+ self.model_select.set("waiting...")
111
+ self.send_button.state(['disabled'])
112
+ self.refresh_button.state(['disabled'])
113
+ self.api_url = self.host_input.get()
114
+ Thread(target=self.fetch_models).start()
115
+
116
+ def fetch_models(self):
117
+ try:
118
+ with urllib.request.urlopen(f'{self.api_url}/api/tags') as response:
119
+ data = json.load(response)
120
+ models = [model['name'] for model in data['models']]
121
+ self.root.after(0, self.update_model_select, models)
122
+ except Exception as _:
123
+ self.root.after(0, self.show_error)
124
+ finally:
125
+ self.root.after(0, lambda: self.refresh_button.state(['!disabled']))
126
+
127
+ def update_model_select(self, models):
128
+ self.model_select['values'] = models
129
+ if models:
130
+ self.model_select.set(models[0])
131
+ self.send_button.state(['!disabled'])
132
+ self.error_label.config(text="")
133
+ else:
134
+ self.show_error()
135
+
136
+ def show_error(self):
137
+ self.model_select.set('')
138
+ self.model_select['values'] = []
139
+ self.send_button.state(['disabled'])
140
+ self.error_label.config(text="error")
141
+
142
+ def send_message(self, event=None):
143
+ message = self.user_input.get("1.0", "end-1c").strip()
144
+ if message:
145
+ self.add_message_to_chat("User", message)
146
+ self.chat_history.append({"role": "user", "content": message})
147
+ self.user_input.delete("1.0", "end")
148
+
149
+ Thread(target=self.add_message_to_chat, args=(f"AI ({self.model_select.get()})",
150
+ self.generate_ai_response())).start()
151
+
152
+ def add_message_to_chat(self, sender, message: Union[str, Generator]):
153
+ with widget_state_context(self, enable_typing=True, enable_sending=False):
154
+ self.chat.insert(tk.END, f"{sender}: \n", ("bold", f"{sender.lower()}_name"))
155
+ if isinstance(message, str):
156
+ self.chat.insert(tk.END, f"{message}")
157
+ else:
158
+ ai_message = ""
159
+ for i in message:
160
+ self.chat.insert(tk.END, f"{i}")
161
+ ai_message += i
162
+ self.chat_history.append({"role": "assistant", "content": ai_message})
163
+
164
+ self.chat.insert(tk.END, "\n\n")
165
+ self.chat.see(tk.END)
166
+
167
+ def generate_ai_response(self):
168
+ request = urllib.request.Request(
169
+ f'{self.api_url}/api/chat',
170
+ data=json.dumps({
171
+ "model": self.model_select.get(),
172
+ "messages": self.chat_history,
173
+ "stream": True,
174
+ }).encode('utf-8'),
175
+ headers={'Content-Type': 'application/json'},
176
+ method='POST'
177
+ )
178
+
179
+ with urllib.request.urlopen(request) as resp:
180
+ for line in resp.read().decode('utf-8').splitlines():
181
+ if line.strip():
182
+ data = json.loads(line)
183
+ if 'message' in data:
184
+ new_content = data['message']['content']
185
+ time.sleep(0.01) # add a small delay to improve readability
186
+ yield new_content
187
+
188
+ def clear_chat(self):
189
+ with widget_state_context(self, enable_typing=True, enable_sending=False):
190
+ self.chat.delete(1.0, tk.END)
191
+ self.chat_history.clear()
192
+
193
+
194
+ def run():
195
+ root = tk.Tk()
196
+ app = AIChatInterface(root)
197
+
198
+ app.chat.tag_configure("bold", font=(app.default_font, 12, "bold"))
199
+ app.chat.tag_configure("user_name", foreground="#007bff")
200
+ app.chat.tag_configure("error", foreground="red")
201
+
202
+ root.mainloop()
203
+
204
+
205
+ if __name__ == '__main__':
206
+ run()
@@ -0,0 +1,21 @@
1
+ [tool.poetry]
2
+ name = "ollama-gui"
3
+ version = "1.0.0"
4
+ description = "A very simple ollama GUI, implemented using the built-in Python Tkinter library, with no additional dependencies."
5
+ authors = ["chyok <chyok@hotmail.com>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+ homepage = "https://github.com/chyok/ollama-gui"
9
+ repository = "https://github.com/chyok/ollama-gui"
10
+ packages = [{ include = "ollama_gui.py" }]
11
+
12
+ [tool.poetry.dependencies]
13
+ python = "^3.6"
14
+
15
+
16
+ [build-system]
17
+ requires = ["poetry-core"]
18
+ build-backend = "poetry.core.masonry.api"
19
+
20
+ [tool.poetry.scripts]
21
+ ollama-gui = "ollama_gui:run"