Uploaded README
Browse files
README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
pipeline_tag: text-classification
|
| 4 |
+
language:
|
| 5 |
+
- en
|
| 6 |
+
tags:
|
| 7 |
+
- reflection
|
| 8 |
+
- cross-encoder
|
| 9 |
+
- motivational-interviewing
|
| 10 |
+
- conversation-analysis
|
| 11 |
+
- roberta
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# PAIR Reflection Scorer (Cross‑Encoder)
|
| 15 |
+
|
| 16 |
+
This repository provides weights for a PAIR‑style cross‑encoder that scores the quality of counselor reflections in Motivational Interviewing (MI). Given a client/patient prompt and a counselor response, the model outputs a scalar score in [0,1] indicating how strongly the response reflects the prompt.
|
| 17 |
+
|
| 18 |
+
This model is based on the approach described in:
|
| 19 |
+
|
| 20 |
+
- Min, Do June; Pérez‑Rosas, Verónica; Resnicow, Kenneth; Mihalcea, Rada. “PAIR: Prompt‑Aware margIn Ranking for Counselor Reflection Scoring in Motivational Interviewing.” EMNLP 2022. https://aclanthology.org/2022.emnlp-main.11/
|
| 21 |
+
|
| 22 |
+
Please credit the authors above when using this model or derivative work.
|
| 23 |
+
|
| 24 |
+
## Task & Motivation (from the paper)
|
| 25 |
+
|
| 26 |
+
- Reflections are a core verbal counseling skill used to convey understanding and acknowledgment of clients’ experiences.
|
| 27 |
+
- The goal is to automatically score counselor reflections to provide timely, useful feedback for training and education.
|
| 28 |
+
- Input to the scorer: a dialog turn consisting of a client prompt (likely to elicit a reflection) and the counselor’s response.
|
| 29 |
+
- Output: a numeric reflection score capturing the quality/strength of the reflection.
|
| 30 |
+
|
| 31 |
+
## Method: Prompt‑Aware Margin Ranking (PAIR)
|
| 32 |
+
|
| 33 |
+
PAIR trains a prompt‑aware cross‑encoder that contrasts positive and negative (prompt, response) pairs. The key idea is to learn, for a given prompt, to rank higher‑quality reflections above lower‑quality or mismatched responses using margin‑based ranking losses.
|
| 34 |
+
|
| 35 |
+
High‑level components reflected by this implementation:
|
| 36 |
+
|
| 37 |
+
- Encoder: `roberta-base` cross‑encoder over concatenated (prompt, response).
|
| 38 |
+
- Scoring head: Small MLP over the [CLS] token (768 → 512 → 1) with ELU.
|
| 39 |
+
- Training objective (as per the paper/code): multi‑gap margin ranking that separates:
|
| 40 |
+
- High‑quality (HQ) reflections from medium‑quality (MQ) and low‑quality (LQ).
|
| 41 |
+
- HQ/MQ reflections from explicit mismatches (responses paired with the wrong prompt).
|
| 42 |
+
- Inference: apply sigmoid to the logit to obtain a reflection score in [0,1].
|
| 43 |
+
|
| 44 |
+
The included `cross_scorer_model.py` shows the MLP head and margin losses consistent with a PAIR‑style training setup.
|
| 45 |
+
|
| 46 |
+
## Files
|
| 47 |
+
|
| 48 |
+
- `reflection_scorer_weight.pt` — fine‑tuned cross‑encoder weights (encoder + head).
|
| 49 |
+
- `cross_scorer_model.py` — `CrossScorerCrossEncoder` module used for inference/training.
|
| 50 |
+
- `min_pair_2022.txt` — text version summary of the PAIR paper (for reference in this repo).
|
| 51 |
+
|
| 52 |
+
## Intended Use & Limitations
|
| 53 |
+
|
| 54 |
+
- Intended for research, education, and tooling around reflection scoring in counseling‑style conversations.
|
| 55 |
+
- Not a clinical or diagnostic tool; do not use for high‑stakes decisions.
|
| 56 |
+
- Scores are not calibrated probabilities; treat relative differences with caution.
|
| 57 |
+
- As with all ML models, outputs may reflect biases in pretraining/fine‑tuning data.
|
| 58 |
+
|
| 59 |
+
## Quickstart
|
| 60 |
+
|
| 61 |
+
```python
|
| 62 |
+
from huggingface_hub import hf_hub_download
|
| 63 |
+
from transformers import AutoModel, AutoTokenizer
|
| 64 |
+
import torch, importlib.util, sys
|
| 65 |
+
|
| 66 |
+
repo_id = "Khriis/PAIR" # replace if you fork
|
| 67 |
+
|
| 68 |
+
# 1) Download weights and model code from the repo
|
| 69 |
+
ckpt_path = hf_hub_download(repo_id=repo_id, filename="reflection_scorer_weight.pt")
|
| 70 |
+
code_path = hf_hub_download(repo_id=repo_id, filename="cross_scorer_model.py")
|
| 71 |
+
|
| 72 |
+
# 2) Import model definition
|
| 73 |
+
spec = importlib.util.spec_from_file_location("cross_scorer_model", code_path)
|
| 74 |
+
mod = importlib.util.module_from_spec(spec)
|
| 75 |
+
sys.modules["cross_scorer_model"] = mod
|
| 76 |
+
spec.loader.exec_module(mod)
|
| 77 |
+
|
| 78 |
+
# 3) Build encoder + head and load state dict
|
| 79 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 80 |
+
encoder = AutoModel.from_pretrained("roberta-base", add_pooling_layer=False)
|
| 81 |
+
model = mod.CrossScorerCrossEncoder(encoder).to(device)
|
| 82 |
+
tokenizer = AutoTokenizer.from_pretrained("roberta-base")
|
| 83 |
+
|
| 84 |
+
state = torch.load(ckpt_path, map_location=device)
|
| 85 |
+
sd = state.get("model_state_dict", state)
|
| 86 |
+
model.load_state_dict(sd)
|
| 87 |
+
model.eval()
|
| 88 |
+
|
| 89 |
+
# 4) Score a (prompt, response) pair
|
| 90 |
+
prompt = "I’ve been overwhelmed at work and can’t focus."
|
| 91 |
+
response = "It sounds like you’re under a lot of pressure, and it’s affecting your ability to concentrate."
|
| 92 |
+
batch = tokenizer(prompt, response, padding="longest", truncation=True, return_tensors="pt").to(device)
|
| 93 |
+
with torch.no_grad():
|
| 94 |
+
score = model.score_forward(**batch).sigmoid().item()
|
| 95 |
+
print("Reflection score:", round(score, 3))
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
### Using in the Toolkit
|
| 99 |
+
|
| 100 |
+
The toolkit can download the file automatically (public repo). For offline use, place `reflection_scorer_weight.pt` locally and set `REFLECTION_CKPT_PATH` to that path.
|
| 101 |
+
|
| 102 |
+
## Citation
|
| 103 |
+
|
| 104 |
+
If you use this model or code, please cite the PAIR paper:
|
| 105 |
+
|
| 106 |
+
Informal citation: “PAIR: Prompt‑Aware margIn Ranking for Counselor Reflection Scoring in Motivational Interviewing” (Min et al., EMNLP 2022). https://aclanthology.org/2022.emnlp-main.11/
|
| 107 |
+
|
| 108 |
+
BibTeX (adapt based on official entry):
|
| 109 |
+
|
| 110 |
+
```bibtex
|
| 111 |
+
@inproceedings{min-etal-2022-pair,
|
| 112 |
+
title = {PAIR: Prompt-Aware margIn Ranking for Counselor Reflection Scoring in Motivational Interviewing},
|
| 113 |
+
author = {Min, Do June and P{\'e}rez-Rosas, Ver{\'o}nica and Resnicow, Kenneth and Mihalcea, Rada},
|
| 114 |
+
booktitle = {Proceedings of the 2022 Conference on Empirical Methods in Natural Language Processing},
|
| 115 |
+
year = {2022},
|
| 116 |
+
url = {https://aclanthology.org/2022.emnlp-main.11/}
|
| 117 |
+
}
|
| 118 |
+
```
|
| 119 |
+
|
| 120 |
+
Also cite RoBERTa:
|
| 121 |
+
|
| 122 |
+
```bibtex
|
| 123 |
+
@misc{liu2019roberta,
|
| 124 |
+
title = {{RoBERTa}: A Robustly Optimized {BERT} Pretraining Approach},
|
| 125 |
+
author = {Liu, Yinhan and others},
|
| 126 |
+
year = {2019},
|
| 127 |
+
url = {https://arxiv.org/abs/1907.11692}
|
| 128 |
+
}
|
| 129 |
+
```
|