Spaces:
Running
Running
Commit ·
a99da1e
1
Parent(s): a0eaaf4
washing space
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import gradio as gr
|
|
| 2 |
import tempfile, os, shutil, zipfile, glob, time
|
| 3 |
import os
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
| 5 |
os.environ["HF_HOME"] = "/tmp/hfhome"
|
| 6 |
os.environ["HF_HUB_CACHE"] = "/tmp/hfhome/hub"
|
| 7 |
os.environ["XDG_CACHE_HOME"] = "/tmp/xdgcache" # au cas où
|
|
@@ -27,7 +29,8 @@ for _p in ["/home/user/.cache", "/home/user/.local/share/Trash", "/home/user/app
|
|
| 27 |
_safe_rmtree(_p)
|
| 28 |
CACHE_DIR = "/tmp/hfhome/transformers" # même valeur que ci-dessus
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
# ======== CONFIG ========
|
| 33 |
DEVICE = "cpu" # Space CPU
|
|
@@ -45,6 +48,28 @@ ar_model = SegformerForSemanticSegmentation.from_pretrained(
|
|
| 45 |
ce_model = SegformerForSemanticSegmentation.from_pretrained(
|
| 46 |
CE_MODEL_ID, cache_dir=CACHE_DIR).to(DEVICE).eval()
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
transform = transforms.Compose([
|
| 49 |
transforms.Resize(TARGET_SIZE),
|
| 50 |
transforms.ToTensor(),
|
|
@@ -114,6 +139,19 @@ def overlay_counter(img_np: np.ndarray, idx: int, total: int) -> Image.Image:
|
|
| 114 |
return im
|
| 115 |
|
| 116 |
def run_job(files_or_zip, use_demo=False, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
global BUSY
|
| 118 |
first_img_time = None # durée mesurée sur la 1ère image
|
| 119 |
current_preview = PLACEHOLDER.copy()
|
|
@@ -353,6 +391,10 @@ def run_job(files_or_zip, use_demo=False, progress=gr.Progress()):
|
|
| 353 |
# ZIP final
|
| 354 |
zip_path = os.path.join(workdir, "results.zip")
|
| 355 |
shutil.make_archive(zip_path[:-4], "zip", out_root)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
elapsed = time.time() - start
|
| 357 |
yield zip_path, f"Done. {processed} image(s) in {elapsed:.1f}s. Download the ZIP below.", current_preview, workdir
|
| 358 |
# On ne supprime pas ici pour laisser le téléchargement se faire
|
|
@@ -372,6 +414,19 @@ def cleanup_all():
|
|
| 372 |
# purge des caches au cas où
|
| 373 |
for _p in ["/tmp/hfhome", "/tmp/xdgcache", "/tmp/torchhome"]:
|
| 374 |
_safe_rmtree(_p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
|
| 376 |
with gr.Blocks(theme=gr.themes.Soft(), title="MicroDeepAerenchyma — Segmentation (CPU)") as demo:
|
| 377 |
BANNER_URL = "https://huggingface.co/spaces/RomainFernandez/DeepAerenchyma/resolve/main/banner.png"
|
|
|
|
| 2 |
import tempfile, os, shutil, zipfile, glob, time
|
| 3 |
import os
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
+
os.environ["GRADIO_TEMP_DIR"] = "/tmp/gradio"
|
| 6 |
+
os.environ["TMPDIR"] = "/tmp"
|
| 7 |
os.environ["HF_HOME"] = "/tmp/hfhome"
|
| 8 |
os.environ["HF_HUB_CACHE"] = "/tmp/hfhome/hub"
|
| 9 |
os.environ["XDG_CACHE_HOME"] = "/tmp/xdgcache" # au cas où
|
|
|
|
| 29 |
_safe_rmtree(_p)
|
| 30 |
CACHE_DIR = "/tmp/hfhome/transformers" # même valeur que ci-dessus
|
| 31 |
|
| 32 |
+
for _p in ["/home/user/.cache", "/home/user/.local/share/Trash", "/home/user/app/tmp","/home/user/.gradio", "/tmp/gradio", "/tmp/hfhome", "/tmp/xdgcache", "/tmp/torchhome"]:
|
| 33 |
+
_safe_rmtree(_p)
|
| 34 |
|
| 35 |
# ======== CONFIG ========
|
| 36 |
DEVICE = "cpu" # Space CPU
|
|
|
|
| 48 |
ce_model = SegformerForSemanticSegmentation.from_pretrained(
|
| 49 |
CE_MODEL_ID, cache_dir=CACHE_DIR).to(DEVICE).eval()
|
| 50 |
|
| 51 |
+
|
| 52 |
+
# ---- PURGE OPTIONNELLE DU CACHE HF (réduit l'usage disque) ----
|
| 53 |
+
try:
|
| 54 |
+
from huggingface_hub import scan_cache_dir
|
| 55 |
+
# CACHE_DIR = "/tmp/hfhome/transformers" -> on monte d'un cran: "/tmp/hfhome"
|
| 56 |
+
cache_root = os.path.abspath(os.path.join(CACHE_DIR, ".."))
|
| 57 |
+
info = scan_cache_dir(cache_root)
|
| 58 |
+
|
| 59 |
+
# ne garder que les 2 dépôts utiles à l'app
|
| 60 |
+
keep = {AR_MODEL_ID, CE_MODEL_ID}
|
| 61 |
+
for repo in info.repos:
|
| 62 |
+
if getattr(repo, "repo_id", None) not in keep:
|
| 63 |
+
# supprime entièrement le repo non utilisé
|
| 64 |
+
repo_path = getattr(repo, "repo_path", None)
|
| 65 |
+
if repo_path and os.path.exists(repo_path):
|
| 66 |
+
_safe_rmtree(repo_path)
|
| 67 |
+
except Exception:
|
| 68 |
+
# pas bloquant si la structure du cache change
|
| 69 |
+
pass
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
transform = transforms.Compose([
|
| 74 |
transforms.Resize(TARGET_SIZE),
|
| 75 |
transforms.ToTensor(),
|
|
|
|
| 139 |
return im
|
| 140 |
|
| 141 |
def run_job(files_or_zip, use_demo=False, progress=gr.Progress()):
|
| 142 |
+
# garde: espace disque minimal requis (ex: 5 Go)
|
| 143 |
+
MIN_FREE = 5 * (1024**3)
|
| 144 |
+
try:
|
| 145 |
+
usage = shutil.disk_usage("/")
|
| 146 |
+
if usage.free < MIN_FREE:
|
| 147 |
+
yield None, (
|
| 148 |
+
"⚠️ Low free disk on Space. Please retry later "
|
| 149 |
+
"(administrator: increase persistent storage or clean caches)."
|
| 150 |
+
), current_preview, None
|
| 151 |
+
return
|
| 152 |
+
except Exception:
|
| 153 |
+
pass
|
| 154 |
+
|
| 155 |
global BUSY
|
| 156 |
first_img_time = None # durée mesurée sur la 1ère image
|
| 157 |
current_preview = PLACEHOLDER.copy()
|
|
|
|
| 391 |
# ZIP final
|
| 392 |
zip_path = os.path.join(workdir, "results.zip")
|
| 393 |
shutil.make_archive(zip_path[:-4], "zip", out_root)
|
| 394 |
+
|
| 395 |
+
# libère l’espace immédiatement : on supprime le dossier des sorties
|
| 396 |
+
_safe_rmtree(out_root)
|
| 397 |
+
|
| 398 |
elapsed = time.time() - start
|
| 399 |
yield zip_path, f"Done. {processed} image(s) in {elapsed:.1f}s. Download the ZIP below.", current_preview, workdir
|
| 400 |
# On ne supprime pas ici pour laisser le téléchargement se faire
|
|
|
|
| 414 |
# purge des caches au cas où
|
| 415 |
for _p in ["/tmp/hfhome", "/tmp/xdgcache", "/tmp/torchhome"]:
|
| 416 |
_safe_rmtree(_p)
|
| 417 |
+
|
| 418 |
+
for wd in list(ACTIVE_WORKDIRS):
|
| 419 |
+
try:
|
| 420 |
+
_safe_rmtree(wd)
|
| 421 |
+
ACTIVE_WORKDIRS.discard(wd)
|
| 422 |
+
except Exception:
|
| 423 |
+
pass
|
| 424 |
+
# Purge de tous les caches/temp usuels
|
| 425 |
+
for _p in [
|
| 426 |
+
"/tmp/gradio", "/tmp/hfhome", "/tmp/xdgcache", "/tmp/torchhome",
|
| 427 |
+
"/home/user/.cache", "/home/user/.gradio", "/home/user/.local/share/Trash"
|
| 428 |
+
]:
|
| 429 |
+
_safe_rmtree(_p)
|
| 430 |
|
| 431 |
with gr.Blocks(theme=gr.themes.Soft(), title="MicroDeepAerenchyma — Segmentation (CPU)") as demo:
|
| 432 |
BANNER_URL = "https://huggingface.co/spaces/RomainFernandez/DeepAerenchyma/resolve/main/banner.png"
|