# Transformers installation ! pip install transformers datasets # To install from source instead of the last release, comment the command above and uncomment the following one. # ! pip install git+https://github.com/huggingface/transformers.git #@title from IPython.display import HTML HTML('') from transformers import pipeline classifier = pipeline("sentiment-analysis") classifier("We are very happy to show you the 🤗 Transformers library.") results = classifier(["We are very happy to show you the 🤗 Transformers library.", "We hope you don't hate it."]) for result in results: print(f"label: {result['label']}, with score: {round(result['score'], 4)}") from transformers import pipeline speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h", device=0) import datasets dataset = datasets.load_dataset("superb", name="asr", split="test") from transformers.pipelines.base import KeyDataset from tqdm.auto import tqdm for out in tqdm(speech_recognizer(KeyDataset(dataset, "file"))): print(out) model_name = "nlptown/bert-base-multilingual-uncased-sentiment" from transformers import AutoTokenizer, AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) from transformers import AutoTokenizer, TFAutoModelForSequenceClassification model = TFAutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer) classifier("Nous sommes très heureux de vous présenter la bibliothèque 🤗 Transformers.") #@title from IPython.display import HTML HTML('') from transformers import AutoTokenizer model_name = "nlptown/bert-base-multilingual-uncased-sentiment" tokenizer = AutoTokenizer.from_pretrained(model_name) encoding = tokenizer("We are very happy to show you the 🤗 Transformers library.") print(encoding) pt_batch = tokenizer( ["We are very happy to show you the 🤗 Transformers library.", "We hope you don't hate it."], padding=True, truncation=True, max_length=512, return_tensors="pt", ) tf_batch = tokenizer( ["We are very happy to show you the 🤗 Transformers library.", "We hope you don't hate it."], padding=True, truncation=True, max_length=512, return_tensors="tf", ) from transformers import AutoModelForSequenceClassification model_name = "nlptown/bert-base-multilingual-uncased-sentiment" pt_model = AutoModelForSequenceClassification.from_pretrained(model_name) from transformers import TFAutoModelForSequenceClassification model_name = "nlptown/bert-base-multilingual-uncased-sentiment" tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name) pt_outputs = pt_model(**pt_batch) tf_outputs = tf_model(tf_batch) from torch import nn pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1) print(pt_predictions) import tensorflow as tf tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1) print(tf_predictions) pt_save_directory = "./pt_save_pretrained" tokenizer.save_pretrained(pt_save_directory) pt_model.save_pretrained(pt_save_directory) tf_save_directory = "./tf_save_pretrained" tokenizer.save_pretrained(tf_save_directory) tf_model.save_pretrained(tf_save_directory) pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained") tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained") from transformers import AutoModel tokenizer = AutoTokenizer.from_pretrained(tf_save_directory) pt_model = AutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True) from transformers import TFAutoModel tokenizer = AutoTokenizer.from_pretrained(pt_save_directory) tf_model = TFAutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True)