Skip to content

Commit df6f6d2

Browse files
committed
Check tiny_vit_sam hash
1 parent dbc5891 commit df6f6d2

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

python/main.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,13 @@ async def medsam_estimation(boxdata: BoxData) -> BoxDataResponse:
826826

827827
def prepare_medsam() -> None:
828828
def initialize_image_embeddings(dcm_hash, img) -> None: # type: ignore[no-untyped-def] # noqa: ANN001
829-
img_embeding_fp = embed_dp / f"{dcm_hash}.pt"
830-
if not os.path.exists(img_embeding_fp):
829+
img_embeding_fp = Path(embed_dp / f"{dcm_hash}.pt")
830+
if not img_embeding_fp.exists():
831831
two_d = 2
832832
img_3c = (
833-
np.repeat(img[:, :, None], 3, axis=-1) if len(img.shape) == two_d else img
833+
np.repeat(img[:, :, None], 3, axis=-1)
834+
if len(img.shape) == two_d
835+
else img
834836
)
835837
img_256 = cv2.resize(src=img_3c, dsize=(256, 256)).astype(np.float32)
836838
img_256 = (img_256 - img_256.min()) / np.clip(
@@ -1525,13 +1527,24 @@ def meddisc() -> None:
15251527
if not csv_path.exists():
15261528
generate_action_groups()
15271529
vit_url = "https://api.github.com/repos/bowang-lab/MedSAM/contents/tiny_vit_sam.py?ref=b042d247f92105d4b03372b18230aa08560959e7"
1530+
vit_hash = "7b2f2103b7bc19a09c1f1e2fe1097ead8105fed59ab62e436430032a3a0e6c49"
15281531
response = requests.get(vit_url, timeout=10)
15291532
content = response.json()["content"]
15301533
decoded_content = base64.b64decode(content).decode("utf-8")
1531-
spec = importlib.util.spec_from_loader("tiny_vit_sam", loader=None, origin=vit_url)
1532-
module = importlib.util.module_from_spec(spec) # type: ignore[arg-type]
1533-
exec(decoded_content, module.__dict__) # noqa: S102
1534-
sys.modules["tiny_vit_sam"] = module
1534+
hash_object = hashlib.sha256(decoded_content.encode())
1535+
calculated_hash = hash_object.hexdigest()
1536+
if calculated_hash == vit_hash:
1537+
spec = importlib.util.spec_from_loader(
1538+
"tiny_vit_sam",
1539+
loader=None,
1540+
origin=vit_url,
1541+
)
1542+
module = importlib.util.module_from_spec(spec) # type: ignore[arg-type]
1543+
exec(decoded_content, module.__dict__) # noqa: S102
1544+
sys.modules["tiny_vit_sam"] = module
1545+
else:
1546+
msg = "E: Generated hash doesn't match"
1547+
raise ValueError(msg)
15351548
os.environ["KERAS_OCR_CACHE_DIR"] = "tmp"
15361549
if os.getenv("STAGING"):
15371550
run(

python/static/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ async function UpdateDICOMInformation(dcm_idx)
364364
document.getElementById('sliderLabel').textContent = dcm_idx_;
365365
DICOMOverview.innerHTML =
366366
`
367-
Raw File Path: ./${current_dicom_data_fp[0]}
367+
Raw File Path: ${current_dicom_data_fp[0]}
368368
</br>
369369
Clean File Path: ${current_dicom_data_fp[1]}
370370
</br>

0 commit comments

Comments
 (0)