#!/usr/bin/env python3 """ Streamlit app for Block 1 – Instruction Following evaluation. Participants hear an audio mixture + model output along with the instruction the model was given, and rate instruction-following quality, extraction quality, and spatial preservation. Run: streamlit run app_block1.py """ from __future__ import annotations import time from typing import Dict, List import streamlit as st from shared import ( BLOCK_1_AUDIO_DIR, MOS_LABELS, MOS_SCALE, advance_sample, append_response, build_response_record, get_block_audio_dir, init_session_state_single_block, load_block_metadata, render_intro_page, render_instruction_page, render_participant_gate, render_sidebar_progress, rerun_app, validate_audio_files, ) BLOCK_NUM = 1 # --------------------------------------------------------------------------- # Text content # --------------------------------------------------------------------------- INTRO_DESCRIPTION = ( "In this study, you'll use headphones to evaluate how well a model " "processes audio scenes.\n\n" "**Instruction Following:** You will hear the original audio mixture " "and the model's output, along with the instruction the model was given. " "You will rate how well the model followed the instruction and the quality " "of the extracted audio.\n\n" "Each sample takes about 30–60 seconds. You can take a break between samples " "by exiting the app and returning later with the same participant ID." ) INSTRUCTIONS_TEXT = ( "Each sample will guide you through these steps:\n\n" "**Step 1: Listen** – Press Play to hear both the original audio mixture " "(before processing) and the model's output (after processing). " "Listen to each clip at least once.\n\n" "**Step 2: Answer Questions** – Rate the model's performance using the " "questions provided.\n\n" "There are four questions per sample:\n" "1. Did the model follow the instruction? (Yes / No)\n" "2. How well did the model follow the instruction? (1–5 MOS scale)\n" "3. Rate the quality of the extracted audio (1–5 MOS scale)\n" "4. Were the spatial cues preserved in the output? (1–5 MOS scale)\n\n" "The MOS scale is:\n" "1 – Very Poor | 2 – Poor | 3 – Fair | 4 – Good | 5 – Excellent\n\n" "Click **I understand and I'm ready to begin** when you're ready." ) # =================================================================== # Block 1 sample renderer # =================================================================== def render_block_1_sample(samples: List[Dict]) -> None: order = st.session_state.sample_order idx = st.session_state.sample_index if idx >= len(order): st.session_state.block_complete = True st.success( "Thank you! You have completed all samples. " "You may close this window." ) return sample_id = order[idx] sample = next(s for s in samples if s["sample_id"] == sample_id) audio_dir = get_block_audio_dir(1) st.markdown( f"### Instruction Following   " f"(Sample {idx + 1} of {len(order)})" ) # Show instruction st.info(f'**Instruction given to the model:** "{sample["instruction"]}"') # Audio playback – two columns col1, col2 = st.columns(2) with col1: st.markdown("**Input Mixture (before processing):**") st.audio(str(audio_dir / sample["mixture_file"])) with col2: st.markdown("**Model Output (after processing):**") st.audio(str(audio_dir / sample["output_file"])) # "I finished listening" gate unlock_key = f"b1_{sample_id}_unlocked" if not st.session_state.get(unlock_key, False): if st.button("I finished listening", key=f"unlock_b1_{sample_id}"): st.session_state[unlock_key] = True st.session_state[f"b1_{sample_id}_start"] = time.time() rerun_app() st.info("Questions will appear after you confirm listening.") return elif f"b1_{sample_id}_start" not in st.session_state: st.session_state[f"b1_{sample_id}_start"] = time.time() # Questions form with st.form(key=f"b1_form_{sample_id}"): st.markdown("#### Question 1: Did the model follow the instruction?") instruction_followed = st.radio( "Did the model follow the instruction?", options=["Yes", "No"], index=None, horizontal=True, key=f"b1_q1_{sample_id}", ) st.markdown("#### Question 2: How well did the model follow the instruction?") instruction_mos = st.select_slider( "Rate from 1 (Very Poor) to 5 (Excellent)", options=MOS_SCALE, format_func=lambda v: MOS_LABELS[v], key=f"b1_q2_{sample_id}", ) st.markdown("#### Question 3: Rate the quality of the extracted audio") quality_mos = st.select_slider( "Rate from 1 (Very Poor) to 5 (Excellent)", options=MOS_SCALE, format_func=lambda v: MOS_LABELS[v], key=f"b1_q3_{sample_id}", ) st.markdown("#### Question 4: Were the spatial cues preserved in the output?") spatial_mos = st.select_slider( "Rate from 1 (Very Poor) to 5 (Excellent)", options=MOS_SCALE, format_func=lambda v: MOS_LABELS[v], key=f"b1_q4_{sample_id}", ) submit = st.form_submit_button("Submit and continue") if submit: errors = [] if instruction_followed is None: errors.append("Please answer whether the model followed the instruction.") if instruction_mos is None: errors.append("Please rate how well the model followed the instruction.") if quality_mos is None: errors.append("Please rate the extraction quality.") if spatial_mos is None: errors.append("Please rate the spatial preservation.") if errors: for e in errors: st.warning(e) else: record = build_response_record( sample, block="block_1", audio_dir=audio_dir, instruction_followed=instruction_followed, instruction_following_mos=instruction_mos, extraction_quality_mos=quality_mos, spatial_preservation_mos=spatial_mos, start_time_key=f"b1_{sample_id}_start", ) append_response(record, st.session_state.participant_id, BLOCK_NUM) advance_sample(1, sample_id) for suffix in [f"b1_q1_{sample_id}", f"b1_q2_{sample_id}", f"b1_q3_{sample_id}", f"b1_q4_{sample_id}"]: st.session_state.pop(suffix, None) rerun_app() # =================================================================== # Main # =================================================================== def main() -> None: st.set_page_config( page_title="Block 1: Instruction Following", layout="centered", ) samples = load_block_metadata(BLOCK_NUM) samples = validate_audio_files(samples, BLOCK_1_AUDIO_DIR) if not samples: st.error( "No valid audio samples found for Block 1. " "Please add audio files to block_1_audio/." ) st.stop() init_session_state_single_block() # Page flow if not st.session_state.get("participant_profile"): render_participant_gate(BLOCK_NUM, samples, study_title="Block 1: Instruction Following") return if not st.session_state.get("intro_page_complete", False): render_intro_page(INTRO_DESCRIPTION) return if not st.session_state.get("instructions_page_complete", False): render_instruction_page(BLOCK_NUM, samples, INSTRUCTIONS_TEXT) return if not st.session_state.participant_initialized: st.warning("Initializing session. Please wait...") return render_sidebar_progress(BLOCK_NUM) render_block_1_sample(samples) if __name__ == "__main__": main()