princessamygdala
Claude Opus 4.8 (1M context)
Scaffold ReactorRoom: observational-learning-of-empathy NiceWebRL task
63879a2 Download questionnaires.py from princessamygdala/ReactorRoom: direct link, hf CLI and curl.
- Browser
- Download file 16.1 kB
-
https://huggingface.co/spaces/princessamygdala/ReactorRoom/resolve/main/questionnaires.py
- Command line
-
hf download hf://spaces/princessamygdala/ReactorRoom/questionnaires.py
-
curl -L -o questionnaires.py https://huggingface.co/spaces/princessamygdala/ReactorRoom/resolve/main/questionnaires.py
16.1 kB
| """ | |
| Questionnaire stages for the Observational-Learning-of-Empathy task | |
| (task_ole-cu-nicewebrl, "ReactorRoom"). Built as NiceWebRL FeedbackStages so each | |
| one collects a dict of responses that gets logged like any other stage. | |
| Patterns (`_slider_battery`, `_ask_mc`, the comprehension retry loop, the | |
| attention-check pass/fail) are copied from the sibling Prosocial-Effort task | |
| (../task_pe-cu-nicewebrl/questionnaires.py) and adapted to this task. | |
| Every stage's returned data dict includes `row_type="survey"` and `measure=<name>` | |
| (plus `when` where relevant), matching the survey-row schema in CONTRACT.md. | |
| Stage factories (import as `import questionnaires as Q`): | |
| * Q.instruction_stage(name, markdown, button="Next") | |
| * Q.comprehension_stage() β ~5 MC items, re-ask missed ones once | |
| * Q.impression_stage(when, coviewer_names) β per co-viewer "how empathic?" 1-9 | |
| * Q.willingness_stage(when) β "how many minutes to help?" 0-60 slider | |
| * Q.iri_stage() β short trait-empathy battery, 1-5, measure="IRI" | |
| * Q.attention_check_stage() β instructed response ("set the slider to 7") | |
| """ | |
| import asyncio | |
| from nicegui import ui, app | |
| import nicewebrl | |
| from nicewebrl import FeedbackStage | |
| async def _ignore_keys(e, container): | |
| return | |
| def _fb(name, display_fn): | |
| return FeedbackStage(name=name, display_fn=display_fn, custom_key_press_fn=_ignore_keys) | |
| # ---------------------------------------------------------------------------- | |
| # Generic slider battery (copied verbatim from PE questionnaires.py) | |
| # ---------------------------------------------------------------------------- | |
| async def _slider_battery(container, title, intro, items, lo_label, hi_label, | |
| lo=0, hi=100, start=50): | |
| """items: list of (key, prompt). Returns {key: value}.""" | |
| nicewebrl.clear_element(container) | |
| ev = asyncio.Event() | |
| sliders = {} | |
| with container.style("align-items:center; max-width:680px;"): | |
| ui.markdown(f"## {title}") | |
| if intro: | |
| ui.markdown(intro) | |
| for key, prompt in items: | |
| with ui.column().style("width:100%; max-width:560px; margin-bottom:10px;"): | |
| ui.markdown(f"**{prompt}**") | |
| s = ui.slider(min=lo, max=hi, value=start).props("label-always") | |
| with ui.row().style("width:100%; justify-content:space-between;"): | |
| ui.label(lo_label).style("font-size:12px; color:#666;") | |
| ui.label(hi_label).style("font-size:12px; color:#666;") | |
| sliders[key] = s | |
| ui.button("Continue", on_click=lambda: ev.set()) | |
| await ev.wait() | |
| return {k: int(s.value) for k, s in sliders.items()} | |
| # ---------------------------------------------------------------------------- | |
| # Single multiple-choice item (copied verbatim from PE questionnaires.py) | |
| # ---------------------------------------------------------------------------- | |
| async def _ask_mc(container, header, item): | |
| """SL-style: pick once -> see explanation feedback -> advance regardless. | |
| Submit + options lock after the first choice.""" | |
| display_choices = [o[0] for o in item["options"]] | |
| correct_idx = item["correct"] | |
| nicewebrl.clear_element(container) | |
| chosen = {"idx": None} | |
| done = asyncio.Event() | |
| with container.style("align-items:center; max-width:680px;"): | |
| ui.markdown(f"### {header}") | |
| ui.markdown(f"**{item['prompt']}**") | |
| radio = ui.radio({i: c for i, c in enumerate(display_choices)}).props("color=primary") | |
| fb = ui.markdown("") | |
| submit = ui.button("Submit") | |
| cont = ui.button("Continue", on_click=lambda: done.set()) | |
| cont.set_visibility(False) | |
| def do_submit(): | |
| if radio.value is None or chosen["idx"] is not None: | |
| return | |
| chosen["idx"] = radio.value | |
| ok = (radio.value == correct_idx) | |
| lead = "β **Correct!** " if ok else "β **Not quite.** " | |
| fb.set_content(lead + item.get("explain", "")) | |
| radio.disable() # lock the options | |
| submit.disable() # grey out submit after one choice | |
| cont.set_visibility(True) # advance regardless of right/wrong | |
| submit.on_click(do_submit) | |
| await done.wait() | |
| return chosen["idx"] | |
| # ---------------------------------------------------------------------------- | |
| # Instruction / cover-story screen (markdown + a button) | |
| # ---------------------------------------------------------------------------- | |
| def instruction_stage(name, markdown, button="Next"): | |
| """A plain instruction/cover-story page: renders `markdown`, waits for a click.""" | |
| async def _display(stage, container): | |
| nicewebrl.clear_element(container) | |
| ev = asyncio.Event() | |
| with container.style("align-items:center; max-width:720px;"): | |
| ui.markdown(markdown) | |
| ui.button(button, on_click=lambda: ev.set()) | |
| await ev.wait() | |
| return dict(row_type="survey", measure="instruction", name=name) | |
| return _fb(f"Instruction_{name}", _display) | |
| # ---------------------------------------------------------------------------- | |
| # Comprehension quiz (~5 MC items; re-ask missed ones once β mirrors PE) | |
| # ---------------------------------------------------------------------------- | |
| COMP_INTRO = ("A few quick questions to check you've got the task. You'll get a short " | |
| "explanation after each one.") | |
| COMP2_INTRO = ("**At least one of your answers was incorrect.** Let's revisit those items.\n\n" | |
| "Remember, on each clip there are TWO phases:\n\n" | |
| "- **OBSERVATION** β you watch the clip and **predict the co-viewer's** feeling " | |
| "rating (0β100), then you are shown the **co-viewer's actual rating**.\n" | |
| "- **SELF** β the **same clip replays** and you rate **your own** feeling (0β100).\n\n" | |
| "A rating is required on every clip β you cannot skip. Please try once more.") | |
| # Each option is (display_text, export_text); we keep the pair so _ask_mc/_comp_round | |
| # stay byte-compatible with PE. correct = index of the right option. | |
| COMP_ITEMS = [ | |
| dict(key="comp_phases", qid="Q1", | |
| prompt="Which of these is TRUE about the two phases of every clip?", | |
| options=[ | |
| ("In OBSERVATION I predict the co-viewer's rating; in SELF I rate my own feeling", | |
| "OBSERVATION=predict co-viewer; SELF=rate self"), | |
| ("In OBSERVATION I rate my own feeling; in SELF I predict the co-viewer's", | |
| "swapped"), | |
| ("I rate the co-viewer in both phases", "co-viewer both"), | |
| ], correct=0, | |
| explain="In **OBSERVATION** you predict the **co-viewer's** rating; in **SELF** you rate " | |
| "**your own** feeling."), | |
| dict(key="comp_scale", qid="Q2", | |
| prompt="What is the range of BOTH the prediction slider and the self-rating slider?", | |
| options=[ | |
| ("0 to 100", "0-100"), | |
| ("1 to 7", "1-7"), | |
| ("0 to 10", "0-10"), | |
| ], correct=0, | |
| explain="Both ratings use a **0β100** slider."), | |
| dict(key="comp_replay", qid="Q3", | |
| prompt="After you make your prediction, what happens before you rate your own feeling?", | |
| options=[ | |
| ("The same clip replays", "same clip replays"), | |
| ("A brand-new clip is shown", "new clip"), | |
| ("Nothing β I rate from memory", "from memory"), | |
| ], correct=0, | |
| explain="The **same clip replays** so you can rate your own feeling to it."), | |
| dict(key="comp_coviewer_shown", qid="Q4", | |
| prompt="During the OBSERVATION phase, after your prediction you are shownβ¦", | |
| options=[ | |
| ("The co-viewer's actual rating", "co-viewer actual rating"), | |
| ("Your score so far", "score"), | |
| ("Nothing", "nothing"), | |
| ], correct=0, | |
| explain="You see the **co-viewer's actual rating** right after your prediction."), | |
| dict(key="comp_skip", qid="Q5", | |
| prompt="Can you skip a clip without rating it?", | |
| options=[ | |
| ("No β a rating is required on every clip", "No, required"), | |
| ("Yes, whenever I like", "Yes"), | |
| ("Only on painful clips", "only painful"), | |
| ], correct=0, | |
| explain="**No** β a rating is required on every clip; you cannot skip."), | |
| ] | |
| async def _comp_round(container, rnd, items): | |
| out = [] | |
| for n, it in enumerate(items, 1): | |
| idx = await _ask_mc(container, f"Quiz β question {n} of {len(items)}", it) | |
| out.append(dict(key=it["key"], qid=it["qid"], round=rnd, choice=idx, | |
| export_text=it["options"][idx][1] if idx is not None else "", | |
| correct=it["correct"], is_correct=(idx == it["correct"]))) | |
| return out | |
| def comprehension_stage(): | |
| async def _display(stage, container): | |
| nicewebrl.clear_element(container) | |
| ev = asyncio.Event() | |
| with container.style("align-items:center; max-width:680px;"): | |
| ui.markdown("## Quick comprehension check") | |
| ui.markdown(COMP_INTRO) | |
| ui.button("Start", on_click=lambda: ev.set()) | |
| await ev.wait() | |
| # round 1; any missed items get re-asked once (round 2) | |
| responses = await _comp_round(container, 1, COMP_ITEMS) | |
| wrong = [COMP_ITEMS[i] for i, r in enumerate(responses) if not r["is_correct"]] | |
| if wrong: | |
| nicewebrl.clear_element(container) | |
| ev2 = asyncio.Event() | |
| with container.style("align-items:center; max-width:680px;"): | |
| ui.markdown("### Let's revisit the ones you missed") | |
| ui.markdown(COMP2_INTRO) | |
| ui.button("OK", on_click=lambda: ev2.set()) | |
| await ev2.wait() | |
| responses += await _comp_round(container, 2, wrong) | |
| nicewebrl.clear_element(container) | |
| ev3 = asyncio.Event() | |
| with container.style("align-items:center; max-width:600px;"): | |
| ui.markdown("## You're all set!") | |
| ui.button("Continue", on_click=lambda: ev3.set()) | |
| await ev3.wait() | |
| n_correct = sum(1 for r in responses if r["round"] == 1 and r["is_correct"]) | |
| return dict(row_type="survey", measure="comprehension", | |
| n_items=len(COMP_ITEMS), n_correct_first_try=n_correct, | |
| responses=responses) | |
| return _fb("Comprehension", _display) | |
| # ---------------------------------------------------------------------------- | |
| # Impression of the co-viewer(s): "How empathic is this person?" 1β9, pre & post | |
| # ---------------------------------------------------------------------------- | |
| def impression_stage(when, coviewer_names): | |
| """One 1β9 item per co-viewer. `when` in {"pre","post"}. `coviewer_names` = list of str.""" | |
| async def _display(stage, container): | |
| names = list(coviewer_names or []) | |
| if not names: | |
| # resolved at display time from the participant's schedule (set by web_app) | |
| names = [c.get("name") for c in (app.storage.user.get("coviewers") or []) | |
| if c.get("name")] | |
| items = [(f"impression_{i}", f"How empathic do you find **{nm}**?") | |
| for i, nm in enumerate(names)] | |
| intro = ("Now that the task is over, please rate the co-viewer(s) you watched with." | |
| if when == "post" else | |
| "Here are the co-viewer(s) you'll watch clips with. Please give a first impression.") | |
| res = await _slider_battery( | |
| container, "The co-viewer(s)", intro, items, | |
| "Not at all empathic", "Extremely empathic", lo=1, hi=9, start=5) | |
| # keep name<->value mapping explicit for the exporter | |
| per_name = {names[i]: res[f"impression_{i}"] for i in range(len(names))} | |
| return dict(row_type="survey", measure="impression", when=when, | |
| coviewer_names=names, ratings=per_name, **res) | |
| return _fb(f"Impression_{when}", _display) | |
| # ---------------------------------------------------------------------------- | |
| # Willingness to help: 0β60 minutes slider, pre & post | |
| # ---------------------------------------------------------------------------- | |
| def willingness_stage(when): | |
| """A single 0β60 slider: minutes willing to spend comforting/helping the person.""" | |
| async def _display(stage, container): | |
| res = await _slider_battery( | |
| container, "Willingness to help", | |
| "Imagine you could actually help one of the people from the clips.", | |
| [("willingness_minutes", | |
| "How many minutes would you be willing to spend to comfort or help the " | |
| "person in the clip?")], | |
| "0 minutes", "60 minutes", lo=0, hi=60, start=0) | |
| return dict(row_type="survey", measure="willingness", when=when, **res) | |
| return _fb(f"Willingness_{when}", _display) | |
| # ---------------------------------------------------------------------------- | |
| # IRI β short trait-empathy battery (1β5 Likert) | |
| # NOTE: PLACEHOLDER items in an empathic-concern / perspective-taking style β | |
| # CONFIRM the final wording/subset against the official IRI before launch. | |
| # ---------------------------------------------------------------------------- | |
| IRI_ITEMS = [ | |
| ("iri_ec1", "I often have tender, concerned feelings for people less fortunate than me."), | |
| ("iri_pt1", "I try to look at everybody's side of a disagreement before I make a decision."), | |
| ("iri_ec2", "Sometimes I don't feel very sorry for other people when they are having problems."), # reverse | |
| ("iri_pt2", "I sometimes try to understand my friends better by imagining how things look " | |
| "from their perspective."), | |
| ("iri_ec3", "When I see someone being taken advantage of, I feel kind of protective toward them."), | |
| ("iri_pt3", "I believe that there are two sides to every question and try to look at them both."), | |
| ] | |
| # 1β5 Likert; iri_ec2 is reverse-keyed (score = 6 - raw) β flag it for the analyst. | |
| IRI_REVERSE = {"iri_ec2"} | |
| def iri_stage(): | |
| async def _display(stage, container): | |
| res = await _slider_battery( | |
| container, "About you", | |
| "Please rate how well each statement describes you.", | |
| IRI_ITEMS, "Does not describe me well", "Describes me very well", | |
| lo=1, hi=5, start=3) | |
| return dict(row_type="survey", measure="IRI", | |
| reverse_keyed=sorted(IRI_REVERSE), **res) | |
| return _fb("IRI", _display) | |
| # ---------------------------------------------------------------------------- | |
| # Attention check β instructed response on a 0β10 slider ("set it to exactly 7") | |
| # ---------------------------------------------------------------------------- | |
| def attention_check_stage(): | |
| """Instructed-response check: move a 0β10 slider to exactly 7. Pass/fail logged.""" | |
| async def _display(stage, container): | |
| nicewebrl.clear_element(container) | |
| ev = asyncio.Event() | |
| slider = {"s": None} | |
| with container.style("align-items:center; max-width:640px;"): | |
| ui.markdown("## Quick check") | |
| ui.markdown("This is an **attention check**. To show you're reading carefully, " | |
| "please move the slider below to **exactly 7**.") | |
| s = ui.slider(min=0, max=10, value=0).props("label-always") | |
| with ui.row().style("width:100%; max-width:400px; justify-content:space-between;"): | |
| ui.label("0").style("font-size:12px; color:#666;") | |
| ui.label("10").style("font-size:12px; color:#666;") | |
| slider["s"] = s | |
| ui.button("Continue", on_click=lambda: ev.set()) | |
| await ev.wait() | |
| val = int(slider["s"].value) | |
| return dict(row_type="survey", measure="attention_check", | |
| target=7, answer=val, is_correct=(val == 7)) | |
| return _fb("AttentionCheck", _display) | |