duongthienz commited on
Commit
30199c9
·
verified ·
1 Parent(s): c823bc3

Update ui.py

Browse files
Files changed (1) hide show
  1. ui.py +47 -29
ui.py CHANGED
@@ -13,9 +13,9 @@ import plotly.express as px
13
  import utils
14
  from state import (
15
  updateCategoryOptions, removeCategory, addCategory,
16
- applyGlobalRenames, enforceGlobalRenameExclusivity,
17
  addGlobalRename, removeGlobalRename,
18
- _global_rename_key, randomize_speaker_clip, apply_inline_rename,
 
19
  )
20
 
21
 
@@ -51,20 +51,31 @@ def render_chart(fig, tab, pdf_path, svg_path, pdf_name, svg_name, pdf_key, svg_
51
  # Sidebar — categories section
52
  # ---------------------------------------------------------------------------
53
 
54
- def render_categories_sidebar(currFile, categorySelections, all_speakers_display,
55
- raw_to_display):
56
- """Render category multiselects, remove buttons, and Add role input."""
57
  st.sidebar.markdown(
58
  "<p style='font-size:0.85rem; color:gray; margin-bottom:2px;'>"
59
- "Create roles (instructor, students, etc.) and add speakers.</p>",
 
60
  unsafe_allow_html=True,
61
  )
 
 
62
  for i, category in enumerate(st.session_state.categories):
63
- ms_key = f"multiselect_{category}"
64
- speakerSet = categorySelections[i]
65
- default_disp = [raw_to_display.get(sp, sp) for sp in speakerSet]
66
  if ms_key not in st.session_state:
67
- st.session_state[ms_key] = default_disp
 
 
 
 
 
 
 
 
 
 
68
  label_col, trash_col = st.sidebar.columns([5, 1])
69
  label_col.markdown(f"**{category}**")
70
  trash_col.button(
@@ -72,12 +83,17 @@ def render_categories_sidebar(currFile, categorySelections, all_speakers_display
72
  on_click=removeCategory, args=(i,),
73
  help=f"Remove {category}",
74
  )
 
 
 
 
 
75
  st.sidebar.multiselect(
76
- category, all_speakers_display,
77
- key=ms_key, on_change=updateCategoryOptions, args=(currFile,),
78
  label_visibility="collapsed",
79
  )
80
- st.sidebar.text_input("Add role", key="categoryInput", on_change=addCategory)
81
 
82
 
83
  # ---------------------------------------------------------------------------
@@ -97,17 +113,11 @@ def render_rename_sidebar(currFile, speakerNames, all_speaker_tokens):
97
 
98
  st.sidebar.divider()
99
 
100
- def _on_grename_change(idx):
101
- st.session_state.globalRenames[idx]["speakers"] = list(
102
- st.session_state[_global_rename_key(idx)]
103
- )
104
- enforceGlobalRenameExclusivity(idx)
105
-
106
- # Build a set of every token already claimed by some entry, for fast lookup
107
  all_claimed = {
108
  token
109
- for e in st.session_state.globalRenames
110
- for token in e["speakers"]
111
  }
112
 
113
  for idx, entry in enumerate(st.session_state.globalRenames):
@@ -121,7 +131,6 @@ def render_rename_sidebar(currFile, speakerNames, all_speaker_tokens):
121
  on_click=removeGlobalRename, args=(idx,),
122
  help=f"Remove '{entry['name']}'",
123
  )
124
- # Options = tokens owned by this entry + tokens not claimed by anyone
125
  this_entry_tokens = set(entry["speakers"])
126
  available_tokens = [
127
  t for t in all_speaker_tokens
@@ -129,7 +138,7 @@ def render_rename_sidebar(currFile, speakerNames, all_speaker_tokens):
129
  ]
130
  st.sidebar.multiselect(
131
  f"Speakers for {entry['name']}", options=available_tokens,
132
- key=grkey, on_change=_on_grename_change, args=(idx,),
133
  label_visibility="collapsed",
134
  )
135
 
@@ -177,8 +186,15 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
177
 
178
  if "inline_rename_active" not in st.session_state:
179
  st.session_state.inline_rename_active = {}
 
 
 
 
180
  if "inline_rename_history" not in st.session_state:
181
  st.session_state.inline_rename_history = []
 
 
 
182
 
183
  # Header
184
  header_cols = st.columns([3, 3, 1])
@@ -205,7 +221,10 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
205
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
206
 
207
  history = st.session_state.inline_rename_history
208
- current_val = display_name if display_name != sp else ""
 
 
 
209
 
210
  _NEW_OPTION = "+ Enter a new name"
211
 
@@ -251,10 +270,9 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
251
  btn_col1, btn_col2 = st.columns(2)
252
  if btn_col1.button("✓", key=confirm_key, help="Confirm rename"):
253
  confirmed = (new_name or "").strip()
254
- if confirmed:
255
- apply_inline_rename(currFile, sp, confirmed)
256
- if confirmed not in st.session_state.inline_rename_history:
257
- st.session_state.inline_rename_history.append(confirmed)
258
  st.session_state.inline_rename_active[edit_key] = False
259
  st.session_state.pop(select_key, None)
260
  st.session_state.pop(input_key, None)
 
13
  import utils
14
  from state import (
15
  updateCategoryOptions, removeCategory, addCategory,
 
16
  addGlobalRename, removeGlobalRename,
17
+ _global_rename_key, on_grename_change,
18
+ randomize_speaker_clip, apply_inline_rename,
19
  )
20
 
21
 
 
51
  # Sidebar — categories section
52
  # ---------------------------------------------------------------------------
53
 
54
+ def render_categories_sidebar(all_speaker_tokens):
55
+ """Render cross-file role multiselects using speaker tokens, matching the rename sidebar pattern."""
56
+ st.sidebar.subheader("Add Speaker's Role")
57
  st.sidebar.markdown(
58
  "<p style='font-size:0.85rem; color:gray; margin-bottom:2px;'>"
59
+ "Add speaker(s) into the Instructor or Students role. You can also add a new role "
60
+ "by typing in the \"Add custom role\" textbox.</p>",
61
  unsafe_allow_html=True,
62
  )
63
+ st.sidebar.divider()
64
+
65
  for i, category in enumerate(st.session_state.categories):
66
+ ms_key = f"multiselect_{category}"
 
 
67
  if ms_key not in st.session_state:
68
+ st.session_state[ms_key] = list(st.session_state.categorySelect[i]) if i < len(st.session_state.categorySelect) else []
69
+
70
+ # Build claimed set from widget keys (source of truth for what's displayed)
71
+ all_claimed = {
72
+ token
73
+ for i, category in enumerate(st.session_state.categories)
74
+ for token in st.session_state.get(f"multiselect_{category}", [])
75
+ }
76
+
77
+ for i, category in enumerate(st.session_state.categories):
78
+ ms_key = f"multiselect_{category}"
79
  label_col, trash_col = st.sidebar.columns([5, 1])
80
  label_col.markdown(f"**{category}**")
81
  trash_col.button(
 
83
  on_click=removeCategory, args=(i,),
84
  help=f"Remove {category}",
85
  )
86
+ this_role_tokens = set(st.session_state.get(ms_key, []))
87
+ available_tokens = [
88
+ t for t in all_speaker_tokens
89
+ if t not in all_claimed or t in this_role_tokens
90
+ ]
91
  st.sidebar.multiselect(
92
+ category, available_tokens,
93
+ key=ms_key, on_change=updateCategoryOptions,
94
  label_visibility="collapsed",
95
  )
96
+ st.sidebar.text_input("Add custom role", key="categoryInput", on_change=addCategory)
97
 
98
 
99
  # ---------------------------------------------------------------------------
 
113
 
114
  st.sidebar.divider()
115
 
116
+ # Build claimed set from the data model (entry["speakers"]) as canonical truth.
 
 
 
 
 
 
117
  all_claimed = {
118
  token
119
+ for entry in st.session_state.globalRenames
120
+ for token in entry["speakers"]
121
  }
122
 
123
  for idx, entry in enumerate(st.session_state.globalRenames):
 
131
  on_click=removeGlobalRename, args=(idx,),
132
  help=f"Remove '{entry['name']}'",
133
  )
 
134
  this_entry_tokens = set(entry["speakers"])
135
  available_tokens = [
136
  t for t in all_speaker_tokens
 
138
  ]
139
  st.sidebar.multiselect(
140
  f"Speakers for {entry['name']}", options=available_tokens,
141
+ key=grkey, on_change=on_grename_change, args=(idx,),
142
  label_visibility="collapsed",
143
  )
144
 
 
186
 
187
  if "inline_rename_active" not in st.session_state:
188
  st.session_state.inline_rename_active = {}
189
+
190
+ # Build the history list from all confirmed names: start from whatever was
191
+ # saved from past sessions, then merge in any names already in globalRenames
192
+ # so that sidebar-added names show up as history options in the tab too.
193
  if "inline_rename_history" not in st.session_state:
194
  st.session_state.inline_rename_history = []
195
+ for entry in st.session_state.globalRenames:
196
+ if entry["name"] not in st.session_state.inline_rename_history:
197
+ st.session_state.inline_rename_history.append(entry["name"])
198
 
199
  # Header
200
  header_cols = st.columns([3, 3, 1])
 
221
  cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
222
 
223
  history = st.session_state.inline_rename_history
224
+ # Read the saved rename directly from speakerRenames so that
225
+ # current_val is "" when no rename exists, even if display_name
226
+ # happens to equal the raw speaker label for some other reason.
227
+ current_val = st.session_state.speakerRenames.get(currFile, {}).get(sp, "")
228
 
229
  _NEW_OPTION = "+ Enter a new name"
230
 
 
270
  btn_col1, btn_col2 = st.columns(2)
271
  if btn_col1.button("✓", key=confirm_key, help="Confirm rename"):
272
  confirmed = (new_name or "").strip()
273
+ apply_inline_rename(currFile, sp, confirmed)
274
+ if confirmed and confirmed not in st.session_state.inline_rename_history:
275
+ st.session_state.inline_rename_history.append(confirmed)
 
276
  st.session_state.inline_rename_active[edit_key] = False
277
  st.session_state.pop(select_key, None)
278
  st.session_state.pop(input_key, None)