Ruby/headspace/dashboard.py

41 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# dashboard.py
import threading
from flask import Flask, render_template_string
app = Flask(__name__)
_organism = None # will be set by run_dashboard()
@app.route("/")
def index():
# Basic stats
total = len(_organism.memory.memory)
# Show up to 10 most recent text interactions
recent = list(_organism.memory.memory)[-10:][::-1]
items = ""
for i in recent:
inp = i.get("input_text", "<no input>")
out = i.get("output_text", "<no output>")
items += f"<li><b>In:</b> {inp}<br/><b>Out:</b> {out}</li>"
html = f"""
<h1>Ruby Dashboard</h1>
<p><strong>Total stored interactions:</strong> {total}</p>
<h2>Last {len(recent)} exchanges</h2>
<ul>{items}</ul>
"""
return render_template_string(html)
def run_dashboard(organism, host="0.0.0.0", port=5000):
"""Call this to launch the dashboard in a background thread."""
global _organism
_organism = organism
# start Flask in its own thread so it doesnt block Discord
thread = threading.Thread(
target=lambda: app.run(host=host, port=port, debug=False, use_reloader=False),
daemon=True,
)
thread.start()