|
|
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 |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
|
|
|
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 == 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}..."): |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
question_stem_input = st.text_area( |
|
|
question_stem_field, |
|
|
key=f"input_{question_stem_field}", |
|
|
|
|
|
|
|
|
kwargs={'field_id': question_stem_field}, |
|
|
height=100, |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
randomize_order_bool = st.checkbox( |
|
|
randomize_order_tick, |
|
|
key=f"input_{randomize_order_tick}", |
|
|
value=False, |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
with st.container(border=True): |
|
|
system_prompt, current_prompt = st.session_state.temporary_questionnaire.get_prompt_for_questionnaire_type(QuestionnaireType.BATTERY) |
|
|
|
|
|
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!") |
|
|
|