File size: 4,350 Bytes
33eefbf 8febde4 33eefbf 8febde4 33eefbf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | # 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 = "<div style='overflow-x:auto; white-space:nowrap; padding:8px;'>"
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"<span style='display:inline-block; margin:2px; border:{border}; border-radius:4px;'><img src='data:image/png;base64,{img_b64}' style='width:{thumb_size}px;height:{thumb_size}px;'><div style='text-align:center;font-size:10px;color:{'red' if highlight else '#333'}'>{i}</div></span>"
slider_html += "</div>"
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()
|