-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathoperators.py
462 lines (383 loc) · 16.3 KB
/
operators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import os
import queue
import sys
import threading
import traceback
import bpy
from .backend import Backend
from .tools import LlamaMeshModelManager, ToolManager
from .utils import get_available_models, get_models_dir
STATUS_CANCELED = "Request canceled by user"
STATUS_DONE = "Agent ran out of steps"
class MESHGEN_OT_Chat(bpy.types.Operator):
bl_idname = "meshgen.chat"
bl_label = "Chat"
bl_description = "Chat with the selected model"
bl_options = {"REGISTER", "INTERNAL"}
def execute(self, context):
prefs = bpy.context.preferences.addons[__package__].preferences
props = context.scene.meshgen_props
backend = Backend.instance()
props.history.clear()
self.temperature = prefs.temperature
self.prompt = props.prompt
self.messages = [{"role": "user", "content": self.prompt}]
self.log_text = bpy.data.texts.get("meshgen log")
if self.log_text is None:
self.log_text = bpy.data.texts.new("meshgen log")
else:
self.log_text.clear()
for window in context.window_manager.windows:
for area in window.screen.areas:
if area.type == "TEXT_EDITOR":
for space in area.spaces:
if (
space.type == "TEXT_EDITOR"
and space.text == self.log_text
):
space.show_word_wrap = True
space.show_line_numbers = True
space.top = 0
break
log_open = False
for window in context.window_manager.windows:
for area in window.screen.areas:
if area.type != "TEXT_EDITOR":
continue
for space in area.spaces:
if space.type == "TEXT_EDITOR" and space.text == self.log_text:
log_open = True
break
if log_open:
break
if log_open:
break
if not log_open:
bpy.ops.screen.area_split(direction="VERTICAL")
new_area = context.screen.areas[-1]
new_area.type = "TEXT_EDITOR"
new_area.spaces.active.text = self.log_text
self.log_text.write("\n----- New Chat -----\n")
self.log_text.write(f"Prompt: {self.prompt}\n")
if not backend.is_loaded():
provider_to_model = {
"LOCAL": prefs.current_model,
"ollama": prefs.ollama_model_name,
"huggingface": prefs.huggingface_model_id,
"anthropic": prefs.anthropic_model_id,
"openai": prefs.openai_model_id,
}
model_name = (
provider_to_model["LOCAL"]
if prefs.backend_type == "LOCAL"
else provider_to_model.get(prefs.llm_provider)
)
self.add_event("LOADING", "Loading...", f"Loading {model_name}...")
try:
backend.load()
self.pop_event()
self.add_event(
"LOADING_SUCCESS", model_name, f"Finished loading {model_name}"
)
except Exception as e:
self.pop_event()
self.add_event(
"LOADING_ERROR",
str(e),
f"Error loading {model_name}:\n{traceback.format_exc()}",
)
return {"CANCELLED"}
self._stop_event = threading.Event()
self._output_queue = backend.start_chat_completion(
self.messages, self.temperature, self._stop_event
)
props.state = "RUNNING"
self.add_event("THINKING", None, "Thinking...")
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {"RUNNING_MODAL"}
def modal(self, context, event):
props = context.scene.meshgen_props
if props.state == "CANCELED" or event.type == "ESC":
self._stop_event.set()
self.add_event("CANCELED", STATUS_CANCELED, "Canceled")
return self.finish(context)
if event.type == "TIMER" and props.state == "RUNNING":
try:
while True:
message_type, content = self._output_queue.get_nowait()
if message_type == "STEP":
thought, full_output = content
self.pop_event()
self.add_event("STEP", thought, full_output)
self.add_event("THINKING", None, "Thinking...")
self.redraw(context)
elif message_type == "STEP_ERROR":
self.pop_event()
self.add_event("ERROR", content, f"Error: {content}")
self.add_event("THINKING", None, "Thinking...")
self.redraw(context)
elif message_type == "ERROR":
error_msg, full_error = content
self.pop_event()
self.add_event("ERROR", error_msg, full_error)
return self.finish(context)
elif message_type == "FINAL_ANSWER":
self.pop_event()
self.add_event("SUCCESS", content, f"Final Answer: {content}")
return self.finish(context)
elif message_type == "CANCELED":
self.pop_event()
self.add_event("CANCELED", STATUS_CANCELED, STATUS_CANCELED)
return self.finish(context)
elif message_type == "DONE":
self.pop_event()
self.add_event("ERROR", STATUS_DONE, STATUS_DONE)
return self.finish(context)
except queue.Empty:
pass
ToolManager.instance().process_tasks(context)
return {"PASS_THROUGH"}
def add_event(self, event_type, message, long_message):
props = bpy.context.scene.meshgen_props
event = props.history.add()
event.type = event_type
if message:
event.content = message
if long_message:
lines = str(long_message).split("\n")
for line in lines:
self.log_text.write(line + "\n")
def pop_event(self):
props = bpy.context.scene.meshgen_props
props.history.remove(len(props.history) - 1)
def finish(self, context):
props = context.scene.meshgen_props
props.state = "READY"
context.window_manager.event_timer_remove(self._timer)
self.redraw(context)
return {"FINISHED"}
def redraw(self, context):
for area in context.screen.areas:
if area.type == "VIEW_3D":
area.tag_redraw()
class MESHGEN_OT_CancelChat(bpy.types.Operator):
bl_idname = "meshgen.cancel_chat"
bl_label = "Cancel chat"
bl_description = "Cancel the current chat process"
bl_options = {"REGISTER", "INTERNAL"}
def execute(self, context):
props = context.scene.meshgen_props
props.state = "CANCELED"
return {"FINISHED"}
class MESHGEN_OT_OpenLog(bpy.types.Operator):
bl_idname = "meshgen.open_log"
bl_label = "Open log"
bl_description = "Open the log file"
bl_options = {"REGISTER", "INTERNAL"}
def execute(self, context):
log_text = bpy.data.texts.get("meshgen log")
if log_text is None:
log_text = bpy.data.texts.new("meshgen log")
for area in context.screen.areas:
if area.type == "TEXT_EDITOR":
area.spaces.active.text = log_text
return {"FINISHED"}
bpy.ops.screen.area_split(direction="VERTICAL")
new_area = context.screen.areas[-1]
new_area.type = "TEXT_EDITOR"
new_area.spaces.active.text = log_text
return {"FINISHED"}
class MESHGEN_OT_DownloadModel(bpy.types.Operator):
bl_idname = "meshgen.download_model"
bl_label = "Download model"
bl_description = "Download model from Hugging Face"
bl_options = {"REGISTER", "INTERNAL"}
__annotations__ = {
"repo_id": bpy.props.StringProperty(name="Repository ID"),
"filename": bpy.props.StringProperty(name="Filename"),
}
_timer = None
_download_thread = None
_progress_queue = None
def execute(self, context):
if self.filename in get_available_models():
self.report({"INFO"}, f"{self.filename} already downloaded")
return {"CANCELLED"}
import queue
import re
import sys
import threading
from huggingface_hub import hf_hub_download
prefs = context.preferences.addons[__package__].preferences
prefs.downloading = True
prefs.download_progress = 0
self._progress_queue = queue.Queue()
class TqdmCapture:
def __init__(self, queue, stream):
self.queue = queue
self.stream = stream
self.original_write = stream.write
self.original_flush = stream.flush
def write(self, string):
match = re.search(r"\r.*?(\d+)%", string)
if match:
try:
percentage = int(match.group(1))
self.queue.put(percentage)
except Exception as e:
self.queue.put(f"Error parsing progress: {string}, {e}")
self.original_write(string)
self.original_flush()
def flush(self):
self.original_flush()
def download_task():
try:
old_stderr = sys.stderr
sys.stderr = TqdmCapture(self._progress_queue, sys.stderr)
hf_hub_download(
self.repo_id,
filename=self.filename,
local_dir=get_models_dir(),
)
prefs = bpy.context.preferences.addons[__package__].preferences
if prefs.downloading:
self._progress_queue.put("finished")
except InterruptedError:
pass
except Exception as e:
prefs = bpy.context.preferences.addons[__package__].preferences
if prefs.downloading:
self._progress_queue.put(f"Error downloading model: {e}")
finally:
sys.stderr = old_stderr
self._download_thread = threading.Thread(target=download_task)
self._download_thread.start()
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {"RUNNING_MODAL"}
def modal(self, context, event):
if event.type == "TIMER":
prefs = context.preferences.addons[__package__].preferences
if not prefs.downloading:
self.report({"INFO"}, "Download canceled")
self.cleanup(context)
return {"CANCELLED"}
while not self._progress_queue.empty():
item = self._progress_queue.get()
if isinstance(item, (int, float)):
prefs.download_progress = item
for window in context.window_manager.windows:
for area in window.screen.areas:
if area.type == "PREFERENCES":
area.tag_redraw()
elif isinstance(item, str):
if item == "finished":
prefs.downloading = False
if len(get_available_models()) == 1:
prefs.current_model = get_available_models()[0]
self.report(
{"INFO"},
f"Successfully downloaded {self.filename} from {self.repo_id}",
)
self.cleanup(context)
return {"FINISHED"}
else:
self.report({"ERROR"}, item)
prefs.downloading = False
self.cleanup(context)
return {"CANCELLED"}
return {"RUNNING_MODAL"}
else:
return {"PASS_THROUGH"}
def cleanup(self, context):
prefs = context.preferences.addons[__package__].preferences
prefs.downloading = False
if self._progress_queue:
try:
while not self._progress_queue.empty():
self._progress_queue.get_nowait()
except Exception:
pass
self._progress_queue = None
if self._timer:
try:
bpy.context.window_manager.event_timer_remove(self._timer)
except Exception:
pass
self._timer = None
self._download_thread = None
for window in context.window_manager.windows:
for area in window.screen.areas:
if area.type == "PREFERENCES" or area.type == "VIEW_3D":
area.tag_redraw()
class MESHGEN_OT_SelectModel(bpy.types.Operator):
bl_idname = "meshgen.select_model"
bl_label = "Select model"
bl_description = "Select the model to use for generation"
bl_options = {"REGISTER", "INTERNAL"}
__annotations__ = {"model": bpy.props.StringProperty(name="Model")}
def execute(self, context):
prefs = context.preferences.addons[__package__].preferences
prefs.current_model = self.model
return {"FINISHED"}
class MESHGEN_OT_OpenModelsFolder(bpy.types.Operator):
bl_idname = "meshgen.open_models_folder"
bl_label = "Open models folder"
bl_description = "Open the folder containing downloaded models"
bl_options = {"REGISTER", "INTERNAL"}
def execute(self, context):
models_dir = get_models_dir()
if not models_dir.exists():
models_dir.mkdir(parents=True)
try:
if os.name == "nt":
os.startfile(models_dir)
elif os.name == "posix":
import subprocess
opener = "open" if sys.platform == "darwin" else "xdg-open"
subprocess.run([opener, models_dir])
except Exception as e:
self.report({"ERROR"}, f"Failed to open models folder: {e}")
return {"CANCELLED"}
return {"FINISHED"}
class MESHGEN_OT_LoadLlamaMesh(bpy.types.Operator):
bl_idname = "meshgen.load_llama_mesh"
bl_label = "Load LLaMA-Mesh"
bl_description = "Load the LLaMA-Mesh model"
bl_options = {"REGISTER", "INTERNAL"}
def execute(self, context):
model_manager = LlamaMeshModelManager.instance()
model_manager.load_model()
Backend.reset()
return {"FINISHED"}
class MESHGEN_OT_UnloadLlamaMesh(bpy.types.Operator):
bl_idname = "meshgen.unload_llama_mesh"
bl_label = "Unload LLaMA-Mesh"
bl_description = "Unload the LLaMA-Mesh model"
bl_options = {"REGISTER", "INTERNAL"}
def execute(self, context):
model_manager = LlamaMeshModelManager.instance()
model_manager.unload_model()
Backend.reset()
return {"FINISHED"}
def register():
bpy.utils.register_class(MESHGEN_OT_Chat)
bpy.utils.register_class(MESHGEN_OT_CancelChat)
bpy.utils.register_class(MESHGEN_OT_OpenLog)
bpy.utils.register_class(MESHGEN_OT_DownloadModel)
bpy.utils.register_class(MESHGEN_OT_SelectModel)
bpy.utils.register_class(MESHGEN_OT_OpenModelsFolder)
bpy.utils.register_class(MESHGEN_OT_LoadLlamaMesh)
bpy.utils.register_class(MESHGEN_OT_UnloadLlamaMesh)
def unregister():
bpy.utils.unregister_class(MESHGEN_OT_Chat)
bpy.utils.unregister_class(MESHGEN_OT_CancelChat)
bpy.utils.unregister_class(MESHGEN_OT_OpenLog)
bpy.utils.unregister_class(MESHGEN_OT_DownloadModel)
bpy.utils.unregister_class(MESHGEN_OT_SelectModel)
bpy.utils.unregister_class(MESHGEN_OT_OpenModelsFolder)
bpy.utils.unregister_class(MESHGEN_OT_LoadLlamaMesh)
bpy.utils.unregister_class(MESHGEN_OT_UnloadLlamaMesh)