Compare commits

...

3 Commits

2 changed files with 97 additions and 19 deletions

62
main.py
View File

@ -1,5 +1,6 @@
import discord
import asyncio
import atexit
import os
from dotenv import load_dotenv
from datetime import datetime, timedelta
@ -25,14 +26,25 @@ class Ruby(discord.Client):
super().__init__(intents=intents)
self.tokenizer = Tokenizer()
self.trainer = RubyTrainer(self.tokenizer)
self.log_path = os.path.join("logs", "messages.log")
self.last_message_time = datetime.utcnow()
self.idle_threshold = timedelta(seconds=120) # adjust as needed
self.loop.create_task(self.idle_dream_loop())
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()
@ -40,10 +52,28 @@ class Ruby(discord.Client):
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()
self.trainer.daydream()
self.last_message_time = datetime.utcnow() # reset after dreaming
await asyncio.sleep(30) # check every 30 seconds
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:
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:
@ -58,7 +88,6 @@ class Ruby(discord.Client):
else:
print("[REPLY] Skipped (empty)")
def log_message(self, message: discord.Message):
timestamp = datetime.utcnow().isoformat()
log_entry = f"{timestamp} | {message.author.name} | {message.content.strip()}\n"
@ -75,6 +104,21 @@ class Ruby(discord.Client):
loss = train_on_tokens(self.model, tokens, self.optimizer, self.criterion, device="cpu")
print(f"[TRAIN] Tokens: {tokens} | Loss: {loss:.4f}")
# Run Ruby
client = Ruby()
client.run(TOKEN)
client = None
try:
client = Ruby()
def on_exit():
if client:
print("[EXIT] Ruby is gracefully shutting down...")
client.trainer.dream()
client.trainer.daydream(rounds=10)
atexit.register(on_exit)
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)

View File

@ -1,6 +1,9 @@
import torch
import torch.nn as nn
import torch.nn.functional as F
import os
class MiniGPT(nn.Module):
def __init__(self, vocab_size, embed_dim=128, n_heads=4, n_layers=2, max_len=128):
@ -23,6 +26,7 @@ class MiniGPT(nn.Module):
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
@ -37,6 +41,15 @@ class RubyTrainer:
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)
@ -106,7 +119,7 @@ class RubyTrainer:
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", max_lines=50):
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):
@ -116,19 +129,40 @@ class RubyTrainer:
with open(log_path, "r", encoding="utf-8") as f:
lines = f.readlines()[-max_lines:]
for line in lines:
parts = line.strip().split("|")
if len(parts) >= 3:
text = parts[2].strip()
self.train_on_tokens_from_text(text)
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("[DREAM] Dream complete.")
print(f"[DREAM] Dream complete. Trained on {learned} memories.")
def daydream(self, rounds=5):
def daydream(self, rounds=5, log_output="logs/dreams.log", say_thought=False):
print("[DAYDREAM] Ruby is imagining new thoughts...")
thoughts = []
for _ in range(rounds):
thought = self.generate_reply()
if thought.strip():
print(f"[THOUGHT] {thought}")
self.train_on_tokens_from_text(thought)
print("[DAYDREAM] Complete.")
thoughts.append(thought)
with open(log_output, "a", encoding="utf-8") as f:
for t in thoughts:
f.write(f"[DAYDREAM] {t}\n")
print(f"[DAYDREAM] Complete. {len(thoughts)} thoughts imagined.")
if say_thought and thoughts:
return thoughts[-1] # last thought spoken aloud
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")