Lara Monteagudo Tubau commited on
Commit Β·
f5ce139
1
Parent(s): e2acfe7
fix _WH_QUESTION bug, rework screener UX, improve setup page labels
Browse filesFiles changed (2)
- app.py+116-78
- metacog_screener.py+1-1
1436 unmodified lines1437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691380 unmodified lines2850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874185 unmodified lines306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031214 unmodified lines312631273128312931303131313231333134313531364 unmodified lines314131423143314431453146314731483149315031513152315331543155315631573158537 unmodified lines3696369736983699370037013702370337043705370637071436 unmodified lines)# ββ Auto-run screener (pre-LLM, fast) ββββββββββββββββββββββββββββββββββββtry:result = screen_transcript(turns)rd = result_to_dict(result)new_state["screener_result"] = rdnew_state["stage"] = "screened"sum_html = _screener_summary_html(rd)turns_html = _screener_turns_html(rd, speaker_map)n_wins = len(rd["candidate_windows"])savings = f"{rd['estimated_llm_savings']:.0%}"screen_msg = (f"Pre-screen complete β {rd['flagged_turns']} / {rd['total_turns']} turns flagged, "f"{n_wins} candidate windows, ~{savings} LLM cost saved.")except Exception as e:rd, sum_html, turns_html = None, f'<div style="color:#b45309">Screener error: {e}</div>', ""screen_msg = f"Transcript parsed ({len(turns)} turns). Screener error: {e}"yield (new_state,_pipeline_bar_html(new_state),_status_card(screen_msg, "success",{"Session": session_id, "Turns": len(turns),"Speakers": ", ".join(speakers)}),_BTN_FREE,sum_html,turns_html,gr.update(visible=True), # screener_to_sections_btn*_show("screener"),)1380 unmodified linesreturn "".join(parts)# Screener handlerdef fn_run_screener(state: dict):turns = state.get("turns", [])if not turns:return state, "No transcript loaded.", "", ""try:result = screen_transcript(turns)rd = result_to_dict(result)state["screener_result"] = rdstate["stage"] = "screened"speaker_map = state.get("speakers", {})return (state,_pipeline_bar_html(state),_screener_summary_html(rd) + _screener_turns_html(rd, speaker_map),_screener_summary_html(rd), # summary only, for the status card)except Exception as e:return state, _pipeline_bar_html(state), f"Screener error: {e}", ""# Lexicon handlers185 unmodified lineswith gr.Column(visible=False) as pg_setup:gr.HTML('<div class="cc-page-nav">''<span class="cc-page-label">Step 1 of 4</span>''<h2 class="cc-page-title">Session Setup</h2>''</div>''<div class="cc-steps">''<span class="cc-step-active">1 Setup</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-next">2 Sections</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-next">3 Code</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-next">4 Done</span>''</div>')gr.HTML('<div class="cc-setup-box">''<p style="margin:0;font-size:0.9em;color:#334155;line-height:1.6">''Choose a transcript from Google Drive, then start coding. ''Session IDs are generated automatically and saved for you.''</p>''</div>')with gr.Row():researcher_id_box = gr.Textbox(label="Researcher ID", placeholder="e.g. R1", scale=1)with gr.Row():drive_refresh_btn = gr.Button("π Refresh Drive transcripts", variant="secondary")load_prev_btn = gr.Button("βΊ Load previous session", variant="secondary")drive_file_dropdown = gr.Dropdown(label="Google Drive transcript",choices=[],interactive=False,)drive_status_html = gr.HTML()gr.HTML('<div class="cc-callout">''<strong>Option A β Google Drive:</strong> refresh the list and select a transcript, ''then click <em>Process Transcript</em>.<br>''<strong>Option B β Local file:</strong> upload a .txt or .vtt transcript below.''</div>')process_btn = gr.Button("Process Drive Transcript β", variant="primary")process_status = gr.HTML()process_log = gr.HTML()gr.HTML('<div style="border-top:1px solid var(--cc-border);margin:20px 0 10px"></div>''<div style="font-weight:700;color:var(--cc-ink);margin-bottom:8px">Or upload a local file</div>')local_file_upload = gr.File(label="Transcript file (.txt, .vtt, .csv)",file_types=[".txt", ".vtt", ".csv"],)process_local_btn = gr.Button("Process Local File β", variant="secondary")local_process_status = gr.HTML()back_setup_btn = gr.Button("β Back to Home", size="sm")4 unmodified lineswith gr.Column(visible=False) as pg_screener:gr.HTML('<div class="cc-page-nav">''<span class="cc-page-label">Step 2 of 5</span>''<h2 class="cc-page-title">Pre-screen Transcript</h2>''</div>''<div class="cc-steps">''<span class="cc-step-done">β Setup</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-active">2 Screen</span>''<span class="cc-step-sep">βΊ</span>'4 unmodified lines'<span class="cc-step-next">5 Done</span>''</div>')gr.HTML('<div class="cc-callout">''The screener scores every turn using the tiered metacognitive lexicon ''before sending anything to the LLM. Only turns inside <strong>candidate windows</strong> ''are passed to Agent 0 β everything else is skipped, saving API cost.''</div>')run_screener_btn = gr.Button("Run Screener", variant="primary")screener_summary_html = gr.HTML()screener_turns_html_c = gr.HTML()screener_to_sections_btn = gr.Button("Continue to Sections β", variant="primary", visible=False)back_screener_btn = gr.Button("β Back to Setup", size="sm")# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# PAGE 3: Sections review537 unmodified linesoutputs=PARSE_OUTPUTS,).then(fn=lambda: gr.update(visible=True), outputs=[pipeline_bar_html])# Screener: manual re-runrun_screener_btn.click(fn=fn_run_screener,inputs=[state],outputs=[state, pipeline_bar_html, screener_turns_html_c, screener_summary_html],).then(fn=lambda: gr.update(visible=True), outputs=[screener_to_sections_btn])# Screener β Sections: Phase 2 (run Agent 0)screener_to_sections_btn.click(1436 unmodified lines14371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761380 unmodified lines2857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890185 unmodified lines30763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131424 unmodified lines314731483149315031513152315331543155315631574 unmodified lines3162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195537 unmodified lines37333734373537363737373837393740374137423743374437451436 unmodified lines)# ββ Auto-run screener (pre-LLM, fast) ββββββββββββββββββββββββββββββββββββscreener_ok = Falsetry:result = screen_transcript(turns)rd = result_to_dict(result)new_state["screener_result"] = rdnew_state["stage"] = "screened"n_wins = len(rd["candidate_windows"])savings = f"{rd['estimated_llm_savings']:.0%}"sum_html = _screener_summary_html(rd)turns_html = _screener_turns_html(rd, speaker_map)screener_ok = Trueexcept Exception as e:sum_html = (f'<div style="background:#fffbeb;border:1px solid #fde68a;border-radius:8px;'f'padding:14px 16px;color:#92400e;font-size:0.9em">'f'<strong>Screener could not run automatically.</strong><br>'f'Error: {html_lib.escape(str(e))}<br>'f'Click <em>Re-run Screener</em> below to try again, 'f'or click <em>Detect Sections β</em> to continue without screening.</div>')turns_html = ""yield (new_state,_pipeline_bar_html(new_state),_status_card(f"Parsed {len(turns)} turns Β· {len(speakers)} speaker(s).","success",{"Session": session_id, "Turns": len(turns), "Speakers": ", ".join(speakers)},),_BTN_FREE,sum_html, # screener_summary_htmlturns_html, # screener_turns_html_cgr.update(visible=True), # screener_to_sections_btn β always show*_show("screener"),)1380 unmodified linesreturn "".join(parts)# Screener handler β returns (state, bar, summary_html, turns_html, rerun_btn_update)def fn_run_screener(state: dict):turns = state.get("turns", [])if not turns:err = ('<div style="background:#fffbeb;border:1px solid #fde68a;border-radius:8px;''padding:14px;color:#92400e">No transcript loaded β go back to Upload.</div>')return state, _pipeline_bar_html(state), err, "", gr.update(visible=True)try:result = screen_transcript(turns)rd = result_to_dict(result)new_state = dict(state)new_state["screener_result"] = rdnew_state["stage"] = "screened"speaker_map = new_state.get("speakers", {})return (new_state,_pipeline_bar_html(new_state),_screener_summary_html(rd),_screener_turns_html(rd, speaker_map),gr.update(visible=False), # hide re-run btn β success)except Exception as e:err = (f'<div style="background:#fffbeb;border:1px solid #fde68a;border-radius:8px;'f'padding:14px;color:#92400e"><strong>Screener error:</strong> 'f'{html_lib.escape(str(e))}</div>')return state, _pipeline_bar_html(state), err, "", gr.update(visible=True)# Lexicon handlers185 unmodified lineswith gr.Column(visible=False) as pg_setup:gr.HTML('<div class="cc-page-nav">''<span class="cc-page-label">Step 1 of 5 β Upload</span>''<h2 class="cc-page-title">Load a Transcript</h2>''</div>''<div class="cc-steps">''<span class="cc-step-active">1 Upload</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-next">2 Screen</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-next">3 Sections</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-next">4 Code</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-next">5 Done</span>''</div>')# ββ Researcher ID βββββββββββββββββββββββββββββββββββββββββββββββββgr.HTML('<div style="font-weight:700;color:var(--cc-ink);margin:16px 0 4px">Your Researcher ID</div>''<div style="font-size:0.82em;color:var(--cc-muted);margin-bottom:8px">''A short label used to identify your coding file (e.g. R1, lara, jsmith).</div>')researcher_id_box = gr.Textbox(label="",placeholder="e.g. R1",max_lines=1,)# ββ Option A: Google Drive ββββββββββββββββββββββββββββββββββββββββgr.HTML('<div style="border-top:1px solid var(--cc-border);margin:20px 0 12px"></div>''<div style="font-weight:700;color:var(--cc-ink);margin-bottom:4px">Option A β Google Drive</div>''<div style="font-size:0.82em;color:var(--cc-muted);margin-bottom:10px">''Refresh the list, pick a transcript, then click Load.</div>')with gr.Row():drive_refresh_btn = gr.Button("Refresh Drive list", variant="secondary", scale=2)load_prev_btn = gr.Button("Resume session", variant="secondary", scale=1)drive_file_dropdown = gr.Dropdown(label="Select transcript",choices=[],interactive=False,)drive_status_html = gr.HTML()process_btn = gr.Button("Load from Drive β", variant="primary")process_status = gr.HTML()process_log = gr.HTML()# ββ Option B: Local file ββββββββββββββββββββββββββββββββββββββββββgr.HTML('<div style="border-top:1px solid var(--cc-border);margin:20px 0 12px"></div>''<div style="font-weight:700;color:var(--cc-ink);margin-bottom:4px">Option B β Local file</div>''<div style="font-size:0.82em;color:var(--cc-muted);margin-bottom:10px">''Upload a .txt, .vtt or .csv transcript from your computer.</div>')local_file_upload = gr.File(label="Transcript file",file_types=[".txt", ".vtt", ".csv"],)process_local_btn = gr.Button("Load Local File β", variant="primary")local_process_status = gr.HTML()back_setup_btn = gr.Button("β Back to Home", size="sm")4 unmodified lineswith gr.Column(visible=False) as pg_screener:gr.HTML('<div class="cc-page-nav">''<span class="cc-page-label">Step 2 of 5 β Pre-screen</span>''<h2 class="cc-page-title">Metacognitive Screener</h2>''</div>''<div class="cc-steps">''<span class="cc-step-done">β Upload</span>''<span class="cc-step-sep">βΊ</span>''<span class="cc-step-active">2 Screen</span>''<span class="cc-step-sep">βΊ</span>'4 unmodified lines'<span class="cc-step-next">5 Done</span>''</div>')# Summary chips β auto-populated when the page loadsscreener_summary_html = gr.HTML(value='<div style="padding:20px;text-align:center;color:var(--cc-muted)">''Running screenerβ¦</div>')# Primary CTA β shown once screener finishesscreener_to_sections_btn = gr.Button("Detect Sections β", variant="primary", size="lg", visible=False)# Collapsible turn detailwith gr.Accordion("Turn-by-turn detail", open=False):gr.HTML('<div style="font-size:0.8em;color:var(--cc-muted);padding:6px 0 10px">''Turns are colour-coded by composite score. ''Orange rows are candidate windows passed to Agent 0; ''grey rows are filtered out.</div>')screener_turns_html_c = gr.HTML()# Re-run (only needed if auto-run failed)run_screener_btn = gr.Button("Re-run Screener", variant="secondary", size="sm", visible=False)back_screener_btn = gr.Button("β Back to Upload", size="sm")# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ# PAGE 3: Sections review537 unmodified linesoutputs=PARSE_OUTPUTS,).then(fn=lambda: gr.update(visible=True), outputs=[pipeline_bar_html])# Screener: manual re-run (only shown when auto-run failed)run_screener_btn.click(fn=fn_run_screener,inputs=[state],outputs=[state, pipeline_bar_html, screener_summary_html,screener_turns_html_c, run_screener_btn],)# Screener β Sections: Phase 2 (run Agent 0)screener_to_sections_btn.click(
213 unmodified lines214215216217218219220213 unmodified lines# Structural signal detectors# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ_WH_RE = re.compile(r'^(what|why|how|where|whether|which)\b', re.IGNORECASE)_QUESTION_END = re.compile(r'\?\s*$')_TRUNCATED_END = re.compile(r'[ββ\-]{1,2}\s*$|β¦\s*$|\.\.\.\s*$')_SHORT_CORRECT = re.compile(r'^(no|actually|wait|hold on|that\'?s not)[,\s]', re.IGNORECASE)213 unmodified lines214215216217218219220213 unmodified lines# Structural signal detectors# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ_WH_QUESTION = re.compile(r'^(what|why|how|where|whether|which)\b', re.IGNORECASE)_QUESTION_END = re.compile(r'\?\s*$')_TRUNCATED_END = re.compile(r'[ββ\-]{1,2}\s*$|β¦\s*$|\.\.\.\s*$')_SHORT_CORRECT = re.compile(r'^(no|actually|wait|hold on|that\'?s not)[,\s]', re.IGNORECASE)
app.py+116-78
metacog_screener.py+1-1