Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -552,8 +552,16 @@ with gr.Blocks(css=css) as demo:
|
|
| 552 |
gr.HTML(lamp_html)
|
| 553 |
gr.Markdown("<h1>πΌοΈ Image Processor</h1>")
|
| 554 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 555 |
with gr.Row():
|
| 556 |
-
#
|
| 557 |
with gr.Column(scale=3, min_width=500):
|
| 558 |
with gr.Group(elem_classes="panel"):
|
| 559 |
mode_toggle = gr.Radio(
|
|
@@ -561,90 +569,71 @@ with gr.Blocks(css=css) as demo:
|
|
| 561 |
value="π Upload Workbook",
|
| 562 |
label="Select Input Method"
|
| 563 |
)
|
| 564 |
-
|
| 565 |
workbook_upload = gr.File(
|
| 566 |
label="π Upload .xlsx/.xlsm Workbook",
|
| 567 |
file_types=['.xlsx', '.xlsm'],
|
| 568 |
visible=True
|
| 569 |
)
|
| 570 |
-
|
| 571 |
upload_box = gr.File(
|
| 572 |
-
label="π Upload Images",
|
|
|
|
|
|
|
| 573 |
)
|
| 574 |
-
|
| 575 |
image_url_input = gr.Textbox(
|
| 576 |
-
label="π Paste Image URL",
|
|
|
|
| 577 |
)
|
| 578 |
|
| 579 |
def toggle_inputs(choice):
|
| 580 |
-
|
| 581 |
return (
|
| 582 |
-
gr.update(visible=
|
| 583 |
-
gr.update(visible=not
|
| 584 |
-
gr.update(visible=not
|
| 585 |
)
|
| 586 |
|
| 587 |
mode_toggle.change(
|
| 588 |
fn=toggle_inputs,
|
| 589 |
inputs=[mode_toggle],
|
| 590 |
-
outputs=[
|
| 591 |
)
|
| 592 |
|
|
|
|
| 593 |
workbook_upload.change(
|
| 594 |
fn=read_uploaded_workbook,
|
| 595 |
inputs=[workbook_upload],
|
| 596 |
-
outputs=[image_data_state,
|
| 597 |
)
|
| 598 |
-
|
| 599 |
upload_box.change(
|
| 600 |
lambda files: f"{len(files)} files ready." if files else "No files selected",
|
| 601 |
-
[upload_box],
|
| 602 |
-
[
|
| 603 |
)
|
| 604 |
|
| 605 |
-
# PROCESS button right after inputs
|
| 606 |
process_btn = gr.Button("βοΈ Process", elem_id="process-btn-url")
|
| 607 |
|
| 608 |
-
# FORMAT options below Process button
|
| 609 |
with gr.Group(elem_classes="panel"):
|
| 610 |
format_choice = gr.Dropdown(
|
| 611 |
-
["JPEG",
|
| 612 |
-
label="πΌοΈ Format",
|
|
|
|
| 613 |
)
|
| 614 |
-
width
|
| 615 |
height = gr.Number(label="Height (px)", value=1000, precision=0)
|
| 616 |
|
| 617 |
-
# Buttons at the bottom
|
| 618 |
with gr.Row(elem_classes="btn-row"):
|
| 619 |
-
stop_btn
|
| 620 |
clear_btn = gr.Button("Clear", elem_id="clear-btn")
|
| 621 |
|
| 622 |
-
#
|
| 623 |
with gr.Column(scale=2, min_width=400):
|
| 624 |
-
status = gr.Textbox(
|
| 625 |
-
label="π£ Status", lines=18, interactive=False, elem_id="status-box"
|
| 626 |
-
)
|
| 627 |
-
workbook_upload.change(
|
| 628 |
-
fn=read_uploaded_workbook,
|
| 629 |
-
inputs=[workbook_upload],
|
| 630 |
-
outputs=[image_data_state, status] # β
status is now defined
|
| 631 |
-
)
|
| 632 |
-
with gr.Group(elem_classes="panel"):
|
| 633 |
-
zip_download_btn = gr.Button("π¦")
|
| 634 |
-
|
| 635 |
with gr.Group(elem_classes="panel"):
|
| 636 |
zip_download_btn = gr.Button("π¦ Download ZIP")
|
| 637 |
-
zip_file_hidden
|
| 638 |
with gr.Accordion("π§· Individual Files", open=False):
|
| 639 |
-
single_downloads = gr.File(
|
| 640 |
-
|
| 641 |
-
with gr.Row(elem_classes="btn-row"):
|
| 642 |
-
stop_btn = gr.Button("Stop", elem_id="stop-btn")
|
| 643 |
-
clear_btn = gr.Button("Clear", elem_id="clear-btn")
|
| 644 |
-
|
| 645 |
-
gr.Markdown("<center style='margin-top:1rem;color:white'>Created with π by Vishakha</center>")
|
| 646 |
|
| 647 |
-
|
| 648 |
process_btn.click(
|
| 649 |
fn=lambda mode, sd, ups, pu, fmt, w, h: (
|
| 650 |
process_url_images(sd, fmt, w, h) if mode.startswith("π") and sd else
|
|
@@ -659,34 +648,19 @@ with gr.Blocks(css=css) as demo:
|
|
| 659 |
zip_download_btn.click(
|
| 660 |
None,
|
| 661 |
inputs=[zip_file_hidden],
|
| 662 |
-
js="(file) =>
|
| 663 |
)
|
| 664 |
|
| 665 |
-
def clear_all(tmp_dir):
|
| 666 |
-
if tmp_dir and os.path.exists(tmp_dir):
|
| 667 |
-
shutil.rmtree(tmp_dir)
|
| 668 |
-
return "", [], [], gr.update(visible=False), [], "Cleared.", None, ""
|
| 669 |
-
|
| 670 |
clear_btn.click(
|
| 671 |
-
clear_all,
|
| 672 |
-
[temp_dir_state],
|
| 673 |
-
[
|
| 674 |
-
workbook_upload,
|
| 675 |
-
|
| 676 |
-
upload_box,
|
| 677 |
-
zip_file_hidden,
|
| 678 |
-
single_downloads,
|
| 679 |
-
status,
|
| 680 |
-
temp_dir_state,
|
| 681 |
-
image_url_input
|
| 682 |
]
|
| 683 |
)
|
| 684 |
|
| 685 |
-
|
| 686 |
-
stop_event.set()
|
| 687 |
-
return "π Stop signal sent"
|
| 688 |
-
|
| 689 |
-
stop_btn.click(stop_processing, outputs=[status])
|
| 690 |
|
| 691 |
if __name__ == "__main__":
|
| 692 |
demo.queue().launch(debug=True)
|
|
|
|
| 552 |
gr.HTML(lamp_html)
|
| 553 |
gr.Markdown("<h1>πΌοΈ Image Processor</h1>")
|
| 554 |
|
| 555 |
+
# βββββββ DEFINE STATUS FIRST βββββββ
|
| 556 |
+
status = gr.Textbox(
|
| 557 |
+
label="π£ Status",
|
| 558 |
+
lines=18,
|
| 559 |
+
interactive=False,
|
| 560 |
+
elem_id="status-box"
|
| 561 |
+
)
|
| 562 |
+
|
| 563 |
with gr.Row():
|
| 564 |
+
# βββββββ LEFT PANEL βββββββ
|
| 565 |
with gr.Column(scale=3, min_width=500):
|
| 566 |
with gr.Group(elem_classes="panel"):
|
| 567 |
mode_toggle = gr.Radio(
|
|
|
|
| 569 |
value="π Upload Workbook",
|
| 570 |
label="Select Input Method"
|
| 571 |
)
|
|
|
|
| 572 |
workbook_upload = gr.File(
|
| 573 |
label="π Upload .xlsx/.xlsm Workbook",
|
| 574 |
file_types=['.xlsx', '.xlsm'],
|
| 575 |
visible=True
|
| 576 |
)
|
|
|
|
| 577 |
upload_box = gr.File(
|
| 578 |
+
label="π Upload Images",
|
| 579 |
+
file_count="multiple",
|
| 580 |
+
visible=False
|
| 581 |
)
|
|
|
|
| 582 |
image_url_input = gr.Textbox(
|
| 583 |
+
label="π Paste Image URL",
|
| 584 |
+
visible=False
|
| 585 |
)
|
| 586 |
|
| 587 |
def toggle_inputs(choice):
|
| 588 |
+
is_wb = choice.startswith("π")
|
| 589 |
return (
|
| 590 |
+
gr.update(visible=is_wb),
|
| 591 |
+
gr.update(visible=not is_wb),
|
| 592 |
+
gr.update(visible=not is_wb)
|
| 593 |
)
|
| 594 |
|
| 595 |
mode_toggle.change(
|
| 596 |
fn=toggle_inputs,
|
| 597 |
inputs=[mode_toggle],
|
| 598 |
+
outputs=[workbook_upload, upload_box, image_url_input]
|
| 599 |
)
|
| 600 |
|
| 601 |
+
# now we can safely reference `status` here
|
| 602 |
workbook_upload.change(
|
| 603 |
fn=read_uploaded_workbook,
|
| 604 |
inputs=[workbook_upload],
|
| 605 |
+
outputs=[image_data_state, status]
|
| 606 |
)
|
|
|
|
| 607 |
upload_box.change(
|
| 608 |
lambda files: f"{len(files)} files ready." if files else "No files selected",
|
| 609 |
+
inputs=[upload_box],
|
| 610 |
+
outputs=[status]
|
| 611 |
)
|
| 612 |
|
|
|
|
| 613 |
process_btn = gr.Button("βοΈ Process", elem_id="process-btn-url")
|
| 614 |
|
|
|
|
| 615 |
with gr.Group(elem_classes="panel"):
|
| 616 |
format_choice = gr.Dropdown(
|
| 617 |
+
["JPEG","PNG","WEBP","TIFF","GIF","JFIF","AVIF"],
|
| 618 |
+
label="πΌοΈ Format",
|
| 619 |
+
value="JPEG"
|
| 620 |
)
|
| 621 |
+
width = gr.Number(label="Width (px)", value=1000, precision=0)
|
| 622 |
height = gr.Number(label="Height (px)", value=1000, precision=0)
|
| 623 |
|
|
|
|
| 624 |
with gr.Row(elem_classes="btn-row"):
|
| 625 |
+
stop_btn = gr.Button("Stop", elem_id="stop-btn")
|
| 626 |
clear_btn = gr.Button("Clear", elem_id="clear-btn")
|
| 627 |
|
| 628 |
+
# βββββββ RIGHT PANEL βββββββ
|
| 629 |
with gr.Column(scale=2, min_width=400):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 630 |
with gr.Group(elem_classes="panel"):
|
| 631 |
zip_download_btn = gr.Button("π¦ Download ZIP")
|
| 632 |
+
zip_file_hidden = gr.File(visible=False)
|
| 633 |
with gr.Accordion("π§· Individual Files", open=False):
|
| 634 |
+
single_downloads = gr.File(file_count="multiple")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
|
| 636 |
+
# βββββββ HOOK UP EVENTS βββββββ
|
| 637 |
process_btn.click(
|
| 638 |
fn=lambda mode, sd, ups, pu, fmt, w, h: (
|
| 639 |
process_url_images(sd, fmt, w, h) if mode.startswith("π") and sd else
|
|
|
|
| 648 |
zip_download_btn.click(
|
| 649 |
None,
|
| 650 |
inputs=[zip_file_hidden],
|
| 651 |
+
js="(file) => file && window.open(file.url)"
|
| 652 |
)
|
| 653 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
clear_btn.click(
|
| 655 |
+
fn=clear_all,
|
| 656 |
+
inputs=[temp_dir_state],
|
| 657 |
+
outputs=[
|
| 658 |
+
workbook_upload, image_data_state, upload_box, zip_file_hidden,
|
| 659 |
+
single_downloads, status, temp_dir_state, image_url_input
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 660 |
]
|
| 661 |
)
|
| 662 |
|
| 663 |
+
stop_btn.click(lambda: ("π Stop signal sent",), outputs=[status])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
|
| 665 |
if __name__ == "__main__":
|
| 666 |
demo.queue().launch(debug=True)
|