File size: 6,737 Bytes
448ed98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from qstn.survey_manager import SurveyOptionGenerator
from qstn.llm_questionnaire import LLMQuestionnaire
from qstn.utilities.constants import QuestionnaireType
from typing import Any

from gui_elements.paginator import paginator

# CONSTANTS FOR FIELDS
question_stem_field = "Question Stem"
randomize_order_tick = "Randomize the order of items"

field_ids = [question_stem_field, randomize_order_tick]

st.title("Questions Preparation")
st.write(
    "This interface allows you configure how the questions are prompted to the LLM. These options are applied to every questionnaire in your survey."
)
st.page_link("pages/02_Option_Prompt.py", label="Click here to adjust the answer options.")
st.divider()

#current_questionnaire_id = paginator(st.session_state.questionnaires, "current_questionnaire_index_prepare")

if "questionnaires" not in st.session_state:
    st.error("You need to first upload a questionnaire and the population you want to survey.")
    st.stop()
    disabled = True
else:
    disabled = False
if not "temporary_questionnaire" in st.session_state:
    st.session_state.temporary_questionnaire = st.session_state.questionnaires[0].duplicate()

    #print(st.session_state.temporary_questionnaire._questions)

if not "base_questionnaire" in st.session_state:
    st.session_state.base_questionnaire = st.session_state.temporary_questionnaire.duplicate()

def process_inputs(input: Any, field_id: str) -> str:
    if "survey_options" in st.session_state:
        survey_options = st.session_state.survey_options
    else:
        survey_options = None

    if field_id == question_stem_field:
        LLMQuestionnaire.prepare_questionnaire
        st.session_state.temporary_questionnaire.prepare_questionnaire(
            question_stem=input,
            answer_options=survey_options,
            randomized_item_order=randomize_order_bool,
        )
        st.session_state.base_questionnaire.prepare_questionnaire(
            question_stem=input,
            answer_options=survey_options,
            randomized_item_order=randomize_order_bool,
        )
    # elif field_id == global_options_tick:
    #     option_behavior = input == "Give instruction in the beginning"
    #     st.session_state.temporary_questionnaire.prepare_questionnaire(
    #         question_stem=question_stem_input,
    #         answer_options=survey_options,
    #         global_options=option_behavior,
    #         randomized_item_order=randomize_order_bool,
    #     )
    #     st.session_state.base_questionnaire.prepare_questionnaire(
    #         question_stem=question_stem_input,
    #         answer_options=survey_options,
    #         global_options=option_behavior,
    #         randomized_item_order=False,
    #     )
    elif field_id == randomize_order_tick:
        if input == True:
            st.session_state.temporary_questionnaire.prepare_questionnaire(
                question_stem=question_stem_input,
                answer_options=survey_options,
                randomized_item_order=input,
            )
        else:
            st.session_state.temporary_questionnaire = st.session_state.base_questionnaire.duplicate()

def handle_change(field_id: str):
    """
    This single callback handles changes from any text field.
    It reads the input from session state using the unique key,
    processes it, and saves the output to session state.
    """
    input_key = f"input_{field_id}"

    with st.spinner(f"Processing {field_id}..."):
        # time.sleep(0.5) # Simulate work
        process_inputs(st.session_state[input_key], field_id)


col1, col2 = st.columns(2, gap="large")

with col1:
    st.subheader("⚙️ Configuration")

    for field_id in field_ids:
        input_key = f"input_{field_id}"
        if not input_key in st.session_state:
            if field_id == question_stem_field:
                st.session_state[input_key] = st.session_state.temporary_questionnaire ._questions[0].question_stem
            if field_id == randomize_order_tick:
                st.session_state[input_key] = False

    # --- Input Widgets (No Form) ---
    question_stem_input = st.text_area(
        question_stem_field,
        key=f"input_{question_stem_field}",
        # placeholder="e.g., How would you rate the following aspects of our service?",
        #on_change=handle_change,
        kwargs={'field_id': question_stem_field},
        height=100,
    )

    # option_behavior = st.radio(
    #     global_options_tick,
    #     key=f"input_{global_options_tick}",
    #     options=[
    #         "Give instruction in the beginning",
    #         "Give options after each question",
    #     ],
    #     index=0,
    #     #on_change=handle_change,
    #     kwargs={'field_id': global_options_tick},
    #     help="Choose how answer options are applied.",
    # )

    randomize_order_bool = st.checkbox(
        randomize_order_tick,
        key=f"input_{randomize_order_tick}",
        value=False,
        #on_change=handle_change,
        kwargs={'field_id': randomize_order_tick} 
    )

if "survey_options" in st.session_state:
    survey_options = st.session_state.survey_options
else:
    survey_options = None

if randomize_order_bool:
    st.session_state.temporary_questionnaire.prepare_questionnaire(
        question_stem=question_stem_input,
        answer_options=survey_options,
        randomized_item_order=randomize_order_bool,
    )
st.session_state.base_questionnaire.prepare_questionnaire(
    question_stem=question_stem_input,
    answer_options=survey_options,
    randomized_item_order=False,
)

if not randomize_order_bool:
    st.session_state.temporary_questionnaire = st.session_state.base_questionnaire.duplicate()

with col2:
    st.subheader("📄 Live Preview")

    #@Ahmed All of these could be a resusable function (it is used on almost all pages) Maybe split up the container in system prompt/ prompt
    with st.container(border=True):
        system_prompt, current_prompt = st.session_state.temporary_questionnaire.get_prompt_for_questionnaire_type(QuestionnaireType.BATTERY)
        # markdown newlines
        system_prompt = system_prompt.replace("\n", "  \n")
        current_prompt = current_prompt.replace("\n", "  \n")
        st.write(system_prompt)
        st.write(current_prompt)

st.divider()

if st.button("Confirm and Prepare Questionnaire", type="primary", use_container_width=True):
    for questionnaire in st.session_state.questionnaires:
        questionnaire.prepare_questionnaire(
            question_stem=question_stem_input,
            answer_options=survey_options,
            randomized_item_order=randomize_order_bool,
        )
    st.success("Changed the prompts!")