Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- S19_app.ipynb +277 -0
- app.py +56 -0
S19_app.ipynb
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"!pip install gradio"
|
| 21 |
+
],
|
| 22 |
+
"metadata": {
|
| 23 |
+
"colab": {
|
| 24 |
+
"base_uri": "https://localhost:8080/"
|
| 25 |
+
},
|
| 26 |
+
"id": "M-8-kXX3AmPE",
|
| 27 |
+
"outputId": "2b8cb780-2dd4-45ee-cc1f-864056678a9d"
|
| 28 |
+
},
|
| 29 |
+
"execution_count": 1,
|
| 30 |
+
"outputs": [
|
| 31 |
+
{
|
| 32 |
+
"output_type": "stream",
|
| 33 |
+
"name": "stdout",
|
| 34 |
+
"text": [
|
| 35 |
+
"Requirement already satisfied: gradio in /usr/local/lib/python3.10/dist-packages (4.36.1)\n",
|
| 36 |
+
"Requirement already satisfied: aiofiles<24.0,>=22.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (23.2.1)\n",
|
| 37 |
+
"Requirement already satisfied: altair<6.0,>=4.2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.2.2)\n",
|
| 38 |
+
"Requirement already satisfied: fastapi in /usr/local/lib/python3.10/dist-packages (from gradio) (0.111.0)\n",
|
| 39 |
+
"Requirement already satisfied: ffmpy in /usr/local/lib/python3.10/dist-packages (from gradio) (0.3.2)\n",
|
| 40 |
+
"Requirement already satisfied: gradio-client==1.0.1 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.0.1)\n",
|
| 41 |
+
"Requirement already satisfied: httpx>=0.24.1 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.27.0)\n",
|
| 42 |
+
"Requirement already satisfied: huggingface-hub>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.23.3)\n",
|
| 43 |
+
"Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.4.0)\n",
|
| 44 |
+
"Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.4)\n",
|
| 45 |
+
"Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.5)\n",
|
| 46 |
+
"Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
|
| 47 |
+
"Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.25.2)\n",
|
| 48 |
+
"Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.10.5)\n",
|
| 49 |
+
"Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (24.1)\n",
|
| 50 |
+
"Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.3)\n",
|
| 51 |
+
"Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0)\n",
|
| 52 |
+
"Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.7.3)\n",
|
| 53 |
+
"Requirement already satisfied: pydub in /usr/local/lib/python3.10/dist-packages (from gradio) (0.25.1)\n",
|
| 54 |
+
"Requirement already satisfied: python-multipart>=0.0.9 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.0.9)\n",
|
| 55 |
+
"Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1)\n",
|
| 56 |
+
"Requirement already satisfied: ruff>=0.2.2 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.4.9)\n",
|
| 57 |
+
"Requirement already satisfied: semantic-version~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.10.0)\n",
|
| 58 |
+
"Requirement already satisfied: tomlkit==0.12.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.12.0)\n",
|
| 59 |
+
"Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.12.3)\n",
|
| 60 |
+
"Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.12.2)\n",
|
| 61 |
+
"Requirement already satisfied: urllib3~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.7)\n",
|
| 62 |
+
"Requirement already satisfied: uvicorn>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.30.1)\n",
|
| 63 |
+
"Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.0.1->gradio) (2023.6.0)\n",
|
| 64 |
+
"Requirement already satisfied: websockets<12.0,>=10.0 in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.0.1->gradio) (11.0.3)\n",
|
| 65 |
+
"Requirement already satisfied: entrypoints in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.4)\n",
|
| 66 |
+
"Requirement already satisfied: jsonschema>=3.0 in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (4.19.2)\n",
|
| 67 |
+
"Requirement already satisfied: toolz in /usr/local/lib/python3.10/dist-packages (from altair<6.0,>=4.2.0->gradio) (0.12.1)\n",
|
| 68 |
+
"Requirement already satisfied: anyio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (3.7.1)\n",
|
| 69 |
+
"Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (2024.6.2)\n",
|
| 70 |
+
"Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (1.0.5)\n",
|
| 71 |
+
"Requirement already satisfied: idna in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (3.7)\n",
|
| 72 |
+
"Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (1.3.1)\n",
|
| 73 |
+
"Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n",
|
| 74 |
+
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (3.14.0)\n",
|
| 75 |
+
"Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0)\n",
|
| 76 |
+
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (4.66.4)\n",
|
| 77 |
+
"Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.2.1)\n",
|
| 78 |
+
"Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.12.1)\n",
|
| 79 |
+
"Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.53.0)\n",
|
| 80 |
+
"Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5)\n",
|
| 81 |
+
"Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.2)\n",
|
| 82 |
+
"Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",
|
| 83 |
+
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2023.4)\n",
|
| 84 |
+
"Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n",
|
| 85 |
+
"Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (0.7.0)\n",
|
| 86 |
+
"Requirement already satisfied: pydantic-core==2.18.4 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (2.18.4)\n",
|
| 87 |
+
"Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
|
| 88 |
+
"Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
|
| 89 |
+
"Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (13.7.1)\n",
|
| 90 |
+
"Requirement already satisfied: starlette<0.38.0,>=0.37.2 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (0.37.2)\n",
|
| 91 |
+
"Requirement already satisfied: fastapi-cli>=0.0.2 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (0.0.4)\n",
|
| 92 |
+
"Requirement already satisfied: ujson!=4.0.2,!=4.1.0,!=4.2.0,!=4.3.0,!=5.0.0,!=5.1.0,>=4.0.1 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (5.10.0)\n",
|
| 93 |
+
"Requirement already satisfied: email_validator>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (2.1.2)\n",
|
| 94 |
+
"Requirement already satisfied: dnspython>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from email_validator>=2.0.0->fastapi->gradio) (2.6.1)\n",
|
| 95 |
+
"Requirement already satisfied: attrs>=22.2.0 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (23.2.0)\n",
|
| 96 |
+
"Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (2023.12.1)\n",
|
| 97 |
+
"Requirement already satisfied: referencing>=0.28.4 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.35.1)\n",
|
| 98 |
+
"Requirement already satisfied: rpds-py>=0.7.1 in /usr/local/lib/python3.10/dist-packages (from jsonschema>=3.0->altair<6.0,>=4.2.0->gradio) (0.18.1)\n",
|
| 99 |
+
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n",
|
| 100 |
+
"Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
|
| 101 |
+
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.16.1)\n",
|
| 102 |
+
"Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio->httpx>=0.24.1->gradio) (1.2.1)\n",
|
| 103 |
+
"Requirement already satisfied: httptools>=0.5.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (0.6.1)\n",
|
| 104 |
+
"Requirement already satisfied: python-dotenv>=0.13 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (1.0.1)\n",
|
| 105 |
+
"Requirement already satisfied: uvloop!=0.15.0,!=0.15.1,>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (0.19.0)\n",
|
| 106 |
+
"Requirement already satisfied: watchfiles>=0.13 in /usr/local/lib/python3.10/dist-packages (from uvicorn>=0.14.0->gradio) (0.22.0)\n",
|
| 107 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.3->gradio) (3.3.2)\n",
|
| 108 |
+
"Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n"
|
| 109 |
+
]
|
| 110 |
+
}
|
| 111 |
+
]
|
| 112 |
+
},
|
| 113 |
+
{
|
| 114 |
+
"cell_type": "code",
|
| 115 |
+
"execution_count": 2,
|
| 116 |
+
"metadata": {
|
| 117 |
+
"id": "243RszHVACUE"
|
| 118 |
+
},
|
| 119 |
+
"outputs": [],
|
| 120 |
+
"source": [
|
| 121 |
+
"import gradio as gr\n",
|
| 122 |
+
"import os\n",
|
| 123 |
+
"\n",
|
| 124 |
+
"import torch\n",
|
| 125 |
+
"import torch.nn as nn\n",
|
| 126 |
+
"import torch.nn.functional as F\n",
|
| 127 |
+
"from model import BigramLanguageModel, ModelConfig"
|
| 128 |
+
]
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"cell_type": "code",
|
| 132 |
+
"source": [
|
| 133 |
+
"with open('input.txt', 'r', encoding='utf-8') as f:\n",
|
| 134 |
+
" text = f.read()\n",
|
| 135 |
+
"\n",
|
| 136 |
+
"# unique characters from text\n",
|
| 137 |
+
"chars = sorted(list(set(text)))\n",
|
| 138 |
+
"vocab_size = len(chars)\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"# create a mapping from chars to ints\n",
|
| 141 |
+
"stoi = { ch:i for i,ch in enumerate(chars) }\n",
|
| 142 |
+
"itos = { i:ch for i,ch in enumerate(chars) }\n",
|
| 143 |
+
"encode = lambda s: [stoi[c] for c in s] # encoder: take a string, output a list of integers\n",
|
| 144 |
+
"decode = lambda l: ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string"
|
| 145 |
+
],
|
| 146 |
+
"metadata": {
|
| 147 |
+
"id": "3UjZaD3NBJN0"
|
| 148 |
+
},
|
| 149 |
+
"execution_count": 3,
|
| 150 |
+
"outputs": []
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"cell_type": "code",
|
| 154 |
+
"source": [
|
| 155 |
+
"# load model\n",
|
| 156 |
+
"model_spec = torch.load(\"ckpt.pt\", map_location=torch.device('cpu'))\n",
|
| 157 |
+
"model_args = model_spec['model_args']\n",
|
| 158 |
+
"model_weights = model_spec['model']\n",
|
| 159 |
+
"modelconf = ModelConfig(**model_args)\n",
|
| 160 |
+
"trained_model = BigramLanguageModel(modelconf)\n",
|
| 161 |
+
"trained_model.load_state_dict(model_weights)"
|
| 162 |
+
],
|
| 163 |
+
"metadata": {
|
| 164 |
+
"colab": {
|
| 165 |
+
"base_uri": "https://localhost:8080/"
|
| 166 |
+
},
|
| 167 |
+
"id": "xL-YJTFSBfq9",
|
| 168 |
+
"outputId": "5081861c-c025-4b06-f148-cde79bbcf4e1"
|
| 169 |
+
},
|
| 170 |
+
"execution_count": 4,
|
| 171 |
+
"outputs": [
|
| 172 |
+
{
|
| 173 |
+
"output_type": "execute_result",
|
| 174 |
+
"data": {
|
| 175 |
+
"text/plain": [
|
| 176 |
+
"<All keys matched successfully>"
|
| 177 |
+
]
|
| 178 |
+
},
|
| 179 |
+
"metadata": {},
|
| 180 |
+
"execution_count": 4
|
| 181 |
+
}
|
| 182 |
+
]
|
| 183 |
+
},
|
| 184 |
+
{
|
| 185 |
+
"cell_type": "code",
|
| 186 |
+
"source": [
|
| 187 |
+
"def generate_text(seed_text, max_new_tokens, temperature):\n",
|
| 188 |
+
" text = seed_text if seed_text is not None else \" \"\n",
|
| 189 |
+
" text = text if text.endswith(\" \") else seed_text + \" \"\n",
|
| 190 |
+
" context = torch.tensor(encode(text), dtype=torch.long).unsqueeze(0)\n",
|
| 191 |
+
" temperature = temperature if temperature > 0 else 1e-5\n",
|
| 192 |
+
" return decode(trained_model.generate(context, temperature = temperature, max_new_tokens=max_new_tokens)[0].tolist())"
|
| 193 |
+
],
|
| 194 |
+
"metadata": {
|
| 195 |
+
"id": "8srx4zfYC4yS"
|
| 196 |
+
},
|
| 197 |
+
"execution_count": 5,
|
| 198 |
+
"outputs": []
|
| 199 |
+
},
|
| 200 |
+
{
|
| 201 |
+
"cell_type": "code",
|
| 202 |
+
"source": [
|
| 203 |
+
"with gr.Blocks() as demo:\n",
|
| 204 |
+
" gr.HTML(\"<h1 align = 'center'> Generate Text Based on simple GPT model <br> (Dataset = William Shakespeare) </h1>\")\n",
|
| 205 |
+
"\n",
|
| 206 |
+
" content = gr.Textbox(label = \"Enter initial text to generate content\")\n",
|
| 207 |
+
" with gr.Row(equal_height=True):\n",
|
| 208 |
+
" with gr.Column():\n",
|
| 209 |
+
" max_tokens = gr.Number(label = \"Maximum tokens to generate content\", value = 100)\n",
|
| 210 |
+
" temp_val = gr.Slider(label = \"Temparature (slide to higher value for higher creativity)\", minimum = 0.0, maximum= 1.0,value = 0.7)\n",
|
| 211 |
+
" generate_btn = gr.Button(value = 'Generate Text')\n",
|
| 212 |
+
" with gr.Column():\n",
|
| 213 |
+
" outputs = [gr.TextArea(label = \"Generated text (William Shakespeare)\", lines = 8)]\n",
|
| 214 |
+
" inputs = [\n",
|
| 215 |
+
" content,\n",
|
| 216 |
+
" max_tokens,\n",
|
| 217 |
+
" temp_val\n",
|
| 218 |
+
" ]\n",
|
| 219 |
+
" generate_btn.click(fn = generate_text, inputs= inputs, outputs = outputs)\n",
|
| 220 |
+
"\n",
|
| 221 |
+
"# for collab\n",
|
| 222 |
+
"demo.launch(debug=True)"
|
| 223 |
+
],
|
| 224 |
+
"metadata": {
|
| 225 |
+
"colab": {
|
| 226 |
+
"base_uri": "https://localhost:8080/",
|
| 227 |
+
"height": 680
|
| 228 |
+
},
|
| 229 |
+
"id": "RqcV9SR0C-XW",
|
| 230 |
+
"outputId": "e24224de-7406-4071-83ab-7d54b4f429fd"
|
| 231 |
+
},
|
| 232 |
+
"execution_count": 23,
|
| 233 |
+
"outputs": [
|
| 234 |
+
{
|
| 235 |
+
"output_type": "stream",
|
| 236 |
+
"name": "stdout",
|
| 237 |
+
"text": [
|
| 238 |
+
"Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
|
| 239 |
+
"\n",
|
| 240 |
+
"Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
|
| 241 |
+
"Running on public URL: https://eaf77dd6c0a8cb86e0.gradio.live\n",
|
| 242 |
+
"\n",
|
| 243 |
+
"This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
|
| 244 |
+
]
|
| 245 |
+
},
|
| 246 |
+
{
|
| 247 |
+
"output_type": "display_data",
|
| 248 |
+
"data": {
|
| 249 |
+
"text/plain": [
|
| 250 |
+
"<IPython.core.display.HTML object>"
|
| 251 |
+
],
|
| 252 |
+
"text/html": [
|
| 253 |
+
"<div><iframe src=\"https://eaf77dd6c0a8cb86e0.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 254 |
+
]
|
| 255 |
+
},
|
| 256 |
+
"metadata": {}
|
| 257 |
+
},
|
| 258 |
+
{
|
| 259 |
+
"output_type": "stream",
|
| 260 |
+
"name": "stdout",
|
| 261 |
+
"text": [
|
| 262 |
+
"Keyboard interruption in main thread... closing server.\n",
|
| 263 |
+
"Killing tunnel 127.0.0.1:7860 <> https://eaf77dd6c0a8cb86e0.gradio.live\n"
|
| 264 |
+
]
|
| 265 |
+
},
|
| 266 |
+
{
|
| 267 |
+
"output_type": "execute_result",
|
| 268 |
+
"data": {
|
| 269 |
+
"text/plain": []
|
| 270 |
+
},
|
| 271 |
+
"metadata": {},
|
| 272 |
+
"execution_count": 23
|
| 273 |
+
}
|
| 274 |
+
]
|
| 275 |
+
}
|
| 276 |
+
]
|
| 277 |
+
}
|
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import torch.nn.functional as F
|
| 7 |
+
from model import BigramLanguageModel, ModelConfig
|
| 8 |
+
|
| 9 |
+
with open('input.txt', 'r', encoding='utf-8') as f:
|
| 10 |
+
text = f.read()
|
| 11 |
+
|
| 12 |
+
# unique characters from text
|
| 13 |
+
chars = sorted(list(set(text)))
|
| 14 |
+
vocab_size = len(chars)
|
| 15 |
+
|
| 16 |
+
# create a mapping from chars to ints
|
| 17 |
+
stoi = { ch:i for i,ch in enumerate(chars) }
|
| 18 |
+
itos = { i:ch for i,ch in enumerate(chars) }
|
| 19 |
+
encode = lambda s: [stoi[c] for c in s] # encoder: take a string, output a list of integers
|
| 20 |
+
decode = lambda l: ''.join([itos[i] for i in l]) # decoder: take a list of integers, output a string
|
| 21 |
+
|
| 22 |
+
# load model
|
| 23 |
+
model_spec = torch.load("ckpt.pt", map_location=torch.device('cpu'))
|
| 24 |
+
model_args = model_spec['model_args']
|
| 25 |
+
model_weights = model_spec['model']
|
| 26 |
+
modelconf = ModelConfig(**model_args)
|
| 27 |
+
trained_model = BigramLanguageModel(modelconf)
|
| 28 |
+
trained_model.load_state_dict(model_weights)
|
| 29 |
+
|
| 30 |
+
def generate_text(seed_text, max_new_tokens, temperature):
|
| 31 |
+
text = seed_text if seed_text is not None else " "
|
| 32 |
+
text = text if text.endswith(" ") else seed_text + " "
|
| 33 |
+
context = torch.tensor(encode(text), dtype=torch.long).unsqueeze(0)
|
| 34 |
+
temperature = temperature if temperature > 0 else 1e-5
|
| 35 |
+
return decode(trained_model.generate(context, temperature = temperature, max_new_tokens=max_new_tokens)[0].tolist())
|
| 36 |
+
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.HTML("<h1 align = 'center'> Generate Text Based on simple GPT model <br> (Dataset = William Shakespeare) </h1>")
|
| 39 |
+
|
| 40 |
+
content = gr.Textbox(label = "Enter initial text to generate content")
|
| 41 |
+
with gr.Row(equal_height=True):
|
| 42 |
+
with gr.Column():
|
| 43 |
+
max_tokens = gr.Number(label = "Maximum tokens to generate content", value = 100)
|
| 44 |
+
temp_val = gr.Slider(label = "Temparature (slide to higher value for higher creativity)", minimum = 0.0, maximum= 1.0,value = 0.7)
|
| 45 |
+
generate_btn = gr.Button(value = 'Generate Text')
|
| 46 |
+
with gr.Column():
|
| 47 |
+
outputs = [gr.TextArea(label = "Generated text (William Shakespeare)", lines = 7)]
|
| 48 |
+
inputs = [
|
| 49 |
+
content,
|
| 50 |
+
max_tokens,
|
| 51 |
+
temp_val
|
| 52 |
+
]
|
| 53 |
+
generate_btn.click(fn = generate_text, inputs= inputs, outputs = outputs)
|
| 54 |
+
|
| 55 |
+
if __name__ == '__main__':
|
| 56 |
+
demo.launch()
|