AndaiMD commited on
Commit
2c353fb
·
1 Parent(s): e570e97
Files changed (1) hide show
  1. app/model_loader.py +48 -26
app/model_loader.py CHANGED
@@ -1,35 +1,57 @@
1
 
2
- import os
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- from peft import PeftModel
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- # Define a safe cache location
7
- HF_CACHE = "/tmp/huggingface_cache"
8
- os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
9
- os.environ["HF_HOME"] = HF_CACHE
10
- os.makedirs(HF_CACHE, exist_ok=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def load_model():
13
- hf_token = os.getenv("HF_TOKEN")
14
- if not hf_token:
15
- raise RuntimeError("HF_TOKEN not set.")
16
-
17
- base_model = AutoModelForCausalLM.from_pretrained(
18
- "meta-llama/Llama-2-7b-chat-hf",
19
- use_auth_token=hf_token,
20
- cache_dir="/tmp/hf_cache",
21
- torch_dtype="auto",
22
- device_map="auto"
23
- )
24
- model = PeftModel.from_pretrained(
25
- base_model,
26
- "BrainGPT/BrainGPT-7B-v0.1",
27
- use_auth_token=hf_token,
28
  cache_dir="/tmp/hf_cache"
29
  )
30
- tokenizer = AutoTokenizer.from_pretrained(
31
- "meta-llama/Llama-2-7b-chat-hf",
32
- use_auth_token=hf_token,
33
  cache_dir="/tmp/hf_cache"
34
  )
 
35
  return model, tokenizer
 
1
 
2
+ # import os
3
+ # from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ # from peft import PeftModel
5
+
6
+ # # Define a safe cache location
7
+ # HF_CACHE = "/tmp/huggingface_cache"
8
+ # os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
9
+ # os.environ["HF_HOME"] = HF_CACHE
10
+ # os.makedirs(HF_CACHE, exist_ok=True)
11
+
12
+ # def load_model():
13
+ # hf_token = os.getenv("HF_TOKEN")
14
+ # if not hf_token:
15
+ # raise RuntimeError("HF_TOKEN not set.")
16
 
17
+ # base_model = AutoModelForCausalLM.from_pretrained(
18
+ # "meta-llama/Llama-2-7b-chat-hf",
19
+ # use_auth_token=hf_token,
20
+ # cache_dir="/tmp/hf_cache",
21
+ # torch_dtype="auto",
22
+ # device_map="auto"
23
+ # )
24
+ # model = PeftModel.from_pretrained(
25
+ # base_model,
26
+ # "BrainGPT/BrainGPT-7B-v0.1",
27
+ # use_auth_token=hf_token,
28
+ # cache_dir="/tmp/hf_cache"
29
+ # )
30
+ # tokenizer = AutoTokenizer.from_pretrained(
31
+ # "meta-llama/Llama-2-7b-chat-hf",
32
+ # use_auth_token=hf_token,
33
+ # cache_dir="/tmp/hf_cache"
34
+ # )
35
+ # return model, tokenizer
36
+
37
+ from transformers import AutoModelForCausalLM, AutoTokenizer
38
+ import os
39
 
40
  def load_model():
41
+ model_name = "gpt2"
42
+
43
+ # Set a cache directory that's safe
44
+ os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
45
+ os.makedirs("/tmp/hf_cache", exist_ok=True)
46
+
47
+ tokenizer = AutoTokenizer.from_pretrained(
48
+ model_name,
 
 
 
 
 
 
 
49
  cache_dir="/tmp/hf_cache"
50
  )
51
+
52
+ model = AutoModelForCausalLM.from_pretrained(
53
+ model_name,
54
  cache_dir="/tmp/hf_cache"
55
  )
56
+
57
  return model, tokenizer