Lara Monteagudo Tubau commited on
Commit Β·
34890bc
1
Parent(s): e605ae7
Improve expandable sections review
Browse files576 unmodified lines5775785795805815825835845855865875885894 unmodified lines5945955965975985996006016026036046056066073 unmodified lines6116126136146156166176186196206216226236246256266273 unmodified lines631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697549 unmodified lines1247124812491250125112521253125412551256125712581259126036 unmodified lines129712981299130013011302130313041305130613071308130913101311999 unmodified lines231123122313231423152316231723182319576 unmodified linesdef _sections_html(sections: list[dict], n_turns: int,agent0_rt: float, n_speakers: int) -> str:"""Rich sections display for the Review Sections page.Shows:1. Agent 0 completion card (what ran, how long, theoretical basis)2. Visual section cards with proportional progress bars3. Size-warning if any section is unusually large"""if not sections:return '<p style="color:#94a3b8;font-style:italic">No sections detected.</p>'4 unmodified lines# ββ 1. Agent completion card βββββββββββββββββββββββββββββββββββββββββββββagent_card = (f'<div style="background:#f0fdf4;border:2px solid #86efac;border-radius:10px;'f'padding:14px 18px;margin-bottom:16px;font-family:system-ui,sans-serif">'f'<div style="display:flex;align-items:flex-start;gap:10px;flex-wrap:wrap">'f'<div style="flex:1;min-width:200px">'f'<div style="display:flex;align-items:center;gap:8px;margin-bottom:6px">'f'<span style="font-size:1.1em">β</span>'f'<strong style="color:#065f46;font-size:0.95em">Agent 0 β Structural Segmenter</strong>'f'<span style="background:#bbf7d0;color:#14532d;font-size:0.72em;font-weight:700;'f'padding:2px 8px;border-radius:20px;margin-left:4px">completed</span>'f'</div>'f'<div style="font-size:0.82em;color:#166534;line-height:1.7">'f'<strong>{total}</strong> turns Β· <strong>{n_speakers}</strong> speaker(s) β '3 unmodified linesf'Β· activity phases (Stahl 2006; Roschelle & Teasley 1995)</em>'f'</div>'f'</div>'f'<div style="text-align:right;min-width:90px">'f'<div style="font-family:monospace;font-size:0.8em;color:#6b7280">'f'Qwen2.5-72B Β· T=0.1</div>'f'<div style="font-family:monospace;font-size:0.85em;color:#065f46;font-weight:700">'f'{agent0_rt:.1f}s</div>'f'</div>'f'</div>'f'</div>')# ββ 2. Section cards βββββββββββββββββββββββββββββββββββββββββββββββββββββpalette = [("#4338ca", "#eef2ff"), # indigo("#b45309", "#fffbeb"), # amber3 unmodified lines("#0e7490", "#ecfeff"), # cyan("#be123c", "#fff1f2"), # rose]cards = []warnings = []for i, s in enumerate(sections):col, bg = palette[i % len(palette)]n = s["end_turn_id"] - s["start_turn_id"] + 1pct = round(n / total * 100) if total else 0bar_w = min(pct, 100)if pct > 30 and len(sections) > 2:warnings.append(f'<strong>{_h(s["label"])}</strong> ({n} turns = {pct}%) is very large β 'f'consider splitting it into distinct activity phases if you notice sub-topics within it.')cards.append(f'<div style="border:1px solid {col}30;border-top:4px solid {col};'f'border-radius:8px;padding:12px;background:{bg};min-width:120px;flex:1">'f'<div style="font-size:0.68em;font-weight:800;color:{col};'f'text-transform:uppercase;letter-spacing:0.08em;margin-bottom:4px">'f'{_h(s["section_id"])}</div>'f'<div style="font-weight:700;color:#1e293b;font-size:0.88em;line-height:1.25;'f'margin-bottom:6px;min-height:2.5em">{_h(s["label"])}</div>'f'<div style="font-size:0.72em;color:#6b7280;font-family:monospace;margin-bottom:6px">'f'{_h(s["start_time"])} β {_h(s["end_time"])}</div>'f'<div style="background:#e2e8f0;border-radius:3px;height:5px;margin-bottom:4px">'f'<div style="height:100%;width:{bar_w}%;background:{col};border-radius:3px"></div>'f'</div>'f'<div style="font-size:0.72em;color:#94a3b8">'f'{n} turns Β· <strong style="color:{col}">{pct}%</strong>'f'</div>'f'</div>')grid = (f'<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:12px">'f'{"".join(cards)}'f'</div>')# ββ 3. Warnings ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββwarn_html = ""if warnings:items = "".join(f"<li style='margin:3px 0'>{w}</li>" for w in warnings)warn_html = (f'<div style="background:#fffbeb;border:1px solid #fbbf24;border-radius:8px;'f'padding:10px 14px;margin-bottom:10px;font-size:0.83em;color:#78350f">'f'<strong>β Size note</strong><ul style="margin:6px 0 0;padding-left:18px">'f'{items}</ul>'f'</div>')# ββ 4. Instruction βββββββββββββββββββββββββββββββββββββββββββββββββββββββinstr = (f'<div style="background:#f8fafc;border:1px solid #e2e8f0;border-left:4px solid #4338ca;'f'border-radius:0 8px 8px 0;padding:10px 14px;font-size:0.84em;color:#334155;'f'margin-bottom:4px">'f'<strong>Edit the section labels</strong> in the box below β one label per line, 'f'in the same order. Labels only; turn boundaries are fixed by the AI. 'f'Click <strong>Confirm Sections β</strong> when ready.'f'</div>')return agent_card + grid + warn_html + instrdef _episode_dialogue_html(segments: list[dict], boundaries: list[dict],549 unmodified lines)+ (f"\n\n*{overview}*" if overview else ""))labels_txt = "\n".join(s["label"] for s in sections)if result.get("warnings"):gr.Warning("Coverage warnings β check the section table.")gr.Info(f"Ready: {len(turns)} turns Β· {len(sections)} sections detected in {agent0_rt}s.")# Build the rich sections HTML (agent card + visual cards + instructions)sec_html = _sections_html(sections, len(turns), agent0_rt, len(speakers))yield (new_state,36 unmodified linesyield (state,*_show("sections"),gr.update(value=_BUSY_HTML), # sections_display_htmlgr.update(interactive=False, value="β³ Savingβ¦"), # confirm_sections_btngr.update(interactive=False), # col_run_all_boundaries (not visible, ignored)gr.update(visible=False), # col_boundary_reviewgr.update(visible=False), # col_coding)# ββ Process (fast: no LLM, just in-memory + disk) βββββββββββββββββββββββββlabels = [l.strip() for l in labels_text.strip().splitlines() if l.strip()]confirmed = [dict(s, label=labels[i] if i < len(labels) else s["label"])for i, s in enumerate(sections)]999 unmodified linessections_display_html = gr.HTML()gr.Markdown("**Edit section labels** β one per line, same order as above:")sections_labels_box = gr.Textbox(lines=8, show_label=False,placeholder="One label per lineβ¦")confirm_sections_btn = gr.Button("Confirm Sections β", variant="primary")back_sections_btn = gr.Button("β Back to Setup", size="sm")576 unmodified lines5775785795805815825835845855865875885895905915924 unmodified lines5975985996006016026036046056066076086096103 unmodified lines6146156166176186196206216226236246256263 unmodified lines630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758549 unmodified lines130813091310131113121313131413151316131713181319132013211322132336 unmodified lines13601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385999 unmodified lines23852386238723882389239023912392239323942395576 unmodified linesdef _sections_html(sections: list[dict], n_turns: int,agent0_rt: float, n_speakers: int,all_turns: list[dict] | None = None) -> str:"""Rich sections display for the Review Sections page.Uses native HTML <details>/<summary> for expand-to-read-content β no JS required.Each section is collapsed by default (researcher sees the label and size at a glance)and can be expanded to read the AI summary + first 8 turns of actual transcript.Label format in the textarea is "s01: Label" so the mapping is unambiguous."""if not sections:return '<p style="color:#94a3b8;font-style:italic">No sections detected.</p>'4 unmodified lines# ββ 1. Agent completion card βββββββββββββββββββββββββββββββββββββββββββββagent_card = (f'<div style="background:#f0fdf4;border:2px solid #86efac;border-radius:10px;'f'padding:14px 18px;margin-bottom:14px;font-family:system-ui,sans-serif">'f'<div style="display:flex;align-items:flex-start;gap:10px;flex-wrap:wrap">'f'<div style="flex:1;min-width:200px">'f'<div style="display:flex;align-items:center;gap:8px;margin-bottom:5px">'f'<span style="font-size:1.1em">β</span>'f'<strong style="color:#065f46;font-size:0.95em">Agent 0 β Structural Segmenter</strong>'f'<span style="background:#bbf7d0;color:#14532d;font-size:0.72em;font-weight:700;'f'padding:2px 8px;border-radius:20px;margin-left:4px">completed in {agent0_rt:.1f}s</span>'f'</div>'f'<div style="font-size:0.82em;color:#166534;line-height:1.7">'f'<strong>{total}</strong> turns Β· <strong>{n_speakers}</strong> speaker(s) β '3 unmodified linesf'Β· activity phases (Stahl 2006; Roschelle & Teasley 1995)</em>'f'</div>'f'</div>'f'<div style="text-align:right;font-family:monospace;font-size:0.78em;color:#6b7280">'f'Qwen2.5-72B Β· T=0.1</div>'f'</div>'f'</div>')# ββ 2. Expandable section list βββββββββββββββββββββββββββββββββββββββββββpalette = [("#4338ca", "#eef2ff"), # indigo("#b45309", "#fffbeb"), # amber3 unmodified lines("#0e7490", "#ecfeff"), # cyan("#be123c", "#fff1f2"), # rose]warnings = []detail_items = []for i, s in enumerate(sections):col, bg = palette[i % len(palette)]n = s["end_turn_id"] - s["start_turn_id"] + 1pct = round(n / total * 100) if total else 0if pct > 30 and len(sections) > 2:warnings.append(f'<strong>{_h(s["label"])}</strong> ({n} turns, {pct}%) is unusually large. 'f'Expand it below to check if it contains distinct sub-topics.')# ββ Bar ββbar = (f'<div style="background:#e2e8f0;border-radius:3px;height:4px;'f'width:80px;display:inline-block;vertical-align:middle;margin-left:8px">'f'<div style="height:100%;width:{min(pct,100)}%;background:{col};border-radius:3px">'f'</div></div>')# ββ Transcript excerpt ββexcerpt_html = ""if all_turns:sec_turns = [t for t in all_turnsif s["start_turn_id"] <= t["turn_id"] <= s["end_turn_id"]]shown = sec_turns[:8]rows = []for t in shown:sp = _h(t.get("speaker", "?"))ts = _h(t.get("start_time", ""))txt = t.get("text", "")if len(txt) > 180:txt = txt[:180] + "β¦"rows.append(f'<div style="padding:5px 0;border-bottom:1px solid #f1f5f9">'f'<span style="font-weight:700;font-size:0.82em;color:{col};margin-right:6px">'f'{sp}</span>'f'<span style="font-size:0.72em;color:#94a3b8;font-family:monospace;'f'margin-right:8px">{ts}</span>'f'<span style="font-size:0.88em;color:#1e293b">{_h(txt)}</span>'f'</div>')if len(sec_turns) > 8:rows.append(f'<div style="font-size:0.75em;color:#94a3b8;padding:4px 0;'f'font-style:italic">β― {len(sec_turns)-8} more turns</div>')excerpt_html = (f'<div style="margin-top:10px">'f'<div style="font-size:0.72em;font-weight:700;text-transform:uppercase;'f'letter-spacing:0.06em;color:#94a3b8;margin-bottom:6px">'f'Transcript excerpt (first {len(shown)} of {n} turns)</div>'f'{"".join(rows)}'f'</div>')summary_text = _h(s.get("summary", "No summary provided."))detail_items.append(f'<details style="border:1px solid {col}40;border-radius:10px;'f'margin-bottom:8px;overflow:hidden;font-family:system-ui,sans-serif">'# ββ Collapsed header (always visible) ββββββββββββββββββββββββββf'<summary style="cursor:pointer;padding:12px 16px;background:{bg};'f'border-left:5px solid {col};display:flex;align-items:center;'f'gap:10px;list-style:none;-webkit-appearance:none">'f'<span style="font-size:0.7em;font-weight:800;color:{col};'f'text-transform:uppercase;letter-spacing:0.08em;min-width:28px">'f'{_h(s["section_id"])}</span>'f'<span style="font-weight:700;color:#1e293b;flex:1;font-size:0.92em">'f'{_h(s["label"])}</span>'f'<span style="font-size:0.72em;color:#64748b;white-space:nowrap;'f'font-family:monospace">{_h(s["start_time"])}β{_h(s["end_time"])}</span>'f'<span style="font-size:0.72em;color:{col};font-weight:700;white-space:nowrap">'f' {n} turns ({pct}%)</span>'f'{bar}'f'<span style="color:{col};font-size:0.8em;margin-left:4px">βΌ read</span>'f'</summary>'# ββ Expanded content ββββββββββββββββββββββββββββββββββββββββββββf'<div style="padding:14px 16px;background:white;'f'border-top:1px solid {col}20">'f'<p style="font-size:0.85em;color:#334155;margin:0 0 8px;line-height:1.65">'f'<strong style="color:{col}">AI summary:</strong> {summary_text}</p>'f'{excerpt_html}'f'</div>'f'</details>')section_list = f'<div style="margin-bottom:12px">{"".join(detail_items)}</div>'# ββ 3. Warnings ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββwarn_html = ""if warnings:items = "".join(f"<li style='margin:4px 0'>{w}</li>" for w in warnings)warn_html = (f'<div style="background:#fffbeb;border:1px solid #fbbf24;border-radius:8px;'f'padding:10px 14px;margin-bottom:12px;font-size:0.83em;color:#78350f">'f'<strong>β Size note</strong>'f'<ul style="margin:6px 0 0;padding-left:18px">{items}</ul>'f'</div>')# ββ 4. Label editor instruction ββββββββββββββββββββββββββββββββββββββββββinstr = (f'<div style="background:#f8fafc;border:1px solid #e2e8f0;'f'border-left:4px solid #4338ca;border-radius:0 8px 8px 0;'f'padding:10px 14px;font-size:0.84em;color:#334155;margin-bottom:4px">'f'<strong>Rename sections below</strong> β each line follows the format 'f'<code style="background:#e2e8f0;padding:1px 5px;border-radius:3px">'f's01: Your Label</code>. 'f'Edit the text after the colon only; the <code>s01:</code> prefix keeps the order clear. 'f'Turn boundaries are fixed β only labels change. 'f'Expand any section above to read its content before renaming.'f'</div>')return agent_card + section_list + warn_html + instrdef _episode_dialogue_html(segments: list[dict], boundaries: list[dict],549 unmodified lines)+ (f"\n\n*{overview}*" if overview else ""))# Pre-fill with "s01: Label" format so the mapping is unambiguouslabels_txt = "\n".join(f"{s['section_id']}: {s['label']}" for s in sections)if result.get("warnings"):gr.Warning("Coverage warnings β check the section table.")gr.Info(f"Ready: {len(turns)} turns Β· {len(sections)} sections detected in {agent0_rt}s.")# Build the rich sections HTML β pass turns so sections are expandablesec_html = _sections_html(sections, len(turns), agent0_rt, len(speakers),all_turns=turns)yield (new_state,36 unmodified linesyield (state,*_show("sections"),gr.update(value=_BUSY_HTML), # sections_display_htmlgr.update(interactive=False, value="β³ Savingβ¦"), # confirm_sections_btngr.update(visible=True), # col_run_all_boundaries (Column β visible only)gr.update(visible=False), # col_boundary_reviewgr.update(visible=False), # col_coding)# ββ Process (fast: no LLM, just in-memory + disk) βββββββββββββββββββββββββ# Parse "s01: My Label" or plain "My Label" (both accepted)raw_lines = [l.strip() for l in labels_text.strip().splitlines() if l.strip()]labels = []for line in raw_lines:# Strip "sXX: " or "sXX:" prefix if presentif ":" in line:prefix, _, rest = line.partition(":")if prefix.strip().lower().startswith("s") and prefix.strip()[1:].isdigit():labels.append(rest.strip())continuelabels.append(line)confirmed = [dict(s, label=labels[i] if i < len(labels) else s["label"])for i, s in enumerate(sections)]999 unmodified linessections_display_html = gr.HTML()gr.Markdown("**Rename sections** β edit the text after the colon, keep the `s01:` prefix:")sections_labels_box = gr.Textbox(lines=10, show_label=False,placeholder="s01: Introduction\ns02: Main Discussion\nβ¦",)confirm_sections_btn = gr.Button("Confirm Sections β", variant="primary")back_sections_btn = gr.Button("β Back to Setup", size="sm")
app.py+141-65