Issue with bert models from hugging face in reticulate

58 Views Asked by At

I am trying to use reticulate to run a Bert model from hugging face, but I keep getting the error: AttributeError: 'BertConfig' object has no attribute 'pretrained_model'

When I try and run the code in Python, it works fine, but no matter which way I try to run it in R, I get an error. See the Python code below:

# Load model directly
from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained("Wellcome/WellcomeBertMesh", trust_remote_code=True)
model = AutoModel.from_pretrained("Wellcome/WellcomeBertMesh", trust_remote_code=True)

def MeshHelp (word_list):
    labels_list = []
    for word in word_list:
        inputs = tokenizer([word], padding="max_length")
        labels = model(**inputs, return_labels=True)
        labels_list.extend(labels)
    return labels_list

It works when I run the Python code outside of R, but not in R.

I have tried the following with reticulate:

# source_python
source_python('script.py')

# py_run_string
py_run_string('from transformers import AutoTokenizer, AutoModel')
py_run_string('tokenizer = AutoTokenizer.from_pretrained("Wellcome/WellcomeBertMesh", trust_remote_code=True)')
py_run_string('model = AutoModel.from_pretrained("Wellcome/WellcomeBertMesh", trust_remote_code=True)')

# pipelines
transformers$pipeline("Wellcome/WellcomeBertMesh")
0

There are 0 best solutions below