File size: 6,332 Bytes
818f654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0529b8b
818f654
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import yaml
import pandas as pd
from cryptography.fernet import Fernet
from dotenv import load_dotenv
from io import StringIO

import modeling

def df_to_csv(df):
    csv = StringIO()
    df.to_csv(csv, index=True)
    csv.seek(0)
    csv_data = csv.getvalue()
    return(csv_data)

def dict_to_yaml(data):
    return yaml.dump(data, default_flow_style=False)

def yaml_to_dict(yaml_str):
    return yaml.safe_load(yaml_str)

def initialize():
    load_dotenv()
    st.session_state.setdefault('model_names', ['SurveyBot3000', 'PsiSent', 'all_mpnet_base_v2'])
    st.session_state.setdefault('loaded_model_name', None)
    st.session_state.setdefault('search_query', None)
    st.session_state.setdefault('db', None)
    st.session_state.setdefault('results', pd.DataFrame())
    st.session_state.setdefault('decrypt_key', None)
    st.session_state.setdefault('valid_decrypt_key', False)

    with open('config.yaml', 'r') as stream:
        st.session_state['config'] = yaml.safe_load(stream)

def main():
    st.set_page_config(page_title='Synth-Net')

    st.markdown("# The Synthetic Nomological Net")
    # st.markdown("#### This is a demo on how to extract trait information from responses to open-ended questions.")

    st.markdown("""
        Psychological science is experiencing rapid growth in constructs and measures, partly due to refinement and new research areas, 
        but also due to excessive proliferation. This proliferation, driven by academic incentives for novelty, may lead to redundant 
        constructs with different names (jangle fallacy) and seemingly similar constructs with little content overlap (jingle fallacy).

        This web application uses state-of-the-art models and methods in natural language processing to search for semantic overlap in measures.
        It analyzes textual data from over 21,000 scales (containing more than 330,000 items) in an effort to reduce redundancies in measures used in the behavioral sciences.

        - πŸ“– **Preprint (Open Access)**: NA
        - πŸ–ŠοΈ **Cite**: NA
        - 🌐 **Project website**: NA
        - πŸ’Ύ **Data**: NA
        - #️⃣ **Social Media**: NA

        The web application is maintained by [magnolia psychometrics](https://www.magnolia-psychometrics.com/).
    """, unsafe_allow_html=True)

    placeholder_demo = st.empty()

    show_demo(placeholder_demo)

def show_demo(placeholder):

    with placeholder:
        with st.container():
            st.divider()
            st.markdown("""               
                ## Try it yourself!
                Define a scale by entering individual items in YAML format. 
                After form submission, a vector representation for the scale is calculated using the selected encoder model.
                Cosine similarities between this vector and the representations of existing scales are then computed.
                The resulting table outputs measures with high semantic overlap. 
            """)

            with st.form("submission_form"):

                if not st.session_state['valid_decrypt_key']:
                    with st.expander(label="Authentication", expanded=True, icon="πŸ”‘"):
                        st.text_input(
                            label="Encryption key", 
                            value="", 
                            max_chars=None, 
                            key='decrypt_key',
                            placeholder="A URL-safe base64-encoded 32-byte key"
                        )

                with st.expander(label="Model", expanded=False, icon="🧠"):

                    if st.session_state['loaded_model_name'] is not None:
                        input_model_index = st.session_state['model_names'].index(st.session_state['input_model_name'])
                    else:
                        input_model_index = 0

                    st.selectbox(
                        label="Select model",
                        options=st.session_state['model_names'],
                        index=input_model_index,
                        key='input_model_name'
                    )
                    
                with st.expander(label="Search Query", expanded=True, icon="πŸ”Ž"):
                    if 'input_items' not in st.session_state:
                        st.session_state['input_items'] = dict_to_yaml(st.session_state['config']['input_items'])

                    st.text_area(
                        label="Search for similar measures by entering items that constitute the scale (YAML-Formatted):",
                        height=175,
                        key='input_items'
                    )

                submitted = st.form_submit_button(
                    label="Search Synth-Net",
                    type="primary",
                    use_container_width=True
                )

                if submitted:

                    try:
                        st.session_state['search_query'] = yaml_to_dict(st.session_state['input_items'])
                    except yaml.YAMLError as e:
                        st.error(f"Yikes, you better get your YAML straight! Check https://yaml.org/ for help! \n {e}")
                        return

                    try:
                        modeling.load_model()
                        modeling.search()
                    except Exception as error:
                        st.error(f"Error while loading model: {error}")
                        return
                                        
            with st.container():
                if not st.session_state['results'].empty:
                    df = st.session_state['results'].style.format({
                        'Match': '{:.2f}'.format,
                        'Scale': str.capitalize,
                        'Instrument': str.capitalize,
                    })
                    st.dataframe(df, use_container_width=True)

                    st.download_button(
                        label="Download References",
                        data=df_to_csv(st.session_state['results']),
                        file_name='scored_survey_responses.csv',
                        mime='text/csv',
                        use_container_width=True
                    )

if __name__ == '__main__':
    initialize()
    main()