Tiresias/web/web.py

98 lines
3.2 KiB
Python

import os
import io
import redis
import sqlite3
import json
from flask import Flask, jsonify, send_from_directory, render_template, render_template_string
# -----------------------------
# ENVIRONMENT CONFIG
# -----------------------------
redis_host = os.getenv("redis_host", "localhost")
redis_port = int(os.getenv("redis_port", "6379"))
stream_url = os.getenv("stream_url", "/path/to/stream")
output_folder = os.getenv("out_folder", "/app/out_folder")
stream_label = os.getenv("stream_label", "default_stream")
stream_label_queue = f"{stream_label}_cubes"
sqlite_path = os.path.join(output_folder, f"{stream_label}_results.db")
gif_folder = os.path.join(output_folder, stream_label)
sqlite_path = os.path.join(output_folder, f"{stream_label}_results.db")
gif_folder = os.path.join(output_folder, stream_label)
# -----------------------------
# REDIS + SQLITE CONNECTIONS
# -----------------------------
redis_conn = redis.Redis(host=redis_host, port=redis_port, db=0, decode_responses=False)
conn = sqlite3.connect(sqlite_path, check_same_thread=False)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS inference_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
gif_name TEXT,
timestamp TIMESTAMP,
prediction INTEGER
)
''')
conn.commit()
# -----------------------------
# REDIS PUSH FUNCTION
# -----------------------------
def toRedis(queue_label, data):
print(f"Pushed data to queue: {queue_label}")
buffer = io.BytesIO()
np.savez(buffer, data=data)
compressed_data = buffer.getvalue()
return redis_conn.rpush(queue_label, compressed_data)
# -----------------------------
# FLASK SETUP
# -----------------------------
app = Flask(__name__, template_folder='/app/templates')
# Root returns a placeholder HTML template
@app.route("/")
def home():
return render_template("index.html", stream_url=stream_url)
# JSON API endpoint for status monitoring
@app.route("/status")
def api_status():
try:
queue_len_1 = redis_conn.llen(stream_label)
queue_len_2 = redis_conn.llen(stream_label_queue)
# Fetch 10 latest positive and negative predictions
cursor.execute("SELECT gif_name, timestamp FROM inference_results WHERE prediction = 1 ORDER BY timestamp DESC LIMIT 10")
positives = [{"gif": row[0], "timestamp": row[1]} for row in cursor.fetchall()]
cursor.execute("SELECT gif_name, timestamp FROM inference_results WHERE prediction = 0 ORDER BY timestamp DESC LIMIT 10")
negatives = [{"gif": row[0], "timestamp": row[1]} for row in cursor.fetchall()]
return jsonify({
"stream_1_queue": queue_len_1,
"stream_2_queue": queue_len_2,
"positives": positives,
"negatives": negatives
})
except Exception as e:
return jsonify({"error": str(e)}), 500
# Serve static GIFs
@app.route("/gifs/<path:filename>")
def serve_gif(filename):
return send_from_directory(gif_folder, filename)
# -----------------------------
# MAIN ENTRY
# -----------------------------
if __name__ == "__main__":
print(f"[INFO] Flask app running — monitoring queues: {stream_label}, {stream_label_queue}")
print(f"[INFO] Serving GIFs from: {gif_folder}")
app.run(host="0.0.0.0", port=8080, debug=True)