Spaces:
Sleeping
Sleeping
Commit
·
da0e039
1
Parent(s):
2826168
add following features: update dropdowns after each rank submission, save the results at the end of the form in the dataset hub
Browse files- app.py +35 -18
- src/utils.py +4 -32
app.py
CHANGED
|
@@ -2,15 +2,18 @@ import gradio as gr
|
|
| 2 |
import yaml
|
| 3 |
import random
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
from src.utils import load_words,
|
| 7 |
from src.style import css
|
| 8 |
from src.user import UserID
|
| 9 |
|
| 10 |
def main():
|
| 11 |
config = yaml.safe_load(open("config/config.yaml"))
|
| 12 |
words = ['grad-cam', 'lime', 'sidu', 'rise']
|
| 13 |
-
options = ['1', '2', '3', '4']
|
| 14 |
class_names = config['dataset'][config['dataset']['name']]['class_names']
|
| 15 |
data_dir = os.path.join(config['dataset']['path'], config['dataset']['name'])
|
| 16 |
|
|
@@ -22,11 +25,11 @@ def main():
|
|
| 22 |
answers = gr.State([])
|
| 23 |
|
| 24 |
with gr.Row():
|
| 25 |
-
target_img_label = gr.Markdown(f"
|
| 26 |
-
gr.Markdown("
|
| 27 |
-
gr.Markdown("
|
| 28 |
-
gr.Markdown("
|
| 29 |
-
gr.Markdown("
|
| 30 |
|
| 31 |
with gr.Row():
|
| 32 |
count = user_state if isinstance(user_state, int) else user_state.value
|
|
@@ -115,19 +118,34 @@ def main():
|
|
| 115 |
submit_button = gr.Button("Submit", visible=(count != max_images-1))
|
| 116 |
return submit_button, finish_button
|
| 117 |
|
| 118 |
-
def update_dropdowns(
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
return
|
| 124 |
|
| 125 |
def redirect():
|
| 126 |
pass
|
| 127 |
|
| 128 |
-
def
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
def add_answer(dropdown1,dropdown2,dropdown3,dropdown4, answers):
|
| 133 |
rank = [dropdown1,dropdown2,dropdown3,dropdown4]
|
|
@@ -156,14 +174,13 @@ def main():
|
|
| 156 |
outputs={target_img, saliency_gradcam, saliency_lime, saliency_sidu, saliency_rise, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16},
|
| 157 |
).then(
|
| 158 |
update_dropdowns,
|
| 159 |
-
inputs={dropdown1, dropdown2, dropdown3, dropdown4},
|
| 160 |
outputs={dropdown1, dropdown2, dropdown3, dropdown4}
|
| 161 |
)
|
| 162 |
|
| 163 |
finish_button.click(
|
| 164 |
add_answer, inputs=[dropdown1, dropdown2, dropdown3, dropdown4, answers],outputs=answers
|
| 165 |
).then(
|
| 166 |
-
|
| 167 |
).then(
|
| 168 |
redirect, js="window.location = 'https://marcoparola.github.io/saliency-evaluation-app/end'")
|
| 169 |
|
|
|
|
| 2 |
import yaml
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
+
import json
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from huggingface_hub import CommitScheduler
|
| 8 |
|
| 9 |
+
from src.utils import load_words, load_image_and_saliency, load_example_images
|
| 10 |
from src.style import css
|
| 11 |
from src.user import UserID
|
| 12 |
|
| 13 |
def main():
|
| 14 |
config = yaml.safe_load(open("config/config.yaml"))
|
| 15 |
words = ['grad-cam', 'lime', 'sidu', 'rise']
|
| 16 |
+
options = ['-', '1', '2', '3', '4']
|
| 17 |
class_names = config['dataset'][config['dataset']['name']]['class_names']
|
| 18 |
data_dir = os.path.join(config['dataset']['path'], config['dataset']['name'])
|
| 19 |
|
|
|
|
| 25 |
answers = gr.State([])
|
| 26 |
|
| 27 |
with gr.Row():
|
| 28 |
+
target_img_label = gr.Markdown(f"Target image: **{class_names[user_state.value]}**")
|
| 29 |
+
gr.Markdown("Grad-cam")
|
| 30 |
+
gr.Markdown("Lime")
|
| 31 |
+
gr.Markdown("Sidu")
|
| 32 |
+
gr.Markdown("Rise")
|
| 33 |
|
| 34 |
with gr.Row():
|
| 35 |
count = user_state if isinstance(user_state, int) else user_state.value
|
|
|
|
| 118 |
submit_button = gr.Button("Submit", visible=(count != max_images-1))
|
| 119 |
return submit_button, finish_button
|
| 120 |
|
| 121 |
+
def update_dropdowns():
|
| 122 |
+
dp1 = gr.Dropdown(choices=options, value=options[0], label="grad-cam")
|
| 123 |
+
dp2 = gr.Dropdown(choices=options, value=options[0], label="lime")
|
| 124 |
+
dp3 = gr.Dropdown(choices=options, value=options[0], label="sidu")
|
| 125 |
+
dp4 = gr.Dropdown(choices=options, value=options[0], label="rise")
|
| 126 |
+
return dp1, dp2, dp3, dp4
|
| 127 |
|
| 128 |
def redirect():
|
| 129 |
pass
|
| 130 |
|
| 131 |
+
def save_results(answers):
|
| 132 |
+
json_file_results = config['results']['exp1_dir']
|
| 133 |
+
JSON_DATASET_DIR = Path("json_dataset")
|
| 134 |
+
JSON_DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
| 135 |
+
JSON_DATASET_PATH = JSON_DATASET_DIR / json_file_results
|
| 136 |
+
scheduler = CommitScheduler(
|
| 137 |
+
repo_id="results",
|
| 138 |
+
repo_type="dataset",
|
| 139 |
+
folder_path=JSON_DATASET_DIR,
|
| 140 |
+
path_in_repo="data",
|
| 141 |
+
)
|
| 142 |
+
with scheduler.lock:
|
| 143 |
+
user_id = time.time()
|
| 144 |
+
with JSON_DATASET_PATH.open("a") as f:
|
| 145 |
+
json.dump({
|
| 146 |
+
"user_id": user_id,
|
| 147 |
+
"answer": {i: answer[i] for i in range(len(answer))}
|
| 148 |
+
}, f)
|
| 149 |
|
| 150 |
def add_answer(dropdown1,dropdown2,dropdown3,dropdown4, answers):
|
| 151 |
rank = [dropdown1,dropdown2,dropdown3,dropdown4]
|
|
|
|
| 174 |
outputs={target_img, saliency_gradcam, saliency_lime, saliency_sidu, saliency_rise, img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16},
|
| 175 |
).then(
|
| 176 |
update_dropdowns,
|
|
|
|
| 177 |
outputs={dropdown1, dropdown2, dropdown3, dropdown4}
|
| 178 |
)
|
| 179 |
|
| 180 |
finish_button.click(
|
| 181 |
add_answer, inputs=[dropdown1, dropdown2, dropdown3, dropdown4, answers],outputs=answers
|
| 182 |
).then(
|
| 183 |
+
save_results, inputs=answers
|
| 184 |
).then(
|
| 185 |
redirect, js="window.location = 'https://marcoparola.github.io/saliency-evaluation-app/end'")
|
| 186 |
|
src/utils.py
CHANGED
|
@@ -32,37 +32,9 @@ def load_words(idx):
|
|
| 32 |
words = [f"word_{idx}_{i}" for i in range(20)]
|
| 33 |
return words
|
| 34 |
|
| 35 |
-
# Function to save results and increment global variable
|
| 36 |
-
def save_results(experiment_dir, answers):
|
| 37 |
-
user_id = time.time()
|
| 38 |
-
folder = os.path.join(config['results']['save_dir'], experiment_dir, str(user_id))
|
| 39 |
-
if not os.path.exists(folder):
|
| 40 |
-
os.makedirs(folder)
|
| 41 |
|
| 42 |
-
# answers is a list of lists
|
| 43 |
-
for idx, answer in enumerate(answers):
|
| 44 |
-
filename = os.path.join(folder, f"results_{idx}.txt")
|
| 45 |
-
with open(filename, 'w') as f:
|
| 46 |
-
f.write("\n".join(answer))
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
if not token:
|
| 54 |
-
print("Token not found. Please login to Hugging Face.")
|
| 55 |
-
return
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
-
api.upload_file(
|
| 59 |
-
path_or_fileobj=filename,
|
| 60 |
-
path_in_repo=filename,
|
| 61 |
-
repo_id=config["repo_id"],
|
| 62 |
-
repo_type="space",
|
| 63 |
-
token=token,
|
| 64 |
-
commit_message="Add results.txt"
|
| 65 |
-
)
|
| 66 |
-
print(f"File {filename} uploaded successfully to {config['repo_id']}.")
|
| 67 |
-
except Exception as e:
|
| 68 |
-
print(f"Failed to upload file: {e}")
|
|
|
|
| 32 |
words = [f"word_{idx}_{i}" for i in range(20)]
|
| 33 |
return words
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|