Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -545,130 +545,157 @@ gr.HTML("""
|
|
545 |
</div>
|
546 |
""")
|
547 |
|
548 |
-
with gr.
|
549 |
-
image_data_state = gr.State([])
|
550 |
-
temp_dir_state = gr.State(None)
|
551 |
-
|
552 |
-
gr.HTML(lamp_html)
|
553 |
-
|
554 |
-
with gr.Row():
|
555 |
gr.Markdown("<h1>πΌοΈ Image Processor</h1>")
|
556 |
|
557 |
with gr.Row():
|
|
|
558 |
with gr.Column(scale=3):
|
559 |
with gr.Group(elem_classes="panel"):
|
560 |
mode_toggle = gr.Radio(
|
561 |
-
["π Upload Workbook", "
|
562 |
value="π Upload Workbook",
|
563 |
label="Select Input Method"
|
564 |
)
|
565 |
|
566 |
-
|
567 |
-
label="
|
568 |
-
file_types=[
|
569 |
visible=True
|
570 |
)
|
571 |
-
status = gr.Textbox(
|
572 |
-
label="π£ Status", lines=6, interactive=False, elem_id="status-box"
|
573 |
-
)
|
574 |
|
575 |
upload_box = gr.File(
|
576 |
-
label="π Upload Images",
|
|
|
|
|
577 |
)
|
578 |
image_url_input = gr.Textbox(
|
579 |
-
label="π Paste Image
|
|
|
580 |
)
|
581 |
|
|
|
582 |
def toggle_inputs(choice):
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
590 |
mode_toggle.change(
|
591 |
fn=toggle_inputs,
|
592 |
inputs=[mode_toggle],
|
593 |
-
outputs=[
|
594 |
-
)
|
595 |
-
|
596 |
-
workbook_upload.change(
|
597 |
-
fn=read_uploaded_workbook,
|
598 |
-
inputs=[workbook_upload],
|
599 |
-
outputs=[image_data_state, status]
|
600 |
)
|
601 |
|
602 |
-
|
603 |
-
lambda files: f"{len(files)} files ready." if files else "No files selected",
|
604 |
-
[upload_box],
|
605 |
-
[status]
|
606 |
-
)
|
607 |
|
608 |
with gr.Group(elem_classes="panel"):
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
615 |
|
|
|
616 |
with gr.Column(scale=2):
|
617 |
with gr.Group(elem_classes="panel"):
|
|
|
|
|
|
|
|
|
|
|
|
|
618 |
zip_download_btn = gr.Button("π¦ Download ZIP")
|
619 |
-
zip_file_hidden
|
620 |
-
|
621 |
-
|
|
|
|
|
|
|
|
|
622 |
|
623 |
with gr.Row(elem_classes="btn-row"):
|
624 |
-
stop_btn
|
625 |
-
clear_btn = gr.Button("Clear", elem_id="clear-btn")
|
626 |
-
|
627 |
-
gr.
|
628 |
-
|
629 |
-
|
630 |
-
|
631 |
-
|
632 |
-
|
633 |
-
|
634 |
-
|
635 |
-
|
636 |
-
|
637 |
-
|
638 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
639 |
)
|
640 |
|
641 |
zip_download_btn.click(
|
642 |
None,
|
643 |
inputs=[zip_file_hidden],
|
644 |
-
js="(file) => { if (file)
|
645 |
)
|
646 |
|
647 |
-
def clear_all(tmp_dir):
|
648 |
-
if tmp_dir and os.path.exists(tmp_dir):
|
649 |
-
shutil.rmtree(tmp_dir)
|
650 |
-
return "", [], [], gr.update(visible=False), [], "Cleared.", None, ""
|
651 |
-
|
652 |
clear_btn.click(
|
653 |
-
|
654 |
-
[temp_dir_state],
|
655 |
-
[
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
image_url_input
|
664 |
]
|
665 |
)
|
666 |
|
667 |
-
def stop_processing():
|
668 |
-
stop_event.set()
|
669 |
-
return "π Stop signal sent"
|
670 |
-
|
671 |
-
stop_btn.click(stop_processing, outputs=[status])
|
672 |
-
|
673 |
if __name__ == "__main__":
|
674 |
demo.queue().launch(debug=True)
|
|
|
545 |
</div>
|
546 |
""")
|
547 |
|
548 |
+
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
549 |
gr.Markdown("<h1>πΌοΈ Image Processor</h1>")
|
550 |
|
551 |
with gr.Row():
|
552 |
+
# βββ Left column: Inputs ββββββββββββββββββββββββββββββββ
|
553 |
with gr.Column(scale=3):
|
554 |
with gr.Group(elem_classes="panel"):
|
555 |
mode_toggle = gr.Radio(
|
556 |
+
["π Upload Workbook", "π Upload Images"],
|
557 |
value="π Upload Workbook",
|
558 |
label="Select Input Method"
|
559 |
)
|
560 |
|
561 |
+
workbook_input = gr.File(
|
562 |
+
label="π Upload .xlsx or .xlsm",
|
563 |
+
file_types=[".xlsx", ".xlsm"],
|
564 |
visible=True
|
565 |
)
|
|
|
|
|
|
|
566 |
|
567 |
upload_box = gr.File(
|
568 |
+
label="π Upload Images",
|
569 |
+
file_count="multiple",
|
570 |
+
visible=False
|
571 |
)
|
572 |
image_url_input = gr.Textbox(
|
573 |
+
label="π Paste Image URLs",
|
574 |
+
visible=False
|
575 |
)
|
576 |
|
577 |
+
# toggle visibility
|
578 |
def toggle_inputs(choice):
|
579 |
+
if choice == "π Upload Workbook":
|
580 |
+
# show only the sheetβuploader
|
581 |
+
return (
|
582 |
+
gr.update(visible=True), # workbook_input
|
583 |
+
gr.update(visible=False), # upload_box
|
584 |
+
gr.update(visible=False) # image_url_input
|
585 |
+
)
|
586 |
+
else:
|
587 |
+
# show BOTH the image uploader AND the URL textbox
|
588 |
+
return (
|
589 |
+
gr.update(visible=False), # workbook_input
|
590 |
+
gr.update(visible=True), # upload_box
|
591 |
+
gr.update(visible=True) # image_url_input
|
592 |
+
)
|
593 |
mode_toggle.change(
|
594 |
fn=toggle_inputs,
|
595 |
inputs=[mode_toggle],
|
596 |
+
outputs=[workbook_input, upload_box, image_url_input]
|
|
|
|
|
|
|
|
|
|
|
|
|
597 |
)
|
598 |
|
599 |
+
process_btn_url = gr.Button("βοΈ Process", elem_id="process-btn-url")
|
|
|
|
|
|
|
|
|
600 |
|
601 |
with gr.Group(elem_classes="panel"):
|
602 |
+
with gr.Row():
|
603 |
+
format_choice = gr.Dropdown(
|
604 |
+
["JPEG", "PNG", "WEBP", "TIFF", "GIF", "JFIF", "AVIF"],
|
605 |
+
label="πΌοΈ Format",
|
606 |
+
value="JPEG",
|
607 |
+
scale=1
|
608 |
+
)
|
609 |
+
width = gr.Number(
|
610 |
+
label="π Width (px)",
|
611 |
+
value=1000,
|
612 |
+
precision=0,
|
613 |
+
scale=1
|
614 |
+
)
|
615 |
+
height = gr.Number(
|
616 |
+
label="π Height (px)",
|
617 |
+
value=1000,
|
618 |
+
precision=0,
|
619 |
+
scale=1
|
620 |
+
)
|
621 |
|
622 |
+
# βββ Right column: Outputs ββββββββββββββββββββββββββββββ
|
623 |
with gr.Column(scale=2):
|
624 |
with gr.Group(elem_classes="panel"):
|
625 |
+
status = gr.Textbox(
|
626 |
+
label="π£ Status",
|
627 |
+
lines=6,
|
628 |
+
interactive=False,
|
629 |
+
elem_id="status-box"
|
630 |
+
)
|
631 |
zip_download_btn = gr.Button("π¦ Download ZIP")
|
632 |
+
zip_file_hidden = gr.File(visible=False)
|
633 |
+
|
634 |
+
with gr.Accordion("π§· Batch Individual Files", open=False):
|
635 |
+
single_downloads = gr.File(
|
636 |
+
label="Files",
|
637 |
+
file_count="multiple"
|
638 |
+
)
|
639 |
|
640 |
with gr.Row(elem_classes="btn-row"):
|
641 |
+
stop_btn = gr.Button("β Stop", elem_id="stop-btn")
|
642 |
+
clear_btn = gr.Button("ποΈ Clear", elem_id="clear-btn")
|
643 |
+
|
644 |
+
gr.HTML("<center style='margin-top:1rem; color:white;'>Created with π by Vishakha</center>")
|
645 |
+
|
646 |
+
|
647 |
+
# βββ Dispatch callback βββββββββββββββββββββββββββββββββββ
|
648 |
+
# βββ Dispatch callback βββββββββββββββββββββββββββββββββββ
|
649 |
+
def dispatch(mode, wb, uploads, url, fmt, w, h):
|
650 |
+
if mode == "π Upload Workbook":
|
651 |
+
files, zip_path, msg = handle_batch(wb, fmt, w, h)
|
652 |
+
return files, zip_path, msg
|
653 |
+
else:
|
654 |
+
if uploads:
|
655 |
+
files, fpath, msg = handle_single_upload(uploads, fmt, w, h)
|
656 |
+
else:
|
657 |
+
files, fpath, msg = handle_single_url(url, fmt, w, h)
|
658 |
+
return ([fpath] if fpath else []), None, msg
|
659 |
+
|
660 |
+
|
661 |
+
# βββ Wire up the Process button here βββββββββββββββββββ
|
662 |
+
process_btn_url.click(
|
663 |
+
fn=dispatch,
|
664 |
+
inputs=[
|
665 |
+
mode_toggle,
|
666 |
+
workbook_input,
|
667 |
+
upload_box,
|
668 |
+
image_url_input,
|
669 |
+
format_choice,
|
670 |
+
width,
|
671 |
+
height
|
672 |
+
],
|
673 |
+
outputs=[
|
674 |
+
single_downloads,
|
675 |
+
zip_file_hidden,
|
676 |
+
status
|
677 |
+
]
|
678 |
)
|
679 |
|
680 |
zip_download_btn.click(
|
681 |
None,
|
682 |
inputs=[zip_file_hidden],
|
683 |
+
js="(file) => { if (file) window.open(file.url,'_blank'); }"
|
684 |
)
|
685 |
|
|
|
|
|
|
|
|
|
|
|
686 |
clear_btn.click(
|
687 |
+
fn=do_clear,
|
688 |
+
inputs=[temp_dir_state],
|
689 |
+
outputs=[
|
690 |
+
workbook_input,
|
691 |
+
image_data_state,
|
692 |
+
upload_box,
|
693 |
+
zip_file_hidden,
|
694 |
+
single_downloads,
|
695 |
+
status,
|
696 |
+
temp_dir_state
|
|
|
697 |
]
|
698 |
)
|
699 |
|
|
|
|
|
|
|
|
|
|
|
|
|
700 |
if __name__ == "__main__":
|
701 |
demo.queue().launch(debug=True)
|