File size: 5,124 Bytes
9a6b7dc
 
a962ffe
 
9a6b7dc
 
00eef23
 
a962ffe
a2dd0df
9a6b7dc
 
 
 
 
 
a2dd0df
9a6b7dc
a2dd0df
9a6b7dc
 
 
a2dd0df
9a6b7dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3eb1a38
9a6b7dc
 
a2dd0df
 
9a6b7dc
 
 
 
 
00eef23
9a6b7dc
a2dd0df
9a6b7dc
 
b616114
9a6b7dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2dd0df
00eef23
9a6b7dc
 
 
 
 
 
 
 
a2dd0df
00eef23
9a6b7dc
 
 
 
 
 
 
00eef23
 
9a6b7dc
00eef23
 
c0c66b4
00eef23
9a6b7dc
00eef23
9a6b7dc
 
00eef23
9a6b7dc
 
c0c66b4
9a6b7dc
 
 
 
 
 
 
 
 
 
c0c66b4
9a6b7dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0c66b4
9a6b7dc
 
 
 
 
 
 
 
 
 
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
from db.schema import Feedback, Response
from db.crud import ingest
import pandas as pd
import streamlit as st
from datetime import datetime


# Load Q&A data
@st.cache_data
def load_data():
    return pd.read_csv("dummy_qa_data.csv")[:3]


# Function to validate Prolific ID (customize this based on your format requirements)
def validate_prolific_id(prolific_id: str) -> bool:
    return bool(prolific_id.strip())  # Example: Check non-empty

    # Function to reset the survey


def reset_survey():
    st.session_state.prolific_id = None
    st.session_state.current_index = 0
    st.session_state.responses = []
    st.session_state.completed = False


def initialization():
    """Initialize session state variables."""
    if "current_index" not in st.session_state:
        st.session_state.current_index = 0
    if "prolific_id" not in st.session_state:
        st.session_state.prolific_id = None
    if "responses" not in st.session_state:
        st.session_state.responses = []
    if "completed" not in st.session_state:
        st.session_state.completed = False


def prolific_id_screen():
    """Display the Prolific ID entry screen."""
    st.title("Welcome to the Feedback Survey")
    prolific_id_input = st.text_input("Enter your Prolific ID and press ENTER:")

    if prolific_id_input:
        if validate_prolific_id(prolific_id_input):
            st.session_state.prolific_id = prolific_id_input
            st.success("Prolific ID recorded! Now, proceed with the questions.")
            st.rerun()
        else:
            st.warning("Invalid Prolific ID. Please check and try again.")


def questions_screen(data):
    """Display the questions screen."""
    current_index = st.session_state.current_index

    # Display question and answer
    question = data.loc[current_index, "Question"]
    generated_answer = data.loc[current_index, "Generated Answer"]

    st.subheader(f"Question {current_index + 1}")
    st.markdown(f"***{question}***")
    st.subheader("Generated Answer:")
    st.write(generated_answer)

    # Prefill responses if they exist
    previous_rating = (
        st.session_state.responses[current_index].ans
        if len(st.session_state.responses) > current_index
        else 0
    )
    previous_feedback = (
        st.session_state.responses[current_index].feedback_text
        if len(st.session_state.responses) > current_index
        else ""
    )

    # Rating slider
    rating = st.slider("Rate the answer (1 = Worst, 5 = Best)", 1, 5, value=previous_rating,
                       key=f"rating_{current_index}")

    # Free-text feedback
    feedback_text = st.text_area("Any additional feedback?", value=previous_feedback, key=f"feedback_{current_index}")

    # Store or update the response
    response = Response(q_id=f"q{current_index + 1}", ans=rating, feedback_text=feedback_text)
    if len(st.session_state.responses) > current_index:
        st.session_state.responses[current_index] = response
    else:
        st.session_state.responses.append(response)

    # Navigation buttons
    navigation_buttons(data, rating, feedback_text)


def navigation_buttons(data, rating, feedback_text):
    """Display navigation buttons."""
    current_index = st.session_state.current_index

    col1, col2, col3 = st.columns([1, 1, 2])

    with col1:  # Back button
        if st.button("Back") and current_index > 0:
            st.session_state.current_index -= 1
            st.rerun()

    with col2:  # Next button
        if st.button("Next"):
            if not rating:
                st.warning("Please provide a rating before proceeding.")
            else:
                if current_index < len(data) - 1:
                    st.session_state.current_index += 1
                    st.rerun()
                else:
                    feedback = Feedback(
                        id=current_index + 1,
                        user_id=st.session_state.prolific_id,
                        time_stamp=datetime.now(),
                        responses=st.session_state.responses,
                    )
                    try:
                        ingest(feedback)
                        st.session_state.completed = True
                        st.rerun()
                    except Exception as e:
                        st.error(f"An error occurred while submitting feedback: {e}")


def reset_survey():
    """Reset the survey state to start over."""
    st.session_state.prolific_id = None
    st.session_state.current_index = 0
    st.session_state.responses = []
    st.session_state.completed = False


def ui():
    """Main function to control the survey flow."""
    data = load_data()
    initialization()

    if st.session_state.completed:
        st.title("Survey Completed")
        st.success("You have completed all questions and your answers have been recorded.")
        if st.button("Retake Survey"):
            reset_survey()
            st.rerun()
        return

    if st.session_state.prolific_id is None:
        prolific_id_screen()
    else:
        questions_screen(data)


if __name__ == "__main__":
    ui()