Siddh12334 commited on
Commit
b3382c5
·
1 Parent(s): 40bdaf7

fix: load model snapshots from writable cache

Browse files

Download base and adapter snapshots into the Space cache before loading so Transformers does not probe remote repos through /.cache.

Made-with: Cursor

Files changed (1) hide show
  1. environment/model_inference.py +21 -6
environment/model_inference.py CHANGED
@@ -77,20 +77,35 @@ def _load_model():
77
  bnb_4bit_use_double_quant=True,
78
  )
79
 
80
- peft_config = PeftConfig.from_pretrained(MODEL_ID, cache_dir=str(_CACHE_DIR))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  base_model = AutoModelForCausalLM.from_pretrained(
82
- peft_config.base_model_name_or_path,
83
  device_map="auto" if torch.cuda.is_available() else "cpu",
84
  torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
85
  quantization_config=quantization_config,
86
  low_cpu_mem_usage=True,
87
- cache_dir=str(_CACHE_DIR),
88
  )
89
- _TOKENIZER = AutoTokenizer.from_pretrained(MODEL_ID, cache_dir=str(_CACHE_DIR))
90
  _MODEL = PeftModel.from_pretrained(
91
  base_model,
92
- MODEL_ID,
93
- cache_dir=str(_CACHE_DIR),
94
  )
95
  _MODEL.eval()
96
  return _MODEL, _TOKENIZER
 
77
  bnb_4bit_use_double_quant=True,
78
  )
79
 
80
+ from huggingface_hub import snapshot_download
81
+
82
+ adapter_path = snapshot_download(
83
+ repo_id=MODEL_ID,
84
+ cache_dir=str(_CACHE_DIR),
85
+ )
86
+ peft_config = PeftConfig.from_pretrained(adapter_path)
87
+ base_path = snapshot_download(
88
+ repo_id=peft_config.base_model_name_or_path,
89
+ cache_dir=str(_CACHE_DIR),
90
+ allow_patterns=[
91
+ "*.json",
92
+ "*.safetensors",
93
+ "*.model",
94
+ "*.txt",
95
+ "*.jinja",
96
+ ],
97
+ )
98
  base_model = AutoModelForCausalLM.from_pretrained(
99
+ base_path,
100
  device_map="auto" if torch.cuda.is_available() else "cpu",
101
  torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
102
  quantization_config=quantization_config,
103
  low_cpu_mem_usage=True,
 
104
  )
105
+ _TOKENIZER = AutoTokenizer.from_pretrained(adapter_path)
106
  _MODEL = PeftModel.from_pretrained(
107
  base_model,
108
+ adapter_path,
 
109
  )
110
  _MODEL.eval()
111
  return _MODEL, _TOKENIZER