initial test
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ import io
|
|
| 15 |
# --- !!! NEW: DEBUG/TESTING MODE !!! ---
|
| 16 |
# Set to True to use local CSV files instead of Hugging Face Hub
|
| 17 |
# This will read from PREDICTIONS_CSV and read/write to LOCAL_DATASET_PATH
|
| 18 |
-
DEBUG_TESTING =
|
| 19 |
LOCAL_DATASET_PATH = "policy_evaluations.csv"
|
| 20 |
PREDICTIONS_CSV = "model_predictions.csv" # From batch_inference.py
|
| 21 |
# --- End Debug Config ---
|
|
@@ -27,13 +27,18 @@ token = 'pQQADyqfDNewBCejvPmyMGlzpdgqDFSAFE'
|
|
| 27 |
HF_DATASET_REPO = "kaburia/policy-evaluations" # Your HF Dataset repo
|
| 28 |
HF_TOKEN = HF + '_' + token
|
| 29 |
|
|
|
|
| 30 |
# --- Email Authentication ---
|
| 31 |
APPROVED_EMAILS = {
|
| 32 |
-
"
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}
|
| 38 |
|
| 39 |
# --- Define Interaction Choices ---
|
|
@@ -56,16 +61,21 @@ def load_data_from_hub(token):
|
|
| 56 |
return None, None, "Error: Hugging Face Token is not configured."
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
# Load the dataset
|
| 60 |
ds = load_dataset(HF_DATASET_REPO, token=token, split="train", cache_dir="./cache")
|
| 61 |
full_df = ds.to_pandas()
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Create a unique key
|
| 68 |
-
full_df['key'] = full_df['PolicyA'] + '||' + full_df['PolicyB']
|
| 69 |
|
| 70 |
# Find rows that have NOT been annotated
|
| 71 |
pending_df = full_df[full_df['UserVerifiedClass'].isnull()].reset_index(drop=True)
|
|
@@ -102,8 +112,9 @@ def load_data_from_local():
|
|
| 102 |
# Load the (now existing) local file
|
| 103 |
full_df = pd.read_csv(LOCAL_DATASET_PATH)
|
| 104 |
|
| 105 |
-
# Ensure columns are present
|
| 106 |
-
|
|
|
|
| 107 |
if col not in full_df.columns:
|
| 108 |
full_df[col] = pd.NA
|
| 109 |
|
|
@@ -136,11 +147,23 @@ def save_annotation_to_hub(index, verified_class, drill_down, user_tag, token, f
|
|
| 136 |
full_df.loc[full_df['key'] == current_key, 'DrillDownInteraction'] = drill_down
|
| 137 |
full_df.loc[full_df['key'] == current_key, 'AnnotatorUsername'] = user_tag
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
# 4.
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
save_status = f"Saved to Hub: {verified_class} | {drill_down} by {user_tag}"
|
| 146 |
|
|
@@ -193,11 +216,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 193 |
gr.Markdown(
|
| 194 |
"""
|
| 195 |
Welcome! This tool is for human-in-the-loop annotation.
|
| 196 |
-
1. Log in with your
|
| 197 |
2. The model's prediction for two policies will be shown.
|
| 198 |
3. **Step 1:** Verify if the model's 3-class prediction (neutral, coherent, incoherent) is correct, or change it.
|
| 199 |
-
4. **Step 2:** Based on your verified choice, select a 7-class drill-down label.
|
| 200 |
-
For example if it is incoherent, we shall ask to choose from "-1 Constraining", "-2 Counteracting", "-3 Cancelling"
|
| 201 |
5. Click 'Save & Next' to submit your annotation and load the next item.
|
| 202 |
|
| 203 |
---
|
|
|
|
| 15 |
# --- !!! NEW: DEBUG/TESTING MODE !!! ---
|
| 16 |
# Set to True to use local CSV files instead of Hugging Face Hub
|
| 17 |
# This will read from PREDICTIONS_CSV and read/write to LOCAL_DATASET_PATH
|
| 18 |
+
DEBUG_TESTING = False
|
| 19 |
LOCAL_DATASET_PATH = "policy_evaluations.csv"
|
| 20 |
PREDICTIONS_CSV = "model_predictions.csv" # From batch_inference.py
|
| 21 |
# --- End Debug Config ---
|
|
|
|
| 27 |
HF_DATASET_REPO = "kaburia/policy-evaluations" # Your HF Dataset repo
|
| 28 |
HF_TOKEN = HF + '_' + token
|
| 29 |
|
| 30 |
+
|
| 31 |
# --- Email Authentication ---
|
| 32 |
APPROVED_EMAILS = {
|
| 33 |
+
"kaburiaaustin1@tahmo.org": "user1",
|
| 34 |
+
"E.Ramos@tudelft.nl" : "user2",
|
| 35 |
+
"eunice.pramos@gmail.com" : "user3",
|
| 36 |
+
"E.Abraham@tudelft.nl" : "user4",
|
| 37 |
+
"dene.abv@gmail.com" : "user5",
|
| 38 |
+
"rafatoufofana.abv@gmail.com" : "user6",
|
| 39 |
+
"annorfrank@tahmo.org" : "user7",
|
| 40 |
+
"n.marley@tahmo.org" : "user8",
|
| 41 |
+
"H.F.Hagenaars@tudelft.nl" : "user9",
|
| 42 |
}
|
| 43 |
|
| 44 |
# --- Define Interaction Choices ---
|
|
|
|
| 61 |
return None, None, "Error: Hugging Face Token is not configured."
|
| 62 |
|
| 63 |
try:
|
| 64 |
+
# Load the dataset (which may be policy_evaluations.csv)
|
| 65 |
ds = load_dataset(HF_DATASET_REPO, token=token, split="train", cache_dir="./cache")
|
| 66 |
full_df = ds.to_pandas()
|
| 67 |
|
| 68 |
+
# --- NEW LOGIC ---
|
| 69 |
+
# Check for annotation columns and add them if they don't exist
|
| 70 |
+
new_cols = ["UserVerifiedClass", "DrillDownInteraction", "AnnotatorUsername"]
|
| 71 |
+
for col in new_cols:
|
| 72 |
+
if col not in full_df.columns:
|
| 73 |
+
print(f"Adding missing column to DataFrame: {col}")
|
| 74 |
+
full_df[col] = pd.NA
|
| 75 |
+
# --- END NEW LOGIC ---
|
| 76 |
|
| 77 |
# Create a unique key
|
| 78 |
+
full_df['key'] = full_df['PolicyA'].astype(str) + '||' + full_df['PolicyB'].astype(str)
|
| 79 |
|
| 80 |
# Find rows that have NOT been annotated
|
| 81 |
pending_df = full_df[full_df['UserVerifiedClass'].isnull()].reset_index(drop=True)
|
|
|
|
| 112 |
# Load the (now existing) local file
|
| 113 |
full_df = pd.read_csv(LOCAL_DATASET_PATH)
|
| 114 |
|
| 115 |
+
# Ensure columns are present (for existing local files)
|
| 116 |
+
new_cols = ["UserVerifiedClass", "DrillDownInteraction", "AnnotatorUsername"]
|
| 117 |
+
for col in new_cols:
|
| 118 |
if col not in full_df.columns:
|
| 119 |
full_df[col] = pd.NA
|
| 120 |
|
|
|
|
| 147 |
full_df.loc[full_df['key'] == current_key, 'DrillDownInteraction'] = drill_down
|
| 148 |
full_df.loc[full_df['key'] == current_key, 'AnnotatorUsername'] = user_tag
|
| 149 |
|
| 150 |
+
# --- NEW SAVE LOGIC ---
|
| 151 |
+
# 3. Convert back to CSV format in memory
|
| 152 |
+
csv_buffer = io.StringIO()
|
| 153 |
+
# Drop the temporary 'key' column before saving
|
| 154 |
+
full_df.drop(columns=['key']).to_csv(csv_buffer, index=False)
|
| 155 |
+
csv_content_bytes = csv_buffer.getvalue().encode('utf-8')
|
| 156 |
|
| 157 |
+
# 4. Upload using HfApi to overwrite the specific file
|
| 158 |
+
api = HfApi()
|
| 159 |
+
api.upload_file(
|
| 160 |
+
path_or_fileobj=io.BytesIO(csv_content_bytes),
|
| 161 |
+
path_in_repo="policy_evaluations.csv", # Explicitly overwrite this file
|
| 162 |
+
repo_id=HF_DATASET_REPO,
|
| 163 |
+
token=token,
|
| 164 |
+
repo_type="dataset"
|
| 165 |
+
)
|
| 166 |
+
# --- END NEW SAVE LOGIC ---
|
| 167 |
|
| 168 |
save_status = f"Saved to Hub: {verified_class} | {drill_down} by {user_tag}"
|
| 169 |
|
|
|
|
| 216 |
gr.Markdown(
|
| 217 |
"""
|
| 218 |
Welcome! This tool is for human-in-the-loop annotation.
|
| 219 |
+
1. Log in with your authorized email.
|
| 220 |
2. The model's prediction for two policies will be shown.
|
| 221 |
3. **Step 1:** Verify if the model's 3-class prediction (neutral, coherent, incoherent) is correct, or change it.
|
| 222 |
+
4. **Step 2:** Based on your verified choice, select a 7-class drill-down label.
|
|
|
|
| 223 |
5. Click 'Save & Next' to submit your annotation and load the next item.
|
| 224 |
|
| 225 |
---
|