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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"source": [
|
| 20 |
+
"\n",
|
| 21 |
+
"# Import the required library\n",
|
| 22 |
+
"from transformers import pipeline\n",
|
| 23 |
+
"\n",
|
| 24 |
+
"# Define the task and model\n",
|
| 25 |
+
"task = \"text-generation\"\n",
|
| 26 |
+
"model_name = \"gpt2\"\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"# Define the input text, maximum output length, and the number of return sequences\n",
|
| 29 |
+
"input_text = \"he draw to the town \"\n",
|
| 30 |
+
"max_output_length = 50\n",
|
| 31 |
+
"num_of_return_sequences = 1\n",
|
| 32 |
+
"\n",
|
| 33 |
+
"# Initialize the text generation pipeline\n",
|
| 34 |
+
"text_generator = pipeline(\n",
|
| 35 |
+
" task,\n",
|
| 36 |
+
" model=model_name\n",
|
| 37 |
+
")\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"# Generate text sequences\n",
|
| 40 |
+
"generated_texts = text_generator(\n",
|
| 41 |
+
" input_text,\n",
|
| 42 |
+
" max_length=max_output_length,\n",
|
| 43 |
+
" num_return_sequences=num_of_return_sequences\n",
|
| 44 |
+
")\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"# Print the generated text sequences\n",
|
| 47 |
+
"for i, text in enumerate(generated_texts):\n",
|
| 48 |
+
" print(f\"Generated Text {i+1}: {text['generated_text']}\")"
|
| 49 |
+
],
|
| 50 |
+
"metadata": {
|
| 51 |
+
"colab": {
|
| 52 |
+
"base_uri": "https://localhost:8080/"
|
| 53 |
+
},
|
| 54 |
+
"id": "0rNsuHRYznUQ",
|
| 55 |
+
"outputId": "dae8e691-a1f0-4d3a-c521-086522ef9e9d"
|
| 56 |
+
},
|
| 57 |
+
"execution_count": 6,
|
| 58 |
+
"outputs": [
|
| 59 |
+
{
|
| 60 |
+
"output_type": "stream",
|
| 61 |
+
"name": "stderr",
|
| 62 |
+
"text": [
|
| 63 |
+
"Truncation was not explicitly activated but `max_length` is provided a specific value, please use `truncation=True` to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to `truncation`.\n",
|
| 64 |
+
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"output_type": "stream",
|
| 69 |
+
"name": "stdout",
|
| 70 |
+
"text": [
|
| 71 |
+
"Generated Text 1: he draw to the town Ṣuṣṭṭhi, the wind was blowing gently in this direction towards the east, and so the wind was as much as the wind of a mountain. Ṣuṣ�\n"
|
| 72 |
+
]
|
| 73 |
+
}
|
| 74 |
+
]
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"cell_type": "code",
|
| 78 |
+
"source": [
|
| 79 |
+
"# Print the generated text sequences\n",
|
| 80 |
+
"for i, text in enumerate(generated_texts):\n",
|
| 81 |
+
" print(f\"Generated Text {i+1}: {text['generated_text']}\")"
|
| 82 |
+
],
|
| 83 |
+
"metadata": {
|
| 84 |
+
"colab": {
|
| 85 |
+
"base_uri": "https://localhost:8080/"
|
| 86 |
+
},
|
| 87 |
+
"id": "wZ6DmKrhsLpC",
|
| 88 |
+
"outputId": "ce00c00d-b839-49f4-bfc2-59028be6a057"
|
| 89 |
+
},
|
| 90 |
+
"execution_count": 7,
|
| 91 |
+
"outputs": [
|
| 92 |
+
{
|
| 93 |
+
"output_type": "stream",
|
| 94 |
+
"name": "stdout",
|
| 95 |
+
"text": [
|
| 96 |
+
"Generated Text 1: he draw to the town Ṣuṣṭṭhi, the wind was blowing gently in this direction towards the east, and so the wind was as much as the wind of a mountain. Ṣuṣ�\n"
|
| 97 |
+
]
|
| 98 |
+
}
|
| 99 |
+
]
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"cell_type": "code",
|
| 103 |
+
"source": [
|
| 104 |
+
"# Print the generated text sequences\n",
|
| 105 |
+
"for i, text in enumerate(generated_texts):\n",
|
| 106 |
+
" print(f\"Generated Text {1}: {text['generated_text']}\")"
|
| 107 |
+
],
|
| 108 |
+
"metadata": {
|
| 109 |
+
"colab": {
|
| 110 |
+
"base_uri": "https://localhost:8080/"
|
| 111 |
+
},
|
| 112 |
+
"id": "2AdczanhsfUz",
|
| 113 |
+
"outputId": "9bf873ae-fae0-495a-b5c5-ee890fcc044c"
|
| 114 |
+
},
|
| 115 |
+
"execution_count": 9,
|
| 116 |
+
"outputs": [
|
| 117 |
+
{
|
| 118 |
+
"output_type": "stream",
|
| 119 |
+
"name": "stdout",
|
| 120 |
+
"text": [
|
| 121 |
+
"Generated Text 1: he draw to the town Ṣuṣṭṭhi, the wind was blowing gently in this direction towards the east, and so the wind was as much as the wind of a mountain. Ṣuṣ�\n"
|
| 122 |
+
]
|
| 123 |
+
}
|
| 124 |
+
]
|
| 125 |
+
},
|
| 126 |
+
{
|
| 127 |
+
"cell_type": "code",
|
| 128 |
+
"source": [
|
| 129 |
+
"generated_texts"
|
| 130 |
+
],
|
| 131 |
+
"metadata": {
|
| 132 |
+
"colab": {
|
| 133 |
+
"base_uri": "https://localhost:8080/"
|
| 134 |
+
},
|
| 135 |
+
"id": "JZEbsQthsuFZ",
|
| 136 |
+
"outputId": "3e047f46-fd3e-492a-c5a0-723fdda985cd"
|
| 137 |
+
},
|
| 138 |
+
"execution_count": 10,
|
| 139 |
+
"outputs": [
|
| 140 |
+
{
|
| 141 |
+
"output_type": "execute_result",
|
| 142 |
+
"data": {
|
| 143 |
+
"text/plain": [
|
| 144 |
+
"[{'generated_text': 'he draw to the town Ṣuṣṭṭhi, the wind was blowing gently in this direction towards the east, and so the wind was as much as the wind of a mountain. Ṣuṣ�'}]"
|
| 145 |
+
]
|
| 146 |
+
},
|
| 147 |
+
"metadata": {},
|
| 148 |
+
"execution_count": 10
|
| 149 |
+
}
|
| 150 |
+
]
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"cell_type": "code",
|
| 154 |
+
"source": [
|
| 155 |
+
"from transformers import GPT2LMHeadModel, GPT2Tokenizer\n",
|
| 156 |
+
"\n",
|
| 157 |
+
"# Define the model and tokenizer\n",
|
| 158 |
+
"model_name = \"gpt2\"\n",
|
| 159 |
+
"model = GPT2LMHeadModel.from_pretrained(model_name)\n",
|
| 160 |
+
"tokenizer = GPT2Tokenizer.from_pretrained(model_name)\n",
|
| 161 |
+
"\n",
|
| 162 |
+
"# Save the model and tokenizer\n",
|
| 163 |
+
"model.save_pretrained(\"model-gpt2-t2t\")\n",
|
| 164 |
+
"tokenizer.save_pretrained(\"tokenizer-gpt2-t2t\")\n"
|
| 165 |
+
],
|
| 166 |
+
"metadata": {
|
| 167 |
+
"colab": {
|
| 168 |
+
"base_uri": "https://localhost:8080/"
|
| 169 |
+
},
|
| 170 |
+
"id": "V0TTg8u0tJOK",
|
| 171 |
+
"outputId": "aba3223e-d603-486d-daed-2c61f667fd65"
|
| 172 |
+
},
|
| 173 |
+
"execution_count": 12,
|
| 174 |
+
"outputs": [
|
| 175 |
+
{
|
| 176 |
+
"output_type": "execute_result",
|
| 177 |
+
"data": {
|
| 178 |
+
"text/plain": [
|
| 179 |
+
"('tokenizer-gpt2-t2t/tokenizer_config.json',\n",
|
| 180 |
+
" 'tokenizer-gpt2-t2t/special_tokens_map.json',\n",
|
| 181 |
+
" 'tokenizer-gpt2-t2t/vocab.json',\n",
|
| 182 |
+
" 'tokenizer-gpt2-t2t/merges.txt',\n",
|
| 183 |
+
" 'tokenizer-gpt2-t2t/added_tokens.json')"
|
| 184 |
+
]
|
| 185 |
+
},
|
| 186 |
+
"metadata": {},
|
| 187 |
+
"execution_count": 12
|
| 188 |
+
}
|
| 189 |
+
]
|
| 190 |
+
},
|
| 191 |
+
{
|
| 192 |
+
"cell_type": "code",
|
| 193 |
+
"source": [],
|
| 194 |
+
"metadata": {
|
| 195 |
+
"id": "g-Scy6tvt_RB"
|
| 196 |
+
},
|
| 197 |
+
"execution_count": null,
|
| 198 |
+
"outputs": []
|
| 199 |
+
}
|
| 200 |
+
]
|
| 201 |
+
}
|
config.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "gpt2",
|
| 3 |
+
"activation_function": "gelu_new",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"GPT2LMHeadModel"
|
| 6 |
+
],
|
| 7 |
+
"attn_pdrop": 0.1,
|
| 8 |
+
"bos_token_id": 50256,
|
| 9 |
+
"embd_pdrop": 0.1,
|
| 10 |
+
"eos_token_id": 50256,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"layer_norm_epsilon": 1e-05,
|
| 13 |
+
"model_type": "gpt2",
|
| 14 |
+
"n_ctx": 1024,
|
| 15 |
+
"n_embd": 768,
|
| 16 |
+
"n_head": 12,
|
| 17 |
+
"n_inner": null,
|
| 18 |
+
"n_layer": 12,
|
| 19 |
+
"n_positions": 1024,
|
| 20 |
+
"reorder_and_upcast_attn": false,
|
| 21 |
+
"resid_pdrop": 0.1,
|
| 22 |
+
"scale_attn_by_inverse_layer_idx": false,
|
| 23 |
+
"scale_attn_weights": true,
|
| 24 |
+
"summary_activation": null,
|
| 25 |
+
"summary_first_dropout": 0.1,
|
| 26 |
+
"summary_proj_to_labels": true,
|
| 27 |
+
"summary_type": "cls_index",
|
| 28 |
+
"summary_use_proj": true,
|
| 29 |
+
"task_specific_params": {
|
| 30 |
+
"text-generation": {
|
| 31 |
+
"do_sample": true,
|
| 32 |
+
"max_length": 50
|
| 33 |
+
}
|
| 34 |
+
},
|
| 35 |
+
"torch_dtype": "float32",
|
| 36 |
+
"transformers_version": "4.41.2",
|
| 37 |
+
"use_cache": true,
|
| 38 |
+
"vocab_size": 50257
|
| 39 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 50256,
|
| 4 |
+
"eos_token_id": 50256,
|
| 5 |
+
"transformers_version": "4.41.2"
|
| 6 |
+
}
|