# To Run: uv run streamlit run human_feedback_ui.py import streamlit as st import json from pathlib import Path from PIL import Image import numpy as np st.set_page_config(layout="wide") data_dir = Path("data/animations") # List all samples json_files = sorted(data_dir.glob("sample-*.webp")) sample_names = [f.stem for f in json_files] # Find first sample without .hf.json def find_next_unlabeled_sample(current=None): for name in sample_names: hf_path = data_dir / f"{name}.hf.json" if not hf_path.exists(): if current is None or name != current: return name return sample_names[0] if sample_names else None # Use session state to track current sample if "sample_choice" not in st.session_state: st.session_state["sample_choice"] = find_next_unlabeled_sample() sample_choice = st.selectbox("Select sample", sample_names, index=sample_names.index(st.session_state["sample_choice"]) if st.session_state["sample_choice"] in sample_names else 0) st.session_state["sample_choice"] = sample_choice json_path = data_dir / f"{sample_choice}.json" webp_path = data_dir / f"{sample_choice}.webp" # Load frames frames = [] if webp_path.exists(): im = Image.open(webp_path) try: while True: frames.append(im.convert("RGB")) im.seek(im.tell() + 1) except EOFError: pass else: st.error(f"WebP file not found: {webp_path}") # Load scene change indices from .hf.json if exists, else from .json hf_path = data_dir / f"{sample_choice}.hf.json" if hf_path.exists(): with open(hf_path) as f: data = json.load(f) scene_change_indices = data.get("scene_change_indices", []) else: with open(json_path) as f: data = json.load(f) scene_change_indices = data.get("scene_change_indices", []) # --- Auto-save and move to next if cuts are exactly at 40 and 80 --- if sorted(scene_change_indices) == [40, 80]: out_path = data_dir / f"{sample_choice}.hf.json" with open(out_path, "w") as f: json.dump({"scene_change_indices": [40, 80]}, f, indent=2) next_sample = find_next_unlabeled_sample(current=sample_choice) st.info(f"Auto-saved human feedback for {sample_choice} (cuts at 40, 80). Moving to next sample...") if next_sample and next_sample != sample_choice: st.session_state["sample_choice"] = next_sample st.experimental_set_query_params(sample=next_sample) st.stop() st.write(f"Total frames: {len(frames)}") # Show thumbnails with selection selected = st.multiselect( "Select transition frames (scene changes)", options=list(range(len(frames))), default=scene_change_indices, format_func=lambda i: f"Frame {i}" ) # Show thumbnails in a grid # Horizontal slider view for thumbnails import streamlit.components.v1 as components import io import base64 thumb_size = 64 # Slightly larger for visibility slider_html = "
" for i, frame in enumerate(frames): buf = io.BytesIO() frame.resize((thumb_size, thumb_size)).save(buf, format='PNG') img_b64 = base64.b64encode(buf.getvalue()).decode('utf-8') highlight = i in selected border = '3px solid red' if highlight else '1px solid #ccc' slider_html += f"
{i}
" slider_html += "
" components.html(slider_html, height=thumb_size+40) # Save button if st.button("Save human feedback"): out_path = data_dir / f"{sample_choice}.hf.json" with open(out_path, "w") as f: json.dump({"scene_change_indices": sorted(selected)}, f, indent=2) st.success(f"Saved to {out_path}") next_sample = find_next_unlabeled_sample(current=sample_choice) if next_sample and next_sample != sample_choice: st.info(f"Next sample available: {next_sample}") if st.button("Go to next sample"): st.session_state["sample_choice"] = next_sample # Force rerun by updating query params (Streamlit best practice) st.experimental_set_query_params(sample=next_sample) st.stop()