Spaces:
Sleeping
Sleeping
gpt2
Browse files- app/model_loader.py +44 -44
app/model_loader.py
CHANGED
|
@@ -3,42 +3,11 @@ import os
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
from peft import PeftModel
|
| 5 |
|
| 6 |
-
def load_model():
|
| 7 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
-
if not hf_token:
|
| 9 |
-
raise RuntimeError("HF_TOKEN not set.")
|
| 10 |
-
|
| 11 |
-
# Use a user-writable cache directory (important for Docker non-root)
|
| 12 |
-
HF_CACHE = os.path.expanduser("~/.cache/huggingface")
|
| 13 |
-
os.makedirs(HF_CACHE, exist_ok=True)
|
| 14 |
-
|
| 15 |
-
os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
|
| 16 |
-
os.environ["HF_HOME"] = HF_CACHE
|
| 17 |
-
|
| 18 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
-
"meta-llama/Llama-2-7b-chat-hf",
|
| 20 |
-
use_auth_token=hf_token,
|
| 21 |
-
cache_dir="/tmp/hf_cache",
|
| 22 |
-
torch_dtype="auto",
|
| 23 |
-
device_map="auto"
|
| 24 |
-
)
|
| 25 |
-
model = PeftModel.from_pretrained(
|
| 26 |
-
base_model,
|
| 27 |
-
"BrainGPT/BrainGPT-7B-v0.1",
|
| 28 |
-
use_auth_token=hf_token,
|
| 29 |
-
cache_dir="/tmp/hf_cache"
|
| 30 |
-
)
|
| 31 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 32 |
-
"meta-llama/Llama-2-7b-chat-hf",
|
| 33 |
-
use_auth_token=hf_token,
|
| 34 |
-
cache_dir="/tmp/hf_cache"
|
| 35 |
-
)
|
| 36 |
-
return model, tokenizer
|
| 37 |
-
|
| 38 |
-
# import os
|
| 39 |
-
# from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 40 |
-
|
| 41 |
# def load_model():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# # Use a user-writable cache directory (important for Docker non-root)
|
| 43 |
# HF_CACHE = os.path.expanduser("~/.cache/huggingface")
|
| 44 |
# os.makedirs(HF_CACHE, exist_ok=True)
|
|
@@ -46,16 +15,47 @@ def load_model():
|
|
| 46 |
# os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
|
| 47 |
# os.environ["HF_HOME"] = HF_CACHE
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# tokenizer = AutoTokenizer.from_pretrained(
|
| 52 |
-
#
|
| 53 |
-
#
|
|
|
|
| 54 |
# )
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
# cache_dir=HF_CACHE
|
| 59 |
-
# )
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
from peft import PeftModel
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# def load_model():
|
| 7 |
+
# hf_token = os.getenv("HF_TOKEN")
|
| 8 |
+
# if not hf_token:
|
| 9 |
+
# raise RuntimeError("HF_TOKEN not set.")
|
| 10 |
+
|
| 11 |
# # Use a user-writable cache directory (important for Docker non-root)
|
| 12 |
# HF_CACHE = os.path.expanduser("~/.cache/huggingface")
|
| 13 |
# os.makedirs(HF_CACHE, exist_ok=True)
|
|
|
|
| 15 |
# os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
|
| 16 |
# os.environ["HF_HOME"] = HF_CACHE
|
| 17 |
|
| 18 |
+
# base_model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
# "meta-llama/Llama-2-7b-chat-hf",
|
| 20 |
+
# use_auth_token=hf_token,
|
| 21 |
+
# cache_dir="/tmp/hf_cache",
|
| 22 |
+
# torch_dtype="auto",
|
| 23 |
+
# device_map="auto"
|
| 24 |
+
# )
|
| 25 |
+
# model = PeftModel.from_pretrained(
|
| 26 |
+
# base_model,
|
| 27 |
+
# "BrainGPT/BrainGPT-7B-v0.1",
|
| 28 |
+
# use_auth_token=hf_token,
|
| 29 |
+
# cache_dir="/tmp/hf_cache"
|
| 30 |
+
# )
|
| 31 |
# tokenizer = AutoTokenizer.from_pretrained(
|
| 32 |
+
# "meta-llama/Llama-2-7b-chat-hf",
|
| 33 |
+
# use_auth_token=hf_token,
|
| 34 |
+
# cache_dir="/tmp/hf_cache"
|
| 35 |
# )
|
| 36 |
+
# return model, tokenizer
|
| 37 |
|
| 38 |
+
import os
|
| 39 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
def load_model():
|
| 42 |
+
# Use a user-writable cache directory (important for Docker non-root)
|
| 43 |
+
HF_CACHE = os.path.expanduser("~/.cache/huggingface")
|
| 44 |
+
os.makedirs(HF_CACHE, exist_ok=True)
|
| 45 |
+
|
| 46 |
+
os.environ["TRANSFORMERS_CACHE"] = HF_CACHE
|
| 47 |
+
os.environ["HF_HOME"] = HF_CACHE
|
| 48 |
+
|
| 49 |
+
model_name = "gpt2"
|
| 50 |
+
|
| 51 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 52 |
+
model_name,
|
| 53 |
+
cache_dir=HF_CACHE
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 57 |
+
model_name,
|
| 58 |
+
cache_dir=HF_CACHE
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return model, tokenizer
|