Spaces:
Sleeping
Sleeping
Ashmi Banerjee commited on
Commit ·
b7a713a
1
Parent(s): 46dae9a
refactored back code
Browse files- app.py +1 -1
- views/questions_screen.py +152 -99
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import streamlit as st
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from views.intro_screen import welcome_screen
|
| 7 |
-
from views.questions_screen import questions_screen
|
| 8 |
from views.continue_survey import continue_survey_screen
|
| 9 |
from css.layout import custom_css
|
| 10 |
st.set_page_config(layout="wide")
|
|
|
|
| 4 |
import os
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from views.intro_screen import welcome_screen
|
| 7 |
+
from views.questions_screen import questions_screen
|
| 8 |
from views.continue_survey import continue_survey_screen
|
| 9 |
from css.layout import custom_css
|
| 10 |
st.set_page_config(layout="wide")
|
views/questions_screen.py
CHANGED
|
@@ -7,77 +7,47 @@ from views.nav_buttons import navigation_buttons
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
"""Display
|
| 12 |
-
st.markdown(
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
st.session_state.show_questions = False
|
| 20 |
st.session_state.completed = True
|
| 21 |
st.session_state.start_new_survey = True
|
| 22 |
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
cols = st.columns(3)
|
| 28 |
-
with cols[0]:
|
| 29 |
-
query_v_ratings = render_query_ratings(model_name, "Query_v",
|
| 30 |
-
config, f"{model_name}_query_v", current_index,
|
| 31 |
-
has_persona_alignment=False)
|
| 32 |
-
with cols[1]:
|
| 33 |
-
query_p0_ratings = render_query_ratings(model_name, "Query_p0",
|
| 34 |
-
config, f"{model_name}_query_p0", current_index,
|
| 35 |
-
has_persona_alignment=True)
|
| 36 |
-
with cols[2]:
|
| 37 |
-
query_p1_ratings = render_query_ratings(model_name, "Query_p1",
|
| 38 |
-
config, f"{model_name}_query_p1",
|
| 39 |
-
current_index, has_persona_alignment=True)
|
| 40 |
-
if "persona_alignment" in query_v_ratings:
|
| 41 |
-
query_v_ratings.pop("persona_alignment")
|
| 42 |
-
return {
|
| 43 |
-
"query_v_ratings": query_v_ratings,
|
| 44 |
-
"query_p0_ratings": query_p0_ratings,
|
| 45 |
-
"query_p1_ratings": query_p1_ratings,
|
| 46 |
-
}
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def render_query_ratings(model_name, query_label, config, query_key, current_index, has_persona_alignment=False):
|
| 50 |
-
"""Helper function to render ratings for a given query."""
|
| 51 |
-
|
| 52 |
previous_ratings = {}
|
| 53 |
-
|
| 54 |
-
if current_index < st.session_state.current_index and len(
|
|
|
|
|
|
|
| 55 |
if st.session_state.previous_ratings:
|
| 56 |
-
previous_ratings = st.session_state.previous_ratings.get(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
elif len(st.session_state.responses) <= current_index:
|
| 63 |
previous_ratings = {}
|
| 64 |
-
# User has already entered some response in the page they are in
|
| 65 |
else:
|
| 66 |
-
# get the saved ratings from session state for this question
|
| 67 |
response_from_session = st.session_state.responses[current_index]
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
previous_ratings = response_from_session["model_ratings"].get("gemini", {})
|
| 73 |
-
else:
|
| 74 |
-
try:
|
| 75 |
-
previous_ratings = response_from_session.model_ratings.get("llama", {})
|
| 76 |
-
except AttributeError:
|
| 77 |
-
previous_ratings = response_from_session["model_ratings"].get("llama", {})
|
| 78 |
|
| 79 |
stored_query_ratings = {}
|
| 80 |
-
|
| 81 |
if previous_ratings:
|
| 82 |
if "query_v" in query_key:
|
| 83 |
try:
|
|
@@ -94,63 +64,146 @@ def render_query_ratings(model_name, query_label, config, query_key, current_ind
|
|
| 94 |
stored_query_ratings = previous_ratings.query_p1_ratings
|
| 95 |
except AttributeError:
|
| 96 |
stored_query_ratings = previous_ratings["query_p1_ratings"]
|
| 97 |
-
else:
|
| 98 |
-
stored_query_ratings = {}
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
if model_name == "gemini":
|
| 105 |
bg_color = "#e0f7fa"
|
| 106 |
else:
|
| 107 |
bg_color = "#f0f4c3"
|
|
|
|
| 108 |
with st.container():
|
| 109 |
-
st.markdown(
|
|
|
|
| 110 |
<div style="background-color:{bg_color}; padding:1rem;">
|
| 111 |
-
<h3 style="color:blue;">
|
| 112 |
-
<p style="text-align:left;">{config[query_key]}</p>
|
| 113 |
</div>
|
| 114 |
-
""",
|
| 115 |
-
|
|
|
|
|
|
|
| 116 |
options = [0, 1, 2, 3, 4]
|
| 117 |
|
| 118 |
persona_alignment_rating = None
|
| 119 |
if has_persona_alignment:
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
"Persona Alignment:", options=[0, 1, 2, 3, 4],
|
| 123 |
-
format_func=lambda x: ["N/A", "Not Aligned", "Partially Aligned", "Aligned", "Unclear"][x],
|
| 124 |
-
key=f"rating_{query_key}_persona_alignment_{current_index}",
|
| 125 |
-
index=stored_persona_alignment if stored_persona_alignment is not None else 0
|
| 126 |
-
)
|
| 127 |
-
with columns[1]:
|
| 128 |
-
relevance_rating = st.radio(
|
| 129 |
-
"Relevance:",
|
| 130 |
options,
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
)
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
return {
|
| 145 |
"clarity": clarity_rating,
|
| 146 |
"relevance": relevance_rating,
|
| 147 |
-
"persona_alignment": persona_alignment_rating if has_persona_alignment else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
}
|
| 149 |
|
| 150 |
|
| 151 |
def questions_screen(data):
|
| 152 |
-
"""Display the questions screen with split layout"""
|
| 153 |
current_index = st.session_state.current_index
|
|
|
|
| 154 |
try:
|
| 155 |
config = data.iloc[current_index]
|
| 156 |
|
|
@@ -163,12 +216,12 @@ def questions_screen(data):
|
|
| 163 |
# Context information
|
| 164 |
st.markdown("### Context Information")
|
| 165 |
with st.expander("Persona", expanded=True):
|
| 166 |
-
st.write(config[
|
| 167 |
with st.expander("Filters & Cities", expanded=True):
|
| 168 |
-
st.write("**Filters:**", config[
|
| 169 |
-
st.write("**Cities:**", config[
|
| 170 |
with st.expander("Full Context", expanded=False):
|
| 171 |
-
st.text_area("", config[
|
| 172 |
|
| 173 |
g_ratings = display_ratings_row("gemini", config, current_index)
|
| 174 |
l_ratings = display_ratings_row("llama", config, current_index)
|
|
@@ -189,12 +242,11 @@ def questions_screen(data):
|
|
| 189 |
query_v_ratings=l_ratings["query_v_ratings"],
|
| 190 |
query_p0_ratings=l_ratings["query_p0_ratings"],
|
| 191 |
query_p1_ratings=l_ratings["query_p1_ratings"],
|
| 192 |
-
)
|
| 193 |
},
|
| 194 |
comment=comment,
|
| 195 |
-
timestamp=datetime.now().isoformat()
|
| 196 |
)
|
| 197 |
-
print(response)
|
| 198 |
try:
|
| 199 |
st.session_state.ratings[current_index] = response["model_ratings"]
|
| 200 |
except TypeError:
|
|
@@ -208,3 +260,4 @@ def questions_screen(data):
|
|
| 208 |
navigation_buttons(data, response)
|
| 209 |
except IndexError:
|
| 210 |
print("Survey completed!")
|
|
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
|
| 10 |
+
def display_completion_message():
|
| 11 |
+
"""Display a standardized survey completion message."""
|
| 12 |
+
st.markdown(
|
| 13 |
+
"""
|
| 14 |
+
<div class='exit-container'>
|
| 15 |
+
<h1>You have already completed the survey! Thank you for participating!</h1>
|
| 16 |
+
<p>Your responses have been saved successfully.</p>
|
| 17 |
+
<p>You can safely close this window or start a new survey.</p>
|
| 18 |
+
</div>
|
| 19 |
+
""",
|
| 20 |
+
unsafe_allow_html=True,
|
| 21 |
+
)
|
| 22 |
st.session_state.show_questions = False
|
| 23 |
st.session_state.completed = True
|
| 24 |
st.session_state.start_new_survey = True
|
| 25 |
|
| 26 |
|
| 27 |
+
def get_previous_ratings(model_name, query_key, current_index):
|
| 28 |
+
"""Retrieve previous ratings from session state."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
previous_ratings = {}
|
| 30 |
+
|
| 31 |
+
if current_index < st.session_state.current_index and len(
|
| 32 |
+
st.session_state.responses
|
| 33 |
+
) > current_index:
|
| 34 |
if st.session_state.previous_ratings:
|
| 35 |
+
previous_ratings = st.session_state.previous_ratings.get(
|
| 36 |
+
st.session_state.data.iloc[current_index]["config_id"], {}
|
| 37 |
+
)
|
| 38 |
+
previous_ratings = previous_ratings.get(
|
| 39 |
+
model_name, None
|
| 40 |
+
) # Fix: Model key from session state
|
| 41 |
elif len(st.session_state.responses) <= current_index:
|
| 42 |
previous_ratings = {}
|
|
|
|
| 43 |
else:
|
|
|
|
| 44 |
response_from_session = st.session_state.responses[current_index]
|
| 45 |
+
try:
|
| 46 |
+
previous_ratings = response_from_session.model_ratings.get(model_name, {})
|
| 47 |
+
except AttributeError:
|
| 48 |
+
previous_ratings = response_from_session["model_ratings"].get(model_name, {})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
stored_query_ratings = {}
|
|
|
|
| 51 |
if previous_ratings:
|
| 52 |
if "query_v" in query_key:
|
| 53 |
try:
|
|
|
|
| 64 |
stored_query_ratings = previous_ratings.query_p1_ratings
|
| 65 |
except AttributeError:
|
| 66 |
stored_query_ratings = previous_ratings["query_p1_ratings"]
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
return stored_query_ratings if stored_query_ratings else {}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def render_single_rating(
|
| 72 |
+
label,
|
| 73 |
+
options,
|
| 74 |
+
format_func,
|
| 75 |
+
key_prefix,
|
| 76 |
+
stored_rating,
|
| 77 |
+
col,
|
| 78 |
+
):
|
| 79 |
+
"""Renders a single rating widget (radio)."""
|
| 80 |
+
with col:
|
| 81 |
+
return st.radio(
|
| 82 |
+
label,
|
| 83 |
+
options=options,
|
| 84 |
+
format_func=format_func,
|
| 85 |
+
key=f"{key_prefix}",
|
| 86 |
+
index=stored_rating if stored_rating is not None else 0,
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
|
| 90 |
+
def render_query_ratings(
|
| 91 |
+
model_name,
|
| 92 |
+
query_label,
|
| 93 |
+
config,
|
| 94 |
+
query_key,
|
| 95 |
+
current_index,
|
| 96 |
+
has_persona_alignment=False,
|
| 97 |
+
):
|
| 98 |
+
"""Helper function to render ratings for a given query."""
|
| 99 |
+
stored_query_ratings = get_previous_ratings(model_name, query_key, current_index)
|
| 100 |
+
stored_relevance = stored_query_ratings.get("relevance", 0)
|
| 101 |
+
stored_clarity = stored_query_ratings.get("clarity", 0)
|
| 102 |
+
stored_persona_alignment = (
|
| 103 |
+
stored_query_ratings.get("persona_alignment", 0) if has_persona_alignment else 0
|
| 104 |
+
)
|
| 105 |
if model_name == "gemini":
|
| 106 |
bg_color = "#e0f7fa"
|
| 107 |
else:
|
| 108 |
bg_color = "#f0f4c3"
|
| 109 |
+
|
| 110 |
with st.container():
|
| 111 |
+
st.markdown(
|
| 112 |
+
f"""
|
| 113 |
<div style="background-color:{bg_color}; padding:1rem;">
|
| 114 |
+
<h3 style="color:blue;">{query_label} </h3>
|
| 115 |
+
<p style="text-align:left;">{config[model_name + "_" + query_key]}</p>
|
| 116 |
</div>
|
| 117 |
+
""",
|
| 118 |
+
unsafe_allow_html=True,
|
| 119 |
+
)
|
| 120 |
+
cols = st.columns(3)
|
| 121 |
options = [0, 1, 2, 3, 4]
|
| 122 |
|
| 123 |
persona_alignment_rating = None
|
| 124 |
if has_persona_alignment:
|
| 125 |
+
persona_alignment_rating = render_single_rating(
|
| 126 |
+
"Persona Alignment:",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
options,
|
| 128 |
+
lambda x: ["N/A", "Not Aligned", "Partially Aligned", "Aligned", "Unclear"][
|
| 129 |
+
x
|
| 130 |
+
],
|
| 131 |
+
f"rating_{model_name}{query_key}_persona_alignment_",
|
| 132 |
+
stored_persona_alignment,
|
| 133 |
+
cols[0],
|
| 134 |
)
|
| 135 |
+
|
| 136 |
+
relevance_rating = render_single_rating(
|
| 137 |
+
"Relevance:",
|
| 138 |
+
options,
|
| 139 |
+
lambda x: ["N/A", "Not Relevant", "Somewhat Relevant", "Relevant", "Unclear"][
|
| 140 |
+
x
|
| 141 |
+
],
|
| 142 |
+
f"rating_{model_name}{query_key}_relevance_",
|
| 143 |
+
stored_relevance,
|
| 144 |
+
cols[1],
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
clarity_rating = render_single_rating(
|
| 148 |
+
"Clarity:",
|
| 149 |
+
[0, 1, 2, 3],
|
| 150 |
+
lambda x: ["N/A", "Not Clear", "Somewhat Clear", "Very Clear"][x],
|
| 151 |
+
f"rating_{model_name}{query_key}_clarity_",
|
| 152 |
+
stored_clarity,
|
| 153 |
+
cols[2],
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
return {
|
| 157 |
"clarity": clarity_rating,
|
| 158 |
"relevance": relevance_rating,
|
| 159 |
+
"persona_alignment": persona_alignment_rating if has_persona_alignment else None,
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
def display_ratings_row(model_name, config, current_index):
|
| 164 |
+
st.markdown(f"## {model_name.capitalize()} Ratings")
|
| 165 |
+
|
| 166 |
+
cols = st.columns(3)
|
| 167 |
+
with cols[0]:
|
| 168 |
+
query_v_ratings = render_query_ratings(
|
| 169 |
+
model_name,
|
| 170 |
+
"Query_v",
|
| 171 |
+
config,
|
| 172 |
+
"query_v",
|
| 173 |
+
current_index,
|
| 174 |
+
has_persona_alignment=False,
|
| 175 |
+
)
|
| 176 |
+
with cols[1]:
|
| 177 |
+
query_p0_ratings = render_query_ratings(
|
| 178 |
+
model_name,
|
| 179 |
+
"Query_p0",
|
| 180 |
+
config,
|
| 181 |
+
"query_p0",
|
| 182 |
+
current_index,
|
| 183 |
+
has_persona_alignment=True,
|
| 184 |
+
)
|
| 185 |
+
with cols[2]:
|
| 186 |
+
query_p1_ratings = render_query_ratings(
|
| 187 |
+
model_name,
|
| 188 |
+
"Query_p1",
|
| 189 |
+
config,
|
| 190 |
+
"query_p1",
|
| 191 |
+
current_index,
|
| 192 |
+
has_persona_alignment=True,
|
| 193 |
+
)
|
| 194 |
+
if "persona_alignment" in query_v_ratings:
|
| 195 |
+
query_v_ratings.pop("persona_alignment")
|
| 196 |
+
return {
|
| 197 |
+
"query_v_ratings": query_v_ratings,
|
| 198 |
+
"query_p0_ratings": query_p0_ratings,
|
| 199 |
+
"query_p1_ratings": query_p1_ratings,
|
| 200 |
}
|
| 201 |
|
| 202 |
|
| 203 |
def questions_screen(data):
|
| 204 |
+
"""Display the questions screen with split layout."""
|
| 205 |
current_index = st.session_state.current_index
|
| 206 |
+
|
| 207 |
try:
|
| 208 |
config = data.iloc[current_index]
|
| 209 |
|
|
|
|
| 216 |
# Context information
|
| 217 |
st.markdown("### Context Information")
|
| 218 |
with st.expander("Persona", expanded=True):
|
| 219 |
+
st.write(config["persona"])
|
| 220 |
with st.expander("Filters & Cities", expanded=True):
|
| 221 |
+
st.write("**Filters:**", config["filters"])
|
| 222 |
+
st.write("**Cities:**", config["city"])
|
| 223 |
with st.expander("Full Context", expanded=False):
|
| 224 |
+
st.text_area("", config["context"], height=300, disabled=False)
|
| 225 |
|
| 226 |
g_ratings = display_ratings_row("gemini", config, current_index)
|
| 227 |
l_ratings = display_ratings_row("llama", config, current_index)
|
|
|
|
| 242 |
query_v_ratings=l_ratings["query_v_ratings"],
|
| 243 |
query_p0_ratings=l_ratings["query_p0_ratings"],
|
| 244 |
query_p1_ratings=l_ratings["query_p1_ratings"],
|
| 245 |
+
),
|
| 246 |
},
|
| 247 |
comment=comment,
|
| 248 |
+
timestamp=datetime.now().isoformat(),
|
| 249 |
)
|
|
|
|
| 250 |
try:
|
| 251 |
st.session_state.ratings[current_index] = response["model_ratings"]
|
| 252 |
except TypeError:
|
|
|
|
| 260 |
navigation_buttons(data, response)
|
| 261 |
except IndexError:
|
| 262 |
print("Survey completed!")
|
| 263 |
+
|