Anushka Sharma commited on
Commit
14d30f7
·
unverified ·
1 Parent(s): 890a7e1

Implement model download from Hugging Face

Browse files

Add functionality to download model from Hugging Face if not found locally.

backend/snake_classifier_backend/classifier.py CHANGED
@@ -9,7 +9,9 @@ import torch
9
  import torch.nn as nn
10
  from torchvision import transforms, models
11
  from PIL import Image
12
-
 
 
13
  import snake_classifier_backend.config as config
14
 
15
  logger = logging.getLogger(__name__)
@@ -92,7 +94,18 @@ class SnakeClassifier:
92
 
93
  def _try_load(self) -> None:
94
  model_path: Path = config.MODEL_PATH
95
-
 
 
 
 
 
 
 
 
 
 
 
96
  if not model_path.exists():
97
  msg = f"Model file not found at '{model_path}'. Place venom.pt in backend/models/."
98
  logger.error(msg)
 
9
  import torch.nn as nn
10
  from torchvision import transforms, models
11
  from PIL import Image
12
+ import os
13
+ import shutil
14
+ from huggingface_hub import hf_hub_download
15
  import snake_classifier_backend.config as config
16
 
17
  logger = logging.getLogger(__name__)
 
94
 
95
  def _try_load(self) -> None:
96
  model_path: Path = config.MODEL_PATH
97
+
98
+ if not model_path.exists() and config.MODEL_HF_ID:
99
+ token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN")
100
+ try:
101
+ hf_file = hf_hub_download(repo_id=config.MODEL_HF_ID, filename=config.MODEL_FILENAME, token=token)
102
+ model_path.parent.mkdir(parent=True, exist_ok=True)
103
+ shutil.copy(hf_file,model_path)
104
+ logger.infor("Downloaded model from HF repo %s to %s", config.MODEL_HF_ID, model_path)
105
+ except Exception as exc:
106
+ logger.warning("Could not download model from HF repo %s:%s", config.MODEL_HF_ID, exc)
107
+
108
+
109
  if not model_path.exists():
110
  msg = f"Model file not found at '{model_path}'. Place venom.pt in backend/models/."
111
  logger.error(msg)