{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: transformers in /srv/conda/envs/notebook/lib/python3.9/site-packages (4.26.1)\n", "Requirement already satisfied: filelock in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (3.8.0)\n", "Requirement already satisfied: numpy>=1.17 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (1.23.3)\n", "Requirement already satisfied: requests in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (2.28.1)\n", "Requirement already satisfied: packaging>=20.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (21.3)\n", "Requirement already satisfied: pyyaml>=5.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (5.4.1)\n", "Requirement already satisfied: regex!=2019.12.17 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (2022.10.31)\n", "Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (0.13.2)\n", "Requirement already satisfied: tqdm>=4.27 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (4.64.1)\n", "Requirement already satisfied: huggingface-hub<1.0,>=0.11.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from transformers) (0.12.1)\n", "Requirement already satisfied: typing-extensions>=3.7.4.3 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from huggingface-hub<1.0,>=0.11.0->transformers) (4.3.0)\n", "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from packaging>=20.0->transformers) (3.0.9)\n", "Requirement already satisfied: certifi>=2017.4.17 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (2022.9.14)\n", "Requirement already satisfied: charset-normalizer<3,>=2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (2.1.1)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (1.26.11)\n", "Requirement already satisfied: idna<4,>=2.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->transformers) (3.3)\n" ] } ], "source": [ "!pip install transformers" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('fine_tuned_tokenizer/tokenizer_config.json',\n", " 'fine_tuned_tokenizer/special_tokens_map.json',\n", " 'fine_tuned_tokenizer/vocab.json',\n", " 'fine_tuned_tokenizer/merges.txt',\n", " 'fine_tuned_tokenizer/added_tokens.json')" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n", "from torch.utils.data import Dataset, DataLoader\n", "\n", "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "\n", "\n", "class TextDataset(Dataset):\n", " def __init__(self, file_path, tokenizer, max_seq_length):\n", " with open(file_path, 'r', encoding='utf-8') as f:\n", " self.text = f.read()\n", " self.tokenizer = tokenizer\n", " self.max_seq_length = max_seq_length\n", " self.segments = []\n", "\n", " for line in self.text.split('\\n'):\n", " if len(line) > 0:\n", " if len(line) > self.max_seq_length:\n", " # Split long lines into shorter segments\n", " segments = [line[i:i+self.max_seq_length] for i in range(0, len(line), self.max_seq_length)]\n", " self.segments.extend(segments)\n", " else:\n", " self.segments.append(line)\n", "\n", " def __len__(self):\n", " return len(self.segments)\n", "\n", " def __getitem__(self, idx):\n", " self.tokenizer.pad_token_id = 0\n", " segment = self.segments[idx]\n", " input_ids = self.tokenizer.encode(segment, add_special_tokens=True)\n", " target_ids = input_ids[1:] + [self.tokenizer.pad_token_id]\n", "\n", " if not input_ids:\n", " return None\n", " \n", "\n", " # Pad the input sequence to the same length\n", " if len(input_ids) > self.max_seq_length:\n", " input_ids = input_ids[:self.max_seq_length]\n", " target_ids = target_ids[:self.max_seq_length]\n", " else:\n", " padding_length = self.max_seq_length - len(input_ids)\n", " input_ids = input_ids + [self.tokenizer.pad_token_id] * padding_length\n", " target_ids = target_ids + [self.tokenizer.pad_token_id] * padding_length\n", " #print(\"segment:\",segment,\"\\n input:\", input_ids, \"\\n targets\" ,target_ids)\n", " #print(\"\\r segment:\",segment, end=\"\")\n", " \n", " return torch.tensor(input_ids), torch.tensor(target_ids)\n", "\n", "\n", "# Initialize the tokenizer and the pre-trained model\n", "tokenizer = GPT2Tokenizer.from_pretrained('gpt2')\n", "model = GPT2LMHeadModel.from_pretrained('gpt2').to(device)\n", "\n", "\n", "# Define the path to the training text file\n", "train_file = 'text.txt'\n", "\n", "# Define the training and validation datasets\n", "max_seq_length = 512\n", "\n", "train_dataset = TextDataset(train_file, tokenizer, max_seq_length)\n", "valid_dataset = TextDataset(train_file, tokenizer, max_seq_length)\n", "\n", "# Define the training hyperparameters\n", "batch_size = 8\n", "num_epochs = 7\n", "learning_rate = 5e-4\n", "\n", "# Define the data loader for the training and validation datasets\n", "train_loader = DataLoader(train_dataset,batch_size=batch_size, shuffle=True)\n", "valid_loader = DataLoader(valid_dataset,batch_size=batch_size, shuffle=True)\n", "\n", "# Define the loss function and the optimizer\n", "loss_fn = torch.nn.CrossEntropyLoss()\n", "optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)\n", "\n", "tokenizer.save_pretrained('fine_tuned_tokenizer')\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Epoch 1/7, Train Loss: 0.5704409927129745, Valid Loss: 0.3897556330009205, lr: 0.0005\n", "Epoch 2/7, Train Loss: 0.403159585849541, Valid Loss: 0.24347291268953464, lr: 0.0001\n", "Epoch 3/7, Train Loss: 0.27275778398644634, Valid Loss: 0.13453135721203757, lr: 2e-05\n", "Epoch 4/7, Train Loss: 0.1717792261482739, Valid Loss: 0.07003633981775038, lr: 4.000000000000001e-06\n", "Epoch 5/7, Train Loss: 0.10565993448764813, Valid Loss: 0.03993069042065522, lr: 8.000000000000002e-07\n", "Epoch 6/7, Train Loss: 0.0703794076675322, Valid Loss: 0.02531332855408148, lr: 1.6000000000000003e-07\n", "Epoch 7/7, Train Batch 7/82, Train Loss: 0.0036747022645502556" ] } ], "source": [ "# Train the model for a fixed number of epochs\n", "for epoch in range(num_epochs):\n", " # Training loop\n", " model.train()\n", " train_loss = 0\n", " i=0\n", " for batch in train_loader:\n", " i+=1\n", " input_ids, target_ids = batch\n", " input_ids, target_ids = input_ids.to(device), target_ids.to(device)\n", " optimizer.zero_grad()\n", " outputs = model(input_ids, labels=target_ids)\n", " loss = loss_fn(outputs.logits.view(-1, outputs.logits.size(-1)), target_ids.view(-1))\n", " loss.backward()\n", " optimizer.step()\n", " train_loss += loss.item()\n", " print(f'\\rEpoch {epoch+1}/{num_epochs}, Train Batch {i}/{len(train_loader)}, Train Loss: {train_loss/len(train_loader)}',end=\"\")\n", "\n", "\n", " # Validation loop\n", " model.eval()\n", " valid_loss = 0\n", " with torch.no_grad():\n", " i=0\n", " for batch in valid_loader:\n", " i+=1\n", " input_ids, target_ids = batch\n", " input_ids, target_ids = input_ids.to(device), target_ids.to(device)\n", " outputs = model(input_ids, labels=target_ids)\n", " loss = loss_fn(outputs.logits.view(-1, outputs.logits.size(-1)), target_ids.view(-1))\n", " valid_loss += loss.item()\n", " print(f'\\rEpoch {epoch+1}/{num_epochs}, Valid Batch {i}/{len(valid_loader)}, Valid Loss: {valid_loss/len(valid_loader)}',end=\"\")\n", "\n", " print(f'\\rEpoch {epoch+1}/{num_epochs}, Train Loss: {train_loss/len(train_loader)}, Valid Loss: {valid_loss/len(valid_loader)}, lr: {learning_rate}',end=\"\\n\")\n", " learning_rate /= 5\n", "model.save_pretrained('brunosan/GPT2-impactscience')\n", "tokenizer.save_pretrained('brunosan/GPT2-impactscience')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model.save_pretrained('brunosan/GPT2-impactscience')\n", "tokenizer.save_pretrained('brunosan/GPT2-impactscience')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n" ] }, { "data": { "text/plain": [ "'Antonio, who died in 2015, was a brilliant engineer who was able to master complex networks of increasingly complex projects. He was also very involved on airborne radars, ground planes, and hovercrafts. In the mid-90s, several European nations, some being primary producers of CFC, began considering regulations, or even creating public funds to assist their efforts. While extremely conscious of the environmental impact, at the same time, he also advocated for non-proliferation, peaceful'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n", "\n", "# Define the device to run the model on\n", "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "\n", "# Load the fine-tuned model\n", "model_path = 'brunosan/GPT2-impactscience'\n", "tokenizer = GPT2Tokenizer.from_pretrained(model_path)\n", "model = GPT2LMHeadModel.from_pretrained(model_path).to(device)\n", "\n", "# Set the pad_token_id to the same value as the unk_token_id\n", "#model.config.pad_token_id = tokenizer.unk_token_id\n", "\n", "# Set the generation parameters\n", "max_length = 100\n", "num_beams = 5\n", "no_repeat_ngram_size = 2\n", "temperature = 1.0\n", "\n", "# Generate text using beam search, n-grams, and other techniques\n", "prompt = \"The impact of climate change on \"\n", "\n", "def generate(prompt):\n", " input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)\n", " attention_mask = torch.ones(input_ids.shape, dtype=torch.long, device=device)\n", " outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask,\n", " max_length=max_length, num_beams=num_beams,\n", " no_repeat_ngram_size=no_repeat_ngram_size,\n", " temperature=temperature, do_sample=True, top_p=0.95,\n", " top_k=50)\n", "\n", " # Convert the generated output to string format\n", " generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", " return generated_text\n", "\n", "generate(prompt)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ece98d6d9c9149ca8f4caee4b2ad9e34", "version_major": 2, "version_minor": 0 }, "text/plain": [ "Upload 1 LFS files: 0%| | 0/1 [00:00=10.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (10.3)\n", "Requirement already satisfied: numpy in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (1.23.3)\n", "Collecting altair>=4.2.0\n", " Downloading altair-4.2.2-py3-none-any.whl (813 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m813.6/813.6 kB\u001b[0m \u001b[31m34.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hCollecting mdit-py-plugins<=0.3.3\n", " Downloading mdit_py_plugins-0.3.3-py3-none-any.whl (50 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.5/50.5 kB\u001b[0m \u001b[31m3.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: fsspec in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (2022.8.2)\n", "Collecting pydub\n", " Downloading pydub-0.25.1-py2.py3-none-any.whl (32 kB)\n", "Requirement already satisfied: httpx in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.23.0)\n", "Requirement already satisfied: pandas in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (1.4.4)\n", "Requirement already satisfied: aiofiles in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.8.0)\n", "Requirement already satisfied: matplotlib in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (3.5.3)\n", "Requirement already satisfied: orjson in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (3.8.0)\n", "Requirement already satisfied: fastapi in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.85.0)\n", "Requirement already satisfied: pillow in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (9.2.0)\n", "Collecting pycryptodome\n", " Downloading pycryptodome-3.17-cp35-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m75.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: jinja2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (3.1.2)\n", "Collecting markdown-it-py[linkify]>=2.0.0\n", " Downloading markdown_it_py-2.2.0-py3-none-any.whl (84 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m84.5/84.5 kB\u001b[0m \u001b[31m7.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hRequirement already satisfied: uvicorn in /srv/conda/envs/notebook/lib/python3.9/site-packages (from gradio) (0.18.3)\n", "Requirement already satisfied: jsonschema>=3.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from altair>=4.2.0->gradio) (4.16.0)\n", "Requirement already satisfied: toolz in /srv/conda/envs/notebook/lib/python3.9/site-packages (from altair>=4.2.0->gradio) (0.12.0)\n", "Requirement already satisfied: entrypoints in /srv/conda/envs/notebook/lib/python3.9/site-packages (from altair>=4.2.0->gradio) (0.4)\n", "Collecting mdurl~=0.1\n", " Downloading mdurl-0.1.2-py3-none-any.whl (10.0 kB)\n", "Collecting linkify-it-py<3,>=1\n", " Downloading linkify_it_py-2.0.0-py3-none-any.whl (19 kB)\n", "Requirement already satisfied: python-dateutil>=2.8.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from pandas->gradio) (2.8.2)\n", "Requirement already satisfied: pytz>=2020.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from pandas->gradio) (2022.2.1)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (1.7.2)\n", "Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (2.1.1)\n", "Requirement already satisfied: attrs>=17.3.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (22.1.0)\n", "Requirement already satisfied: aiosignal>=1.1.2 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (1.2.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (6.0.2)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (4.0.2)\n", "Requirement already satisfied: frozenlist>=1.1.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from aiohttp->gradio) (1.3.1)\n", "Requirement already satisfied: starlette==0.20.4 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from fastapi->gradio) (0.20.4)\n", "Requirement already satisfied: anyio<5,>=3.4.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from starlette==0.20.4->fastapi->gradio) (3.6.1)\n", "Requirement already satisfied: httpcore<0.16.0,>=0.15.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (0.15.0)\n", "Requirement already satisfied: sniffio in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (1.3.0)\n", "Requirement already satisfied: certifi in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (2022.9.14)\n", "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from httpx->gradio) (1.5.0)\n", "Requirement already satisfied: fonttools>=4.22.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (4.37.2)\n", "Requirement already satisfied: cycler>=0.10 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (0.11.0)\n", "Requirement already satisfied: pyparsing>=2.2.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (3.0.9)\n", "Requirement already satisfied: packaging>=20.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (21.3)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from matplotlib->gradio) (1.4.4)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->gradio) (1.26.11)\n", "Requirement already satisfied: idna<4,>=2.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from requests->gradio) (3.3)\n", "Requirement already satisfied: click>=7.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from uvicorn->gradio) (8.0.4)\n", "Requirement already satisfied: h11>=0.8 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from uvicorn->gradio) (0.12.0)\n", "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from jsonschema>=3.0->altair>=4.2.0->gradio) (0.18.1)\n", "Collecting uc-micro-py\n", " Downloading uc_micro_py-1.0.1-py3-none-any.whl (6.2 kB)\n", "Requirement already satisfied: six>=1.5 in /srv/conda/envs/notebook/lib/python3.9/site-packages (from python-dateutil>=2.8.1->pandas->gradio) (1.16.0)\n", "Building wheels for collected packages: ffmpy\n", " Building wheel for ffmpy (setup.py) ... \u001b[?25ldone\n", "\u001b[?25h Created wheel for ffmpy: filename=ffmpy-0.3.0-py3-none-any.whl size=4693 sha256=46e8ead5afd51fafc1a56d6493e03067d54d956c9883722536d9642763e1d1f4\n", " Stored in directory: /home/jovyan/.cache/pip/wheels/91/e2/96/f676aa08bfd789328c6576cd0f1fde4a3d686703bb0c247697\n", "Successfully built ffmpy\n", "Installing collected packages: pydub, ffmpy, uc-micro-py, python-multipart, pycryptodome, mdurl, markdown-it-py, linkify-it-py, mdit-py-plugins, altair, gradio\n", "Successfully installed altair-4.2.2 ffmpy-0.3.0 gradio-3.19.1 linkify-it-py-2.0.0 markdown-it-py-2.2.0 mdit-py-plugins-0.3.3 mdurl-0.1.2 pycryptodome-3.17 pydub-0.25.1 python-multipart-0.0.6 uc-micro-py-1.0.1\n" ] } ], "source": [ "!pip install gradio" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/inputs.py:27: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", " warnings.warn(\n", "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `numeric` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/srv/conda/envs/notebook/lib/python3.9/site-packages/gradio/outputs.py:22: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\n", "Running on public URL: https://6989ff3075913a1f9b.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" }, { "name": "stderr", "output_type": "stream", "text": [ "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n", "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n" ] } ], "source": [ "import gradio as gr\n", "import torch\n", "from transformers import GPT2LMHeadModel, GPT2Tokenizer\n", "\n", "# Load the fine-tuned model and tokenizer\n", "model_path = 'fine_tuned_model'\n", "tokenizer_path = 'fine_tuned_tokenizer'\n", "\n", "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "tokenizer = GPT2Tokenizer.from_pretrained(tokenizer_path)\n", "model = GPT2LMHeadModel.from_pretrained(model_path).to(device)\n", "\n", "# Define the generation function\n", "def generate_text(prompt):\n", " input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)\n", " attention_mask = torch.ones(input_ids.shape, dtype=torch.long, device=device)\n", " outputs = model.generate(input_ids=input_ids, attention_mask=attention_mask,\n", " max_length=100, num_beams=9,\n", " no_repeat_ngram_size=2,\n", " temperature=1.0, do_sample=True,\n", " top_p=0.95, top_k=50)\n", "\n", " generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n", " return generated_text\n", "\n", "# Create a Gradio interface\n", "input_text = gr.inputs.Textbox(lines=2, label=\"Enter the starting text\")\n", "output_text = gr.outputs.Textbox(label=\"Generated Text\")\n", "\n", "interface = gr.Interface(fn=generate_text, inputs=input_text, outputs=output_text,\n", " title=\"GPT-2 Impact Science Text Generator\", description=\"Generate text using a fine-tuned GPT-2 model onthe Impact Science book.\")\n", "\n", "# Export the Gradio interface to the Hugging Face Model Hub\n", "interface.launch(share=True)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:notebook] *", "language": "python", "name": "conda-env-notebook-py" }, "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.9.13" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "43343113618a7691a2af3d61372ef21fdddd71fdb8d3292e4208750f8afc007e" } } }, "nbformat": 4, "nbformat_minor": 2 }