|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "In order to run this notebook, you will need to install the huggingface library with the following command: `pip install transformers`" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [ |
| 15 | + { |
| 16 | + "name": "stderr", |
| 17 | + "output_type": "stream", |
| 18 | + "text": [ |
| 19 | + "/opt/homebrew/Caskroom/miniforge/base/envs/torchtext39/lib/python3.9/site-packages/tqdm-4.64.0-py3.9.egg/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", |
| 20 | + " from .autonotebook import tqdm as notebook_tqdm\n" |
| 21 | + ] |
| 22 | + } |
| 23 | + ], |
| 24 | + "source": [ |
| 25 | + "from transformers import T5ForConditionalGeneration, T5Tokenizer, BartForConditionalGeneration, BartTokenizer, GPT2LMHeadModel, GPT2Tokenizer\n", |
| 26 | + "from torchtext.prototype.generate import GenerationUtil" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 2, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "t5 = T5ForConditionalGeneration.from_pretrained(\"t5-base\")\n", |
| 36 | + "bart = BartForConditionalGeneration.from_pretrained(\"facebook/bart-large-cnn\")\n", |
| 37 | + "gpt2 = GPT2LMHeadModel.from_pretrained(\"gpt2\")" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": 3, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [ |
| 45 | + { |
| 46 | + "name": "stderr", |
| 47 | + "output_type": "stream", |
| 48 | + "text": [ |
| 49 | + "/opt/homebrew/Caskroom/miniforge/base/envs/torchtext39/lib/python3.9/site-packages/transformers/models/t5/tokenization_t5.py:164: FutureWarning: This tokenizer was incorrectly instantiated with a model max length of 512 which will be corrected in Transformers v5.\n", |
| 50 | + "For now, this behavior is kept to avoid breaking backwards compatibility when padding/encoding with `truncation is True`.\n", |
| 51 | + "- Be aware that you SHOULD NOT rely on t5-base automatically truncating your input to 512 when padding/encoding.\n", |
| 52 | + "- If you want to encode/pad to sequences longer than 512 you can either instantiate this tokenizer with `model_max_length` or pass `max_length` when encoding/padding.\n", |
| 53 | + "- To avoid this warning, please instantiate this tokenizer with `model_max_length` set to your preferred value.\n", |
| 54 | + " warnings.warn(\n" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "name": "stdout", |
| 59 | + "output_type": "stream", |
| 60 | + "text": [ |
| 61 | + "['owning a dog is good for you, according to studies. a dog is']\n" |
| 62 | + ] |
| 63 | + } |
| 64 | + ], |
| 65 | + "source": [ |
| 66 | + "# Testing Huggingface's T5\n", |
| 67 | + "test_sequence = [\"summarize: studies have shown that owning a dog is good for you\"]\n", |
| 68 | + "generative_hf_t5 = GenerationUtil(t5, is_encoder_decoder=True, is_huggingface_model=True)\n", |
| 69 | + "t5_tokenizer = T5Tokenizer.from_pretrained(\"t5-base\")\n", |
| 70 | + "test_sequence_tk = t5_tokenizer(test_sequence, return_tensors=\"pt\").input_ids\n", |
| 71 | + "tokens = generative_hf_t5.generate(test_sequence_tk, max_len=20, pad_idx=t5.config.pad_token_id)\n", |
| 72 | + "print(t5_tokenizer.batch_decode(tokens, skip_special_tokens=True))" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": 4, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [ |
| 80 | + { |
| 81 | + "name": "stdout", |
| 82 | + "output_type": "stream", |
| 83 | + "text": [ |
| 84 | + "['PG. PG&E said it scheduled the blackouts in response to forecasts for high winds.']\n" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "# Testing Huggingface's BART\n", |
| 90 | + "test_sequence = [\"PG&E stated it scheduled the blackouts in response to forecasts for high winds \"\n", |
| 91 | + " \"amid dry conditions. The aim is to reduce the risk of wildfires. Nearly 800 thousand customers were \"\n", |
| 92 | + " \"scheduled to be affected by the shutoffs which were expected to last through at least midday tomorrow.\"]\n", |
| 93 | + "generative_hf_bart = GenerationUtil(bart, is_encoder_decoder=True, is_huggingface_model=True)\n", |
| 94 | + "bart_tokenizer = BartTokenizer.from_pretrained(\"facebook/bart-large-cnn\")\n", |
| 95 | + "test_sequence_tk = bart_tokenizer(test_sequence, return_tensors=\"pt\").input_ids\n", |
| 96 | + "tokens = generative_hf_bart.generate(test_sequence_tk, max_len=20, pad_idx=bart.config.pad_token_id)\n", |
| 97 | + "print(bart_tokenizer.batch_decode(tokens, skip_special_tokens=True))" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 5, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [ |
| 105 | + { |
| 106 | + "name": "stdout", |
| 107 | + "output_type": "stream", |
| 108 | + "text": [ |
| 109 | + "[\"I enjoy walking with my cute dog, but I'm not sure if I'll ever be able to\"]\n" |
| 110 | + ] |
| 111 | + } |
| 112 | + ], |
| 113 | + "source": [ |
| 114 | + "# Testing Huggingface's GPT2\n", |
| 115 | + "test_sequence = [\"I enjoy walking with my cute dog\"]\n", |
| 116 | + "generative_hf_gpt2 = GenerationUtil(gpt2, is_encoder_decoder=False, is_huggingface_model=True)\n", |
| 117 | + "gpt2_tokenizer = GPT2Tokenizer.from_pretrained(\"gpt2\")\n", |
| 118 | + "test_sequence_tk = gpt2_tokenizer(test_sequence, return_tensors=\"pt\").input_ids\n", |
| 119 | + "tokens = generative_hf_gpt2.generate(test_sequence_tk, max_len=20, pad_idx=gpt2.config.pad_token_id)\n", |
| 120 | + "print(gpt2_tokenizer.batch_decode(tokens, skip_special_tokens=True))" |
| 121 | + ] |
| 122 | + } |
| 123 | + ], |
| 124 | + "metadata": { |
| 125 | + "kernelspec": { |
| 126 | + "display_name": "Python 3.9.13 ('torchtext39')", |
| 127 | + "language": "python", |
| 128 | + "name": "python3" |
| 129 | + }, |
| 130 | + "language_info": { |
| 131 | + "codemirror_mode": { |
| 132 | + "name": "ipython", |
| 133 | + "version": 3 |
| 134 | + }, |
| 135 | + "file_extension": ".py", |
| 136 | + "mimetype": "text/x-python", |
| 137 | + "name": "python", |
| 138 | + "nbconvert_exporter": "python", |
| 139 | + "pygments_lexer": "ipython3", |
| 140 | + "version": "3.9.13" |
| 141 | + }, |
| 142 | + "orig_nbformat": 4, |
| 143 | + "vscode": { |
| 144 | + "interpreter": { |
| 145 | + "hash": "63c8862cb56f124e3ee7674b73de745eeb216416a9b24f78d1fcb7c775bff1b7" |
| 146 | + } |
| 147 | + } |
| 148 | + }, |
| 149 | + "nbformat": 4, |
| 150 | + "nbformat_minor": 2 |
| 151 | +} |
0 commit comments