Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
Add a reset button for rename
Browse files
ui.py
CHANGED
|
@@ -243,69 +243,86 @@ def render_speaker_samples_tab(speakerNames, raw_to_display, currFile):
|
|
| 243 |
input_key = f"inline_rename_input_{currFile}_{sp}"
|
| 244 |
confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
|
| 245 |
cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
|
|
|
|
|
|
|
| 246 |
|
| 247 |
history = st.session_state.inline_rename_history
|
| 248 |
-
# Read the saved rename directly from speakerRenames so that
|
| 249 |
-
# current_val is "" when no rename exists, even if display_name
|
| 250 |
-
# happens to equal the raw speaker label for some other reason.
|
| 251 |
current_val = st.session_state.speakerRenames.get(currFile, {}).get(sp, "")
|
| 252 |
|
| 253 |
_NEW_OPTION = "οΌ Enter a new name"
|
| 254 |
|
| 255 |
with row_cols[0]:
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
if input_key not in st.session_state:
|
| 273 |
-
st.session_state[input_key] =
|
| 274 |
new_name = st.text_input(
|
| 275 |
-
"
|
| 276 |
key=input_key,
|
| 277 |
label_visibility="collapsed",
|
| 278 |
placeholder=f"Rename {sp}β¦",
|
| 279 |
)
|
| 280 |
-
else:
|
| 281 |
-
new_name = chosen
|
| 282 |
-
st.session_state.pop(input_key, None)
|
| 283 |
-
else:
|
| 284 |
-
# No history yet β just a plain text input
|
| 285 |
-
if input_key not in st.session_state:
|
| 286 |
-
st.session_state[input_key] = current_val
|
| 287 |
-
new_name = st.text_input(
|
| 288 |
-
"Rename",
|
| 289 |
-
key=input_key,
|
| 290 |
-
label_visibility="collapsed",
|
| 291 |
-
placeholder=f"Rename {sp}β¦",
|
| 292 |
-
)
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
st.
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
else:
|
| 310 |
with row_cols[0]:
|
| 311 |
name_col, pencil_col = st.columns([4, 1])
|
|
|
|
| 243 |
input_key = f"inline_rename_input_{currFile}_{sp}"
|
| 244 |
confirm_key = f"inline_rename_confirm_{currFile}_{sp}"
|
| 245 |
cancel_key = f"inline_rename_cancel_{currFile}_{sp}"
|
| 246 |
+
reset_key = f"inline_rename_reset_{currFile}_{sp}"
|
| 247 |
+
form_key = f"inline_rename_form_{currFile}_{sp}"
|
| 248 |
|
| 249 |
history = st.session_state.inline_rename_history
|
|
|
|
|
|
|
|
|
|
| 250 |
current_val = st.session_state.speakerRenames.get(currFile, {}).get(sp, "")
|
| 251 |
|
| 252 |
_NEW_OPTION = "οΌ Enter a new name"
|
| 253 |
|
| 254 |
with row_cols[0]:
|
| 255 |
+
# Wrap in a form so pressing Enter submits (confirms) the rename
|
| 256 |
+
with st.form(key=form_key, border=False):
|
| 257 |
+
if history:
|
| 258 |
+
options = history + [_NEW_OPTION]
|
| 259 |
+
default_idx = (
|
| 260 |
+
history.index(current_val) if current_val in history
|
| 261 |
+
else len(options) - 1
|
| 262 |
+
)
|
| 263 |
+
chosen = st.selectbox(
|
| 264 |
+
"Rename",
|
| 265 |
+
options=options,
|
| 266 |
+
index=default_idx,
|
| 267 |
+
key=select_key,
|
| 268 |
+
label_visibility="collapsed",
|
| 269 |
+
)
|
| 270 |
+
if chosen == _NEW_OPTION:
|
| 271 |
+
if input_key not in st.session_state:
|
| 272 |
+
st.session_state[input_key] = ""
|
| 273 |
+
new_name = st.text_input(
|
| 274 |
+
"New name",
|
| 275 |
+
key=input_key,
|
| 276 |
+
label_visibility="collapsed",
|
| 277 |
+
placeholder=f"Rename {sp}β¦",
|
| 278 |
+
)
|
| 279 |
+
else:
|
| 280 |
+
new_name = chosen
|
| 281 |
+
st.session_state.pop(input_key, None)
|
| 282 |
+
else:
|
| 283 |
+
# No history yet β just a plain text input
|
| 284 |
if input_key not in st.session_state:
|
| 285 |
+
st.session_state[input_key] = current_val
|
| 286 |
new_name = st.text_input(
|
| 287 |
+
"Rename",
|
| 288 |
key=input_key,
|
| 289 |
label_visibility="collapsed",
|
| 290 |
placeholder=f"Rename {sp}β¦",
|
| 291 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
+
# Right-align the three buttons: confirm > cancel > reset
|
| 294 |
+
st.markdown(
|
| 295 |
+
"<style>div[data-testid='stFormSubmitButton'] {"
|
| 296 |
+
"display:flex; justify-content:flex-end; gap:4px;}</style>",
|
| 297 |
+
unsafe_allow_html=True,
|
| 298 |
+
)
|
| 299 |
+
_, btn_col1, btn_col2, btn_col3 = st.columns([4, 1, 1, 1])
|
| 300 |
+
confirmed_submit = btn_col1.form_submit_button("β", help="Confirm rename (or press Enter)")
|
| 301 |
+
cancel_clicked = btn_col2.form_submit_button("β", help="Cancel")
|
| 302 |
+
reset_clicked = btn_col3.form_submit_button("β³", help="Reset to original speaker label")
|
| 303 |
+
|
| 304 |
+
if confirmed_submit:
|
| 305 |
+
confirmed = (new_name or "").strip()
|
| 306 |
+
apply_inline_rename(currFile, sp, confirmed)
|
| 307 |
+
if confirmed and confirmed not in st.session_state.inline_rename_history:
|
| 308 |
+
st.session_state.inline_rename_history.append(confirmed)
|
| 309 |
+
st.session_state.inline_rename_active[edit_key] = False
|
| 310 |
+
st.session_state.pop(select_key, None)
|
| 311 |
+
st.session_state.pop(input_key, None)
|
| 312 |
+
st.rerun()
|
| 313 |
+
if cancel_clicked:
|
| 314 |
+
st.session_state.inline_rename_active[edit_key] = False
|
| 315 |
+
st.session_state.pop(select_key, None)
|
| 316 |
+
st.session_state.pop(input_key, None)
|
| 317 |
+
st.rerun()
|
| 318 |
+
if reset_clicked:
|
| 319 |
+
# Revert to raw speaker label β same as removing the token
|
| 320 |
+
# from a rename entry in the sidebar
|
| 321 |
+
apply_inline_rename(currFile, sp, "")
|
| 322 |
+
st.session_state.inline_rename_active[edit_key] = False
|
| 323 |
+
st.session_state.pop(select_key, None)
|
| 324 |
+
st.session_state.pop(input_key, None)
|
| 325 |
+
st.rerun()
|
| 326 |
else:
|
| 327 |
with row_cols[0]:
|
| 328 |
name_col, pencil_col = st.columns([4, 1])
|