Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -787,35 +787,98 @@ def create_demo():
|
|
| 787 |
preview_cache[url] = (preview, False)
|
| 788 |
return preview
|
| 789 |
|
| 790 |
-
|
| 791 |
-
#
|
|
|
|
| 792 |
def apply_preview(path: str, last_path: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 793 |
if path == last_path:
|
| 794 |
-
|
| 795 |
return gr.update(), gr.update(), ""
|
| 796 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 797 |
try:
|
| 798 |
-
#
|
|
|
|
|
|
|
| 799 |
if any(path.lower().endswith(ext) for ext in IMAGE_EXTS):
|
| 800 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 801 |
|
| 802 |
-
#
|
|
|
|
|
|
|
| 803 |
if any(path.lower().endswith(ext) for ext in VIDEO_EXTS):
|
| 804 |
-
return
|
| 805 |
-
|
| 806 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 807 |
img = Image.open(path)
|
| 808 |
-
img.verify()
|
| 809 |
-
return
|
| 810 |
-
|
|
|
|
|
|
|
|
|
|
| 811 |
except Exception as e:
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 817 |
demo.queue()
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
if __name__ == "__main__":
|
| 821 |
-
create_demo().launch(share=False, server_name="0.0.0.0", server_port=7860, max_threads=8)
|
|
|
|
| 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 |
+
"""
|
| 795 |
+
Returns:
|
| 796 |
+
(image_update, video_update, status_msg)
|
| 797 |
+
|
| 798 |
+
* If the path is the same as the previous one we do nothing – this stops
|
| 799 |
+
the UI from flickering on every job completion.
|
| 800 |
+
* Otherwise we decide whether the file is an image or a video and show the
|
| 801 |
+
appropriate component.
|
| 802 |
+
"""
|
| 803 |
+
# ------------------------------------------------------------------
|
| 804 |
+
# 1️⃣ No‑op when the preview hasn’t changed
|
| 805 |
+
# ------------------------------------------------------------------
|
| 806 |
if path == last_path:
|
| 807 |
+
# keep the current UI state unchanged
|
| 808 |
return gr.update(), gr.update(), ""
|
| 809 |
+
|
| 810 |
+
# ------------------------------------------------------------------
|
| 811 |
+
# 2️⃣ Nothing to show
|
| 812 |
+
# ------------------------------------------------------------------
|
| 813 |
+
if not path:
|
| 814 |
+
return (
|
| 815 |
+
gr.update(value=None, visible=False),
|
| 816 |
+
gr.update(value=None, visible=False),
|
| 817 |
+
"",
|
| 818 |
+
)
|
| 819 |
+
|
| 820 |
try:
|
| 821 |
+
# ------------------------------------------------------------------
|
| 822 |
+
# 3️⃣ Image preview
|
| 823 |
+
# ------------------------------------------------------------------
|
| 824 |
if any(path.lower().endswith(ext) for ext in IMAGE_EXTS):
|
| 825 |
+
return (
|
| 826 |
+
gr.update(value=path, visible=True),
|
| 827 |
+
gr.update(value=None, visible=False),
|
| 828 |
+
"Preview updated.",
|
| 829 |
+
)
|
| 830 |
|
| 831 |
+
# ------------------------------------------------------------------
|
| 832 |
+
# 4️⃣ Video preview
|
| 833 |
+
# ------------------------------------------------------------------
|
| 834 |
if any(path.lower().endswith(ext) for ext in VIDEO_EXTS):
|
| 835 |
+
return (
|
| 836 |
+
gr.update(value=None, visible=False),
|
| 837 |
+
gr.update(value=path, visible=True),
|
| 838 |
+
"Preview updated.",
|
| 839 |
+
)
|
| 840 |
+
|
| 841 |
+
# ------------------------------------------------------------------
|
| 842 |
+
# 5️⃣ Fallback – try to open as an image (covers odd extensions)
|
| 843 |
+
# ------------------------------------------------------------------
|
| 844 |
img = Image.open(path)
|
| 845 |
+
img.verify() # raises if not a valid image
|
| 846 |
+
return (
|
| 847 |
+
gr.update(value=path, visible=True),
|
| 848 |
+
gr.update(value=None, visible=False),
|
| 849 |
+
"Preview updated.",
|
| 850 |
+
)
|
| 851 |
except Exception as e:
|
| 852 |
+
# Log the problem – the UI will simply hide both previews
|
| 853 |
+
print(f"Failed to update preview for {path!r}: {e}")
|
| 854 |
+
return (
|
| 855 |
+
gr.update(value=None, visible=False),
|
| 856 |
+
gr.update(value=None, visible=False),
|
| 857 |
+
"",
|
| 858 |
+
)
|
| 859 |
+
|
| 860 |
+
# ----------------------------------------------------------------------
|
| 861 |
+
# Bind the function – we feed both the current preview path and the
|
| 862 |
+
# previously‑shown path, then store the new path back into both states.
|
| 863 |
+
# ----------------------------------------------------------------------
|
| 864 |
+
preview_path_state.change(
|
| 865 |
+
fn=apply_preview,
|
| 866 |
+
inputs=[preview_path_state, prev_preview_state],
|
| 867 |
+
outputs=[preview_image, preview_video, preview_status],
|
| 868 |
+
)
|
| 869 |
+
|
| 870 |
+
# After the preview is updated we also need to remember the new path.
|
| 871 |
+
# This tiny helper copies the new path into the “previous” state.
|
| 872 |
+
def _store_new_path(new_path: str):
|
| 873 |
+
return new_path, new_path # (prev_state, preview_state)
|
| 874 |
+
|
| 875 |
+
preview_path_state.change(
|
| 876 |
+
fn=_store_new_path,
|
| 877 |
+
inputs=[preview_path_state],
|
| 878 |
+
outputs=[prev_preview_state, preview_path_state],
|
| 879 |
+
queue=False,
|
| 880 |
+
)
|
| 881 |
+
|
| 882 |
+
# ----------------------------------------------------------------------
|
| 883 |
demo.queue()
|
| 884 |
+
return demo
|
|
|
|
|
|
|
|
|