Spaces:
Running
Running
adding queue system, rotate transformations, and scroll to the top when click on submit button
Browse files- app.py +2 -1
- logic/image_transforms.py +80 -0
- ui/layout.py +41 -2
- ui/main_page.py +7 -0
app.py
CHANGED
|
@@ -17,8 +17,9 @@ concepts = load_concepts()
|
|
| 17 |
metadata = load_metadata()
|
| 18 |
|
| 19 |
demo = build_ui(concepts, metadata, HF_API_TOKEN, HF_DATASET_NAME)
|
|
|
|
| 20 |
# demo.launch()
|
| 21 |
-
demo.launch(debug=False)
|
| 22 |
|
| 23 |
demo.close()
|
| 24 |
# gr.close_all()
|
|
|
|
| 17 |
metadata = load_metadata()
|
| 18 |
|
| 19 |
demo = build_ui(concepts, metadata, HF_API_TOKEN, HF_DATASET_NAME)
|
| 20 |
+
demo.queue(max_size=50, default_concurrency_limit=5) # Enable queue system with max 30 requests
|
| 21 |
# demo.launch()
|
| 22 |
+
demo.launch(debug=False, server_port=7861)
|
| 23 |
|
| 24 |
demo.close()
|
| 25 |
# gr.close_all()
|
logic/image_transforms.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def rotate_image_90_left(image):
|
| 7 |
+
"""
|
| 8 |
+
Rotate image 90 degrees counter-clockwise (left)
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
image: Input image as numpy array
|
| 12 |
+
|
| 13 |
+
Returns:
|
| 14 |
+
Rotated image or None if input is None
|
| 15 |
+
"""
|
| 16 |
+
if image is None:
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
return cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"Error rotating image left: {e}")
|
| 23 |
+
return image
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def rotate_image_90_right(image):
|
| 27 |
+
"""
|
| 28 |
+
Rotate image 90 degrees clockwise (right)
|
| 29 |
+
|
| 30 |
+
Args:
|
| 31 |
+
image: Input image as numpy array
|
| 32 |
+
|
| 33 |
+
Returns:
|
| 34 |
+
Rotated image or None if input is None
|
| 35 |
+
"""
|
| 36 |
+
if image is None:
|
| 37 |
+
return None
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
return cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
print(f"Error rotating image right: {e}")
|
| 43 |
+
return image
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def rotate_image_180(image):
|
| 47 |
+
"""
|
| 48 |
+
Rotate image 180 degrees
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
image: Input image as numpy array
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
Rotated image or None if input is None
|
| 55 |
+
"""
|
| 56 |
+
if image is None:
|
| 57 |
+
return None
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
return cv2.rotate(image, cv2.ROTATE_180)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Error rotating image 180 degrees: {e}")
|
| 63 |
+
return image
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def reset_image_to_original(current_image, original_image):
|
| 67 |
+
"""
|
| 68 |
+
Reset image to its original state
|
| 69 |
+
|
| 70 |
+
Args:
|
| 71 |
+
current_image: Current modified image (fallback if no original)
|
| 72 |
+
original_image: Original image to restore (Gradio State object)
|
| 73 |
+
|
| 74 |
+
Returns:
|
| 75 |
+
Original image if available, otherwise current image (may be None)
|
| 76 |
+
"""
|
| 77 |
+
if original_image is None:
|
| 78 |
+
return current_image
|
| 79 |
+
|
| 80 |
+
return original_image.value
|
ui/layout.py
CHANGED
|
@@ -6,6 +6,7 @@ from logic.data_utils import CustomHFDatasetSaver
|
|
| 6 |
from data.lang2eng_map import lang2eng_mapping
|
| 7 |
from gradio_modal import Modal
|
| 8 |
from logic.handlers import *
|
|
|
|
| 9 |
from config.settings import *
|
| 10 |
from functools import partial
|
| 11 |
|
|
@@ -39,6 +40,17 @@ function() {
|
|
| 39 |
}
|
| 40 |
"""
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
def login_user(email, password):
|
| 43 |
result = auth_handler.login(email, password)
|
| 44 |
if result['success']:
|
|
@@ -324,6 +336,11 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 324 |
modal_submit = cmp_main_ui["modal_submit"]
|
| 325 |
vlm_cancel_btn = cmp_main_ui["vlm_cancel_btn"]
|
| 326 |
vlm_model_dropdown = cmp_main_ui["vlm_model_dropdown"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
|
| 328 |
# dictionary to store ALL vlm_outputs and vlm_models by exampleid
|
| 329 |
vlm_captions = gr.State(None)
|
|
@@ -442,6 +459,25 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 442 |
outputs=[image_inp] # is_blurred
|
| 443 |
)
|
| 444 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
# ===============================
|
| 446 |
with gr.Column(visible=False, elem_id="browse_data") as browse_data_placeholder:
|
| 447 |
# Browse Data
|
|
@@ -600,7 +636,8 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 600 |
# submit_btn.click(lambda: Modal(visible=True), None, modal_vlm)
|
| 601 |
submit_btn.click(submit_button_clicked,
|
| 602 |
inputs=[vlm_output],
|
| 603 |
-
outputs=[modal_vlm, modal_submit]
|
|
|
|
| 604 |
|
| 605 |
# submit_btn.click(partial(submit_button_clicked, save_fn=hf_writer.save,
|
| 606 |
# data_outputs=data_outputs),
|
|
@@ -714,7 +751,9 @@ def build_ui(concepts_dict, metadata_dict, HF_API_TOKEN, HF_DATASET_NAME):
|
|
| 714 |
gen_button.click(
|
| 715 |
fn=generate_vlm_caption, # processor=processor, model=model
|
| 716 |
inputs=[image_inp, vlm_model_dropdown],
|
| 717 |
-
outputs=[vlm_output, vlm_feedback, vlm_done_btn, vlm_no_btn, gen_button, vlm_model_dropdown]
|
|
|
|
|
|
|
| 718 |
)
|
| 719 |
# vlm_output.change(
|
| 720 |
# fn=lambda : gr.update(interactive=False) if vlm_output.value else gr.update(interactive=True),
|
|
|
|
| 6 |
from data.lang2eng_map import lang2eng_mapping
|
| 7 |
from gradio_modal import Modal
|
| 8 |
from logic.handlers import *
|
| 9 |
+
from logic.image_transforms import rotate_image_90_left, rotate_image_90_right, reset_image_to_original
|
| 10 |
from config.settings import *
|
| 11 |
from functools import partial
|
| 12 |
|
|
|
|
| 40 |
}
|
| 41 |
"""
|
| 42 |
|
| 43 |
+
scroll_to_top_js = """
|
| 44 |
+
function() {
|
| 45 |
+
// Scroll to the top of the page smoothly
|
| 46 |
+
window.scrollTo({
|
| 47 |
+
top: 0,
|
| 48 |
+
behavior: 'smooth'
|
| 49 |
+
});
|
| 50 |
+
return "";
|
| 51 |
+
}
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
def login_user(email, password):
|
| 55 |
result = auth_handler.login(email, password)
|
| 56 |
if result['success']:
|
|
|
|
| 336 |
modal_submit = cmp_main_ui["modal_submit"]
|
| 337 |
vlm_cancel_btn = cmp_main_ui["vlm_cancel_btn"]
|
| 338 |
vlm_model_dropdown = cmp_main_ui["vlm_model_dropdown"]
|
| 339 |
+
|
| 340 |
+
# Image rotation buttons
|
| 341 |
+
rotate_left_btn = cmp_main_ui["rotate_left_btn"]
|
| 342 |
+
rotate_right_btn = cmp_main_ui["rotate_right_btn"]
|
| 343 |
+
reset_image_btn = cmp_main_ui["reset_image_btn"]
|
| 344 |
|
| 345 |
# dictionary to store ALL vlm_outputs and vlm_models by exampleid
|
| 346 |
vlm_captions = gr.State(None)
|
|
|
|
| 459 |
outputs=[image_inp] # is_blurred
|
| 460 |
)
|
| 461 |
|
| 462 |
+
# ============= Image Rotation ============= #
|
| 463 |
+
rotate_left_btn.click(
|
| 464 |
+
fn=rotate_image_90_left,
|
| 465 |
+
inputs=[image_inp],
|
| 466 |
+
outputs=[image_inp]
|
| 467 |
+
)
|
| 468 |
+
|
| 469 |
+
rotate_right_btn.click(
|
| 470 |
+
fn=rotate_image_90_right,
|
| 471 |
+
inputs=[image_inp],
|
| 472 |
+
outputs=[image_inp]
|
| 473 |
+
)
|
| 474 |
+
|
| 475 |
+
reset_image_btn.click(
|
| 476 |
+
fn=reset_image_to_original,
|
| 477 |
+
inputs=[image_inp, ori_img],
|
| 478 |
+
outputs=[image_inp]
|
| 479 |
+
)
|
| 480 |
+
|
| 481 |
# ===============================
|
| 482 |
with gr.Column(visible=False, elem_id="browse_data") as browse_data_placeholder:
|
| 483 |
# Browse Data
|
|
|
|
| 636 |
# submit_btn.click(lambda: Modal(visible=True), None, modal_vlm)
|
| 637 |
submit_btn.click(submit_button_clicked,
|
| 638 |
inputs=[vlm_output],
|
| 639 |
+
outputs=[modal_vlm, modal_submit],
|
| 640 |
+
js=scroll_to_top_js)
|
| 641 |
|
| 642 |
# submit_btn.click(partial(submit_button_clicked, save_fn=hf_writer.save,
|
| 643 |
# data_outputs=data_outputs),
|
|
|
|
| 751 |
gen_button.click(
|
| 752 |
fn=generate_vlm_caption, # processor=processor, model=model
|
| 753 |
inputs=[image_inp, vlm_model_dropdown],
|
| 754 |
+
outputs=[vlm_output, vlm_feedback, vlm_done_btn, vlm_no_btn, gen_button, vlm_model_dropdown],
|
| 755 |
+
concurrency_limit=1,
|
| 756 |
+
concurrency_id="vlm_queue"
|
| 757 |
)
|
| 758 |
# vlm_output.change(
|
| 759 |
# fn=lambda : gr.update(interactive=False) if vlm_output.value else gr.update(interactive=True),
|
ui/main_page.py
CHANGED
|
@@ -87,6 +87,10 @@ def build_main_page(concepts_dict, metadata_dict, local_storage):
|
|
| 87 |
hide_all_faces_btn = gr.Button("π€ Hide All Faces", elem_id="hide_all_faces_btn")
|
| 88 |
hide_faces_btn = gr.Button("π€ Hide Specific Faces", elem_id="hide_faces_btn")
|
| 89 |
unhide_faces_btn = gr.Button("π Unhide Faces", elem_id="unhide_faces_btn")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
with gr.Column():
|
| 92 |
# short_caption_inp = gr.Textbox(lines=2, label="Short Description", elem_id="short_caption_inp")
|
|
@@ -252,6 +256,9 @@ def build_main_page(concepts_dict, metadata_dict, local_storage):
|
|
| 252 |
"hide_faces_btn": hide_faces_btn,
|
| 253 |
"hide_all_faces_btn": hide_all_faces_btn,
|
| 254 |
"unhide_faces_btn": unhide_faces_btn,
|
|
|
|
|
|
|
|
|
|
| 255 |
"exclude_btn": exclude_btn,
|
| 256 |
"modal_exclude_confirm": modal_exclude_confirm,
|
| 257 |
"cancel_exclude_btn": cancel_exclude_btn,
|
|
|
|
| 87 |
hide_all_faces_btn = gr.Button("π€ Hide All Faces", elem_id="hide_all_faces_btn")
|
| 88 |
hide_faces_btn = gr.Button("π€ Hide Specific Faces", elem_id="hide_faces_btn")
|
| 89 |
unhide_faces_btn = gr.Button("π Unhide Faces", elem_id="unhide_faces_btn")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
rotate_left_btn = gr.Button("βΊ Rotate Left", elem_id="rotate_left_btn")
|
| 92 |
+
rotate_right_btn = gr.Button("β» Rotate Right", elem_id="rotate_right_btn")
|
| 93 |
+
reset_image_btn = gr.Button("π Reset Image", elem_id="reset_image_btn")
|
| 94 |
|
| 95 |
with gr.Column():
|
| 96 |
# short_caption_inp = gr.Textbox(lines=2, label="Short Description", elem_id="short_caption_inp")
|
|
|
|
| 256 |
"hide_faces_btn": hide_faces_btn,
|
| 257 |
"hide_all_faces_btn": hide_all_faces_btn,
|
| 258 |
"unhide_faces_btn": unhide_faces_btn,
|
| 259 |
+
"rotate_left_btn": rotate_left_btn,
|
| 260 |
+
"rotate_right_btn": rotate_right_btn,
|
| 261 |
+
"reset_image_btn": reset_image_btn,
|
| 262 |
"exclude_btn": exclude_btn,
|
| 263 |
"modal_exclude_confirm": modal_exclude_confirm,
|
| 264 |
"cancel_exclude_btn": cancel_exclude_btn,
|