File size: 7,892 Bytes
f1846be
164cb45
a746976
164cb45
a746976
164cb45
4ebac20
 
164cb45
a746976
164cb45
 
 
 
 
4ebac20
164cb45
4ebac20
164cb45
4ebac20
 
 
164cb45
 
 
 
 
 
4ebac20
164cb45
 
 
 
 
4ebac20
 
 
164cb45
 
 
4ebac20
 
 
 
 
 
 
a746976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ebac20
a746976
4ebac20
 
 
 
 
a746976
 
4ebac20
a746976
 
4ebac20
a746976
 
 
 
4ebac20
a746976
 
4ebac20
a746976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ebac20
 
a746976
4ebac20
a746976
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ebac20
a746976
4ebac20
a746976
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os
import torch
import dash
import streamlit as st
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from transformers import pipeline
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly.express as px


def z_score(y, mean=.04853076, sd=.9409466):
    return (y - mean) / sd

def indicator_plot(value, title, value_range, domain):

    plot = go.Indicator(
        mode = "gauge+delta",
        value = value,
        domain = domain,
        title = title,
        delta = {
            'reference': 0, 
            'decreasing': {'color': "#ec4899"},
            'increasing': {'color': "#36def1"}
            },
        gauge = {
            'axis': {'range': value_range, 'tickwidth': 1, 'tickcolor': "black"},
            'bar': {'color': "#4361ee"},
            'bgcolor': "white",
            'borderwidth': 2,
            'bordercolor': "#efefef",
            'steps': [
                {'range': [value_range[0], 0], 'color': '#efefef'},
                {'range': [0, value_range[1]], 'color': '#efefef'}
            ],
            'threshold': {
                'line': {'color': "#4361ee", 'width': 8},
                'thickness': 0.75,
                'value': value
            }
        }
    )

    return plot

def scatter_plot(df, group_var):

    colors = ['#36def1', '#4361ee'] if group_var else ['#4361ee']

    plot = px.scatter(
        df, 
        x='Machine-ratings', 
        y='Human-ratings',
        color=group_var,
        facet_col='x_group', 
        facet_col_wrap=2,
        trendline='ols',
        trendline_scope='trace',
        hover_data={
            'Text': df.text,
            'Language': False,
            'x_group': False,            
            'Human-ratings': ':.2f',
            'Machine-ratings': ':.2f',
            'Study': df.study,
            'Instrument': df.instrument,
        },
        width=400,
        height=400,
        color_discrete_sequence=colors
    )
    
    plot.for_each_annotation(lambda a: a.update(text=a.text.split('=')[-1]))
    plot.update_layout(
        legend={
            'orientation':'h',
            'yanchor': 'bottom',
            'y': -.30
        })
    plot.update_xaxes(title_standoff = 0)

    return plot

# data import and wrangling
covariate_columns = {
        'content_domain': 'Content Domain',
        'language': 'Language',
        'rater_group': 'Rater Group',
    }

df = (
    pd
    .read_feather(path='data.feather').query('partition == "test" | partition == "dev"')
    .melt(
        value_vars=['sentiment_model', 'desirability_model'],
        var_name='x_group',
        value_name='x',
        id_vars=['mean_z', 'text', 'content_domain', 'language', 'rater_group', 'study', 'instrument']
        )
    .replace(
        to_replace={
            'en': 'English',
            'de': 'German',
            'other': 'Other',
            'personality': 'Personality',
            'laypeople': 'Laypeople',
            'students': 'Students',
            'sentiment_model': 'Sentiment Model',
            'desirability_model': 'Desirability Model'
        }    
    )
    .rename(columns=covariate_columns)
    .rename(
        columns={
            'mean_z': 'Human-ratings',
            'x': 'Machine-ratings',
        }
    )
)

st.markdown("""
    # NLP for Item Desirability Ratings
    This web application accompanies the paper "*Expanding the Methodological Toolbox: Machine-Based Item Desirability Ratings as an Alternative to Human-Based Ratings*".

    ## What is this research about?
    Researchers use personality scales to measure people's traits and behaviors, but biases can affect the accuracy of these scales. 
    Socially desirable responding is a common bias that can skew results. To overcome this, researchers gather item desirability ratings, e.g., to ensure that questions are neutral. 
    Recently, advancements in natural language processing have made it possible to use machines to estimate social desirability ratings, 
    which can provide a viable alternative to human ratings and help researchers, scale developers, and practitioners improve the accuracy of personality scales.  
""")

# demo
st.markdown("""
    ## Try it yourself!
    Use the text field below to enter a statement that might be part of a psychological questionnaire (e.g., "I love a good fight.").    
    The left dial indicates how socially desirable it might be to endorse this item. 
    The right dial indicates sentiment (i.e., valence) as estimated by regular sentiment analysis (using the `cardiffnlp/twitter-xlm-roberta-base-sentiment` model).
""")

## desirability model
with st.spinner('Processing...'):
    
    if os.environ.get('item-desirability'):
        model_path = 'magnolia-psychometrics/item-desirability'
    else:
        model_path = '/nlp/nlp/models/finetuned/twitter-xlm-roberta-base-regressive-desirability-ft-4'   

    auth_token = os.environ.get('item-desirability') or True

    if 'tokenizer' not in globals():
        tokenizer = AutoTokenizer.from_pretrained(
            pretrained_model_name_or_path=model_path,
            use_fast=True,
            use_auth_token=auth_token
        )

    if 'model' not in globals():
        model = AutoModelForSequenceClassification.from_pretrained(
            pretrained_model_name_or_path=model_path, 
            num_labels=1, 
            ignore_mismatched_sizes=True,
            use_auth_token=auth_token
        )

    ## sentiment model
    if 'classifier' not in globals():
        sentiment_model = 'cardiffnlp/twitter-xlm-roberta-base-sentiment'
        classifier = pipeline("sentiment-analysis", model=sentiment_model, tokenizer=sentiment_model, use_fast=False, top_k=3)

    input_text = st.text_input(
        label='Estimate item desirability:',
        value='I love a good fight.',
        placeholder='Enter item text'
    )

    if input_text:    
        
        classifier_output = classifier(input_text)
        classifier_output_dict = {x['label']: x['score'] for x in classifier_output[0]}
        classifier_score = classifier_output_dict['positive'] - classifier_output_dict['negative']

        inputs = tokenizer(input_text, padding=True, return_tensors='pt')

        with torch.no_grad():
            score = model(**inputs).logits.squeeze().tolist()
            z = z_score(score)

        p1 = indicator_plot(
            value=classifier_score,
            title=f'Item Sentiment',
            value_range=[-1, 1],
            domain={'x': [.55, 1], 'y': [0, 1]}        
        )

        p2 = indicator_plot(
            value=z,
            title=f'Item Desirability',
            value_range=[-4, 4],
            domain={'x': [0, .45], 'y': [0, 1]},    
        )

        fig = go.Figure()
        fig.add_trace(p1)
        fig.add_trace(p2)

        fig.update_layout(
            title=dict(text=f'"{input_text}"', font=dict(size=36),yref='paper'),
            paper_bgcolor = "white", 
            font = {'color': "black", 'family': "Arial"})
            
        st.plotly_chart(fig, theme=None, use_container_width=True)
                
        st.markdown("""
            Item sentiment: Absolute differences between positive and negative sentiment.
            Item desirability: z-transformed values, 0 indicated "neutral".        
        """)

## plot
st.markdown("""
    ## Explore the data
    Figures show the accuarcy in precitions of human-rated item desirability by the sentiment model (left) and the desirability model (right), using `test`-partition data only.
""")


show_covariates = st.checkbox('Show covariates', value=True)

if show_covariates:
    option = st.selectbox('Group by', options=list(covariate_columns.values()))
else:
    option = None

plot = scatter_plot(df, option)

st.plotly_chart(plot, theme=None, use_container_width=True)