From 2cf713ca978989c4508559114b9c0b271131ccc0 Mon Sep 17 00:00:00 2001 From: Dani Date: Thu, 24 Apr 2025 13:28:53 -0400 Subject: [PATCH] added contextual awareness --- context/context.py | 34 ++++++++++++++++++++++++++++++++++ dashboard/dashboard.py | 5 ++++- dashboard/templates/index.html | 1 + model/train.py | 9 +++++++-- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/context/context.py b/context/context.py index e69de29..6506866 100644 --- a/context/context.py +++ b/context/context.py @@ -0,0 +1,34 @@ +import json +import os +import time +from typing import List + +CONTEXT_FILE = "data/memory/context.json" +MAX_MEMORY = 100 + + +def load_context() -> List[dict]: + if os.path.exists(CONTEXT_FILE): + with open(CONTEXT_FILE, "r", encoding="utf-8") as f: + return json.load(f) + return [] + + +def save_context(mem: List[dict]): + with open(CONTEXT_FILE, "w", encoding="utf-8") as f: + json.dump(mem[-MAX_MEMORY:], f, indent=2) + + +def add_to_context(text: str, source: str = "user"): + mem = load_context() + mem.append({ + "timestamp": time.time(), + "source": source, + "text": text + }) + save_context(mem) + + +def get_recent_context(n: int = 5) -> List[str]: + mem = load_context() + return [entry["text"] for entry in mem[-n:]] diff --git a/dashboard/dashboard.py b/dashboard/dashboard.py index c8749b4..cb66b6d 100644 --- a/dashboard/dashboard.py +++ b/dashboard/dashboard.py @@ -1,6 +1,7 @@ from flask import Flask, render_template from model.memory import load_dreams from model.tokenizer import Tokenizer +from context.context import load_context import threading @@ -12,9 +13,11 @@ tokenizer = Tokenizer() def index(): dreams = load_dreams() top_dreams = dreams[:5] + memory_size = len(load_context()) return render_template("index.html", vocab_size=len(tokenizer.vocab), - top_dreams=top_dreams) + top_dreams=top_dreams, + memory_size=memory_size) def run_dashboard(): diff --git a/dashboard/templates/index.html b/dashboard/templates/index.html index 819776f..5c0b5aa 100644 --- a/dashboard/templates/index.html +++ b/dashboard/templates/index.html @@ -6,6 +6,7 @@

Ruby is running

Vocabulary Size: {{ vocab_size }}

+

Memory Entries: {{ memory_size }}

🏆 Highest Scoring Dreams