Candle commited on
Commit ·
33eefbf
1
Parent(s): 27b724d
human feedback
Browse files- human_feedback_ui.py +105 -0
- requirements.txt +2 -1
human_feedback_ui.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# To Run: uv run streamlit run human_feedback_ui.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import json
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
st.set_page_config(layout="wide")
|
| 9 |
+
|
| 10 |
+
data_dir = Path("data/animations")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# List all samples
|
| 14 |
+
json_files = sorted(data_dir.glob("sample-*.webp"))
|
| 15 |
+
sample_names = [f.stem for f in json_files]
|
| 16 |
+
|
| 17 |
+
# Find first sample without .hf.json
|
| 18 |
+
def find_next_unlabeled_sample(current=None):
|
| 19 |
+
for name in sample_names:
|
| 20 |
+
hf_path = data_dir / f"{name}.hf.json"
|
| 21 |
+
if not hf_path.exists():
|
| 22 |
+
if current is None or name != current:
|
| 23 |
+
return name
|
| 24 |
+
return sample_names[0] if sample_names else None
|
| 25 |
+
|
| 26 |
+
# Use session state to track current sample
|
| 27 |
+
if "sample_choice" not in st.session_state:
|
| 28 |
+
st.session_state["sample_choice"] = find_next_unlabeled_sample()
|
| 29 |
+
|
| 30 |
+
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)
|
| 31 |
+
st.session_state["sample_choice"] = sample_choice
|
| 32 |
+
|
| 33 |
+
json_path = data_dir / f"{sample_choice}.json"
|
| 34 |
+
webp_path = data_dir / f"{sample_choice}.webp"
|
| 35 |
+
|
| 36 |
+
# Load frames
|
| 37 |
+
frames = []
|
| 38 |
+
if webp_path.exists():
|
| 39 |
+
im = Image.open(webp_path)
|
| 40 |
+
try:
|
| 41 |
+
while True:
|
| 42 |
+
frames.append(im.convert("RGB"))
|
| 43 |
+
im.seek(im.tell() + 1)
|
| 44 |
+
except EOFError:
|
| 45 |
+
pass
|
| 46 |
+
else:
|
| 47 |
+
st.error(f"WebP file not found: {webp_path}")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Load scene change indices from .hf.json if exists, else from .json
|
| 51 |
+
hf_path = data_dir / f"{sample_choice}.hf.json"
|
| 52 |
+
if hf_path.exists():
|
| 53 |
+
with open(hf_path) as f:
|
| 54 |
+
data = json.load(f)
|
| 55 |
+
scene_change_indices = data.get("scene_change_indices", [])
|
| 56 |
+
else:
|
| 57 |
+
with open(json_path) as f:
|
| 58 |
+
data = json.load(f)
|
| 59 |
+
scene_change_indices = data.get("scene_change_indices", [])
|
| 60 |
+
|
| 61 |
+
st.write(f"Total frames: {len(frames)}")
|
| 62 |
+
|
| 63 |
+
# Show thumbnails with selection
|
| 64 |
+
selected = st.multiselect(
|
| 65 |
+
"Select transition frames (scene changes)",
|
| 66 |
+
options=list(range(len(frames))),
|
| 67 |
+
default=scene_change_indices,
|
| 68 |
+
format_func=lambda i: f"Frame {i}"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Show thumbnails in a grid
|
| 72 |
+
|
| 73 |
+
# Horizontal slider view for thumbnails
|
| 74 |
+
|
| 75 |
+
import streamlit.components.v1 as components
|
| 76 |
+
import io
|
| 77 |
+
import base64
|
| 78 |
+
thumb_size = 64 # Slightly larger for visibility
|
| 79 |
+
slider_html = "<div style='overflow-x:auto; white-space:nowrap; padding:8px;'>"
|
| 80 |
+
for i, frame in enumerate(frames):
|
| 81 |
+
buf = io.BytesIO()
|
| 82 |
+
frame.resize((thumb_size, thumb_size)).save(buf, format='PNG')
|
| 83 |
+
img_b64 = base64.b64encode(buf.getvalue()).decode('utf-8')
|
| 84 |
+
highlight = i in selected
|
| 85 |
+
border = '3px solid red' if highlight else '1px solid #ccc'
|
| 86 |
+
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>"
|
| 87 |
+
slider_html += "</div>"
|
| 88 |
+
components.html(slider_html, height=thumb_size+40)
|
| 89 |
+
|
| 90 |
+
# Save button
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if st.button("Save human feedback"):
|
| 94 |
+
out_path = data_dir / f"{sample_choice}.hf.json"
|
| 95 |
+
with open(out_path, "w") as f:
|
| 96 |
+
json.dump({"scene_change_indices": sorted(selected)}, f, indent=2)
|
| 97 |
+
st.success(f"Saved to {out_path}")
|
| 98 |
+
next_sample = find_next_unlabeled_sample(current=sample_choice)
|
| 99 |
+
if next_sample and next_sample != sample_choice:
|
| 100 |
+
st.info(f"Next sample available: {next_sample}")
|
| 101 |
+
if st.button("Go to next sample"):
|
| 102 |
+
st.session_state["sample_choice"] = next_sample
|
| 103 |
+
# Force rerun by updating query params (Streamlit best practice)
|
| 104 |
+
st.experimental_set_query_params(sample=next_sample)
|
| 105 |
+
st.stop()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
pillow
|
| 2 |
torch
|
| 3 |
numpy
|
| 4 |
-
matplotlib
|
|
|
|
|
|
| 1 |
pillow
|
| 2 |
torch
|
| 3 |
numpy
|
| 4 |
+
matplotlib
|
| 5 |
+
streamlit
|