Workpam commited on
Commit
d78f366
Β·
verified Β·
1 Parent(s): 63e7d21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -82
app.py CHANGED
@@ -545,130 +545,157 @@ gr.HTML("""
545
  </div>
546
  """)
547
 
548
- with gr.Blocks(css=css) as demo:
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", "πŸ“€ Upload Images"],
562
  value="πŸ“„ Upload Workbook",
563
  label="Select Input Method"
564
  )
565
 
566
- workbook_upload = gr.File(
567
- label="πŸ“‚ Upload .xlsx/.xlsm Workbook",
568
- file_types=['.xlsx', '.xlsm'],
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", file_count="multiple", visible=False
 
 
577
  )
578
  image_url_input = gr.Textbox(
579
- label="🌐 Paste Image URL", visible=False
 
580
  )
581
 
 
582
  def toggle_inputs(choice):
583
- is_workbook = choice.startswith("πŸ“„")
584
- return (
585
- gr.update(visible=is_workbook),
586
- gr.update(visible=not is_workbook),
587
- gr.update(visible=not is_workbook)
588
- )
589
-
 
 
 
 
 
 
 
590
  mode_toggle.change(
591
  fn=toggle_inputs,
592
  inputs=[mode_toggle],
593
- outputs=[workbook_upload, upload_box, image_url_input]
594
- )
595
-
596
- workbook_upload.change(
597
- fn=read_uploaded_workbook,
598
- inputs=[workbook_upload],
599
- outputs=[image_data_state, status]
600
  )
601
 
602
- upload_box.change(
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
- format_choice = gr.Dropdown(
610
- ["JPEG", "PNG", "WEBP", "TIFF", "GIF", "JFIF", "AVIF"],
611
- label="πŸ–ΌοΈ Format", value="JPEG", scale=1
612
- )
613
- width = gr.Number(label=" Width (px)", value=1000, precision=0, scale=1)
614
- height = gr.Number(label=" Height (px)", value=1000, precision=0, scale=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = gr.File(visible=False)
620
- with gr.Accordion("🧷 Individual Files", open=False):
621
- single_downloads = gr.File(label="Files", file_count="multiple")
 
 
 
 
622
 
623
  with gr.Row(elem_classes="btn-row"):
624
- stop_btn = gr.Button("Stop", elem_id="stop-btn")
625
- clear_btn = gr.Button("Clear", elem_id="clear-btn")
626
-
627
- gr.Markdown("<center style='margin-top:1rem;color:white'>Created with πŸ’œ by Vishakha</center>")
628
-
629
- process_btn = gr.Button("βš™οΈ Process", elem_id="process-btn-url")
630
- process_btn.click(
631
- fn=lambda mode, sd, ups, pu, fmt, w, h: (
632
- process_url_images(sd, fmt, w, h) if mode.startswith("πŸ“„") and sd else
633
- process_uploaded_images(ups, fmt, w, h) if mode.startswith("πŸ“€") and ups else
634
- process_single_url_image(pu, fmt, w, h) if pu.strip() else
635
- ([], None, "⚠️ No valid input provided", None)
636
- ),
637
- inputs=[mode_toggle, image_data_state, upload_box, image_url_input, format_choice, width, height],
638
- outputs=[single_downloads, zip_file_hidden, status, temp_dir_state]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
639
  )
640
 
641
  zip_download_btn.click(
642
  None,
643
  inputs=[zip_file_hidden],
644
- js="(file) => { if (file) { window.open(file.url, '_blank'); } }"
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
- clear_all,
654
- [temp_dir_state],
655
- [
656
- workbook_upload,
657
- image_data_state,
658
- upload_box,
659
- zip_file_hidden,
660
- single_downloads,
661
- status,
662
- temp_dir_state,
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)