59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
import random
|
|
import torch
|
|
import torch.nn.functional as F
|
|
from model.brain import model, tokenizer, DEVICE, score_sentence
|
|
from model.trainer import train_on_message
|
|
from model.dreams import save_dream, load_dreams
|
|
from model.journal import record_to_journal
|
|
from model.dynamic_expand import expand_model_if_needed
|
|
from context.context import load_context
|
|
|
|
recent_dreams = []
|
|
|
|
|
|
def daydream():
|
|
model.eval()
|
|
seed = torch.tensor([random.randint(0, tokenizer.next_id - 1)], device=DEVICE).unsqueeze(0)
|
|
dream = []
|
|
|
|
for _ in range(12):
|
|
out = model(seed)
|
|
logits = out[:, -1, :]
|
|
probs = F.softmax(logits, dim=-1)
|
|
token = torch.multinomial(probs, num_samples=1)
|
|
dream.append(token.item())
|
|
seed = torch.cat([seed, token], dim=1)
|
|
|
|
sentence = tokenizer.detokenize(dream)
|
|
score = score_sentence(sentence)
|
|
|
|
if score > 0.5:
|
|
save_dream(sentence, score)
|
|
record_to_journal(sentence)
|
|
train_on_message(sentence)
|
|
|
|
if len(recent_dreams) > 10:
|
|
recent_dreams.pop(0)
|
|
|
|
|
|
def replay_dreams():
|
|
expand_model_if_needed()
|
|
|
|
dreams = load_dreams()
|
|
context = load_context()
|
|
|
|
if not dreams or not context:
|
|
return
|
|
|
|
selected_dreams = random.sample(dreams, min(len(dreams), 5))
|
|
selected_contexts = random.sample(context, min(len(context), 5))
|
|
|
|
# Mix dreams and past contexts into a chaotic dream
|
|
all_sources = [d["sentence"] for d in selected_dreams] + [c["text"] for c in selected_contexts]
|
|
random.shuffle(all_sources)
|
|
|
|
mixed_sentence = " ".join(random.sample(all_sources, min(len(all_sources), 3)))
|
|
|
|
if mixed_sentence:
|
|
train_on_message(mixed_sentence, source="dream")
|