Workpam commited on
Commit
a30ca62
Β·
verified Β·
1 Parent(s): 9bcc299

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -65
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
- # === LEFT SIDE: Inputs ===
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", file_count="multiple", visible=False
 
 
573
  )
574
-
575
  image_url_input = gr.Textbox(
576
- label="🌐 Paste Image URL", visible=False
 
577
  )
578
 
579
  def toggle_inputs(choice):
580
- is_workbook = choice.startswith("πŸ“„")
581
  return (
582
- gr.update(visible=is_workbook),
583
- gr.update(visible=not is_workbook),
584
- gr.update(visible=not is_workbook)
585
  )
586
 
587
  mode_toggle.change(
588
  fn=toggle_inputs,
589
  inputs=[mode_toggle],
590
- outputs=[image_data_state, status]
591
  )
592
 
 
593
  workbook_upload.change(
594
  fn=read_uploaded_workbook,
595
  inputs=[workbook_upload],
596
- outputs=[image_data_state, gr.Textbox.update()]
597
  )
598
-
599
  upload_box.change(
600
  lambda files: f"{len(files)} files ready." if files else "No files selected",
601
- [upload_box],
602
- [gr.Textbox.update()]
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", "PNG", "WEBP", "TIFF", "GIF", "JFIF", "AVIF"],
612
- label="πŸ–ΌοΈ Format", value="JPEG"
 
613
  )
614
- width = gr.Number(label="Width (px)", value=1000, precision=0)
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 = gr.Button("Stop", elem_id="stop-btn")
620
  clear_btn = gr.Button("Clear", elem_id="clear-btn")
621
 
622
- # === RIGHT SIDE: Status & Output ===
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 = gr.File(visible=False)
638
  with gr.Accordion("🧷 Individual Files", open=False):
639
- single_downloads = gr.File(label="Files", file_count="multiple")
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
- process_btn = gr.Button("βš™οΈ Process", elem_id="process-btn-url")
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) => { if (file) { window.open(file.url, '_blank'); } }"
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
- image_data_state,
676
- upload_box,
677
- zip_file_hidden,
678
- single_downloads,
679
- status,
680
- temp_dir_state,
681
- image_url_input
682
  ]
683
  )
684
 
685
- def stop_processing():
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)