From 7664bb57f59985be5588963e58cddc1e16590c06 Mon Sep 17 00:00:00 2001 From: Dawood Khan Date: Tue, 16 May 2023 18:43:15 -0700 Subject: [PATCH] Fixes chatbot_dialogpt demo (#4238) * demo fix * changelog * fix * demo --------- Co-authored-by: Abubakar Abid --- CHANGELOG.md | 1 + demo/chatbot_dialogpt/run.ipynb | 2 +- demo/chatbot_dialogpt/run.py | 29 +++++++++++++++-------------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccd9b851c0..61c8251f6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,7 @@ No changes to highlight. - Fix "TypeError: issubclass() arg 1 must be a class" When use Optional[Types] by [@lingfengchencn](https://github.com/lingfengchencn) in [PR 4200](https://github.com/gradio-app/gradio/pull/4200). - Ensure cancelling functions work correctly by [@pngwn](https://github.com/pngwn) in [PR 4225](https://github.com/gradio-app/gradio/pull/4225) - Fixes a bug with typing.get_type_hints() on Python 3.9 by [@abidlabs](https://github.com/abidlabs) in [PR 4228](https://github.com/gradio-app/gradio/pull/4228). +- Fix `chatbot_dialogpt` demo by [@dawoodkhan82](https://github.com/dawoodkhan82) in [PR 4238](https://github.com/gradio-app/gradio/pull/4238). ## Other Changes: diff --git a/demo/chatbot_dialogpt/run.ipynb b/demo/chatbot_dialogpt/run.ipynb index 58b5d3bf58..52b6304fc1 100644 --- a/demo/chatbot_dialogpt/run.ipynb +++ b/demo/chatbot_dialogpt/run.ipynb @@ -1 +1 @@ -{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: chatbot_dialogpt"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "import torch\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(\"microsoft/DialoGPT-medium\")\n", "model = AutoModelForCausalLM.from_pretrained(\"microsoft/DialoGPT-medium\")\n", "\n", "def user(message, history):\n", " return \"\", history + [[message, None]]\n", "\n", "\n", "# bot_message = random.choice([\"Yes\", \"No\"])\n", "# history[-1][1] = bot_message\n", "# time.sleep(1)\n", "# return history\n", "\n", "# def predict(input, history=[]):\n", "# # tokenize the new input sentence\n", "\n", "def bot(history):\n", " user_message = history[-1][0]\n", " new_user_input_ids = tokenizer.encode(user_message + tokenizer.eos_token, return_tensors='pt')\n", "\n", " # append the new user input tokens to the chat history\n", " bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)\n", "\n", " # generate a response \n", " history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()\n", "\n", " # convert the tokens to text, and then split the responses into lines\n", " response = tokenizer.decode(history[0]).split(\"<|endoftext|>\")\n", " response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list\n", " return history\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot()\n", " msg = gr.Textbox()\n", " clear = gr.Button(\"Clear\")\n", "\n", " msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(\n", " bot, chatbot, chatbot\n", " )\n", " clear.click(lambda: None, None, chatbot, queue=False)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file +{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: chatbot_dialogpt"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", "import torch\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(\"microsoft/DialoGPT-medium\")\n", "model = AutoModelForCausalLM.from_pretrained(\"microsoft/DialoGPT-medium\")\n", "\n", "\n", "def user(message, history):\n", " return \"\", history + [[message, None]]\n", "\n", "\n", "def bot(history):\n", " user_message = history[-1][0]\n", " new_user_input_ids = tokenizer.encode(\n", " user_message + tokenizer.eos_token, return_tensors=\"pt\"\n", " )\n", "\n", " # append the new user input tokens to the chat history\n", " bot_input_ids = torch.cat([torch.LongTensor([]), new_user_input_ids], dim=-1)\n", "\n", " # generate a response\n", " response = model.generate(\n", " bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id\n", " ).tolist()\n", "\n", " # convert the tokens to text, and then split the responses into lines\n", " response = tokenizer.decode(response[0]).split(\"<|endoftext|>\")\n", " response = [\n", " (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)\n", " ] # convert to tuples of list\n", " history[-1] = response[0]\n", " return history\n", "\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot()\n", " msg = gr.Textbox()\n", " clear = gr.Button(\"Clear\")\n", "\n", " msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(\n", " bot, chatbot, chatbot\n", " )\n", " clear.click(lambda: None, None, chatbot, queue=False)\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file diff --git a/demo/chatbot_dialogpt/run.py b/demo/chatbot_dialogpt/run.py index 18bbfdf190..8548880ebf 100644 --- a/demo/chatbot_dialogpt/run.py +++ b/demo/chatbot_dialogpt/run.py @@ -5,33 +5,34 @@ import torch tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium") model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium") + def user(message, history): return "", history + [[message, None]] -# bot_message = random.choice(["Yes", "No"]) -# history[-1][1] = bot_message -# time.sleep(1) -# return history - -# def predict(input, history=[]): -# # tokenize the new input sentence - def bot(history): user_message = history[-1][0] - new_user_input_ids = tokenizer.encode(user_message + tokenizer.eos_token, return_tensors='pt') + new_user_input_ids = tokenizer.encode( + user_message + tokenizer.eos_token, return_tensors="pt" + ) # append the new user input tokens to the chat history - bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1) + bot_input_ids = torch.cat([torch.LongTensor([]), new_user_input_ids], dim=-1) - # generate a response - history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist() + # generate a response + response = model.generate( + bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id + ).tolist() # convert the tokens to text, and then split the responses into lines - response = tokenizer.decode(history[0]).split("<|endoftext|>") - response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list + response = tokenizer.decode(response[0]).split("<|endoftext|>") + response = [ + (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2) + ] # convert to tuples of list + history[-1] = response[0] return history + with gr.Blocks() as demo: chatbot = gr.Chatbot() msg = gr.Textbox()