import json import os import re from flask import Flask, render_template_string, send_from_directory # --- CONFIGURATION --- # Make sure these match your actual folder names exactly! FOLDERS = { 'baseline': 'baseline', 'teacache': 'teacache', 'magcache': 'magcache', 'ours': 'ours' } PROMPT_FILE = 'prompts.json' # --- LOGIC --- app = Flask(__name__) BASE_DIR = os.path.abspath(os.path.dirname(__file__)) def get_prompts(): path = os.path.join(BASE_DIR, PROMPT_FILE) if not os.path.exists(path): return [] with open(path, 'r', encoding='utf-8') as f: try: content = json.load(f) if isinstance(content, list): return content except: f.seek(0) lines = [line.strip() for line in f if line.strip()] return lines def find_file_by_id(folder, video_id): dir_path = os.path.join(BASE_DIR, folder) if not os.path.exists(dir_path): return None for fname in os.listdir(dir_path): if fname.startswith(video_id) and fname.lower().endswith('.mp4'): return fname return None def build_tasks(): tasks = [] prompts = get_prompts() baseline_path = os.path.join(BASE_DIR, FOLDERS['baseline']) if not os.path.exists(baseline_path): print("Baseline folder missing!") return [] baseline_files = sorted([f for f in os.listdir(baseline_path) if f.lower().endswith('.mp4')]) id_regex = re.compile(r'^(\d{4})') for i, b_file in enumerate(baseline_files): match = id_regex.match(b_file) if not match: continue vid_id = match.group(1) prompt_text = prompts[i] if i < len(prompts) else "(No prompt)" tea_file = find_file_by_id(FOLDERS['teacache'], vid_id) mag_file = find_file_by_id(FOLDERS['magcache'], vid_id) our_file = find_file_by_id(FOLDERS['ours'], vid_id) tasks.append({ "index": i + 1, "id": vid_id, "prompt": prompt_text, "baseline": f"videos/{FOLDERS['baseline']}/{b_file}", "teacache": f"videos/{FOLDERS['teacache']}/{tea_file}" if tea_file else "", "magcache": f"videos/{FOLDERS['magcache']}/{mag_file}" if mag_file else "", "ours": f"videos/{FOLDERS['ours']}/{our_file}" if our_file else "" }) print(f"Loaded {len(tasks)} videos.") return tasks TASKS = build_tasks() # --- HTML TEMPLATE --- HTML = """ 2x2 Visualizer
ID: -- 0 / 0

Loading...

Baseline
TeaCache
MagCache
Ours
""" @app.route('/') def home(): return render_template_string(HTML, tasks_json=json.dumps(TASKS)) @app.route('/videos//') def serve_file(folder, filename): if folder in FOLDERS: return send_from_directory(os.path.join(BASE_DIR, FOLDERS[folder]), filename) return "Error", 404 if __name__ == '__main__': print("Starting server...") app.run(port=5001, debug=True)