-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolem.py
More file actions
473 lines (377 loc) · 20.2 KB
/
golem.py
File metadata and controls
473 lines (377 loc) · 20.2 KB
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
463
464
465
466
467
468
469
470
471
472
473
from __future__ import annotations
print("\n 🎲 starting GPT-2-mini 💎 Sapphire β inference wrapper\n")
import os, sys, types, importlib.machinery as _machinery
# ---- HARD DISABLE TENSORFLOW / FLAX ----
#os.environ["TRANSFORMERS_NO_TF"] = "1"
#os.environ["TRANSFORMERS_NO_FLAX"] = "1"
#os.environ["USE_TF"] = "0"
"""
# Create a dummy tensorflow module *with a valid __spec__*
_tf_stub = types.ModuleType("tensorflow")
_tf_stub.__dict__["__getattr__"] = lambda name: None # any attr → None
_tf_stub.__spec__ = _machinery.ModuleSpec("tensorflow", loader=None)
_tf_stub.__path__ = [] # pretend it's a pkg
sys.modules["tensorflow"] = _tf_stub
# Same for flax (rarely, but keeps everything clean)
_flax_stub = types.ModuleType("flax")
_flax_stub.__getattr__ = lambda name: None
_flax_stub.__spec__ = _machinery.ModuleSpec("flax", loader=None)
_flax_stub.__path__ = []
sys.modules["flax"] = _flax_stub
# ----------------------------------------
# now safe to import any HuggingFace / torch code
from transformers import GPT2Tokenizer, GPT2LMHeadModel
"""
import sys, os
# 🔥 Force local transformers override
#local_transformers = os.path.join(os.path.dirname(__file__), "transformers")
#if os.path.isdir(local_transformers):
# sys.path.insert(0, local_transformers)
# print("🔧 Using local transformers from:", local_transformers)
#else:
# print("⚠️ WARNING: Local transformers directory not found!")
MAX_FORWARD_TOKENS = 75
import warnings
warnings.filterwarnings("ignore")
# --- locate Sapphire repo root ------------------------------------------------
import pathlib
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "cli")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "core")))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "utils")))
print("💾 working directory : ", os.path.abspath(os.path.dirname(__file__)))
from itertools import chain
from collections.abc import Mapping
import warnings, os, re, json, glob, argparse, shutil, math, time
from dataclasses import dataclass
from datetime import datetime
from typing import List, Sequence, Tuple
import torch, torch.nn.functional as F
import numpy as np
import transformers
from difflib import SequenceMatcher
from collections import Counter
from transformers import (
GPT2Tokenizer, GPT2LMHeadModel, Trainer, TrainingArguments, default_data_collator
)
from sentence_transformers import SentenceTransformer, util
import nltk
nltk.download('punkt') # you can comment out dwnload after first shot gets i
import glob
from cli.settings_manager import handle_settings_command
### Hardware check
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
###
from core.param_store import ParamStore
# ────────────────────────────────────────────────────────────────────────
# 1. PROMPT CLASSIFIER
# ────────────────────────────────────────────────────────────────────────
class PromptClassifier:
CAUSAL_RE = re.compile(r"\bwhy\b|\bhow (?:do|does)\b|\bbecause\b", re.I)
CONDIT_RE = re.compile(r"\bif .* then\b", re.I)
ARITH_RE = re.compile(r"\d+\s*[\+\-\*/]\s*\d+", re.I)
def classify(self, prompt: str) -> str:
if self.ARITH_RE.search(prompt): return "arithmetic"
if self.CONDIT_RE.search(prompt): return "conditional"
if self.CAUSAL_RE.search(prompt): return "causal"
return "chat"
# ────────────────────────────────────────────────────────────────────────
# 2. NHCE MEMORY + SOFT‑LOGIT Sampler
# ────────────────────────────────────────────────────────────────────────
from core.sapphire_core_9_2 import (
NHCE_Engine,
ManualSampler,
GPT2CustomTrainer,
MemoryLoader,
MemoryNode,
)
from ui.definitions_picker_9 import DefinitionsPicker
from ui.hyperparameter_dashboard_8 import HyperparameterDashboard
from ui.presets_panel_8 import PresetBankPanel
from core.system_banner import print_system_banner
from ui.hyperparams_launcher_panel import HyperparamsLauncherPanel
from ui.umb_wordcloud import UMBWordCloudFlower
# ────────────────────────────────────────────────────────────────────────
# 4. CLI – CHAT with a 'Presence'
# ────────────────────────────────────────────────────────────────────────
import random
from typing import List, Tuple, Sequence
# --------------------------------------------------
def main():
global EOS_ID
trainer = GPT2CustomTrainer()
nhce = NHCE_Engine(trainer.model, trainer.tok)
gen = ManualSampler(trainer.model, trainer.tok, nhce, _embedder = SentenceTransformer("all-MiniLM-L6-v2").to(DEVICE))
clf = PromptClassifier()
loader = MemoryLoader()
live_params = {
"top_n": 13,
"top_t": 7,
"temp": 0.567,
"top_p": 0.72,
"top_k": 42,
"repetition_penalty": 1.25,
"max_forward_tokens": 77,
"max_reply_sentences": 3,
"bias_vec_gain": 0.1,
"tau": 0.1456,
"sigma": 0.1777,
"n_sieve": 5,
"lam": 0.5,
"sieve_rank_mem": 2,
"inference_mem": 1,
"mem_cos_lex_blend": 0.5,
"prompt_constr": "memory;prompt;memory;",
"prompt_gain": 0.9,
"mem_blend" : 0.6,
"mem_gain" : 0.8,
"tail_blend" : 0.6,
"tail_gain" : 0.5,
"comp_precedence" : "memory",
"comp_blend" : 0.3,
"comp_gain" : 0.8,
"master_gain" : 0.97,
"memory_write": True,
}
def update_model_with_live_params(lp, gen):
gen.top_n = lp.get("top_n")
gen.top_t = lp.get("top_t")
gen.temp = lp.get("temp")
gen.top_p = lp.get("top_p")
gen.top_k = lp.get("top_k")
gen.repetition_penalty = lp.get("repetition_penalty")
gen.max_forward_tokens = lp.get("max_forward_tokens")
gen.max_reply_sentences = lp.get("max_reply_sentences")
gen.bias_vec_gain = lp.get("bias_vec_gain")
gen.tau = lp.get("tau")
gen.sigma = lp.get("sigma")
gen.n_sieve = lp.get("n_sieve")
gen.lam = lp.get("lam")
gen.sieve_rank_mem = lp.get("sieve_rank_mem")
gen.inference_mem = lp.get("inference_mem")
gen.mem.mem_cos_lex_blend = lp.get("mem_cos_lex_blend")
gen.prompt_constr = lp.get("prompt_constr")
gen.prompt_gain = lp.get("prompt_gain")
gen.mem_blend = lp.get("mem_blend")
gen.mem_gain = lp.get("mem_gain")
gen.tail_blend = lp.get("tail_blend")
gen.tail_gain = lp.get("tail_gain")
gen.comp_precedence = lp.get("comp_precedence")
gen.comp_blend = lp.get("comp_blend")
gen.comp_gain = lp.get("comp_gain")
gen.master_gain = lp.get("master_gain")
gen.mem.memory_write = lp.get("memory_write")
print("\n───────────────────────────────────────────────────────────────────────────────────")
print(""" ████
░░███
███████ ██████ ░███ ██████ █████████████
███░░███ ███░░███ ░███ ███░░███░░███░░███░░███
░███ ░███░███ ░███ ░███ ░███████ ░███ ░███ ░███
░███ ░███░███ ░███ ░███ ░███░░░ ░███ ░███ ░███
░░███████░░██████ █████░░██████ █████░███ █████
░░░░░███ ░░░░░░ ░░░░░ ░░░░░░ ░░░░░ ░░░ ░░░░░
███ ░███ * SAPPHIRE β CORE * GPT-2-mini *
░░██████
░░░░░░ """)
print("\n───────────────────────────────────────────────────────────────────────────────────")
print(" 🎲 GPT-2-mini 🗃️ microsoft/DialoGPT-small model 💎 Sapphire β inference engine ")
print("───────────────────────────────────────────────────────────────────────────────────\n")
print(" 🧩 rendering device: ", DEVICE)
print(" ❓ questions? type \"config help\"")
# ----------------------------
# Corpora load handler
# ----------------------------
def select_txt_file(
prompt = "[config] Select corpora .txt file (number) ➜ ",
search_dir = "./corpora",
) -> str:
"""
Lists *.txt files in *search_dir* and lets the user pick one,
or enter any valid path. Keeps asking until a readable .txt file
is supplied and returns its absolute path.
"""
txts = sorted(glob.glob(os.path.join(search_dir, "*.txt")))
if txts:
print("🗂 Available corpora:\n")
for i, fp in enumerate(txts, 1):
print(f" {i}) {os.path.basename(fp)}")
while True:
choice = input(prompt).strip()
# numeric pick from list -------------------------------
if choice.isdigit() and txts and 0 <= int(choice) <= len(txts):
if choice == "0":
choice = input("↳ path: ").strip()
else:
choice = txts[int(choice) - 1]
# validation -------------------------------------------
if os.path.isfile(choice) and choice.lower().endswith(".txt"):
return os.path.abspath(choice)
print("🚫 Not a valid .txt file; try again.")
#####
# ─────── MAIN CLI loop
live_params, msg = handle_settings_command("config load master_default", live_params)
update_model_with_live_params(live_params, gen)
print(" 📓 for chat history type \"tail\"\n---")
while True:
print('---')
usr = input(" 🗣 > ")
if len(usr) == 0: #nextusr
continue
if usr.lower() == "exit":
print("---\n 🤖 > Good bye!!\n---\n")
break
if usr.lower().strip() == "cloud":
UMBWordCloudFlower().show_from_memory(nhce.memory, max_terms=450)
continue # Skip standard generation
if usr.lower().strip() == "tail":
print(" 📓 chat history\n---")
for chatlog in nhce.tail_memories(n=4):
print(chatlog)
print("--")
print("-----")
continue # Skip standard generation
if usr.lower().strip() == "chats":
loader.choose_memory_file()
nhce.memory_file = loader.memory_file #update cross system
nhce.memory = loader.load_memory()
print(" 📓 chat history \n")
for chatlog in nhce.tail_memories(n=4):
print(chatlog, "\n--")
continue # Skip standard generation
if usr.lower().strip() == "umb":
print(">> 💾 ", nhce.memory_file)
continue # Skip standard generation
if usr.lower().strip() == "write on":
print(">> 💽 - UMB memory session write: True")
live_params["memory_write"] = True
update_model_with_live_params(live_params, gen)
continue # Skip standard generation
if usr.lower().strip() == "write off":
print(">> 💽 - UMB memory session write: False")
live_params["memory_write"] = False
update_model_with_live_params(live_params, gen)
continue # Skip standard generation
if usr.lower().strip() == "memory":
print(">> 💽 - UMB memory session write: ", nhce.memory_write)
continue # Skip standard generation
if usr.lower().strip()[0:6] == "create":
anchors = usr.split()
if len(anchors) == 3:
prompt_anchor = anchors[1]
llm_anchor = anchors[2]
print("🔢 initializing UMB", flush=True )
# seed with root‑identity prompt
now = datetime.utcnow().isoformat()
root = MemoryNode(now, prompt_anchor, llm_anchor, "identity", 0.95, 1.0, 1.0)
nhce.memory = [] #wiped
nhce.memory.append(root)
nhce.memory_file = "./memory/emergence_UMB.json"
with open(nhce.memory_file, "w") as fh:
json.dump([root.__dict__ for m in nhce.memory], fh, indent=2)
print("[config] UMB initialized")
continue # skip gen
print("[config] ❓ your did not provide init root pair of 'prompt inference'.\n[config] please append to 'create' command ")
continue #nextusr
if usr.lower().strip() == "reload":
with open("./memory/emergence_UMB.json") as fh:
raw = json.load(fh)
nhce.memory = [MemoryNode(**m) for m in raw]
nhce.memory_file = "./memory/emergence_UMB.json"
print("\n>> 🚅 ", "Default UMB reloaded")
continue # Skip standard regeneration
if usr.lower().startswith("saveumb"):
# ------------------------------------------------------------
# Expected syntax: saveumb <filename>
# ------------------------------------------------------------
parts = usr.split(maxsplit=1)
if len(parts) < 2:
print("[config] error: please provide a filename – usage: saveumb <filename>")
continue # Skip generation and wait for next input
file_candidate = parts[1].strip()
# ---- validation -------------------------------------------------------
if any(ch in file_candidate for ch in ".\\/"):
print("[config] error: filename must not contain '.', '/' or '\\'.")
continue
# Optionally append an extension so on‑disk files stay consistent
file_target = f"emergence_UMB_{file_candidate}.json"
# Update NHCE engine so future loads know which file we just wrote
nhce.memory_file = 'memory/' + file_target
# Persist the current memory bank
nhce._persist(nhce.memory)
print(f"\n>> 🚅 UMB saved as : {file_target}")
continue # Skip standard regeneration
if usr.lower().startswith("makedef"):
# ------------------------------------------------------------
# Expected syntax: saveumb <filename>
# ------------------------------------------------------------
# Optionally append an extension so on‑disk files stay consistent
file_target = "emergence_UMB.json"
# Update NHCE engine so future loads know which file we just wrote
nhce.memory_file = 'memory/' + file_target
# Persist the current memory bank
nhce._persist(nhce.memory)
print(f"\n>> 🚅 UMB saved as : {file_target}")
continue # Skip standard regeneration
if usr.lower().strip() == "train":
corpus = select_txt_file()
epochs = int(input("[trainer] number of epochs : "))
trainer.ckpt_name = input("[trainer] output model name : ")
print("\n")
trainer.finetune(corpus, epochs)
continue # Skip standard regeneration
if usr.lower().strip() == "model":
trainer.maybe_load_latest()
continue # Skip standard regeneration
if usr.lower().strip() == "hyper":
from ui.hyperparams_isolated import run_hyperparams_isolated
updated = run_hyperparams_isolated(
live_params,
module_name="hyperparameter_dashboard_8" # where HyperparameterDashboard lives
# timeout=900, # optional: seconds
)
if updated:
# merge and apply to gen.* via your existing callback
live_params.update(updated)
update_model_with_live_params(live_params, gen)
print("[hyper] applied updated parameters.")
else:
print("[hyper] no changes returned (window closed without Apply).")
continue # Skip standard regeneration
if usr.lower().strip() == "turns":
print("🗣 ♟️🤖 chat turns: ", len(nhce.memory), "\n---")
continue # Skip standard regeneration
if usr.lower().strip() == "sys":
print("---")
print_system_banner(model=trainer.model, umb_path=nhce.memory_file)
print("---")
continue # Skip standard regeneration
if usr.lower().strip()[:6] == "preset":
name = PresetBankPanel(params_path="presets/sapphire_params.json").run()
if name:
print("[presets] selected:", name)
live_params, msg = handle_settings_command("config load " + name, live_params)
print(msg)
update_model_with_live_params(live_params, gen)
continue
else:
print("[presets] canceled")
continue # Skip standard regeneration
if usr.lower().strip() == "defs":
chosen = DefinitionsPicker(path="presets").run()
if not chosen:
print("[defs] canceled or no selection")
continue # return to CLI loop
print("[defs 📚] selected:", chosen)
reply = gen.generate(chosen, write_memory=nhce.memory_write)
print("\n 🤖📚 > ", reply.strip(), "\n--")
continue #skip std gen
if usr.lower().startswith("config"):
live_params, msg = handle_settings_command(usr, live_params)
print(msg)
update_model_with_live_params(live_params, gen)
continue
else:
reply = gen.generate(usr, write_memory=nhce.memory_write)
print("\n 🤖 > ", reply.strip(), "\n--")
if __name__ == "__main__":
main()