Spaces:
Sleeping
Sleeping
fix(space): use writable HF cache dir; set envs; run as user 1000; pin numpy<2; avoid permission + ABI issues
Browse files- Dockerfile +11 -1
- app.py +15 -10
Dockerfile
CHANGED
@@ -6,7 +6,13 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
6 |
|
7 |
ENV PIP_NO_CACHE_DIR=1 \
|
8 |
MPLBACKEND=Agg \
|
9 |
-
MIM_IGNORE_INSTALL_PYTORCH=1
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
WORKDIR /app
|
12 |
|
@@ -23,6 +29,10 @@ RUN python -m pip install --upgrade pip wheel setuptools openmim \
|
|
23 |
|
24 |
COPY . /app
|
25 |
|
|
|
|
|
|
|
|
|
26 |
EXPOSE 7860
|
27 |
|
28 |
CMD ["python", "app.py"]
|
|
|
6 |
|
7 |
ENV PIP_NO_CACHE_DIR=1 \
|
8 |
MPLBACKEND=Agg \
|
9 |
+
MIM_IGNORE_INSTALL_PYTORCH=1 \
|
10 |
+
HF_HOME=/data/hf \
|
11 |
+
HF_CACHE_DIR=/data/hf-cache \
|
12 |
+
HUGGINGFACE_HUB_CACHE=/data/hf-cache \
|
13 |
+
MPLCONFIGDIR=/data/matplotlib
|
14 |
+
|
15 |
+
RUN mkdir -p /data/hf /data/hf-cache /data/matplotlib && chmod -R 777 /data
|
16 |
|
17 |
WORKDIR /app
|
18 |
|
|
|
29 |
|
30 |
COPY . /app
|
31 |
|
32 |
+
# run as non-root user 1000 if available
|
33 |
+
RUN useradd -m -u 1000 user || true
|
34 |
+
USER 1000
|
35 |
+
|
36 |
EXPOSE 7860
|
37 |
|
38 |
CMD ["python", "app.py"]
|
app.py
CHANGED
@@ -6,6 +6,13 @@ import torch
|
|
6 |
import numpy as np
|
7 |
import cv2
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
# Add custom modules to path - try multiple possible locations
|
10 |
possible_paths = [
|
11 |
"./custom_models",
|
@@ -60,15 +67,14 @@ class MedSAMIntegrator:
|
|
60 |
from segment_anything import sam_model_registry as _reg
|
61 |
import torch as _torch
|
62 |
|
63 |
-
# Preferred local path
|
64 |
-
medsam_ckpt_path = "
|
65 |
|
66 |
# If not present, fetch from HF Hub using provided repo or default
|
67 |
if not os.path.exists(medsam_ckpt_path):
|
68 |
try:
|
69 |
from huggingface_hub import hf_hub_download, list_repo_files
|
70 |
repo_id = os.environ.get("HF_MEDSAM_REPO", "Aniketg6/Fine-Tuned-MedSAM")
|
71 |
-
# Try to find a .pth/.pt in the repo
|
72 |
print(f"π Trying to download MedSAM checkpoint from {repo_id} ...")
|
73 |
files = list_repo_files(repo_id)
|
74 |
candidate = None
|
@@ -78,9 +84,8 @@ class MedSAMIntegrator:
|
|
78 |
candidate = f
|
79 |
break
|
80 |
if candidate is None:
|
81 |
-
# Fallback to a common name
|
82 |
candidate = "medsam_vit_b.pth"
|
83 |
-
ckpt_path = hf_hub_download(repo_id=repo_id, filename=candidate, cache_dir=
|
84 |
medsam_ckpt_path = ckpt_path
|
85 |
print(f"β
Downloaded MedSAM checkpoint: {medsam_ckpt_path}")
|
86 |
except Exception as dl_err:
|
@@ -379,7 +384,7 @@ try:
|
|
379 |
chart_type_path = hf_hub_download(
|
380 |
repo_id="hanszhu/ChartTypeNet-DocFigure",
|
381 |
filename="chart_type.pth",
|
382 |
-
cache_dir=
|
383 |
)
|
384 |
print(f"β
Downloaded to: {chart_type_path}")
|
385 |
|
@@ -478,7 +483,7 @@ if MM_DET_AVAILABLE:
|
|
478 |
element_checkpoint = hf_hub_download(
|
479 |
repo_id="hanszhu/ChartElementNet-MultiClass",
|
480 |
filename="chart_label+.pth",
|
481 |
-
cache_dir=
|
482 |
)
|
483 |
print(f"β
Downloaded to: {element_checkpoint}")
|
484 |
|
@@ -504,7 +509,7 @@ if MM_DET_AVAILABLE:
|
|
504 |
datapoint_checkpoint = hf_hub_download(
|
505 |
repo_id="hanszhu/ChartPointNet-InstanceSeg",
|
506 |
filename="chart_datapoint.pth",
|
507 |
-
cache_dir=
|
508 |
)
|
509 |
print(f"β
Downloaded to: {datapoint_checkpoint}")
|
510 |
|
@@ -690,14 +695,14 @@ def analyze_with_medsam(base_result, image):
|
|
690 |
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
|
691 |
import cv2 as _cv2
|
692 |
# If ViT-H checkpoint present, use SAM automatic mask generator (download if missing)
|
693 |
-
vit_h_ckpt = "
|
694 |
if not os.path.exists(vit_h_ckpt):
|
695 |
try:
|
696 |
from huggingface_hub import hf_hub_download
|
697 |
vit_h_ckpt = hf_hub_download(
|
698 |
repo_id="Aniketg6/SAM",
|
699 |
filename="sam_vit_h_4b8939.pth",
|
700 |
-
cache_dir=
|
701 |
)
|
702 |
print(f"β
Downloaded SAM ViT-H checkpoint to: {vit_h_ckpt}")
|
703 |
except Exception as dlh:
|
|
|
6 |
import numpy as np
|
7 |
import cv2
|
8 |
|
9 |
+
# Writable cache directory for HF downloads
|
10 |
+
HF_CACHE_DIR = os.getenv("HF_CACHE_DIR", "/data/hf-cache")
|
11 |
+
try:
|
12 |
+
os.makedirs(HF_CACHE_DIR, exist_ok=True)
|
13 |
+
except Exception:
|
14 |
+
pass
|
15 |
+
|
16 |
# Add custom modules to path - try multiple possible locations
|
17 |
possible_paths = [
|
18 |
"./custom_models",
|
|
|
67 |
from segment_anything import sam_model_registry as _reg
|
68 |
import torch as _torch
|
69 |
|
70 |
+
# Preferred local path in HF cache
|
71 |
+
medsam_ckpt_path = os.path.join(HF_CACHE_DIR, "medsam_vit_b.pth")
|
72 |
|
73 |
# If not present, fetch from HF Hub using provided repo or default
|
74 |
if not os.path.exists(medsam_ckpt_path):
|
75 |
try:
|
76 |
from huggingface_hub import hf_hub_download, list_repo_files
|
77 |
repo_id = os.environ.get("HF_MEDSAM_REPO", "Aniketg6/Fine-Tuned-MedSAM")
|
|
|
78 |
print(f"π Trying to download MedSAM checkpoint from {repo_id} ...")
|
79 |
files = list_repo_files(repo_id)
|
80 |
candidate = None
|
|
|
84 |
candidate = f
|
85 |
break
|
86 |
if candidate is None:
|
|
|
87 |
candidate = "medsam_vit_b.pth"
|
88 |
+
ckpt_path = hf_hub_download(repo_id=repo_id, filename=candidate, cache_dir=HF_CACHE_DIR)
|
89 |
medsam_ckpt_path = ckpt_path
|
90 |
print(f"β
Downloaded MedSAM checkpoint: {medsam_ckpt_path}")
|
91 |
except Exception as dl_err:
|
|
|
384 |
chart_type_path = hf_hub_download(
|
385 |
repo_id="hanszhu/ChartTypeNet-DocFigure",
|
386 |
filename="chart_type.pth",
|
387 |
+
cache_dir=HF_CACHE_DIR
|
388 |
)
|
389 |
print(f"β
Downloaded to: {chart_type_path}")
|
390 |
|
|
|
483 |
element_checkpoint = hf_hub_download(
|
484 |
repo_id="hanszhu/ChartElementNet-MultiClass",
|
485 |
filename="chart_label+.pth",
|
486 |
+
cache_dir=HF_CACHE_DIR
|
487 |
)
|
488 |
print(f"β
Downloaded to: {element_checkpoint}")
|
489 |
|
|
|
509 |
datapoint_checkpoint = hf_hub_download(
|
510 |
repo_id="hanszhu/ChartPointNet-InstanceSeg",
|
511 |
filename="chart_datapoint.pth",
|
512 |
+
cache_dir=HF_CACHE_DIR
|
513 |
)
|
514 |
print(f"β
Downloaded to: {datapoint_checkpoint}")
|
515 |
|
|
|
695 |
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
|
696 |
import cv2 as _cv2
|
697 |
# If ViT-H checkpoint present, use SAM automatic mask generator (download if missing)
|
698 |
+
vit_h_ckpt = os.path.join(HF_CACHE_DIR, "sam_vit_h_4b8939.pth")
|
699 |
if not os.path.exists(vit_h_ckpt):
|
700 |
try:
|
701 |
from huggingface_hub import hf_hub_download
|
702 |
vit_h_ckpt = hf_hub_download(
|
703 |
repo_id="Aniketg6/SAM",
|
704 |
filename="sam_vit_h_4b8939.pth",
|
705 |
+
cache_dir=HF_CACHE_DIR
|
706 |
)
|
707 |
print(f"β
Downloaded SAM ViT-H checkpoint to: {vit_h_ckpt}")
|
708 |
except Exception as dlh:
|