DheivaCodes commited on
Commit
889635c
·
verified ·
1 Parent(s): 0ca18b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -14
app.py CHANGED
@@ -360,18 +360,23 @@ def load_image_model():
360
  if pth is None:
361
  print(f"⚠️ Image model not found. models/={os.listdir(MODELS_DIR) if os.path.exists(MODELS_DIR) else 'missing'}")
362
  return None
363
- net=tv_models.efficientnet_b0(weights=None)
364
- net.classifier[1]=nn.Linear(net.classifier[1].in_features,2)
365
- ckpt=torch.load(pth,map_location=DEVICE,weights_only=False)
366
- if isinstance(ckpt,dict):
367
- state=(ckpt.get("model_state") or ckpt.get("model_state_dict")
368
- or ckpt.get("state_dict") or ckpt)
369
- else:
370
- state=ckpt
371
- net.load_state_dict(state,strict=True)
372
- net.to(DEVICE).eval()
373
- print(f"✅ Image model loaded: {pth}")
374
- return net
 
 
 
 
 
375
 
376
  IMAGE_MODEL=load_image_model()
377
 
@@ -685,7 +690,24 @@ def predict_clinical(*args):
685
 
686
  def predict_image_single(image):
687
  if IMAGE_MODEL is None:
688
- return ("❌ Image model not loaded.\n\nUpload `best_model.pth` to the `models/` folder."),None,None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
689
  if image is None:
690
  return "⚠️ Please upload an ultrasound image.",None,None
691
  try:
@@ -714,7 +736,9 @@ def predict_image_single(image):
714
 
715
  def predict_image_batch(files):
716
  if IMAGE_MODEL is None:
717
- return "❌ Image model not loaded.",None
 
 
718
  if not files:
719
  return "⚠️ Upload at least one image.",None
720
  rows=[]; pcos_c=0; notp_c=0
 
360
  if pth is None:
361
  print(f"⚠️ Image model not found. models/={os.listdir(MODELS_DIR) if os.path.exists(MODELS_DIR) else 'missing'}")
362
  return None
363
+ try:
364
+ net=tv_models.efficientnet_b0(weights=None)
365
+ net.classifier[1]=nn.Linear(net.classifier[1].in_features,2)
366
+ ckpt=torch.load(pth,map_location=DEVICE,weights_only=False)
367
+ if isinstance(ckpt,dict):
368
+ state=(ckpt.get("model_state") or ckpt.get("model_state_dict")
369
+ or ckpt.get("state_dict") or ckpt)
370
+ else:
371
+ state=ckpt
372
+ net.load_state_dict(state,strict=True)
373
+ net.to(DEVICE).eval()
374
+ print(f"✅ Image model loaded: {pth}")
375
+ return net
376
+ except Exception as _img_err:
377
+ print(f"⚠️ Image model failed to load ({_img_err}). "
378
+ f"Re-save best_model.pth in Colab with current PyTorch and re-upload.")
379
+ return None
380
 
381
  IMAGE_MODEL=load_image_model()
382
 
 
690
 
691
  def predict_image_single(image):
692
  if IMAGE_MODEL is None:
693
+ fix = (
694
+ "## ❌ Image model not loaded\n\n"
695
+ "**Cause:** `best_model.pth` was saved with an old PyTorch version "
696
+ "that Python 3.13 cannot unpickle (`STACK_GLOBAL requires str`).\n\n"
697
+ "**Fix — run in Colab then re-upload `best_model_fixed.pth` → rename to `best_model.pth`:**\n"
698
+ "```python\n"
699
+ "import torch\nfrom torchvision import models\nimport torch.nn as nn\n\n"
700
+ "old = torch.load('/content/drive/MyDrive/PCOS_RESULTS/EfficientNetB0/best_model.pth',\n"
701
+ " map_location='cpu', weights_only=False)\n"
702
+ "net = models.efficientnet_b0(weights=None)\n"
703
+ "net.classifier[1] = nn.Linear(net.classifier[1].in_features, 2)\n"
704
+ "net.load_state_dict(old['model_state'])\n"
705
+ "torch.save({'model_state': net.state_dict()}, '/content/best_model_fixed.pth')\n"
706
+ "print('Done — download and upload to models/ on HF')\n"
707
+ "```\n\n"
708
+ "The **Clinical tab works normally** — only the image tabs are affected."
709
+ )
710
+ return fix, None, None
711
  if image is None:
712
  return "⚠️ Please upload an ultrasound image.",None,None
713
  try:
 
736
 
737
  def predict_image_batch(files):
738
  if IMAGE_MODEL is None:
739
+ return ("## ❌ Image model not loaded\n\n"
740
+ "Re-save `best_model.pth` in Colab (see Single Image tab for instructions)\n\n"
741
+ "The **Clinical tab works normally**."), None
742
  if not files:
743
  return "⚠️ Upload at least one image.",None
744
  rows=[]; pcos_c=0; notp_c=0