Spaces:
Running
Running
fixing multiuser issue
Browse files- .gitignore +1 -0
- logic/__pycache__/data_utils.cpython-310.pyc +0 -0
- logic/__pycache__/handlers.cpython-310.pyc +0 -0
- logic/handlers.py +11 -10
- ui/__pycache__/layout.cpython-310.pyc +0 -0
- ui/__pycache__/layout.cpython-313.pyc +0 -0
- ui/__pycache__/main_page.cpython-310.pyc +0 -0
- ui/__pycache__/selection_page.cpython-310.pyc +0 -0
- ui/layout.py +41 -12
- ui/main_page.py +2 -25
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
**/__pycache__/**
|
logic/__pycache__/data_utils.cpython-310.pyc
DELETED
|
Binary file (5.74 kB)
|
|
|
logic/__pycache__/handlers.cpython-310.pyc
DELETED
|
Binary file (12.9 kB)
|
|
|
logic/handlers.py
CHANGED
|
@@ -62,7 +62,7 @@ def exit():
|
|
| 62 |
def validate_inputs(image, ori_img): # is_blurred
|
| 63 |
# Perform your validation logic here
|
| 64 |
if image is None:
|
| 65 |
-
return gr.Button("Submit", variant="primary", interactive=False), None, None, False
|
| 66 |
|
| 67 |
# Define maximum dimensions
|
| 68 |
MAX_WIDTH = 1024
|
|
@@ -177,7 +177,8 @@ def update_user_data(username, password, country, language_choice, HF_DATASET_NA
|
|
| 177 |
return gr.Dataset(samples=[]), None
|
| 178 |
|
| 179 |
|
| 180 |
-
def update_language(
|
|
|
|
| 181 |
# my_translator = GoogleTranslator(source='english', target=metadata_dict[country][language])
|
| 182 |
categories = concepts_dict[country][lang2eng_mapping.get(language, language)]
|
| 183 |
if language in words_mapping:
|
|
@@ -309,7 +310,7 @@ def handle_click_example(user_examples, concepts_dict):
|
|
| 309 |
# dropdown_values.append(None)
|
| 310 |
|
| 311 |
# Need to return values for each category dropdown
|
| 312 |
-
return [image_inp, image_url_inp, long_caption_inp, exampleid_btn, category_btn, concept_btn] + additional_concepts_by_category
|
| 313 |
|
| 314 |
|
| 315 |
def is_password_correct(hashed_password, entered_password):
|
|
@@ -435,13 +436,13 @@ def blur_selected_faces(image, blur_faces_ids, faces_info, face_img, faces_count
|
|
| 435 |
print(f"Face blurring time: {blurring_time:.4f} seconds")
|
| 436 |
|
| 437 |
if face_count == 0:
|
| 438 |
-
return image, Modal(visible=True), face_img, faces_count, blur_faces_ids
|
| 439 |
else:
|
| 440 |
-
return image, Modal(visible=False), None, None, gr.update(value=[])
|
| 441 |
|
| 442 |
def blur_all_faces(image):
|
| 443 |
if image is None:
|
| 444 |
-
return None, Modal(visible=False)
|
| 445 |
else:
|
| 446 |
# Base blur amount and bounds
|
| 447 |
MIN_BLUR = 31 # Minimum blur amount (must be odd)
|
|
@@ -511,17 +512,17 @@ def blur_all_faces(image):
|
|
| 511 |
print(f"Average time per face: {(total_time/max(1, face_count)):.4f} seconds")
|
| 512 |
|
| 513 |
if face_count == 0:
|
| 514 |
-
return image, Modal(visible=True)
|
| 515 |
else:
|
| 516 |
-
return output_image, Modal(visible=False)
|
| 517 |
|
| 518 |
def unhide_faces(img, ori_img): # is_blurred
|
| 519 |
if img is None:
|
| 520 |
-
return None
|
| 521 |
elif np.array_equal(img, ori_img.value):
|
| 522 |
return img # is_blurred
|
| 523 |
else:
|
| 524 |
-
return ori_img.value
|
| 525 |
|
| 526 |
def check_exclude_fn(image):
|
| 527 |
if image is None:
|
|
|
|
| 62 |
def validate_inputs(image, ori_img): # is_blurred
|
| 63 |
# Perform your validation logic here
|
| 64 |
if image is None:
|
| 65 |
+
return gr.Button("Submit", variant="primary", interactive=False), None, None, # False
|
| 66 |
|
| 67 |
# Define maximum dimensions
|
| 68 |
MAX_WIDTH = 1024
|
|
|
|
| 177 |
return gr.Dataset(samples=[]), None
|
| 178 |
|
| 179 |
|
| 180 |
+
def update_language(local_storage, metadata_dict, concepts_dict):
|
| 181 |
+
country, language, email, password, = local_storage
|
| 182 |
# my_translator = GoogleTranslator(source='english', target=metadata_dict[country][language])
|
| 183 |
categories = concepts_dict[country][lang2eng_mapping.get(language, language)]
|
| 184 |
if language in words_mapping:
|
|
|
|
| 310 |
# dropdown_values.append(None)
|
| 311 |
|
| 312 |
# Need to return values for each category dropdown
|
| 313 |
+
return [image_inp, image_url_inp, long_caption_inp, exampleid_btn, category_btn, concept_btn] + additional_concepts_by_category + [True]
|
| 314 |
|
| 315 |
|
| 316 |
def is_password_correct(hashed_password, entered_password):
|
|
|
|
| 436 |
print(f"Face blurring time: {blurring_time:.4f} seconds")
|
| 437 |
|
| 438 |
if face_count == 0:
|
| 439 |
+
return image, Modal(visible=True), face_img, faces_count, blur_faces_ids
|
| 440 |
else:
|
| 441 |
+
return image, Modal(visible=False), None, None, gr.update(value=[])
|
| 442 |
|
| 443 |
def blur_all_faces(image):
|
| 444 |
if image is None:
|
| 445 |
+
return None, Modal(visible=False)
|
| 446 |
else:
|
| 447 |
# Base blur amount and bounds
|
| 448 |
MIN_BLUR = 31 # Minimum blur amount (must be odd)
|
|
|
|
| 512 |
print(f"Average time per face: {(total_time/max(1, face_count)):.4f} seconds")
|
| 513 |
|
| 514 |
if face_count == 0:
|
| 515 |
+
return image, Modal(visible=True)
|
| 516 |
else:
|
| 517 |
+
return output_image, Modal(visible=False)
|
| 518 |
|
| 519 |
def unhide_faces(img, ori_img): # is_blurred
|
| 520 |
if img is None:
|
| 521 |
+
return None
|
| 522 |
elif np.array_equal(img, ori_img.value):
|
| 523 |
return img # is_blurred
|
| 524 |
else:
|
| 525 |
+
return ori_img.value
|
| 526 |
|
| 527 |
def check_exclude_fn(image):
|
| 528 |
if image is None:
|
ui/__pycache__/layout.cpython-310.pyc
DELETED
|
Binary file (7.66 kB)
|
|
|
ui/__pycache__/layout.cpython-313.pyc
DELETED
|
Binary file (12.4 kB)
|
|
|
ui/__pycache__/main_page.cpython-310.pyc
DELETED
|
Binary file (5.96 kB)
|
|
|
ui/__pycache__/selection_page.cpython-310.pyc
DELETED
|
Binary file (2 kB)
|
|
|
ui/layout.py
CHANGED
|
@@ -10,6 +10,25 @@ from functools import partial
|
|
| 10 |
from .selection_page import build_selection_page
|
| 11 |
from .main_page import build_main_page
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def switch_ui(country, language, username, password, flag=False, metadata_dict=None):
|
| 14 |
print(f"Language: {language}, Country: {country}")
|
| 15 |
|
|
@@ -37,7 +56,8 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 37 |
}
|
| 38 |
"""
|
| 39 |
with gr.Blocks(css=custom_css) as ui:
|
| 40 |
-
local_storage = gr.
|
|
|
|
| 41 |
# First page: selection
|
| 42 |
|
| 43 |
selection_page, country_choice, language_choice, proceed_btn, username, password, intro_markdown = build_selection_page(metadata_dict)
|
|
@@ -77,6 +97,15 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 77 |
cancel_exclude_btn = cmp_main_ui["cancel_exclude_btn"]
|
| 78 |
confirm_exclude_btn = cmp_main_ui["confirm_exclude_btn"]
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
# is_blurred = gr.State(False) # Initialize as False
|
| 81 |
|
| 82 |
# Notification image is false
|
|
@@ -186,7 +215,7 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 186 |
image_inp, image_url_inp, long_caption_inp, exampleid_btn,
|
| 187 |
category_btn, concept_btn,
|
| 188 |
category_concept_dropdowns[0], category_concept_dropdowns[1], category_concept_dropdowns[2],
|
| 189 |
-
category_concept_dropdowns[3], category_concept_dropdowns[4]
|
| 190 |
],
|
| 191 |
)
|
| 192 |
|
|
@@ -198,8 +227,12 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 198 |
# Submit Button Click events
|
| 199 |
|
| 200 |
proceed_btn.click(
|
| 201 |
-
fn=partial(
|
| 202 |
inputs=[country_choice, language_choice, username, password],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
outputs=[
|
| 204 |
country_inp, language_inp, username_inp, password_inp, category_btn, concept_btn, image_inp,
|
| 205 |
image_url_inp, long_caption_inp, intro_text_inp, intro_text_inst_inp, instruct_btn, clear_btn,
|
|
@@ -208,10 +241,6 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 208 |
category_concept_dropdowns[0], category_concept_dropdowns[1], category_concept_dropdowns[2],
|
| 209 |
category_concept_dropdowns[3], category_concept_dropdowns[4]
|
| 210 |
]
|
| 211 |
-
).then(
|
| 212 |
-
fn=partial(switch_ui, flag=False),
|
| 213 |
-
inputs=[country_choice, language_choice, username, password],
|
| 214 |
-
outputs=[local_storage, selection_page, main_ui_placeholder, browse_data_placeholder],
|
| 215 |
).then(
|
| 216 |
fn=partial(update_user_data, HF_DATASET_NAME=HF_DATASET_NAME, local_ds_directory_path = LOCAL_DS_DIRECTORY_PATH),
|
| 217 |
inputs=[username_inp, password_inp, country_choice, language_choice],
|
|
@@ -310,11 +339,11 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 310 |
instruct_btn.click(lambda: Modal(visible=True), None, modal)
|
| 311 |
# ============================================ #
|
| 312 |
|
| 313 |
-
# Load saved values from local storage
|
| 314 |
-
@ui.load(inputs=[local_storage], outputs=[country_choice, language_choice, username, password])
|
| 315 |
-
def load_from_local_storage(saved_values):
|
| 316 |
-
|
| 317 |
-
|
| 318 |
|
| 319 |
# ============================================= #
|
| 320 |
# Exclude button
|
|
|
|
| 10 |
from .selection_page import build_selection_page
|
| 11 |
from .main_page import build_main_page
|
| 12 |
|
| 13 |
+
def get_key_by_value(dictionary, value):
|
| 14 |
+
for key, val in dictionary.items():
|
| 15 |
+
if val == value:
|
| 16 |
+
return key
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
def load_concepts(category, concept_btn, local_storage, loading_example, concepts):
|
| 20 |
+
country, lang, _, _ = local_storage
|
| 21 |
+
if category:
|
| 22 |
+
if lang in words_mapping:
|
| 23 |
+
category = get_key_by_value(words_mapping[lang], category) # translate back the category name
|
| 24 |
+
concept_val = None
|
| 25 |
+
# I'm doing this to avoid the case where the user load a previous example and the concept 'reamins' selected even if category changed
|
| 26 |
+
if loading_example:
|
| 27 |
+
concept_val = concept_btn if concept_btn else None
|
| 28 |
+
return gr.update(choices=sorted(concepts[country][lang2eng_mapping.get(lang, lang)][category]), value=concept_val), False
|
| 29 |
+
else:
|
| 30 |
+
return gr.update(choices=[], value=None), False
|
| 31 |
+
|
| 32 |
def switch_ui(country, language, username, password, flag=False, metadata_dict=None):
|
| 33 |
print(f"Language: {language}, Country: {country}")
|
| 34 |
|
|
|
|
| 56 |
}
|
| 57 |
"""
|
| 58 |
with gr.Blocks(css=custom_css) as ui:
|
| 59 |
+
local_storage = gr.State([None, None, "", ""])
|
| 60 |
+
loading_example = gr.State(False) # to check if the values are loaded from a user click on an example in
|
| 61 |
# First page: selection
|
| 62 |
|
| 63 |
selection_page, country_choice, language_choice, proceed_btn, username, password, intro_markdown = build_selection_page(metadata_dict)
|
|
|
|
| 97 |
cancel_exclude_btn = cmp_main_ui["cancel_exclude_btn"]
|
| 98 |
confirm_exclude_btn = cmp_main_ui["confirm_exclude_btn"]
|
| 99 |
|
| 100 |
+
|
| 101 |
+
### Category button
|
| 102 |
+
category_btn.change(
|
| 103 |
+
fn=partial(load_concepts, concepts=concepts_dict),
|
| 104 |
+
inputs=[category_btn, concept_btn, local_storage, loading_example],
|
| 105 |
+
outputs=[concept_btn, loading_example]
|
| 106 |
+
)
|
| 107 |
+
####
|
| 108 |
+
|
| 109 |
# is_blurred = gr.State(False) # Initialize as False
|
| 110 |
|
| 111 |
# Notification image is false
|
|
|
|
| 215 |
image_inp, image_url_inp, long_caption_inp, exampleid_btn,
|
| 216 |
category_btn, concept_btn,
|
| 217 |
category_concept_dropdowns[0], category_concept_dropdowns[1], category_concept_dropdowns[2],
|
| 218 |
+
category_concept_dropdowns[3], category_concept_dropdowns[4], loading_example
|
| 219 |
],
|
| 220 |
)
|
| 221 |
|
|
|
|
| 227 |
# Submit Button Click events
|
| 228 |
|
| 229 |
proceed_btn.click(
|
| 230 |
+
fn=partial(switch_ui, flag=False),
|
| 231 |
inputs=[country_choice, language_choice, username, password],
|
| 232 |
+
outputs=[local_storage, selection_page, main_ui_placeholder, browse_data_placeholder],
|
| 233 |
+
).then(
|
| 234 |
+
fn=partial(update_language, metadata_dict=metadata_dict, concepts_dict=concepts_dict),
|
| 235 |
+
inputs=[local_storage],
|
| 236 |
outputs=[
|
| 237 |
country_inp, language_inp, username_inp, password_inp, category_btn, concept_btn, image_inp,
|
| 238 |
image_url_inp, long_caption_inp, intro_text_inp, intro_text_inst_inp, instruct_btn, clear_btn,
|
|
|
|
| 241 |
category_concept_dropdowns[0], category_concept_dropdowns[1], category_concept_dropdowns[2],
|
| 242 |
category_concept_dropdowns[3], category_concept_dropdowns[4]
|
| 243 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
).then(
|
| 245 |
fn=partial(update_user_data, HF_DATASET_NAME=HF_DATASET_NAME, local_ds_directory_path = LOCAL_DS_DIRECTORY_PATH),
|
| 246 |
inputs=[username_inp, password_inp, country_choice, language_choice],
|
|
|
|
| 339 |
instruct_btn.click(lambda: Modal(visible=True), None, modal)
|
| 340 |
# ============================================ #
|
| 341 |
|
| 342 |
+
# # Load saved values from local storage (browser storage)
|
| 343 |
+
# @ui.load(inputs=[local_storage], outputs=[country_choice, language_choice, username, password])
|
| 344 |
+
# def load_from_local_storage(saved_values):
|
| 345 |
+
# print("loading from local storage", saved_values)
|
| 346 |
+
# return saved_values[0], saved_values[1], saved_values[2], saved_values[3]
|
| 347 |
|
| 348 |
# ============================================= #
|
| 349 |
# Exclude button
|
ui/main_page.py
CHANGED
|
@@ -5,30 +5,13 @@ from data.lang2eng_map import lang2eng_mapping
|
|
| 5 |
from data.words_map import words_mapping
|
| 6 |
import datetime
|
| 7 |
|
| 8 |
-
|
| 9 |
-
def get_key_by_value(dictionary, value):
|
| 10 |
-
for key, val in dictionary.items():
|
| 11 |
-
if val == value:
|
| 12 |
-
return key
|
| 13 |
-
return None
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def load_concepts(category, concepts, local_storage):
|
| 17 |
-
country, lang, _, _ = local_storage.value
|
| 18 |
-
if category:
|
| 19 |
-
if lang in words_mapping:
|
| 20 |
-
category = get_key_by_value(words_mapping[lang], category) # translate back the category name
|
| 21 |
-
return gr.update(choices=sorted(concepts[country][lang2eng_mapping.get(lang, lang)][category])) # value=[]
|
| 22 |
-
else:
|
| 23 |
-
return gr.update(choices=[], value=[])
|
| 24 |
-
|
| 25 |
-
|
| 26 |
def build_main_page(concepts_dict, metadata_dict, local_storage):
|
| 27 |
try:
|
| 28 |
country, lang, _, _ = local_storage.value
|
|
|
|
|
|
|
| 29 |
except (TypeError, ValueError):
|
| 30 |
country, lang = "USA", "English"
|
| 31 |
-
|
| 32 |
with gr.Column(visible=False, elem_id="main_page") as main_ui_placeholder:
|
| 33 |
# Read the markdown file
|
| 34 |
with open(metadata_dict["USA"]["English"]["Task"], "r", encoding="utf-8") as f:
|
|
@@ -56,12 +39,6 @@ def build_main_page(concepts_dict, metadata_dict, local_storage):
|
|
| 56 |
allow_custom_value=False, elem_id="category_btn", multiselect=False, value=None)
|
| 57 |
concept_btn = gr.Dropdown(choices=[], interactive=True, label="Main Concept",
|
| 58 |
allow_custom_value=True, elem_id="concept_btn", multiselect=False)
|
| 59 |
-
|
| 60 |
-
category_btn.change(
|
| 61 |
-
fn=partial(load_concepts, concepts=concepts_dict, local_storage=local_storage),
|
| 62 |
-
inputs=category_btn,
|
| 63 |
-
outputs=[concept_btn]
|
| 64 |
-
)
|
| 65 |
|
| 66 |
image_inp = gr.Image(label="Image", elem_id="image_inp", format="png", height=512, width=768)
|
| 67 |
with gr.Row():
|
|
|
|
| 5 |
from data.words_map import words_mapping
|
| 6 |
import datetime
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def build_main_page(concepts_dict, metadata_dict, local_storage):
|
| 9 |
try:
|
| 10 |
country, lang, _, _ = local_storage.value
|
| 11 |
+
if not country and not lang:
|
| 12 |
+
country, lang = "USA", "English"
|
| 13 |
except (TypeError, ValueError):
|
| 14 |
country, lang = "USA", "English"
|
|
|
|
| 15 |
with gr.Column(visible=False, elem_id="main_page") as main_ui_placeholder:
|
| 16 |
# Read the markdown file
|
| 17 |
with open(metadata_dict["USA"]["English"]["Task"], "r", encoding="utf-8") as f:
|
|
|
|
| 39 |
allow_custom_value=False, elem_id="category_btn", multiselect=False, value=None)
|
| 40 |
concept_btn = gr.Dropdown(choices=[], interactive=True, label="Main Concept",
|
| 41 |
allow_custom_value=True, elem_id="concept_btn", multiselect=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
image_inp = gr.Image(label="Image", elem_id="image_inp", format="png", height=512, width=768)
|
| 44 |
with gr.Row():
|