Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TRUST_REMOTE_CODE env from config when downloading custom models #91

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions download.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
import nltk
from config import TRUST_REMOTE_CODE
from transformers import (
AutoModel,
AutoTokenizer,
Expand Down Expand Up @@ -82,9 +83,9 @@ def quantization_config(onnx_cpu_arch: str):
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.save_pretrained(onnx_path)

def download_model(model_name: str, model_dir: str):
print(f"Downloading model {model_name} from huggingface model hub")
config = AutoConfig.from_pretrained(model_name)
def download_model(model_name: str, model_dir: str, trust_remote_code: bool = False):
print(f"Downloading model {model_name} from huggingface model hub ({trust_remote_code=})")
config = AutoConfig.from_pretrained(model_name, trust_remote_code=trust_remote_code)
model_type = config.to_dict()['model_type']

if (model_type is not None and model_type == "t5") or use_sentence_transformers_vectorizer.lower() == "true":
Expand All @@ -100,11 +101,11 @@ def download_model(model_name: str, model_dir: str):
model = klass_architecture.from_pretrained(model_name)
except AttributeError:
print(f"{config.architectures[0]} not found in transformers, fallback to AutoModel")
model = AutoModel.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name, trust_remote_code=trust_remote_code)
else:
model = AutoModel.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name, trust_remote_code=trust_remote_code)

tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=trust_remote_code)

model.save_pretrained(model_dir)
tokenizer.save_pretrained(model_dir)
Expand All @@ -114,4 +115,4 @@ def download_model(model_name: str, model_dir: str):
if onnx_runtime == "true":
download_onnx_model(model_name, model_dir)
else:
download_model(model_name, model_dir)
download_model(model_name, model_dir, TRUST_REMOTE_CODE)
Loading