update: Enhance session state management for search results and reactions in the evaluation interface
Browse files
app.py
CHANGED
@@ -32,11 +32,15 @@ def save_reactions_to_dataset(user_type, query, results):
|
|
32 |
def main():
|
33 |
st.title("Semantic Text Retrieval Evaluation Interface")
|
34 |
|
35 |
-
# Initialize session state
|
|
|
|
|
|
|
|
|
36 |
if "reactions" not in st.session_state:
|
37 |
st.session_state.reactions = {}
|
38 |
-
if "
|
39 |
-
st.session_state.
|
40 |
|
41 |
# Select device
|
42 |
device = "cuda" if t.cuda.is_available() else "cpu"
|
@@ -74,41 +78,49 @@ def main():
|
|
74 |
# Get top 3 results
|
75 |
top_results_dot_product = t.topk(dot_scores, k=3)
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
st.subheader("Query Results")
|
78 |
st.write(f"Query: {query}")
|
79 |
|
80 |
-
|
81 |
-
st.session_state.results = []
|
82 |
-
|
83 |
-
for _, idx in zip(top_results_dot_product[0], top_results_dot_product[1]):
|
84 |
text = df.iloc[int(idx)]["ext"]
|
85 |
st.write(f"**Text:** {text}")
|
86 |
|
87 |
-
|
88 |
-
if
|
89 |
-
st.session_state.reactions[
|
90 |
|
91 |
-
# Display reaction options and update session state
|
92 |
reaction = st.radio(
|
93 |
label=f"Rate this result (Result {idx}):",
|
94 |
options=["π", "π€·", "π"],
|
95 |
-
index=["π", "π€·", "π"].index(st.session_state.reactions[
|
96 |
-
key=
|
97 |
horizontal=True
|
98 |
)
|
99 |
-
st.session_state.reactions[
|
100 |
-
|
101 |
-
# Append reaction for this result
|
102 |
-
st.session_state.results.append({"text": text, "reaction": st.session_state.reactions[f"reaction_{idx}"]})
|
103 |
|
104 |
-
# Save
|
105 |
if st.button("Save Reactions"):
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
st.success("Reactions saved successfully!")
|
108 |
|
109 |
-
#
|
|
|
|
|
110 |
st.session_state.reactions = {}
|
111 |
-
st.session_state.results = []
|
112 |
|
113 |
except Exception as e:
|
114 |
st.error(f"Failed to load database: {str(e)}")
|
|
|
32 |
def main():
|
33 |
st.title("Semantic Text Retrieval Evaluation Interface")
|
34 |
|
35 |
+
# Initialize session state variables
|
36 |
+
if "search_performed" not in st.session_state:
|
37 |
+
st.session_state.search_performed = False
|
38 |
+
if "top_results" not in st.session_state:
|
39 |
+
st.session_state.top_results = []
|
40 |
if "reactions" not in st.session_state:
|
41 |
st.session_state.reactions = {}
|
42 |
+
if "results_saved" not in st.session_state:
|
43 |
+
st.session_state.results_saved = False
|
44 |
|
45 |
# Select device
|
46 |
device = "cuda" if t.cuda.is_available() else "cpu"
|
|
|
78 |
# Get top 3 results
|
79 |
top_results_dot_product = t.topk(dot_scores, k=3)
|
80 |
|
81 |
+
# Store the top results indices in session_state
|
82 |
+
st.session_state.top_results = top_results_dot_product[1].tolist()
|
83 |
+
st.session_state.search_performed = True
|
84 |
+
|
85 |
+
# Display results and collect reactions
|
86 |
+
if st.session_state.search_performed and not st.session_state.results_saved:
|
87 |
st.subheader("Query Results")
|
88 |
st.write(f"Query: {query}")
|
89 |
|
90 |
+
for idx in st.session_state.top_results:
|
|
|
|
|
|
|
91 |
text = df.iloc[int(idx)]["ext"]
|
92 |
st.write(f"**Text:** {text}")
|
93 |
|
94 |
+
key = f"reaction_{idx}"
|
95 |
+
if key not in st.session_state.reactions:
|
96 |
+
st.session_state.reactions[key] = "π€·"
|
97 |
|
|
|
98 |
reaction = st.radio(
|
99 |
label=f"Rate this result (Result {idx}):",
|
100 |
options=["π", "π€·", "π"],
|
101 |
+
index=["π", "π€·", "π"].index(st.session_state.reactions[key]),
|
102 |
+
key=key,
|
103 |
horizontal=True
|
104 |
)
|
105 |
+
st.session_state.reactions[key] = reaction
|
|
|
|
|
|
|
106 |
|
107 |
+
# Save reactions when the button is clicked
|
108 |
if st.button("Save Reactions"):
|
109 |
+
# Collect the results to save
|
110 |
+
results = []
|
111 |
+
for idx in st.session_state.top_results:
|
112 |
+
key = f"reaction_{idx}"
|
113 |
+
results.append({
|
114 |
+
"text": df.iloc[int(idx)]["ext"],
|
115 |
+
"reaction": st.session_state.reactions[key]
|
116 |
+
})
|
117 |
+
save_reactions_to_dataset(user_type, query, results)
|
118 |
st.success("Reactions saved successfully!")
|
119 |
|
120 |
+
# Reset flags
|
121 |
+
st.session_state.search_performed = False
|
122 |
+
st.session_state.results_saved = True
|
123 |
st.session_state.reactions = {}
|
|
|
124 |
|
125 |
except Exception as e:
|
126 |
st.error(f"Failed to load database: {str(e)}")
|