diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/3.0 b/3.0 new file mode 100644 index 0000000..352e6d7 --- /dev/null +++ b/3.0 @@ -0,0 +1,30 @@ +Collecting opentok + Downloading https://files.pythonhosted.org/packages/e5/36/39ff61bab71ad73bfaeab4a5f347dba94ab7889ef7d22958b84c424455b4/opentok-3.1.0.tar.gz + Installing build dependencies: started + Installing build dependencies: still running... + Installing build dependencies: finished with status 'done' + Getting requirements to build wheel: started + Getting requirements to build wheel: finished with status 'done' + Preparing wheel metadata: started + Preparing wheel metadata: finished with status 'done' +Collecting python-jose (from opentok) + Downloading https://files.pythonhosted.org/packages/bd/2d/e94b2f7bab6773c70efc70a61d66e312e1febccd9e0db6b9e0adf58cbad1/python_jose-3.3.0-py2.py3-none-any.whl +Requirement already satisfied: requests in c:\users\bunmi\onedrive\documents\lib\site-packages (from opentok) (2.25.0) +Requirement already satisfied: six in c:\users\bunmi\onedrive\documents\lib\site-packages (from opentok) (1.15.0) +Requirement already satisfied: pytz in c:\users\bunmi\onedrive\documents\lib\site-packages (from opentok) (2019.3) +Requirement already satisfied: pyasn1 in c:\users\bunmi\onedrive\documents\lib\site-packages (from python-jose->opentok) (0.4.8) +Collecting ecdsa!=0.15 (from python-jose->opentok) + Downloading https://files.pythonhosted.org/packages/4a/b6/b678b080967b2696e9a201c096dc076ad756fb35c87dca4e1d1a13496ff7/ecdsa-0.17.0-py2.py3-none-any.whl (119kB) +Requirement already satisfied: rsa in c:\users\bunmi\onedrive\documents\lib\site-packages (from python-jose->opentok) (4.6) +Requirement already satisfied: idna<3,>=2.5 in c:\users\bunmi\onedrive\documents\lib\site-packages (from requests->opentok) (2.10) +Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\bunmi\onedrive\documents\lib\site-packages (from requests->opentok) (1.26.2) +Requirement already satisfied: certifi>=2017.4.17 in c:\users\bunmi\onedrive\documents\lib\site-packages (from requests->opentok) (2020.11.8) +Requirement already satisfied: chardet<4,>=3.0.2 in c:\users\bunmi\onedrive\documents\lib\site-packages (from requests->opentok) (3.0.4) +Building wheels for collected packages: opentok + Building wheel for opentok (PEP 517): started + Building wheel for opentok (PEP 517): finished with status 'done' + Created wheel for opentok: filename=opentok-3.1.0-cp37-none-any.whl size=26318 sha256=db4026e59a81901a990ff2badc1e78a888fe684e8773ef83e0c6464aab24ff45 + Stored in directory: C:\Users\Bunmi\AppData\Local\pip\Cache\wheels\f4\fb\17\0db41a13ec5785399e8047b1d2437725745a19eda37752d691 +Successfully built opentok +Installing collected packages: ecdsa, python-jose, opentok +Successfully installed ecdsa-0.17.0 opentok-3.1.0 python-jose-3.3.0 diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..58c41f3 --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,473 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import logging\n", + "import random\n", + "from argparse import ArgumentParser\n", + "from itertools import chain\n", + "from pprint import pformat\n", + "import warnings\n", + "\n", + "import torch\n", + "import torch.nn.functional as F\n", + "\n", + "from transformers import OpenAIGPTLMHeadModel, OpenAIGPTTokenizer, GPT2LMHeadModel, GPT2Tokenizer\n", + "from train import SPECIAL_TOKENS, build_input_from_segments, add_special_tokens_\n", + "#from utils import get_dataset, download_pretrained_model" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def sample_sequence(personality, history, tokenizer, model, args, current_output=None):\n", + " special_tokens_ids = tokenizer.convert_tokens_to_ids(SPECIAL_TOKENS)\n", + " if current_output is None:\n", + " current_output = []\n", + "\n", + " for i in range(args['max_length']):\n", + " instance = build_input_from_segments(personality, history, current_output, tokenizer, with_eos=False)\n", + "\n", + " input_ids = torch.tensor(instance[\"input_ids\"], device=args['device']).unsqueeze(0)\n", + " token_type_ids = torch.tensor(instance[\"token_type_ids\"], device=args['device']).unsqueeze(0)\n", + "\n", + " logits = model(input_ids, token_type_ids=token_type_ids)\n", + " if isinstance(logits, tuple): # for gpt2 and maybe others\n", + " logits = logits[0]\n", + " logits = logits[0, -1, :] / args['temperature']\n", + " logits = top_filtering(logits, top_k=args['top_k'], top_p=args['top_p'])\n", + " probs = F.softmax(logits, dim=-1)\n", + "\n", + " prev = torch.topk(probs, 1)[1] if args['no_sample'] else torch.multinomial(probs, 1)\n", + " if i < args['min_length'] and prev.item() in special_tokens_ids:\n", + " while prev.item() in special_tokens_ids:\n", + " if probs.max().item() == 1:\n", + " warnings.warn(\"Warning: model generating special token with probability 1.\")\n", + " break # avoid infinitely looping over special token\n", + " prev = torch.multinomial(probs, num_samples=1)\n", + "\n", + " if prev.item() in special_tokens_ids:\n", + " break\n", + " current_output.append(prev.item())\n", + "\n", + " return current_output\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "args['seed']" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "WARNING:transformers.tokenization_openai:ftfy or spacy is not installed using BERT BasicTokenizer instead of SpaCy & ftfy.\n", + "ERROR:root:Internal Python error in the inspect module.\n", + "Below is the traceback from this internal error.\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 3326, in run_code\n", + " exec(code_obj, self.user_global_ns, self.user_ns)\n", + " File \"\", line 8, in \n", + " logger.info(\"Sample a personality\")\n", + "NameError: name 'logger' is not defined\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 2040, in showtraceback\n", + " stb = value._render_traceback_()\n", + "AttributeError: 'NameError' object has no attribute '_render_traceback_'\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow.py\", line 58, in \n", + " from tensorflow.python.pywrap_tensorflow_internal import *\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 28, in \n", + " _pywrap_tensorflow_internal = swig_import_helper()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 24, in swig_import_helper\n", + " _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 242, in load_module\n", + " return load_dynamic(name, filename, file)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 342, in load_dynamic\n", + " return _load(spec)\n", + "ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 1101, in get_records\n", + " return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 319, in wrapped\n", + " return f(*args, **kwargs)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 353, in _fixed_getinnerframes\n", + " records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 1502, in getinnerframes\n", + " frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 1460, in getframeinfo\n", + " filename = getsourcefile(frame) or getfile(frame)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 696, in getsourcefile\n", + " if getattr(getmodule(object, filename), '__loader__', None) is not None:\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 733, in getmodule\n", + " if ismodule(module) and hasattr(module, '__file__'):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 50, in __getattr__\n", + " module = self._load()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 44, in _load\n", + " module = _importlib.import_module(self.__name__)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\importlib\\__init__.py\", line 127, in import_module\n", + " return _bootstrap._gcd_import(name[level:], package, level)\n", + " File \"\", line 1006, in _gcd_import\n", + " File \"\", line 983, in _find_and_load\n", + " File \"\", line 953, in _find_and_load_unlocked\n", + " File \"\", line 219, in _call_with_frames_removed\n", + " File \"\", line 1006, in _gcd_import\n", + " File \"\", line 983, in _find_and_load\n", + " File \"\", line 967, in _find_and_load_unlocked\n", + " File \"\", line 677, in _load_unlocked\n", + " File \"\", line 728, in exec_module\n", + " File \"\", line 219, in _call_with_frames_removed\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\__init__.py\", line 28, in \n", + " from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 50, in __getattr__\n", + " module = self._load()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 44, in _load\n", + " module = _importlib.import_module(self.__name__)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\importlib\\__init__.py\", line 127, in import_module\n", + " return _bootstrap._gcd_import(name[level:], package, level)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\__init__.py\", line 49, in \n", + " from tensorflow.python import pywrap_tensorflow\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow.py\", line 74, in \n", + " raise ImportError(msg)\n", + "ImportError: Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 3326, in run_code\n", + " exec(code_obj, self.user_global_ns, self.user_ns)\n", + " File \"\", line 8, in \n", + " logger.info(\"Sample a personality\")\n", + "NameError: name 'logger' is not defined\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 2040, in showtraceback\n", + " stb = value._render_traceback_()\n", + "AttributeError: 'NameError' object has no attribute '_render_traceback_'\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow.py\", line 58, in \n", + " from tensorflow.python.pywrap_tensorflow_internal import *\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 28, in \n", + " _pywrap_tensorflow_internal = swig_import_helper()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 24, in swig_import_helper\n", + " _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 242, in load_module\n", + " return load_dynamic(name, filename, file)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 342, in load_dynamic\n", + " return _load(spec)\n", + "ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.\n", + "\n", + "\n", + "Failed to load the native TensorFlow runtime.\n", + "\n", + "See https://www.tensorflow.org/install/errors\n", + "\n", + "for some common reasons and solutions. Include the entire stack trace\n", + "above this error message when asking for help.\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'logger' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m" + ] + } + ], + "source": [ + "args = {\"min_length\": 1, \"max_length\": 20, \"temperature\":0.7, \"device\":\"cpu\", \"top_k\":0, \"top_p\":0.9, \"seed\":0, \n", + " \"model_checkpoint\": \"C:/Users/Bunmi/Desktop/GitHub/transfer-learning-conv-ai/deploy/\"\n", + " \"dataset_cache\": \"C:/Users/Bunmi/Desktop/GitHub/transfer-learning-conv-ai/\"}\n", + "tokenizer_class, model_class = (OpenAIGPTTokenizer, OpenAIGPTLMHeadModel)\n", + "tokenizer = tokenizer_class.from_pretrained(args['model_checkpoint'])\n", + " \n", + "model = model_class.from_pretrained(args['model_checkpoint'])\n", + "model.to(\"cpu\")\n", + " \n", + "logger.info(\"Sample a personality\")\n", + "#dataset = get_dataset(tokenizer, args.dataset_path, args.dataset_cache)\n", + "#personalities = [dialog[\"personality\"] for dataset in dataset.values() for dialog in dataset]\n", + "personalities = [\"i like to remodel homes .\", \"i like to go hunting .\",\n", + " \"i like to shoot a bow .\", \"my favorite holiday is halloween .\"]\n", + "print(personalities)\n", + "personality = random.choice(personalities)\n", + "logger.info(\"Selected personality: %s\", tokenizer.decode(chain(*personality)))\n", + "\n", + "history = []\n", + "while True:\n", + " raw_text = input(\">>> \")\n", + " while not raw_text:\n", + " print('Prompt should not be empty!')\n", + " raw_text = input(\">>> \")\n", + " history.append(tokenizer.encode(raw_text))\n", + " with torch.no_grad():\n", + " out_ids = sample_sequence(personality, history, tokenizer, model, args)\n", + " history.append(out_ids)\n", + " history = history[-(2*args.max_history+1):]\n", + " out_text = tokenizer.decode(out_ids, skip_special_tokens=True)\n", + " print(out_text)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "from utils import get_dataset, download_pretrained_model" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Sample a personality\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "ERROR:root:Internal Python error in the inspect module.\n", + "Below is the traceback from this internal error.\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 3326, in run_code\n", + " exec(code_obj, self.user_global_ns, self.user_ns)\n", + " File \"\", line 5, in \n", + " dataset = get_dataset(tokenizer, args['dataset_cache'], args['dataset_cache'])\n", + " File \"C:\\Users\\Bunmi\\Desktop\\GitHub\\transfer-learning-conv-ai\\utils.py\", line 41, in get_dataset\n", + " with open(personachat_file, \"r\", encoding=\"utf-8\") as f:\n", + "PermissionError: [Errno 13] Permission denied: 'C:/Users/Bunmi/Desktop/GitHub/transfer-learning-conv-ai/'\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 2040, in showtraceback\n", + " stb = value._render_traceback_()\n", + "AttributeError: 'PermissionError' object has no attribute '_render_traceback_'\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow.py\", line 58, in \n", + " from tensorflow.python.pywrap_tensorflow_internal import *\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 28, in \n", + " _pywrap_tensorflow_internal = swig_import_helper()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 24, in swig_import_helper\n", + " _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 242, in load_module\n", + " return load_dynamic(name, filename, file)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 342, in load_dynamic\n", + " return _load(spec)\n", + "ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 1101, in get_records\n", + " return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 319, in wrapped\n", + " return f(*args, **kwargs)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\ultratb.py\", line 353, in _fixed_getinnerframes\n", + " records = fix_frame_records_filenames(inspect.getinnerframes(etb, context))\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 1502, in getinnerframes\n", + " frameinfo = (tb.tb_frame,) + getframeinfo(tb, context)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 1460, in getframeinfo\n", + " filename = getsourcefile(frame) or getfile(frame)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 696, in getsourcefile\n", + " if getattr(getmodule(object, filename), '__loader__', None) is not None:\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\inspect.py\", line 733, in getmodule\n", + " if ismodule(module) and hasattr(module, '__file__'):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 50, in __getattr__\n", + " module = self._load()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 44, in _load\n", + " module = _importlib.import_module(self.__name__)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\importlib\\__init__.py\", line 127, in import_module\n", + " return _bootstrap._gcd_import(name[level:], package, level)\n", + " File \"\", line 1006, in _gcd_import\n", + " File \"\", line 983, in _find_and_load\n", + " File \"\", line 953, in _find_and_load_unlocked\n", + " File \"\", line 219, in _call_with_frames_removed\n", + " File \"\", line 1006, in _gcd_import\n", + " File \"\", line 983, in _find_and_load\n", + " File \"\", line 967, in _find_and_load_unlocked\n", + " File \"\", line 677, in _load_unlocked\n", + " File \"\", line 728, in exec_module\n", + " File \"\", line 219, in _call_with_frames_removed\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\__init__.py\", line 28, in \n", + " from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 50, in __getattr__\n", + " module = self._load()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow\\__init__.py\", line 44, in _load\n", + " module = _importlib.import_module(self.__name__)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\importlib\\__init__.py\", line 127, in import_module\n", + " return _bootstrap._gcd_import(name[level:], package, level)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\__init__.py\", line 49, in \n", + " from tensorflow.python import pywrap_tensorflow\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow.py\", line 74, in \n", + " raise ImportError(msg)\n", + "ImportError: Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 3326, in run_code\n", + " exec(code_obj, self.user_global_ns, self.user_ns)\n", + " File \"\", line 5, in \n", + " dataset = get_dataset(tokenizer, args['dataset_cache'], args['dataset_cache'])\n", + " File \"C:\\Users\\Bunmi\\Desktop\\GitHub\\transfer-learning-conv-ai\\utils.py\", line 41, in get_dataset\n", + " with open(personachat_file, \"r\", encoding=\"utf-8\") as f:\n", + "PermissionError: [Errno 13] Permission denied: 'C:/Users/Bunmi/Desktop/GitHub/transfer-learning-conv-ai/'\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\IPython\\core\\interactiveshell.py\", line 2040, in showtraceback\n", + " stb = value._render_traceback_()\n", + "AttributeError: 'PermissionError' object has no attribute '_render_traceback_'\n", + "\n", + "During handling of the above exception, another exception occurred:\n", + "\n", + "Traceback (most recent call last):\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow.py\", line 58, in \n", + " from tensorflow.python.pywrap_tensorflow_internal import *\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 28, in \n", + " _pywrap_tensorflow_internal = swig_import_helper()\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\site-packages\\tensorflow_core\\python\\pywrap_tensorflow_internal.py\", line 24, in swig_import_helper\n", + " _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 242, in load_module\n", + " return load_dynamic(name, filename, file)\n", + " File \"C:\\Users\\Bunmi\\OneDrive\\Documents\\lib\\imp.py\", line 342, in load_dynamic\n", + " return _load(spec)\n", + "ImportError: DLL load failed: A dynamic link library (DLL) initialization routine failed.\n", + "\n", + "\n", + "Failed to load the native TensorFlow runtime.\n", + "\n", + "See https://www.tensorflow.org/install/errors\n", + "\n", + "for some common reasons and solutions. Include the entire stack trace\n", + "above this error message when asking for help.\n" + ] + }, + { + "ename": "PermissionError", + "evalue": "[Errno 13] Permission denied: 'C:/Users/Bunmi/Desktop/GitHub/transfer-learning-conv-ai/'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m" + ] + } + ], + "source": [ + "args = {\"min_length\": 1, \"max_length\": 20, \"temperature\":0.7, \"device\":\"cpu\", \"top_k\":0, \"top_p\":0.9, \"seed\":0, \n", + " \"model_checkpoint\": \"C:/Users/Bunmi/Desktop/GitHub/transfer-learning-conv-ai/deploy/\",\n", + " \"dataset_cache\": \"C:/Users/Bunmi/Desktop/GitHub/transfer-learning-conv-ai/\"}\n", + "print(\"Sample a personality\")\n", + "dataset = get_dataset(tokenizer, args['dataset_cache'], args['dataset_cache'])\n", + "personalities = [dialog[\"personality\"] for dataset in dataset.values() for dialog in dataset]\n", + "#personalities = [\"i like to remodel homes .\", \"i like to go hunting .\",\n", + "# \"i like to shoot a bow .\", \"my favorite holiday is halloween .\"]\n", + "print(personalities)\n", + "personality = random.choice(personalities)\n", + "print(\"Selected personality: %s\", tokenizer.decode(chain(*personality)))\n", + "\n", + "history = []\n", + "while True:\n", + " raw_text = input(\">>> \")\n", + " while not raw_text:\n", + " print('Prompt should not be empty!')\n", + " raw_text = input(\">>> \")\n", + " history.append(tokenizer.encode(raw_text))\n", + " with torch.no_grad():\n", + " out_ids = sample_sequence(personality, history, tokenizer, model, args)\n", + " history.append(out_ids)\n", + " history = history[-(2*args.max_history+1):]\n", + " out_text = tokenizer.decode(out_ids, skip_special_tokens=True)\n", + " print(out_text)\n" + ] + }, + { + "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.7.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/app.py b/app.py new file mode 100644 index 0000000..3e9257e --- /dev/null +++ b/app.py @@ -0,0 +1,6 @@ +import uvicorn + +if __name__ == "__main__": + uvicorn.run("fast:app", host="0.0.0.0", port=8000, reload=True) + + diff --git a/fast.py b/fast.py new file mode 100644 index 0000000..a567219 --- /dev/null +++ b/fast.py @@ -0,0 +1,176 @@ +# # Copyright (c) 2019-present, HuggingFace Inc. +# All rights reserved. +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. +import logging +import random +from argparse import ArgumentParser +from itertools import chain +from pprint import pformat +import warnings + +import torch +import torch.nn.functional as F + +from transformers import OpenAIGPTLMHeadModel, OpenAIGPTTokenizer, GPT2LMHeadModel, GPT2Tokenizer +from train import SPECIAL_TOKENS, build_input_from_segments, add_special_tokens_ +from utils import get_dataset, download_pretrained_model + +from transformers import AutoTokenizer, AutoModel +from fastapi import FastAPI + +from fastapi import FastAPI +from fastapi import APIRouter, Body + +app = FastAPI() + +@app.get("/") +def hello(): + return {"message":"Hello TutLinks.com"} + +def top_filtering(logits, top_k=0., top_p=0.9, threshold=-float('Inf'), filter_value=-float('Inf')): + """ Filter a distribution of logits using top-k, top-p (nucleus) and/or threshold filtering + Args: + logits: logits distribution shape (vocabulary size) + top_k: <=0: no filtering, >0: keep only top k tokens with highest probability. + top_p: <=0.0: no filtering, >0.0: keep only a subset S of candidates, where S is the smallest subset + whose total probability mass is greater than or equal to the threshold top_p. + In practice, we select the highest probability tokens whose cumulative probability mass exceeds + the threshold top_p. + threshold: a minimal threshold to keep logits + """ + assert logits.dim() == 1 # Only work for batch size 1 for now - could update but it would obfuscate a bit the code + top_k = min(top_k, logits.size(-1)) + if top_k > 0: + # Remove all tokens with a probability less than the last token in the top-k tokens + indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None] + logits[indices_to_remove] = filter_value + + if top_p > 0.0: + # Compute cumulative probabilities of sorted tokens + sorted_logits, sorted_indices = torch.sort(logits, descending=True) + cumulative_probabilities = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1) + + # Remove tokens with cumulative probability above the threshold + sorted_indices_to_remove = cumulative_probabilities > top_p + # Shift the indices to the right to keep also the first token above the threshold + sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone() + sorted_indices_to_remove[..., 0] = 0 + + # Back to unsorted indices and set them to -infinity + indices_to_remove = sorted_indices[sorted_indices_to_remove] + logits[indices_to_remove] = filter_value + + indices_to_remove = logits < threshold + logits[indices_to_remove] = filter_value + + return logits + + +def sample_sequence(personality, history, tokenizer, model, args, current_output=None): + special_tokens_ids = tokenizer.convert_tokens_to_ids(SPECIAL_TOKENS) + if current_output is None: + current_output = [] + + for i in range(args.max_length): + instance = build_input_from_segments(personality, history, current_output, tokenizer, with_eos=False) + + input_ids = torch.tensor(instance["input_ids"], device=args.device).unsqueeze(0) + token_type_ids = torch.tensor(instance["token_type_ids"], device=args.device).unsqueeze(0) + + logits = model(input_ids, token_type_ids=token_type_ids) + if isinstance(logits, tuple): # for gpt2 and maybe others + logits = logits[0] + logits = logits[0, -1, :] / args.temperature + logits = top_filtering(logits, top_k=args.top_k, top_p=args.top_p) + probs = F.softmax(logits, dim=-1) + + prev = torch.topk(probs, 1)[1] if args.no_sample else torch.multinomial(probs, 1) + if i < args.min_length and prev.item() in special_tokens_ids: + while prev.item() in special_tokens_ids: + if probs.max().item() == 1: + warnings.warn("Warning: model generating special token with probability 1.") + break # avoid infinitely looping over special token + prev = torch.multinomial(probs, num_samples=1) + + if prev.item() in special_tokens_ids: + break + current_output.append(prev.item()) + + return current_output + + +def run(): + parser = ArgumentParser() + parser.add_argument("--dataset_path", type=str, default="", help="Path or url of the dataset. If empty download from S3.") + parser.add_argument("--dataset_cache", type=str, default='./dataset_cache', help="Path or url of the dataset cache") + parser.add_argument("--model", type=str, default="openai-gpt", help="Model type (openai-gpt or gpt2)", choices=['openai-gpt', 'gpt2']) # anything besides gpt2 will load openai-gpt + parser.add_argument("--model_checkpoint", type=str, default="", help="Path, url or short name of the model") + parser.add_argument("--max_history", type=int, default=2, help="Number of previous utterances to keep in history") + parser.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu", help="Device (cuda or cpu)") + + parser.add_argument("--no_sample", action='store_true', help="Set to use greedy decoding instead of sampling") + parser.add_argument("--max_length", type=int, default=20, help="Maximum length of the output utterances") + parser.add_argument("--min_length", type=int, default=1, help="Minimum length of the output utterances") + parser.add_argument("--seed", type=int, default=0, help="Seed") + parser.add_argument("--temperature", type=float, default=0.7, help="Sampling softmax temperature") + parser.add_argument("--top_k", type=int, default=0, help="Filter top-k tokens before sampling (<=0: no filtering)") + parser.add_argument("--top_p", type=float, default=0.9, help="Nucleus filtering (top-p) before sampling (<=0.0: no filtering)") + args = parser.parse_args() + + logging.basicConfig(level=logging.INFO) + logger = logging.getLogger(__file__) + logger.info(pformat(args)) + + if args.model_checkpoint == "": + if args.model == 'gpt2': + raise ValueError("Interacting with GPT2 requires passing a finetuned model_checkpoint") + else: + args.model_checkpoint = download_pretrained_model() + + + if args.seed != 0: + random.seed(args.seed) + torch.random.manual_seed(args.seed) + torch.cuda.manual_seed(args.seed) + + + logger.info("Get pretrained model and tokenizer") + + + tokenizer_class, model_class = (GPT2Tokenizer, GPT2LMHeadModel) if args.model == 'gpt2' else (OpenAIGPTTokenizer, OpenAIGPTLMHeadModel) + global tokenizer + tokenizer = tokenizer_class.from_pretrained(args.model_checkpoint) + global model + model = model_class.from_pretrained(args.model_checkpoint) + model.to(args.device) + add_special_tokens_(model, tokenizer) + + logger.info("Sample a personality") + dataset = get_dataset(tokenizer, args.dataset_path, args.dataset_cache) + personalities = [dialog["personality"] for dataset in dataset.values() for dialog in dataset] + personality = random.choice(personalities) + logger.info("Selected personality: %s", tokenizer.decode(chain(*personality))) + + return model, tokenizer, args, personality + +model, tokenizer, args, personality = run() + +@app.get('/predict/{raw_text}') +def predictions(raw_text): + + history = [] + while True: + while not raw_text: + print('Prompt should not be empty!') + raw_text = 'hi' + history.append(tokenizer.encode(raw_text)) + with torch.no_grad(): + out_ids = sample_sequence(personality, history, tokenizer, model, args) + history.append(out_ids) + history = history[-(2*args.max_history+1):] + out_text = tokenizer.decode(out_ids, skip_special_tokens=True) + return {'text': out_text} + + + \ No newline at end of file diff --git a/interact.py b/interact.py index d368204..67bf3e0 100644 --- a/interact.py +++ b/interact.py @@ -133,6 +133,7 @@ def run(): dataset = get_dataset(tokenizer, args.dataset_path, args.dataset_cache) personalities = [dialog["personality"] for dataset in dataset.values() for dialog in dataset] personality = random.choice(personalities) + print(personalities) logger.info("Selected personality: %s", tokenizer.decode(chain(*personality))) history = [] @@ -151,4 +152,4 @@ def run(): if __name__ == "__main__": - run() + run() \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..be16a90 --- /dev/null +++ b/main.py @@ -0,0 +1,6 @@ +import uvicorn + +if __name__ == "__main__": + uvicorn.run("fast:app", host="0.0.0.0", port=8118, reload=True) + + diff --git a/requirements.txt b/requirements.txt index a931b0c..bca0174 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,8 @@ -torch +torch == 1.7.1 pytorch-ignite transformers==2.5.1 -tensorboardX==1.8 -tensorflow # for tensorboardX +fastapi ==0.70.0 +uvicorn == 0.15.0 spacy +tensorboardX==1.8 +tensorflow # for tensorboardX \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..91f387d --- /dev/null +++ b/style.css @@ -0,0 +1,50 @@ + +body{ + font:15px/1.5 Arial, Helvetica,sans-serif; + padding: 0; + background-color: #f4f3f3; +} + +.container{ + text-align:center; + width:100%; + margin: auto; + overflow: hidden; +} + +header{ + background: #03a9f4; + border-bottom: #740a39 3px solid; + height:120px; + width:100%; + padding-top:30px; + +} + +.main-header{ + text-align:center; + background-color: #03a9f4; + height:100px; + width:100%; + margin:0; +} + +.brandname{ + text-align:center; + font-size:30px; + color: #161515; + margin: 10px; +} + +header h2{ + text-align:center; + color:#fff; +} + +.results{ + border-radius: 15px 50px; + background: #345fe4; + padding: 20px; + width: 200px; + height: 150px; +} \ No newline at end of file