Hug0endob commited on
Commit
cdb71d7
·
verified ·
1 Parent(s): e7c4cd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -45
app.py CHANGED
@@ -717,16 +717,15 @@ def create_demo():
717
  return "error", f"Unexpected worker error: {exc}", ""
718
 
719
  def _start_processing(url, prompt, key):
720
- # set state to busy and launch the worker in the same call
721
- return "busy", None, None # temporary values; the real result will replace them
722
-
723
  submit_btn.click(
724
  fn=_start_processing,
725
  inputs=[url_input, custom_prompt, api_key],
726
  outputs=[status_state, output_md, preview_path_state],
727
- queue=False, # this tiny wrapper runs instantly
728
  )
729
-
730
  submit_btn.click(
731
  fn=worker,
732
  inputs=[url_input, custom_prompt, api_key],
@@ -743,16 +742,14 @@ def create_demo():
743
  "done": "Done!",
744
  "error": "Retry",
745
  }
746
- return labels.get(s, "Submit") # Default case
747
 
748
- # after submit_btn / clear_btn definitions
749
  cancel_btn = gr.Button("Cancel", variant="secondary")
750
-
751
  def _cancel():
752
- # abort any queued jobs
753
  demo.cancel_all()
754
  return "idle", "", "", "idle", "Idle", "", ""
755
-
756
  cancel_btn.click(
757
  fn=_cancel,
758
  inputs=[],
@@ -762,60 +759,58 @@ def create_demo():
762
  ],
763
  queue=False,
764
  )
765
-
766
  status_state.change(fn=lambda s: _btn_label_for_status(s), inputs=[status_state], outputs=[submit_btn])
767
 
768
- # map status to progress text
769
  def status_to_progress_text(s):
770
- return {"idle":"Idle","busy":"Processing…","done":"Completed","error":"Error — see output"}.get(s, s)
771
  status_state.change(fn=status_to_progress_text, inputs=[status_state], outputs=[progress_md])
772
 
773
- preview_cache = {} # module‑level dict: url → (preview_path, is_video)
774
 
775
  def _make_preview(url: str, raw: bytes) -> str:
776
- # reuse existing preview if we already have one for this URL
777
  if url in preview_cache:
778
  return preview_cache[url][0]
779
-
780
- if determine_media_type(url)[1]: # video
781
  tmp = _temp_file(raw, suffix=ext_from_src(url) or ".mp4")
782
  preview = _convert_video_for_preview_if_needed(tmp)
783
- # keep the raw temp file for later clean‑up
784
  preview_cache[url] = (preview, True)
785
- else: # image
786
  preview = _temp_file(convert_to_jpeg_bytes(raw, base_h=1024), suffix=".jpg")
787
  preview_cache[url] = (preview, False)
788
  return preview
789
 
790
- preview_path_state = gr.State("") # current preview file
791
- prev_preview_state = gr.State("") # remembers the last file we showed
792
-
793
- def apply_preview(path: str, last_path: str):
794
- if path == last_path:
795
- return gr.update(), gr.update(), ""
796
-
797
- if not path:
798
- return gr.update(), gr.update(), ""
799
-
800
- try:
801
- if any(path.lower().endswith(ext) for ext in IMAGE_EXTS):
 
 
 
 
 
 
 
802
  return gr.update(value=path, visible=True), gr.update(value=None, visible=False), "Preview updated."
803
-
804
- if any(path.lower().endswith(ext) for ext in VIDEO_EXTS):
805
- return gr.update(value=None, visible=False), gr.update(value=path, visible=True), "Preview updated."
806
-
807
- img = Image.open(path)
808
- img.verify()
809
- return gr.update(value=path, visible=True), gr.update(value=None, visible=False), "Preview updated."
810
-
811
- except Exception as e:
812
- print(f"Failed to update preview: {e}")
813
- return gr.update(value=None, visible=False), gr.update(value=None, visible=False), ""
814
-
815
- preview_path_state.change(fn=apply_preview, inputs=[preview_path_state], outputs=[preview_image, preview_video, preview_status])
816
 
817
  demo.queue()
818
- return demo
819
 
820
  if __name__ == "__main__":
821
  create_demo().launch(share=False, server_name="0.0.0.0", server_port=7860, max_threads=8)
 
 
717
  return "error", f"Unexpected worker error: {exc}", ""
718
 
719
  def _start_processing(url, prompt, key):
720
+ return "busy", None, None
721
+
 
722
  submit_btn.click(
723
  fn=_start_processing,
724
  inputs=[url_input, custom_prompt, api_key],
725
  outputs=[status_state, output_md, preview_path_state],
726
+ queue=False,
727
  )
728
+
729
  submit_btn.click(
730
  fn=worker,
731
  inputs=[url_input, custom_prompt, api_key],
 
742
  "done": "Done!",
743
  "error": "Retry",
744
  }
745
+ return labels.get(s, "Submit")
746
 
 
747
  cancel_btn = gr.Button("Cancel", variant="secondary")
748
+
749
  def _cancel():
 
750
  demo.cancel_all()
751
  return "idle", "", "", "idle", "Idle", "", ""
752
+
753
  cancel_btn.click(
754
  fn=_cancel,
755
  inputs=[],
 
759
  ],
760
  queue=False,
761
  )
762
+
763
  status_state.change(fn=lambda s: _btn_label_for_status(s), inputs=[status_state], outputs=[submit_btn])
764
 
 
765
  def status_to_progress_text(s):
766
+ return {"idle": "Idle", "busy": "Processing…", "done": "Completed", "error": "Error — see output"}.get(s, s)
767
  status_state.change(fn=status_to_progress_text, inputs=[status_state], outputs=[progress_md])
768
 
769
+ preview_cache = {}
770
 
771
  def _make_preview(url: str, raw: bytes) -> str:
 
772
  if url in preview_cache:
773
  return preview_cache[url][0]
774
+
775
+ if determine_media_type(url)[1]:
776
  tmp = _temp_file(raw, suffix=ext_from_src(url) or ".mp4")
777
  preview = _convert_video_for_preview_if_needed(tmp)
 
778
  preview_cache[url] = (preview, True)
779
+ else:
780
  preview = _temp_file(convert_to_jpeg_bytes(raw, base_h=1024), suffix=".jpg")
781
  preview_cache[url] = (preview, False)
782
  return preview
783
 
784
+ preview_path_state = gr.State("")
785
+ prev_preview_state = gr.State("")
786
+
787
+ def apply_preview(path: str, last_path: str):
788
+ if path == last_path:
789
+ return gr.update(), gr.update(), ""
790
+
791
+ if not path:
792
+ return gr.update(), gr.update(), ""
793
+
794
+ try:
795
+ if any(path.lower().endswith(ext) for ext in IMAGE_EXTS):
796
+ return gr.update(value=path, visible=True), gr.update(value=None, visible=False), "Preview updated."
797
+
798
+ if any(path.lower().endswith(ext) for ext in VIDEO_EXTS):
799
+ return gr.update(value=None, visible=False), gr.update(value=path, visible=True), "Preview updated."
800
+
801
+ img = Image.open(path)
802
+ img.verify()
803
  return gr.update(value=path, visible=True), gr.update(value=None, visible=False), "Preview updated."
804
+
805
+ except Exception as e:
806
+ print(f"Failed to update preview: {e}")
807
+ return gr.update(value=None, visible=False), gr.update(value=None, visible=False), ""
808
+
809
+ preview_path_state.change(fn=apply_preview, inputs=[preview_path_state], outputs=[preview_image, preview_video, preview_status])
 
 
 
 
 
 
 
810
 
811
  demo.queue()
812
+ return demo
813
 
814
  if __name__ == "__main__":
815
  create_demo().launch(share=False, server_name="0.0.0.0", server_port=7860, max_threads=8)
816
+