Kalana commited on
Commit
845bb58
Β·
1 Parent(s): 5a42ed8

Improve correction UI: inline word display with dynamic popover layout

Browse files

- Show all words inline as natural text flow (no column grid)
- Blue dashed underline for swappable words, green tick for changed
- Popover buttons rendered only for swappable words with proportional widths
- Update caption to accurately describe the interaction

Files changed (1) hide show
  1. app.py +65 -35
app.py CHANGED
@@ -170,45 +170,75 @@ if "output_words" in st.session_state and st.session_state["output_words"]:
170
  )
171
 
172
  if correction_mode:
173
- st.caption("Click any highlighted word to see alternatives and swap it.")
174
-
175
- # Word chips in rows β€” only ambiguous words are interactive
176
- ROW_SIZE = 6
177
- for row_start in range(0, len(output_words), ROW_SIZE):
178
- row_slice = list(enumerate(diagnostics[row_start:row_start + ROW_SIZE], start=row_start))
179
- cols = st.columns(len(row_slice))
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
- for col, (i, diag) in zip(cols, row_slice):
182
- has_alts = len(diag.candidate_breakdown) > 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  was_changed = output_words[i] != original_words[i]
184
  with col:
185
- if has_alts:
186
- chip = f":green[**{output_words[i]}**] :material/check:" if was_changed else f":blue[**{output_words[i]}**]"
187
- with st.popover(chip, use_container_width=True):
188
- st.markdown(f"**`{diag.input_word}`** β€” pick alternative:")
189
- for scored in diag.candidate_breakdown[:5]:
190
- eng_tag = " πŸ”€" if scored.is_english else ""
191
- is_sel = scored.text == output_words[i]
192
- if st.button(
193
- f"{'βœ… ' if is_sel else ''}{scored.text}{eng_tag}",
194
- key=f"alt_{i}_{scored.text}",
195
- help=f"Score: {scored.combined_score:.2f}",
196
- use_container_width=True,
197
- type="primary" if is_sel else "secondary",
198
- ):
199
- st.session_state["output_words"][i] = scored.text
200
- st.rerun()
201
- st.markdown("---")
202
- custom = st.text_input(
203
- "Not listed? Type correct word:",
204
- key=f"custom_{i}",
205
- placeholder="Type Sinhala word",
206
- )
207
- if custom and st.button("Use this", key=f"custom_apply_{i}", use_container_width=True):
208
- st.session_state["output_words"][i] = custom
209
  st.rerun()
210
- else:
211
- st.markdown(f"**{output_words[i]}**")
 
 
 
 
 
 
 
 
 
212
 
213
  # ── Submit correction button (only when changes exist, once per result) ──
214
  # Guard key: (original sentence, original output) β€” stable regardless of swaps
 
170
  )
171
 
172
  if correction_mode:
173
+ st.caption("Use the buttons below to swap alternative transliterations.")
174
+
175
+ # ── Inline sentence display (natural text flow, no grid) ─────
176
+ word_spans = []
177
+ for i, diag in enumerate(diagnostics):
178
+ has_alts = len(diag.candidate_breakdown) > 1
179
+ was_changed = output_words[i] != original_words[i]
180
+ w = html_lib.escape(output_words[i])
181
+ if was_changed:
182
+ word_spans.append(
183
+ f'<span style="color:#68d391;font-weight:700;">{w} βœ“</span>'
184
+ )
185
+ elif has_alts:
186
+ word_spans.append(
187
+ f'<span style="color:#63b3ed;font-weight:700;'
188
+ f'border-bottom:2px dashed #63b3ed;cursor:default;">{w}</span>'
189
+ )
190
+ else:
191
+ word_spans.append(f'<span style="font-weight:600;">{w}</span>')
192
 
193
+ st.markdown(
194
+ '<div style="font-size:1.15em;line-height:2.4;">'
195
+ + " &ensp; ".join(word_spans)
196
+ + "</div>",
197
+ unsafe_allow_html=True,
198
+ )
199
+ # ── Popover buttons only for swappable words ─────────────────
200
+ swappable = [
201
+ (i, diag)
202
+ for i, diag in enumerate(diagnostics)
203
+ if len(diag.candidate_breakdown) > 1
204
+ ]
205
+ if swappable:
206
+ widths = [max(len(output_words[i]), 3) for i, _ in swappable]
207
+ cols = st.columns(widths, gap="small")
208
+
209
+ for col, (i, diag) in zip(cols, swappable):
210
  was_changed = output_words[i] != original_words[i]
211
  with col:
212
+ chip = (
213
+ f":green[**{output_words[i]}**] βœ“"
214
+ if was_changed
215
+ else f":blue[**{output_words[i]}**]"
216
+ )
217
+ with st.popover(chip, use_container_width=True):
218
+ st.markdown(f"**`{diag.input_word}`** β€” pick alternative:")
219
+ for scored in diag.candidate_breakdown[:5]:
220
+ eng_tag = " πŸ”€" if scored.is_english else ""
221
+ is_sel = scored.text == output_words[i]
222
+ if st.button(
223
+ f"{'βœ… ' if is_sel else ''}{scored.text}{eng_tag}",
224
+ key=f"alt_{i}_{scored.text}",
225
+ help=f"Score: {scored.combined_score:.2f}",
226
+ use_container_width=True,
227
+ type="primary" if is_sel else "secondary",
228
+ ):
229
+ st.session_state["output_words"][i] = scored.text
 
 
 
 
 
 
230
  st.rerun()
231
+ st.markdown("---")
232
+ custom = st.text_input(
233
+ "Not listed? Type correct word:",
234
+ key=f"custom_{i}",
235
+ placeholder="Type Sinhala word",
236
+ )
237
+ if custom and st.button(
238
+ "Use this", key=f"custom_apply_{i}", use_container_width=True
239
+ ):
240
+ st.session_state["output_words"][i] = custom
241
+ st.rerun()
242
 
243
  # ── Submit correction button (only when changes exist, once per result) ──
244
  # Guard key: (original sentence, original output) β€” stable regardless of swaps