from threading import Thread import numpy as np from flask import Flask, render_template_string, request, send_from_directory from jinja2 import BaseLoader from openrewind.config import appdata_folder, screenshots_path from openrewind.database import create_db, get_all_entries, get_timestamps from openrewind.nlp import cosine_similarity, get_embedding from openrewind.screenshot import record_screenshots_thread from openrewind.utils import human_readable_time, timestamp_to_human_readable app = Flask(__name__) app.jinja_env.filters["human_readable_time"] = human_readable_time app.jinja_env.filters["timestamp_to_human_readable"] = timestamp_to_human_readable base_template = """ OpenRecall {% block content %} {% endblock %} """ class StringLoader(BaseLoader): def get_source(self, environment, template): if template == "base_template": return base_template, None, lambda: True return None, None, None app.jinja_env.loader = StringLoader() @app.route("/") def timeline(): # connect to db timestamps = get_timestamps() return render_template_string( """ {% extends "base_template" %} {% block content %} {% if timestamps|length > 0 %}
{{timestamps[0] | timestamp_to_human_readable }}
Image for timestamp
{% else %}
{% endif %} {% endblock %} """, timestamps=timestamps, ) @app.route("/search") def search(): q = request.args.get("q") entries = get_all_entries() embeddings = [ np.frombuffer(entry.embedding, dtype=np.float64) for entry in entries ] query_embedding = get_embedding(q) similarities = [cosine_similarity(query_embedding, emb) for emb in embeddings] indices = np.argsort(similarities)[::-1] sorted_entries = [entries[i] for i in indices] return render_template_string( """ {% extends "base_template" %} {% block content %}
{% for entry in entries %} {% endfor %}
{% endblock %} """, entries=sorted_entries, ) @app.route("/static/") def serve_image(filename): return send_from_directory(screenshots_path, filename) if __name__ == "__main__": create_db() print(f"Appdata folder: {appdata_folder}") # Start the thread to record screenshots t = Thread(target=record_screenshots_thread) t.start() app.run(port=8082)