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 = []
|
|
|
|
|
|
async def daydream():
|
|
model.eval()
|
|
max_token_id = model.head.out_features - 1
|
|
seed = torch.randint(0, max_token_id + 1, (1, 1), device=DEVICE)
|
|
dream = []
|
|
|
|
for _ in range(12):
|
|
out = model(seed)
|
|
logits = out[:, -1, :]
|
|
probs = F.softmax(logits, dim=-1)
|
|
token = torch.multinomial(probs, num_samples=1)
|
|
token = torch.clamp(token, max=max_token_id)
|
|
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)
|
|
await train_on_message(sentence)
|
|
|
|
if len(recent_dreams) > 10:
|
|
recent_dreams.pop(0)
|
|
|
|
|
|
async def replay_dreams():
|
|
await 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))
|
|
|
|
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:
|
|
await train_on_message(mixed_sentence, source="dream")
|