iatechaways commited on
Commit
dcf17b8
·
2 Parent(s): 93905e029e5c2f

Merge branch 'main' of hf.co:spaces/iatechaways/mail_bot

Browse files
Files changed (1) hide show
  1. model_loader.py +9 -3
model_loader.py CHANGED
@@ -3,6 +3,7 @@ import os
3
  import torch
4
  import gc
5
  import json
 
6
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
  from peft import PeftModel
8
 
@@ -10,6 +11,9 @@ from peft import PeftModel
10
  BASE_MODEL = "meta-llama/Meta-Llama-3.2-3B" # or local path if you prefer
11
  ADAPTER = "iatechaways/crm_model" # your LoRA adapter on HF
12
 
 
 
 
13
  # NOTE: For ZeroGPU we LOAD on CPU and move to GPU only during inference.
14
  def load_model_cpu():
15
  """
@@ -17,7 +21,7 @@ def load_model_cpu():
17
  Return a dict that holds references to model and tokenizer.
18
  """
19
  # load tokenizer
20
- tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True)
21
  if tokenizer.pad_token is None:
22
  tokenizer.pad_token = tokenizer.eos_token
23
  tokenizer.padding_side = "right"
@@ -27,7 +31,8 @@ def load_model_cpu():
27
  BASE_MODEL,
28
  device_map={"": "cpu"},
29
  torch_dtype=torch.float32,
30
- low_cpu_mem_usage=True
 
31
  )
32
 
33
  model = PeftModel.from_pretrained(
@@ -36,7 +41,8 @@ def load_model_cpu():
36
  device_map={"": "cpu"},
37
  torch_dtype=torch.float32,
38
  is_trainable=False,
39
- low_cpu_mem_usage=True
 
40
  )
41
 
42
  # don't create pipeline here; we'll call generate with model directly
 
3
  import torch
4
  import gc
5
  import json
6
+ from huggingface_hub import login
7
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
8
  from peft import PeftModel
9
 
 
11
  BASE_MODEL = "meta-llama/Meta-Llama-3.2-3B" # or local path if you prefer
12
  ADAPTER = "iatechaways/crm_model" # your LoRA adapter on HF
13
 
14
+ # Get HF token from environment
15
+ HF_TOKEN = os.getenv("HF_TOKEN")
16
+
17
  # NOTE: For ZeroGPU we LOAD on CPU and move to GPU only during inference.
18
  def load_model_cpu():
19
  """
 
21
  Return a dict that holds references to model and tokenizer.
22
  """
23
  # load tokenizer
24
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, use_fast=True, token=HF_TOKEN)
25
  if tokenizer.pad_token is None:
26
  tokenizer.pad_token = tokenizer.eos_token
27
  tokenizer.padding_side = "right"
 
31
  BASE_MODEL,
32
  device_map={"": "cpu"},
33
  torch_dtype=torch.float32,
34
+ low_cpu_mem_usage=True,
35
+ token=HF_TOKEN
36
  )
37
 
38
  model = PeftModel.from_pretrained(
 
41
  device_map={"": "cpu"},
42
  torch_dtype=torch.float32,
43
  is_trainable=False,
44
+ low_cpu_mem_usage=True,
45
+ token=HF_TOKEN
46
  )
47
 
48
  # don't create pipeline here; we'll call generate with model directly