Workpam commited on
Commit
a44f19f
Β·
verified Β·
1 Parent(s): b37fc74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -103
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", "πŸ“ Upload Images"],
561
  value="πŸ“„ Upload Workbook",
562
  label="Select Input Method"
563
  )
564
 
565
- workbook_input = gr.File(
566
- label="πŸ“„ Upload .xlsx or .xlsm",
567
- file_types=[".xlsx", ".xlsm"],
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 URLs",
578
- visible=False
579
  )
580
 
581
- # toggle visibility
582
  def toggle_inputs(choice):
583
- if choice == "πŸ“„ Upload Workbook":
584
- # show only the sheet‐uploader
585
- return (
586
- gr.update(visible=True), # workbook_input
587
- gr.update(visible=False), # upload_box
588
- gr.update(visible=False) # image_url_input
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=[workbook_input, upload_box, image_url_input]
601
  )
602
 
603
- process_btn_url = gr.Button("βš™οΈ Process", elem_id="process-btn-url")
 
 
 
 
 
 
 
 
 
 
604
 
605
  with gr.Group(elem_classes="panel"):
606
- with gr.Row():
607
- format_choice = gr.Dropdown(
608
- ["JPEG", "PNG", "WEBP", "TIFF", "GIF", "JFIF", "AVIF"],
609
- label="πŸ–ΌοΈ Format",
610
- value="JPEG",
611
- scale=1
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 = gr.File(visible=False)
637
-
638
- with gr.Accordion("🧷 Batch Individual Files", open=False):
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 = gr.Button("β›” Stop", elem_id="stop-btn")
646
- clear_btn = gr.Button("πŸ—‘οΈ Clear", elem_id="clear-btn")
647
-
648
- gr.HTML("<center style='margin-top:1rem; color:white;'>Created with πŸ’œ by Vishakha</center>")
649
-
650
-
651
- # ─── Dispatch callback ───────────────────────────────────
652
- # ─── Dispatch callback ───────────────────────────────────
653
- def dispatch(mode, wb, uploads, url, fmt, w, h):
654
- if mode == "πŸ“„ Upload Workbook":
655
- files, zip_path, msg = handle_batch(wb, fmt, w, h)
656
- return files, zip_path, msg
657
- else:
658
- if uploads:
659
- files, fpath, msg = handle_single_upload(uploads, fmt, w, h)
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
- clear_btn.click(
691
- fn=clear_all,
692
- inputs=[temp_dir_state],
693
- outputs=[
694
- workbook_upload,
 
 
 
 
 
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
+