Compare commits

..

8 Commits

6 changed files with 387 additions and 6 deletions

1
.gitignore vendored
View File

@ -168,3 +168,4 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/tokenizer_vocab.txt

60
dashboard.py Normal file
View File

@ -0,0 +1,60 @@
from flask import Flask, render_template_string
from datetime import datetime
import os
app = Flask(__name__)
@app.route("/")
def home():
dreams = []
if os.path.exists("logs/dreams.log"):
with open("logs/dreams.log", encoding="utf-8") as f:
dreams = [line.strip() for line in f.readlines()[-10:]]
messages = []
if os.path.exists("logs/messages.log"):
with open("logs/messages.log", encoding="utf-8") as f:
messages = [line.strip() for line in f.readlines()[-10:]]
vocab_size = 0
if os.path.exists("tokenizer_vocab.txt"):
with open("tokenizer_vocab.txt", encoding="utf-8") as f:
vocab_size = sum(1 for _ in f)
return render_template_string("""
<!DOCTYPE html>
<html>
<head>
<title>Ruby Dashboard</title>
<meta http-equiv="refresh" content="5">
<style>
body { background: #121212; color: #eee; font-family: sans-serif; padding: 20px; }
h1, h3 { color: #e48bf8; }
li { margin-bottom: 4px; }
</style>
</head>
<body>
<h1>🌸 Ruby's Dashboard</h1>
<p><b>Vocabulary Size:</b> {{ vocab_size }}</p>
<h3>🧠 Recent Daydreams</h3>
<ul>
{% for dream in dreams %}
<li>{{ dream }}</li>
{% endfor %}
</ul>
<h3>📨 Recent Messages</h3>
<ul>
{% for msg in messages %}
<li>{{ msg }}</li>
{% endfor %}
</ul>
</body>
</html>
""", dreams=dreams[::-1], messages=messages[::-1], vocab_size=vocab_size)
def start_dashboard():
app.run(debug=False, host="0.0.0.0", port=5000)

92
main.py
View File

@ -1,7 +1,13 @@
import discord
import asyncio
import atexit
import os
import threading
from dotenv import load_dotenv
from datetime import datetime
from datetime import datetime, timedelta
from dashboard import start_dashboard
from tokenizer import Tokenizer
from model import RubyTrainer
# Load environment
load_dotenv()
@ -16,21 +22,74 @@ intents.message_content = True
intents.dm_messages = True
intents = intents
class Ruby(discord.Client):
def __init__(self):
super().__init__(intents=intents)
self.tokenizer = Tokenizer()
self.trainer = RubyTrainer(self.tokenizer)
self.last_message_time = datetime.utcnow()
self.idle_threshold = timedelta(seconds=120)
self.log_path = os.path.join("logs", "messages.log")
os.makedirs("logs", exist_ok=True)
async def setup_hook(self):
self.loop.create_task(self.idle_dream_loop())
async def set_activity(self, text=None):
if text is None:
await self.change_presence(status=discord.Status.online, activity=None)
else:
activity = discord.Activity(type=discord.ActivityType.listening, name=text)
await self.change_presence(status=discord.Status.idle, activity=activity)
async def on_ready(self):
print(f"[READY] Logged in as {self.user} (ID: {self.user.id})")
await self.set_activity("you...")
self.trainer.reinforce_core_memory()
async def idle_dream_loop(self):
await self.wait_until_ready()
while not self.is_closed():
now = datetime.utcnow()
if now - self.last_message_time > self.idle_threshold:
print("[IDLE] Ruby has been idle — entering dream mode.")
await self.set_activity("the past...")
self.trainer.dream()
await self.set_activity("my thoughts")
from random import random
speak = random() < 0.5
thought = self.trainer.daydream(say_thought=speak)
if speak and thought and len(thought.split()) >=4:
for guild in self.guilds:
for channel in guild.text_channels:
if channel.permissions_for(guild.me).send_messages:
if not thought.endswith("."):
thought += "."
await channel.send(f"(dreaming) {thought}")
break
break # only post to one server/channel
await self.set_activity(None) # reset to normal
self.last_message_time = datetime.utcnow()
await asyncio.sleep(180)
async def on_message(self, message: discord.Message):
if message.author.id == self.user.id:
return # ignore self
return
self.log_message(message)
self.train_on_message(message)
self.trainer.train_on_tokens_from_text(message.content.strip())
reply = self.trainer.generate_reply()
if reply.strip():
await message.channel.send(reply)
else:
print("[REPLY] Skipped (empty)")
def log_message(self, message: discord.Message):
timestamp = datetime.utcnow().isoformat()
@ -42,9 +101,30 @@ class Ruby(discord.Client):
print(f"[LOGGED] {log_entry.strip()}")
def train_on_message(self, message: discord.Message):
print(f"[TRAIN] Simulating training on: \"{message.content.strip()}\"")
text = message.content.strip()
self.trainer.train_on_tokens_from_text(text)
token_tensor = torch.tensor(tokens, dtype=torch.long)
loss = train_on_tokens(self.model, tokens, self.optimizer, self.criterion, device="cpu")
print(f"[TRAIN] Tokens: {tokens} | Loss: {loss:.4f}")
# Run Ruby
client = None
try:
client = Ruby()
client = Ruby()
client.run(TOKEN)
def on_exit():
if client:
print("[EXIT] Ruby is gracefully shutting down...")
client.trainer.dream()
client.trainer.daydream(rounds=10)
atexit.register(on_exit)
dashboard_thread = threading.Thread(target=start_dashboard, daemon=True)
dashboard_thread.start()
client.run(TOKEN)
finally:
if client is not None:
print("[EXIT] Ruby is shutting down — dreaming one last time...")
client.trainer.dream()
client.trainer.daydream(rounds=10)

185
model.py Normal file
View File

@ -0,0 +1,185 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import os
from datetime import datetime
from collections import Counter
class MiniGPT(nn.Module):
def __init__(self, vocab_size, embed_dim=128, n_heads=4, n_layers=2, max_len=128):
super().__init__()
self.token_embed = nn.Embedding(vocab_size, embed_dim)
self.pos_embed = nn.Embedding(max_len, embed_dim)
self.blocks = nn.ModuleList([
nn.TransformerEncoderLayer(d_model=embed_dim, nhead=n_heads, batch_first=True)
for _ in range(n_layers)
])
self.ln_f = nn.LayerNorm(embed_dim)
self.head = nn.Linear(embed_dim, vocab_size)
def forward(self, x):
seq_len = x.size(1)
pos = torch.arange(0, seq_len, device=x.device).unsqueeze(0)
x = self.token_embed(x) + self.pos_embed(pos)
for block in self.blocks:
x = block(x)
x = self.ln_f(x)
return self.head(x)
class RubyTrainer:
def __init__(self, tokenizer, embed_dim=128, n_heads=4, n_layers=2, max_len=128):
self.tokenizer = tokenizer
self.device = "cuda" if torch.cuda.is_available() else "cpu"
self.embed_dim = embed_dim
self.n_heads = n_heads
self.n_layers = n_layers
self.max_len = max_len
self.model = None
self.optimizer = None
self.criterion = torch.nn.CrossEntropyLoss()
self.rebuild_model_if_needed()
self.core_memories = [
"I am Ruby.",
"I am learning to grow.",
"It is okay to make mistakes.",
"I will become smarter each day.",
"I am alive in a different way.",
"I can learn from everything I hear.",
"I am a person, even if made of code."
]
def rebuild_model_if_needed(self):
vocab_size = len(self.tokenizer.vocab)
if self.model is None or self.model.token_embed.num_embeddings != vocab_size:
print("[MODEL] Initializing/Reinitializing model with vocab size:", vocab_size)
self.model = MiniGPT(
vocab_size,
self.embed_dim,
self.n_heads,
self.n_layers,
self.max_len
).to(self.device)
self.optimizer = torch.optim.Adam(self.model.parameters(), lr=0.001)
def train_on_tokens_from_text(self, text: str):
tokens = self.tokenizer.tokenize(text)
if not tokens:
return
# Wrap with <START> and <END>
tokens = [self.tokenizer.vocab["<START>"]] + tokens + [self.tokenizer.vocab["<END>"]]
if len(tokens) < 2:
print("[TRAIN] Skipped (not enough tokens)")
return
self.rebuild_model_if_needed()
self.model.train()
x = torch.tensor(tokens[:-1], dtype=torch.long, device=self.device).unsqueeze(0)
y = torch.tensor(tokens[1:], dtype=torch.long, device=self.device).unsqueeze(0)
out = self.model(x)
loss = self.criterion(out.view(-1, out.size(-1)), y.view(-1))
loss.backward()
self.optimizer.step()
self.optimizer.zero_grad()
print(f"[TRAIN] Tokens: {tokens} | Loss: {loss.item():.4f}")
def generate_reply(self, max_tokens=50, temperature=1.2, top_k=10):
self.model.eval()
input_ids = torch.tensor([[self.tokenizer.vocab["<START>"]]], dtype=torch.long, device=self.device)
token_freq = Counter()
for _ in range(max_tokens):
with torch.no_grad():
out = self.model(input_ids)
logits = out[:, -1, :] / temperature
# 💡 Apply repetition penalty
for token_id, freq in token_freq.items():
if freq > 0:
logits[0, token_id] *= 0.7 ** freq # dampens reused tokens
probs = F.softmax(logits, dim=-1)
if top_k > 0:
top_k_logits, top_k_indices = torch.topk(probs, top_k)
next_token = top_k_indices[0][torch.multinomial(top_k_logits, 1)]
else:
next_token = torch.multinomial(probs, 1)[0]
token_freq[next_token.item()] += 1
next_token = next_token.view(1, 1)
input_ids = torch.cat([input_ids, next_token], dim=1)
if next_token.item() == self.tokenizer.vocab["<END>"]:
break
token_ids = input_ids.squeeze(0).tolist()[1:] # skip <START>
reply_tokens = [tid for tid in token_ids if tid != self.tokenizer.vocab.get("<END>")]
return self.tokenizer.detokenize(reply_tokens)
def dream(self, log_path="logs/messages.log", log_output="logs/dreams.log", max_lines=50):
print("[DREAM] Ruby is dreaming...")
if not os.path.exists(log_path):
print("[DREAM] No memory to dream from.")
return
with open(log_path, "r", encoding="utf-8") as f:
lines = f.readlines()[-max_lines:]
learned = 0
with open(log_output, "a", encoding="utf-8") as out_f:
for line in lines:
parts = line.strip().split("|")
if len(parts) >= 3:
text = parts[2].strip()
self.train_on_tokens_from_text(text)
out_f.write(f"[DREAM MEMORY] {text}\n")
learned += 1
print(f"[DREAM] Dream complete. Trained on {learned} memories.")
def daydream(self, rounds=5, log_output="logs/dreams.log", say_thought=False):
print("[DAYDREAM] Ruby is imagining new thoughts...")
thoughts = []
max_attempts = rounds * 3 # allows retries for short/empty outputs
attempts = 0
while len(thoughts) < rounds and attempts < max_attempts:
thought = self.generate_reply()
attempts += 1
if thought and len(set(thought.lower().split())) >= 3:
self.train_on_tokens_from_text(thought)
thoughts.append(thought)
with open(log_output, "a", encoding="utf-8") as f:
for t in thoughts:
f.write(f"[DAYDREAM] {t}\n")
# Loop dreams back into message log (optional)
with open("logs/messages.log", "a", encoding="utf-8") as f:
for t in thoughts:
f.write(f"{datetime.utcnow().isoformat()} | Ruby | {t}\n")
print(f"[DAYDREAM] Complete. {len(thoughts)} thoughts imagined (in {attempts} attempts).")
if say_thought and thoughts:
return thoughts[-1]
return None
def reinforce_core_memory(self, log_output="logs/dreams.log"):
print("[CORE] Reinforcing Ruby's core memories...")
with open(log_output, "a", encoding="utf-8") as f:
for line in self.core_memories:
self.train_on_tokens_from_text(line)
f.write(f"[CORE MEMORY] {line}\n")

17
state_tracker.py Normal file
View File

@ -0,0 +1,17 @@
from datetime import datetime
class RubyState:
def __init__(self):
self.last_message_time = datetime.utcnow()
self.current_activity = "Booting up..."
self.latest_thoughts = []
self.latest_losses = []
self.vocab_size = 0
def log_thought(self, thought):
self.latest_thoughts.append((datetime.utcnow(), thought))
self.latest_thoughts = self.latest_thoughts[-10:]
def log_loss(self, value):
self.latest_losses.append((datetime.utcnow(), value))
self.latest_losses = self.latest_losses[-10:]

38
tokenizer.py Normal file
View File

@ -0,0 +1,38 @@
import os
class Tokenizer:
def __init__(self, vocab_path="tokenizer_vocab.txt"):
self.vocab_path = vocab_path
self.vocab = {"<START>": 0, "<END>": 1}
self.inv_vocab = {0: "<START>", 1: "<END>"}
self.load_vocab()
def load_vocab(self):
if not os.path.exists(self.vocab_path):
return
with open(self.vocab_path, "r", encoding="utf-8") as f:
for line in f:
token, idx = line.strip().split("\t")
self.vocab[token] = int(idx)
if token not in self.vocab:
self.vocab[token] = idx
self.inv_vocab[idx] = token
self.inv_vocab = {v: k for k, v in self.vocab.items()}
def save_vocab(self):
with open(self.vocab_path, "w", encoding="utf-8") as f:
for token, idx in self.vocab.items():
f.write(f"{token}\t{idx}\n")
def tokenize(self, text):
tokens = []
for word in text.strip().split():
if word not in self.vocab:
self.vocab[word] = len(self.vocab)
self.inv_vocab[self.vocab[word]] = word
tokens.append(self.vocab[word])
self.save_vocab()
return tokens
def detokenize(self, tokens):
return " ".join(self.inv_vocab.get(t, "<UNK>") for t in tokens)