from flask import Flask, render_template_string import os app = Flask(__name__) ruby_client = None # This will be set externally def tail(filepath, num_lines=10): if not os.path.exists(filepath): return [] with open(filepath, encoding="utf-8") as f: return f.readlines()[-num_lines:] def get_best_dream(): if not os.path.exists("logs/best_dream.txt"): return "No high-scoring dream yet." with open("logs/best_dream.txt", encoding="utf-8") as f: return f.read().strip() @app.route("/") def home(): 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) dreams = [line.strip() for line in tail("logs/dreams.log", 10)] messages = [line.strip() for line in tail("logs/messages.log", 10)] errors = [line.strip() for line in tail("logs/error.log", 15)] best_dream = get_best_dream() # Handle book progress if Ruby has a reader book = { "book": "Not reading", "line": 0, "total": 0, "percent": 0.0, "last_sentence": "" } if ruby_client and hasattr(ruby_client, "reader"): book = ruby_client.reader.progress() return render_template_string(""" Ruby Dashboard

🌸 Ruby's Dashboard

Vocabulary Size: {{ vocab_size }}

📖 Book Progress

{{ book.book }} – Line {{ book.line }} of {{ book.total }} ({{ book.percent }}%)

{{ book.last_sentence }}

🏆 Highest Scoring Dream

{{ best_dream }}

🧠 Recent Daydreams

📨 Recent Messages

⚠️ Recent Errors

        {% for err in errors %}
{{ err }}
        {% endfor %}
        
""", best_dream=best_dream, dreams=dreams[::-1], messages=messages[::-1], errors=errors[::-1], vocab_size=vocab_size, book=book) def start_dashboard_background(): import threading thread = threading.Thread(target=lambda: app.run(debug=False, host="0.0.0.0", port=5000)) thread.daemon = True thread.start()