skarugu commited on
Commit
e8bcf9c
·
verified ·
1 Parent(s): de3c9a7

Upload streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +24 -8
src/streamlit_app.py CHANGED
@@ -968,17 +968,34 @@ def submit_uploaded_correction_pairs(nuc_files, myo_files, artifacts: dict, sele
968
 
969
  @st.cache_resource
970
  def load_model(device: str):
971
- # MYOSEG_MODEL_PATH lets you run offline / test a local checkpoint.
972
- local = os.environ.get("MYOSEG_MODEL_PATH") or hf_hub_download(
973
- repo_id=MODEL_REPO_ID,
974
- filename=MODEL_FILENAME,
975
- force_download=False,
976
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
977
  file_sha = sha256_file(local)
978
  info = {
979
  "sha256": file_sha,
980
  "modified": time.ctime(os.path.getmtime(local)),
981
  "size_mb": round(os.path.getsize(local) / 1e6, 2),
 
982
  }
983
  ckpt = torch.load(local, map_location=device, weights_only=False)
984
  state = ckpt["model"] if isinstance(ckpt, dict) and "model" in ckpt else ckpt
@@ -992,8 +1009,7 @@ def show_model_details(info: dict):
992
  """Drawn on every run (outside the cache) so it is always visible."""
993
  with st.sidebar.expander("Model details", expanded=False):
994
  st.caption(f"App version: `{APP_VERSION}`")
995
- st.caption(f"Repo: `{MODEL_REPO_ID}`")
996
- st.caption(f"File: `{MODEL_FILENAME}`")
997
  st.caption(f"Size: {info['size_mb']:.2f} MB")
998
  st.caption(f"Modified: {info['modified']}")
999
  st.code(info["sha256"], language=None)
 
968
 
969
  @st.cache_resource
970
  def load_model(device: str):
971
+ # Where the weights come from, in order:
972
+ # 1. MYOSEG_MODEL_PATH (env var / Space variable)
973
+ # 2. model_final.pt committed in the Space repo itself (next to src/ or inside it)
974
+ # 3. the Hugging Face model repo MODEL_REPO_ID (needs HF_TOKEN if that repo is private)
975
+ app_dir = Path(__file__).resolve().parent
976
+ candidates = [os.environ.get("MYOSEG_MODEL_PATH"),
977
+ str(app_dir.parent / MODEL_FILENAME), str(app_dir / MODEL_FILENAME)]
978
+ local = next((c for c in candidates if c and os.path.exists(c)), None)
979
+ source = local
980
+ if local is None:
981
+ try:
982
+ local = hf_hub_download(repo_id=MODEL_REPO_ID, filename=MODEL_FILENAME,
983
+ force_download=False, token=os.environ.get("HF_TOKEN"))
984
+ source = f"hf://{MODEL_REPO_ID}/{MODEL_FILENAME}"
985
+ except Exception as exc:
986
+ st.error(
987
+ f"Could not load the model. No `{MODEL_FILENAME}` was found in the Space, and "
988
+ f"downloading it from `{MODEL_REPO_ID}` failed ({type(exc).__name__}). "
989
+ "Fix: add model_final.pt to the Space repo, or make the model repo public, "
990
+ "or add a valid HF_TOKEN secret."
991
+ )
992
+ st.stop()
993
  file_sha = sha256_file(local)
994
  info = {
995
  "sha256": file_sha,
996
  "modified": time.ctime(os.path.getmtime(local)),
997
  "size_mb": round(os.path.getsize(local) / 1e6, 2),
998
+ "source": source,
999
  }
1000
  ckpt = torch.load(local, map_location=device, weights_only=False)
1001
  state = ckpt["model"] if isinstance(ckpt, dict) and "model" in ckpt else ckpt
 
1009
  """Drawn on every run (outside the cache) so it is always visible."""
1010
  with st.sidebar.expander("Model details", expanded=False):
1011
  st.caption(f"App version: `{APP_VERSION}`")
1012
+ st.caption(f"Loaded from: `{info.get('source', MODEL_REPO_ID)}`")
 
1013
  st.caption(f"Size: {info['size_mb']:.2f} MB")
1014
  st.caption(f"Modified: {info['modified']}")
1015
  st.code(info["sha256"], language=None)