{ "cells": [ { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Collecting transformers\n", " Using cached transformers-4.46.0-py3-none-any.whl.metadata (44 kB)\n", "Collecting torch\n", " Using cached torch-2.5.0-cp310-cp310-win_amd64.whl.metadata (28 kB)\n", "Collecting filelock (from transformers)\n", " Using cached filelock-3.16.1-py3-none-any.whl.metadata (2.9 kB)\n", "Collecting huggingface-hub<1.0,>=0.23.2 (from transformers)\n", " Using cached huggingface_hub-0.26.1-py3-none-any.whl.metadata (13 kB)\n", "Requirement already satisfied: numpy>=1.17 in c:\\python310\\lib\\site-packages (from transformers) (2.0.2)\n", "Requirement already satisfied: packaging>=20.0 in c:\\users\\devcloud\\appdata\\roaming\\python\\python310\\site-packages (from transformers) (24.1)\n", "Collecting pyyaml>=5.1 (from transformers)\n", " Using cached PyYAML-6.0.2-cp310-cp310-win_amd64.whl.metadata (2.1 kB)\n", "Collecting regex!=2019.12.17 (from transformers)\n", " Using cached regex-2024.9.11-cp310-cp310-win_amd64.whl.metadata (41 kB)\n", "Collecting requests (from transformers)\n", " Using cached requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)\n", "Collecting safetensors>=0.4.1 (from transformers)\n", " Using cached safetensors-0.4.5-cp310-none-win_amd64.whl.metadata (3.9 kB)\n", "Collecting tokenizers<0.21,>=0.20 (from transformers)\n", " Using cached tokenizers-0.20.1-cp310-none-win_amd64.whl.metadata (6.9 kB)\n", "Collecting tqdm>=4.27 (from transformers)\n", " Using cached tqdm-4.66.5-py3-none-any.whl.metadata (57 kB)\n", "Requirement already satisfied: typing-extensions>=4.8.0 in c:\\users\\devcloud\\appdata\\roaming\\python\\python310\\site-packages (from torch) (4.12.2)\n", "Collecting networkx (from torch)\n", " Using cached networkx-3.4.2-py3-none-any.whl.metadata (6.3 kB)\n", "Collecting jinja2 (from torch)\n", " Using cached jinja2-3.1.4-py3-none-any.whl.metadata (2.6 kB)\n", "Collecting fsspec (from torch)\n", " Using cached fsspec-2024.10.0-py3-none-any.whl.metadata (11 kB)\n", "Collecting sympy==1.13.1 (from torch)\n", " Using cached sympy-1.13.1-py3-none-any.whl.metadata (12 kB)\n", "Collecting mpmath<1.4,>=1.1.0 (from sympy==1.13.1->torch)\n", " Using cached mpmath-1.3.0-py3-none-any.whl.metadata (8.6 kB)\n", "Requirement already satisfied: colorama in c:\\users\\devcloud\\appdata\\roaming\\python\\python310\\site-packages (from tqdm>=4.27->transformers) (0.4.6)\n", "Collecting MarkupSafe>=2.0 (from jinja2->torch)\n", " Using cached MarkupSafe-3.0.2-cp310-cp310-win_amd64.whl.metadata (4.1 kB)\n", "Collecting charset-normalizer<4,>=2 (from requests->transformers)\n", " Using cached charset_normalizer-3.4.0-cp310-cp310-win_amd64.whl.metadata (34 kB)\n", "Collecting idna<4,>=2.5 (from requests->transformers)\n", " Using cached idna-3.10-py3-none-any.whl.metadata (10 kB)\n", "Collecting urllib3<3,>=1.21.1 (from requests->transformers)\n", " Using cached urllib3-2.2.3-py3-none-any.whl.metadata (6.5 kB)\n", "Collecting certifi>=2017.4.17 (from requests->transformers)\n", " Using cached certifi-2024.8.30-py3-none-any.whl.metadata (2.2 kB)\n", "Using cached transformers-4.46.0-py3-none-any.whl (10.0 MB)\n", "Using cached torch-2.5.0-cp310-cp310-win_amd64.whl (203.1 MB)\n", "Using cached sympy-1.13.1-py3-none-any.whl (6.2 MB)\n", "Using cached huggingface_hub-0.26.1-py3-none-any.whl (447 kB)\n", "Using cached fsspec-2024.10.0-py3-none-any.whl (179 kB)\n", "Using cached PyYAML-6.0.2-cp310-cp310-win_amd64.whl (161 kB)\n", "Using cached regex-2024.9.11-cp310-cp310-win_amd64.whl (274 kB)\n", "Using cached safetensors-0.4.5-cp310-none-win_amd64.whl (285 kB)\n", "Using cached tokenizers-0.20.1-cp310-none-win_amd64.whl (2.4 MB)\n", "Using cached tqdm-4.66.5-py3-none-any.whl (78 kB)\n", "Using cached filelock-3.16.1-py3-none-any.whl (16 kB)\n", "Using cached jinja2-3.1.4-py3-none-any.whl (133 kB)\n", "Downloading networkx-3.4.2-py3-none-any.whl (1.7 MB)\n", " ---------------------------------------- 0.0/1.7 MB ? eta -:--:--\n", " ---------------------------------------- 1.7/1.7 MB 91.3 MB/s eta 0:00:00\n", "Using cached requests-2.32.3-py3-none-any.whl (64 kB)\n", "Using cached certifi-2024.8.30-py3-none-any.whl (167 kB)\n", "Using cached charset_normalizer-3.4.0-cp310-cp310-win_amd64.whl (102 kB)\n", "Using cached idna-3.10-py3-none-any.whl (70 kB)\n", "Using cached MarkupSafe-3.0.2-cp310-cp310-win_amd64.whl (15 kB)\n", "Using cached mpmath-1.3.0-py3-none-any.whl (536 kB)\n", "Using cached urllib3-2.2.3-py3-none-any.whl (126 kB)\n", "Installing collected packages: mpmath, urllib3, tqdm, sympy, safetensors, regex, pyyaml, networkx, MarkupSafe, idna, fsspec, filelock, charset-normalizer, certifi, requests, jinja2, torch, huggingface-hub, tokenizers, transformers\n", "Successfully installed MarkupSafe-3.0.2 certifi-2024.8.30 charset-normalizer-3.4.0 filelock-3.16.1 fsspec-2024.10.0 huggingface-hub-0.26.1 idna-3.10 jinja2-3.1.4 mpmath-1.3.0 networkx-3.4.2 pyyaml-6.0.2 regex-2024.9.11 requests-2.32.3 safetensors-0.4.5 sympy-1.13.1 tokenizers-0.20.1 torch-2.5.0 tqdm-4.66.5 transformers-4.46.0 urllib3-2.2.3\n" ] } ], "source": [ "!pip install transformers torch" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Once upon a time, the world was a place of great beauty and great danger. The world of the gods was the place where the great gods were born, and where they were to live.\n", "\n", "The world that was created was not the same as the one that is now. It was an endless, endless world. And the Gods were not born of nothing. They were created of a single, single thing. That was why the universe was so beautiful. Because the cosmos was made of two\n" ] } ], "source": [ "import warnings\n", "warnings.filterwarnings('ignore')\n", "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n", "\n", "# Load pre-trained model and tokenizer\n", "model_name = \"gpt2\" # You can change this to the specific model you downloaded\n", "tokenizer = GPT2Tokenizer.from_pretrained(model_name)\n", "tokenizer.pad_token = tokenizer.eos_token\n", "tokenizer.pad_token_id = tokenizer.eos_token_id\n", "model = GPT2LMHeadModel.from_pretrained(model_name)\n", "\n", "def generate_text(prompt, max_length=100):\n", " # Encode the input and add the EOS token\n", " inputs = tokenizer(prompt, return_tensors='pt', padding=True, truncation=True)\n", " \n", " # Generate text\n", " output = model.generate(inputs.input_ids,\n", " attention_mask=inputs.attention_mask,\n", " max_length=max_length,\n", " num_return_sequences=1,\n", " no_repeat_ngram_size=2,\n", " top_k=50,\n", " top_p=0.95,\n", " temperature=0.7,\n", " pad_token_id=tokenizer.pad_token_id)\n", " \n", " # Decode and return the output\n", " return tokenizer.decode(output[0], skip_special_tokens=True)\n", "\n", "# Test the function\n", "prompt = \"Once upon a time\"\n", "generated_text = generate_text(prompt)\n", "print(generated_text)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.11" } }, "nbformat": 4, "nbformat_minor": 2 }