Skip to content

Instantly share code, notes, and snippets.

@Rocketknight1
Last active August 3, 2022 11:24
Show Gist options
  • Save Rocketknight1/b479d57e3d2f94420b11ca8d319cc68f to your computer and use it in GitHub Desktop.
Save Rocketknight1/b479d57e3d2f94420b11ca8d319cc68f to your computer and use it in GitHub Desktop.
# This is a new feature, so make sure to update to the latest version of transformers!
# You will also need to pip install tensorflow_text
import tensorflow as tf
from transformers import TFAutoModel, TFBertTokenizer
class EndToEndModel(tf.keras.Model):
def __init__(self, checkpoint):
super().__init__()
self.tokenizer = TFBertTokenizer.from_pretrained(checkpoint)
self.model = TFAutoModel.from_pretrained(checkpoint)
def call(self, inputs):
tokenized = self.tokenizer(inputs)
return self.model(**tokenized)
model = EndToEndModel(checkpoint='bert-base-cased')
test_inputs = [
'This is a test sentence!',
'This is another one!'
]
model.predict(test_inputs) # Pass strings straight to model!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment