Spaces:
Sleeping
Sleeping
titi
commited on
Commit
·
19bc1e0
1
Parent(s):
b120daa
changes in cleaning of tempfiles
Browse files- app.py +40 -9
- core/utils.py +47 -0
app.py
CHANGED
|
@@ -6,8 +6,15 @@ import imageio.v2 as iio
|
|
| 6 |
import tifffile
|
| 7 |
import tempfile
|
| 8 |
import urllib.request
|
|
|
|
| 9 |
|
| 10 |
-
from core.utils import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Globals
|
| 13 |
original_frames, aligned_frames = [], []
|
|
@@ -90,8 +97,13 @@ def load_from_url(request: gr.Request):
|
|
| 90 |
if "file_url" in params:
|
| 91 |
try:
|
| 92 |
url = params["file_url"]
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
return [gr.update(value=tmp_path)]
|
| 96 |
except Exception as e:
|
| 97 |
print(f"[URL load failed] {e}")
|
|
@@ -276,8 +288,13 @@ with gr.Blocks() as demo:
|
|
| 276 |
if "file_url" in params:
|
| 277 |
try:
|
| 278 |
url = params["file_url"]
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
stack = iio.mimread(tmp_path)
|
| 282 |
max_frame = len(stack) - 1
|
| 283 |
|
|
@@ -293,10 +310,24 @@ with gr.Blocks() as demo:
|
|
| 293 |
# Two-stack file case (for stack-based alignment)
|
| 294 |
if "file_url_1" in params and "file_url_2" in params:
|
| 295 |
try:
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
results[5] = tmp_path_1 # ref_input
|
| 301 |
results[6] = tmp_path_2 # mov_input
|
| 302 |
except Exception as e:
|
|
|
|
| 6 |
import tifffile
|
| 7 |
import tempfile
|
| 8 |
import urllib.request
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
from core.utils import (
|
| 12 |
+
get_sr_mode, normalize_stack, upscale, load_stack,
|
| 13 |
+
_is_demo_url, _demo_path_for_url,
|
| 14 |
+
_start_cleaner, citation_markdown, documentation_markdown
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
_start_cleaner()
|
| 18 |
|
| 19 |
# Globals
|
| 20 |
original_frames, aligned_frames = [], []
|
|
|
|
| 97 |
if "file_url" in params:
|
| 98 |
try:
|
| 99 |
url = params["file_url"]
|
| 100 |
+
if _is_demo_url(url):
|
| 101 |
+
tmp_path = _demo_path_for_url(url)
|
| 102 |
+
if not os.path.exists(tmp_path):
|
| 103 |
+
urllib.request.urlretrieve(url, tmp_path)
|
| 104 |
+
else:
|
| 105 |
+
tmp_path = tempfile.mktemp(suffix=".tif") # goes to WORK_DIR
|
| 106 |
+
urllib.request.urlretrieve(url, tmp_path)
|
| 107 |
return [gr.update(value=tmp_path)]
|
| 108 |
except Exception as e:
|
| 109 |
print(f"[URL load failed] {e}")
|
|
|
|
| 288 |
if "file_url" in params:
|
| 289 |
try:
|
| 290 |
url = params["file_url"]
|
| 291 |
+
if _is_demo_url(url):
|
| 292 |
+
tmp_path = _demo_path_for_url(url)
|
| 293 |
+
if not os.path.exists(tmp_path):
|
| 294 |
+
urllib.request.urlretrieve(url, tmp_path)
|
| 295 |
+
else:
|
| 296 |
+
tmp_path = tempfile.mktemp(suffix=".tif") # goes to WORK_DIR
|
| 297 |
+
urllib.request.urlretrieve(url, tmp_path)
|
| 298 |
stack = iio.mimread(tmp_path)
|
| 299 |
max_frame = len(stack) - 1
|
| 300 |
|
|
|
|
| 310 |
# Two-stack file case (for stack-based alignment)
|
| 311 |
if "file_url_1" in params and "file_url_2" in params:
|
| 312 |
try:
|
| 313 |
+
u1, u2 = params["file_url_1"], params["file_url_2"]
|
| 314 |
+
|
| 315 |
+
if _is_demo_url(u1):
|
| 316 |
+
tmp_path_1 = _demo_path_for_url(u1)
|
| 317 |
+
if not os.path.exists(tmp_path_1):
|
| 318 |
+
urllib.request.urlretrieve(u1, tmp_path_1)
|
| 319 |
+
else:
|
| 320 |
+
tmp_path_1 = tempfile.mktemp(suffix=".tif") # goes to WORK_DIR
|
| 321 |
+
urllib.request.urlretrieve(u1, tmp_path_1)
|
| 322 |
+
|
| 323 |
+
if _is_demo_url(u2):
|
| 324 |
+
tmp_path_2 = _demo_path_for_url(u2)
|
| 325 |
+
if not os.path.exists(tmp_path_2):
|
| 326 |
+
urllib.request.urlretrieve(u2, tmp_path_2)
|
| 327 |
+
else:
|
| 328 |
+
tmp_path_2 = tempfile.mktemp(suffix=".tif") # goes to WORK_DIR
|
| 329 |
+
urllib.request.urlretrieve(u2, tmp_path_2)
|
| 330 |
+
|
| 331 |
results[5] = tmp_path_1 # ref_input
|
| 332 |
results[6] = tmp_path_2 # mov_input
|
| 333 |
except Exception as e:
|
core/utils.py
CHANGED
|
@@ -2,6 +2,11 @@ from pystackreg import StackReg
|
|
| 2 |
import numpy as np
|
| 3 |
import imageio.v2 as iio
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def get_sr_mode(mode_str): return {
|
| 7 |
"TRANSLATION": StackReg.TRANSLATION,
|
|
@@ -31,6 +36,48 @@ def load_stack(file):
|
|
| 31 |
return normalize_stack(stack)
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
##### Markdowns #####
|
| 35 |
|
| 36 |
citation_markdown = """
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import imageio.v2 as iio
|
| 4 |
from PIL import Image
|
| 5 |
+
import time
|
| 6 |
+
import threading
|
| 7 |
+
import hashlib
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
def get_sr_mode(mode_str): return {
|
| 12 |
"TRANSLATION": StackReg.TRANSLATION,
|
|
|
|
| 36 |
return normalize_stack(stack)
|
| 37 |
|
| 38 |
|
| 39 |
+
##### CACHE #####
|
| 40 |
+
|
| 41 |
+
# App-scoped cache dirs under the OS temp
|
| 42 |
+
APP_TMP_ROOT = os.path.join(tempfile.gettempdir(), "psr_cache")
|
| 43 |
+
WORK_DIR = os.path.join(APP_TMP_ROOT, "work") # cleaned periodically
|
| 44 |
+
DEMO_DIR = os.path.join(APP_TMP_ROOT, "demo") # persistent demo cache
|
| 45 |
+
os.makedirs(WORK_DIR, exist_ok=True)
|
| 46 |
+
os.makedirs(DEMO_DIR, exist_ok=True)
|
| 47 |
+
|
| 48 |
+
# Direct all tempfile.* calls to WORK_DIR so cleanup is safe
|
| 49 |
+
tempfile.tempdir = WORK_DIR
|
| 50 |
+
|
| 51 |
+
TTL_SECONDS = 30 * 60 # 30 minutes
|
| 52 |
+
|
| 53 |
+
def _cleanup_old_files(folder, older_than_seconds):
|
| 54 |
+
now = time.time()
|
| 55 |
+
for root, _, files in os.walk(folder):
|
| 56 |
+
for name in files:
|
| 57 |
+
p = os.path.join(root, name)
|
| 58 |
+
try:
|
| 59 |
+
if now - os.path.getmtime(p) > older_than_seconds:
|
| 60 |
+
os.remove(p)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"[cleanup] {e}")
|
| 63 |
+
|
| 64 |
+
def _start_cleaner():
|
| 65 |
+
def loop():
|
| 66 |
+
while True:
|
| 67 |
+
_cleanup_old_files(WORK_DIR, TTL_SECONDS) # only clean WORK_DIR
|
| 68 |
+
time.sleep(TTL_SECONDS)
|
| 69 |
+
threading.Thread(target=loop, daemon=True).start()
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def _is_demo_url(url: str) -> bool:
|
| 73 |
+
return ("github.com/glichtner/pystackreg" in url) or \
|
| 74 |
+
("raw.githubusercontent.com/glichtner/pystackreg" in url)
|
| 75 |
+
|
| 76 |
+
def _demo_path_for_url(url: str, suffix=".tif"):
|
| 77 |
+
h = hashlib.sha256(url.encode("utf-8")).hexdigest()[:16]
|
| 78 |
+
return os.path.join(DEMO_DIR, f"psr-demo-{h}{suffix}")
|
| 79 |
+
|
| 80 |
+
|
| 81 |
##### Markdowns #####
|
| 82 |
|
| 83 |
citation_markdown = """
|