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