Spaces:
Build error
Build error
the user selects the choice, then optionally the checkbox and then the Next button
Browse files
app.py
CHANGED
|
@@ -8,11 +8,9 @@ CSV_FILENAME = "user_selections.csv"
|
|
| 8 |
|
| 9 |
def assign_samples(csv_path):
|
| 10 |
df = pd.read_csv(csv_path)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
group_3 = df[(df["pool_pos"] == 3) & (~df["question_category"].str.endswith("_B"))].head(10)
|
| 15 |
-
|
| 16 |
return {
|
| 17 |
"Bernardo": group_1,
|
| 18 |
"Alessandro": group_1,
|
|
@@ -25,32 +23,32 @@ def assign_samples(csv_path):
|
|
| 25 |
}
|
| 26 |
|
| 27 |
def load_existing_annotations():
|
| 28 |
-
"""Load the existing annotations from the HF dataset."""
|
| 29 |
try:
|
| 30 |
file_path = hf_hub_download(HF_REPO, CSV_FILENAME, repo_type="dataset", token=st.secrets["HF_TOKEN"])
|
| 31 |
return pd.read_csv(file_path)
|
| 32 |
except Exception:
|
| 33 |
-
return pd.DataFrame(columns=["username", "id"])
|
| 34 |
|
| 35 |
-
# Load datasets
|
| 36 |
csv_file = "static/mc.csv"
|
| 37 |
assignments = assign_samples(csv_file)
|
| 38 |
existing_annotations = load_existing_annotations()
|
| 39 |
|
| 40 |
valid_users = list(assignments.keys())
|
| 41 |
|
| 42 |
-
# Initialize session state
|
| 43 |
if "username" not in st.session_state:
|
| 44 |
st.session_state.username = None
|
| 45 |
if "index" not in st.session_state:
|
| 46 |
st.session_state.index = 0
|
| 47 |
if "results" not in st.session_state:
|
| 48 |
st.session_state.results = []
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
def update_name():
|
| 51 |
-
"""Set username and reset index."""
|
| 52 |
st.session_state.username = st.session_state.selected_user
|
| 53 |
-
st.session_state.index = 0
|
| 54 |
|
| 55 |
if st.session_state.username is None:
|
| 56 |
with st.form("user_form"):
|
|
@@ -59,12 +57,10 @@ if st.session_state.username is None:
|
|
| 59 |
submit_button = st.form_submit_button("Start", on_click=update_name)
|
| 60 |
st.stop()
|
| 61 |
|
| 62 |
-
# Get assigned dataset and remove already labeled samples
|
| 63 |
full_dataset = assignments[st.session_state.username].reset_index(drop=True)
|
| 64 |
user_labeled_ids = existing_annotations[existing_annotations["username"] == st.session_state.username]["id"].tolist()
|
| 65 |
dataset = full_dataset[~full_dataset["id"].isin(user_labeled_ids)].reset_index(drop=True)
|
| 66 |
|
| 67 |
-
# If all samples are labeled, stop execution
|
| 68 |
if dataset.empty:
|
| 69 |
st.write("### Great! You have completed your assignment. ๐")
|
| 70 |
st.stop()
|
|
@@ -78,7 +74,11 @@ def push_to_hf_hub(csv_path):
|
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Error pushing to HF: {e}")
|
| 80 |
|
| 81 |
-
def save_choice(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
sample = dataset.iloc[st.session_state.index]
|
| 83 |
st.session_state.results.append({
|
| 84 |
"username": st.session_state.username,
|
|
@@ -86,15 +86,16 @@ def save_choice(choice_index):
|
|
| 86 |
"video_id": sample["video_id"],
|
| 87 |
"answer1": sample["answer1"],
|
| 88 |
"answer2": sample["answer2"],
|
| 89 |
-
"selected_answer":
|
| 90 |
"target": sample["target"],
|
| 91 |
-
"not_enough_info": not_enough_info
|
| 92 |
})
|
| 93 |
|
| 94 |
st.session_state.index += 1
|
| 95 |
-
st.session_state.
|
|
|
|
| 96 |
|
| 97 |
-
if st.session_state.index >= len(dataset):
|
| 98 |
st.write("### Great! You have completed your assignment. ๐")
|
| 99 |
result_df = pd.DataFrame(st.session_state.results)
|
| 100 |
csv_path = "user_selections.csv"
|
|
@@ -107,43 +108,37 @@ def save_choice(choice_index):
|
|
| 107 |
st.stop()
|
| 108 |
return
|
| 109 |
|
| 110 |
-
# Select the current sample
|
| 111 |
sample = dataset.iloc[st.session_state.index]
|
| 112 |
|
| 113 |
-
# Title
|
| 114 |
st.markdown("<h1 style='text-align: center; font-size: 50px;'>MAIA Sample</h1>", unsafe_allow_html=True)
|
| 115 |
-
|
| 116 |
-
# Centered user name
|
| 117 |
st.markdown(f"<h3 style='text-align: center;'>User: {st.session_state.username}</h3>", unsafe_allow_html=True)
|
|
|
|
| 118 |
|
| 119 |
-
st.write("\n\n") # Add empty space
|
| 120 |
-
|
| 121 |
-
# Instructions
|
| 122 |
st.markdown("""
|
| 123 |
### Instructions:
|
| 124 |
- Look at the video thumbnail, do not play it!
|
| 125 |
-
- Select
|
| 126 |
-
-
|
|
|
|
| 127 |
""")
|
|
|
|
| 128 |
|
| 129 |
-
st.write("---") # Adds a horizontal divider for better separation
|
| 130 |
-
|
| 131 |
-
# Display Video
|
| 132 |
st.video(sample["video_url"])
|
| 133 |
|
| 134 |
-
# Question Prompt
|
| 135 |
st.markdown("<h4 style='text-align: center; margin-top: 20px;'>Scegli la descrizione corretta tra A e B</h4>", unsafe_allow_html=True)
|
| 136 |
|
| 137 |
-
# Checkbox for uncertainty
|
| 138 |
-
st.markdown("<div style='text-align: center;'>", unsafe_allow_html=True)
|
| 139 |
-
not_enough_info = st.checkbox("The frame does not provide enough information to answer the question.", key='checkbox')
|
| 140 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 141 |
-
|
| 142 |
-
st.write("\n") # Add spacing before buttons
|
| 143 |
-
|
| 144 |
-
# Buttons (Centered)
|
| 145 |
col1, col2 = st.columns(2)
|
| 146 |
with col1:
|
| 147 |
-
st.button(f"A: {sample.get('answer1', 'No answer1 available')}"
|
|
|
|
| 148 |
with col2:
|
| 149 |
-
st.button(f"B: {sample.get('answer2', 'No answer2 available')}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def assign_samples(csv_path):
|
| 10 |
df = pd.read_csv(csv_path)
|
| 11 |
+
group_1 = df[(df["pool_pos"] == 1) & (~df["question_category"].str.endswith("_B"))].head(5)
|
| 12 |
+
group_2 = df[(df["pool_pos"] == 2) & (~df["question_category"].str.endswith("_B"))].head(5)
|
| 13 |
+
group_3 = df[(df["pool_pos"] == 3) & (~df["question_category"].str.endswith("_B"))].head(5)
|
|
|
|
|
|
|
| 14 |
return {
|
| 15 |
"Bernardo": group_1,
|
| 16 |
"Alessandro": group_1,
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
def load_existing_annotations():
|
|
|
|
| 26 |
try:
|
| 27 |
file_path = hf_hub_download(HF_REPO, CSV_FILENAME, repo_type="dataset", token=st.secrets["HF_TOKEN"])
|
| 28 |
return pd.read_csv(file_path)
|
| 29 |
except Exception:
|
| 30 |
+
return pd.DataFrame(columns=["username", "id"])
|
| 31 |
|
|
|
|
| 32 |
csv_file = "static/mc.csv"
|
| 33 |
assignments = assign_samples(csv_file)
|
| 34 |
existing_annotations = load_existing_annotations()
|
| 35 |
|
| 36 |
valid_users = list(assignments.keys())
|
| 37 |
|
|
|
|
| 38 |
if "username" not in st.session_state:
|
| 39 |
st.session_state.username = None
|
| 40 |
if "index" not in st.session_state:
|
| 41 |
st.session_state.index = 0
|
| 42 |
if "results" not in st.session_state:
|
| 43 |
st.session_state.results = []
|
| 44 |
+
if "selected_answer" not in st.session_state:
|
| 45 |
+
st.session_state.selected_answer = None
|
| 46 |
+
if "not_enough_info" not in st.session_state:
|
| 47 |
+
st.session_state.not_enough_info = False
|
| 48 |
|
| 49 |
def update_name():
|
|
|
|
| 50 |
st.session_state.username = st.session_state.selected_user
|
| 51 |
+
st.session_state.index = 0
|
| 52 |
|
| 53 |
if st.session_state.username is None:
|
| 54 |
with st.form("user_form"):
|
|
|
|
| 57 |
submit_button = st.form_submit_button("Start", on_click=update_name)
|
| 58 |
st.stop()
|
| 59 |
|
|
|
|
| 60 |
full_dataset = assignments[st.session_state.username].reset_index(drop=True)
|
| 61 |
user_labeled_ids = existing_annotations[existing_annotations["username"] == st.session_state.username]["id"].tolist()
|
| 62 |
dataset = full_dataset[~full_dataset["id"].isin(user_labeled_ids)].reset_index(drop=True)
|
| 63 |
|
|
|
|
| 64 |
if dataset.empty:
|
| 65 |
st.write("### Great! You have completed your assignment. ๐")
|
| 66 |
st.stop()
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
print(f"Error pushing to HF: {e}")
|
| 76 |
|
| 77 |
+
def save_choice():
|
| 78 |
+
if st.session_state.selected_answer is None:
|
| 79 |
+
st.warning("Please select an answer before proceeding.")
|
| 80 |
+
return
|
| 81 |
+
|
| 82 |
sample = dataset.iloc[st.session_state.index]
|
| 83 |
st.session_state.results.append({
|
| 84 |
"username": st.session_state.username,
|
|
|
|
| 86 |
"video_id": sample["video_id"],
|
| 87 |
"answer1": sample["answer1"],
|
| 88 |
"answer2": sample["answer2"],
|
| 89 |
+
"selected_answer": st.session_state.selected_answer,
|
| 90 |
"target": sample["target"],
|
| 91 |
+
"not_enough_info": st.session_state.not_enough_info
|
| 92 |
})
|
| 93 |
|
| 94 |
st.session_state.index += 1
|
| 95 |
+
st.session_state.selected_answer = None
|
| 96 |
+
st.session_state.not_enough_info = False
|
| 97 |
|
| 98 |
+
if st.session_state.index >= len(dataset):
|
| 99 |
st.write("### Great! You have completed your assignment. ๐")
|
| 100 |
result_df = pd.DataFrame(st.session_state.results)
|
| 101 |
csv_path = "user_selections.csv"
|
|
|
|
| 108 |
st.stop()
|
| 109 |
return
|
| 110 |
|
|
|
|
| 111 |
sample = dataset.iloc[st.session_state.index]
|
| 112 |
|
|
|
|
| 113 |
st.markdown("<h1 style='text-align: center; font-size: 50px;'>MAIA Sample</h1>", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 114 |
st.markdown(f"<h3 style='text-align: center;'>User: {st.session_state.username}</h3>", unsafe_allow_html=True)
|
| 115 |
+
st.write("\n\n")
|
| 116 |
|
|
|
|
|
|
|
|
|
|
| 117 |
st.markdown("""
|
| 118 |
### Instructions:
|
| 119 |
- Look at the video thumbnail, do not play it!
|
| 120 |
+
- Select one of the two answers.
|
| 121 |
+
- If you think the frame does not provide enough information, select the checkbox.
|
| 122 |
+
- Click 'Next' to proceed.
|
| 123 |
""")
|
| 124 |
+
st.write("---")
|
| 125 |
|
|
|
|
|
|
|
|
|
|
| 126 |
st.video(sample["video_url"])
|
| 127 |
|
|
|
|
| 128 |
st.markdown("<h4 style='text-align: center; margin-top: 20px;'>Scegli la descrizione corretta tra A e B</h4>", unsafe_allow_html=True)
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
col1, col2 = st.columns(2)
|
| 131 |
with col1:
|
| 132 |
+
if st.button(f"A: {sample.get('answer1', 'No answer1 available')}"):
|
| 133 |
+
st.session_state.selected_answer = 0
|
| 134 |
with col2:
|
| 135 |
+
if st.button(f"B: {sample.get('answer2', 'No answer2 available')}"):
|
| 136 |
+
st.session_state.selected_answer = 1
|
| 137 |
+
|
| 138 |
+
st.markdown("<div style='text-align: center;'>", unsafe_allow_html=True)
|
| 139 |
+
st.session_state.not_enough_info = st.checkbox("The frame does not provide enough information to answer the question.")
|
| 140 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 141 |
+
st.write("\n")
|
| 142 |
+
|
| 143 |
+
if st.button("Next"):
|
| 144 |
+
save_choice()
|