From ca616c516d2f3182e345e9a5dfb5323e1ad491fa Mon Sep 17 00:00:00 2001 From: Pranav0-0Aggarwal <96537779+Pranav0-0Aggarwal@users.noreply.github.com> Date: Tue, 4 Apr 2023 22:00:08 +0530 Subject: [PATCH] Created using Colaboratory --- chatbot/pygoatbot.ipynb | 410 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 410 insertions(+) create mode 100644 chatbot/pygoatbot.ipynb diff --git a/chatbot/pygoatbot.ipynb b/chatbot/pygoatbot.ipynb new file mode 100644 index 000000000..4b7c1c473 --- /dev/null +++ b/chatbot/pygoatbot.ipynb @@ -0,0 +1,410 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "authorship_tag": "ABX9TyMFbN9Hl74O897o7MFKnACj", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + }, + "gpuClass": "standard" + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "code", + "source": [ + "pip install chatterbot==1.0.2\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uYnWRxB0KzVe", + "outputId": "f2994ae9-d748-4c3c-d7dd-6bcb4d144ff8" + }, + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", + "Collecting chatterbot==1.0.2\n", + " Downloading ChatterBot-1.0.2-py2.py3-none-any.whl (65 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m65.8/65.8 KB\u001b[0m \u001b[31m2.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting sqlalchemy<1.3,>=1.2\n", + " Downloading SQLAlchemy-1.2.19.tar.gz (5.7 MB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.7/5.7 MB\u001b[0m \u001b[31m33.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + "Requirement already satisfied: nltk<4.0,>=3.2 in /usr/local/lib/python3.9/dist-packages (from chatterbot==1.0.2) (3.8.1)\n", + "Collecting mathparse<0.2,>=0.1\n", + " Downloading mathparse-0.1.2-py3-none-any.whl (7.2 kB)\n", + "Collecting pymongo<4.0,>=3.3\n", + " Downloading pymongo-3.13.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (515 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m515.5/515.5 KB\u001b[0m \u001b[31m9.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting python-dateutil<2.8,>=2.7\n", + " Downloading python_dateutil-2.7.5-py2.py3-none-any.whl (225 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m225.7/225.7 KB\u001b[0m \u001b[31m12.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting pint>=0.8.1\n", + " Downloading Pint-0.20.1-py3-none-any.whl (269 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m269.5/269.5 KB\u001b[0m \u001b[31m4.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting chatterbot-corpus<1.3,>=1.2\n", + " Downloading chatterbot_corpus-1.2.0-py2.py3-none-any.whl (117 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m117.3/117.3 KB\u001b[0m \u001b[31m4.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25hCollecting PyYAML<4.0,>=3.12\n", + " Downloading PyYAML-3.13.tar.gz (270 kB)\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m270.6/270.6 KB\u001b[0m \u001b[31m13.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.9/dist-packages (from nltk<4.0,>=3.2->chatterbot==1.0.2) (2022.10.31)\n", + "Requirement already satisfied: tqdm in /usr/local/lib/python3.9/dist-packages (from nltk<4.0,>=3.2->chatterbot==1.0.2) (4.65.0)\n", + "Requirement already satisfied: joblib in /usr/local/lib/python3.9/dist-packages (from nltk<4.0,>=3.2->chatterbot==1.0.2) (1.1.1)\n", + "Requirement already satisfied: click in /usr/local/lib/python3.9/dist-packages (from nltk<4.0,>=3.2->chatterbot==1.0.2) (8.1.3)\n", + "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.9/dist-packages (from python-dateutil<2.8,>=2.7->chatterbot==1.0.2) (1.16.0)\n", + "Building wheels for collected packages: sqlalchemy, PyYAML\n", + " Building wheel for sqlalchemy (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + " Created wheel for sqlalchemy: filename=SQLAlchemy-1.2.19-cp39-cp39-linux_x86_64.whl size=1157816 sha256=922b29a3390946efcb35b47ccdfe5d923eeb8a2052229f7e2b11130708e7c628\n", + " Stored in directory: /root/.cache/pip/wheels/12/75/ee/52355e3658b18c812a419a7fdf581ab885b01c0675e25e9b01\n", + " Building wheel for PyYAML (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + " Created wheel for PyYAML: filename=PyYAML-3.13-cp39-cp39-linux_x86_64.whl size=43098 sha256=59a2dbcb61dc3619f01f5798fde2819e7f5314a91a2486497a6bc9521f1d3afa\n", + " Stored in directory: /root/.cache/pip/wheels/81/6e/87/725bed1db7f86e1c7091ef5f4a4f11b0fcf7023c2be4fc29db\n", + "Successfully built sqlalchemy PyYAML\n", + "Installing collected packages: sqlalchemy, PyYAML, mathparse, python-dateutil, pymongo, pint, chatterbot-corpus, chatterbot\n", + " Attempting uninstall: sqlalchemy\n", + " Found existing installation: SQLAlchemy 1.4.47\n", + " Uninstalling SQLAlchemy-1.4.47:\n", + " Successfully uninstalled SQLAlchemy-1.4.47\n", + " Attempting uninstall: PyYAML\n", + " Found existing installation: PyYAML 6.0\n", + " Uninstalling PyYAML-6.0:\n", + " Successfully uninstalled PyYAML-6.0\n", + " Attempting uninstall: python-dateutil\n", + " Found existing installation: python-dateutil 2.8.2\n", + " Uninstalling python-dateutil-2.8.2:\n", + " Successfully uninstalled python-dateutil-2.8.2\n", + "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", + "prophet 1.1.2 requires python-dateutil>=2.8.0, but you have python-dateutil 2.7.5 which is incompatible.\n", + "pandas 1.4.4 requires python-dateutil>=2.8.1, but you have python-dateutil 2.7.5 which is incompatible.\n", + "pandas-profiling 3.2.0 requires PyYAML>=5.0.0, but you have pyyaml 3.13 which is incompatible.\n", + "flax 0.6.8 requires PyYAML>=5.4.1, but you have pyyaml 3.13 which is incompatible.\n", + "dask 2022.12.1 requires pyyaml>=5.3.1, but you have pyyaml 3.13 which is incompatible.\u001b[0m\u001b[31m\n", + "\u001b[0mSuccessfully installed PyYAML-3.13 chatterbot-1.0.2 chatterbot-corpus-1.2.0 mathparse-0.1.2 pint-0.20.1 pymongo-3.13.0 python-dateutil-2.7.5 sqlalchemy-1.2.19\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "pip install nltk" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Dgn025lMxOLq", + "outputId": "5de3b172-ff9c-4d54-f723-34d3c9ed8318" + }, + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", + "Requirement already satisfied: nltk in /usr/local/lib/python3.9/dist-packages (3.8.1)\n", + "Requirement already satisfied: regex>=2021.8.3 in /usr/local/lib/python3.9/dist-packages (from nltk) (2022.10.31)\n", + "Requirement already satisfied: tqdm in /usr/local/lib/python3.9/dist-packages (from nltk) (4.65.0)\n", + "Requirement already satisfied: joblib in /usr/local/lib/python3.9/dist-packages (from nltk) (1.1.1)\n", + "Requirement already satisfied: click in /usr/local/lib/python3.9/dist-packages (from nltk) (8.1.3)\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "pip install logger" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "aCCtNKLj6J8a", + "outputId": "fba612a7-0870-41e4-b670-751d1490319b" + }, + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", + "Collecting logger\n", + " Downloading logger-1.4.tar.gz (1.2 kB)\n", + " Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + "Building wheels for collected packages: logger\n", + " Building wheel for logger (setup.py) ... \u001b[?25l\u001b[?25hdone\n", + " Created wheel for logger: filename=logger-1.4-py3-none-any.whl size=1780 sha256=375578a39f0783a695470050f7bfeed4759edc359a2f928ea57bbcf181fe7a17\n", + " Stored in directory: /root/.cache/pip/wheels/d6/2c/20/0bf3428f60a053875adaf43bee20695fff6e5d5a2cffaceaad\n", + "Successfully built logger\n", + "Installing collected packages: logger\n", + "Successfully installed logger-1.4\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "training_data = [\n", + " 'What is OWASP PyGoat?',\n", + " 'OWASP PyGoat is an intentionally vulnerable web application used for learning web security testing.',\n", + " 'Why should I learn web security testing?',\n", + " 'Learning web security testing can help you understand how to identify and prevent web application attacks.',\n", + " 'What types of vulnerabilities can PyGoat help me learn about?',\n", + " 'PyGoat can help you learn about various types of web application vulnerabilities, including injection attacks, cross-site scripting (XSS), and broken authentication and session management.',\n", + " 'How can I use PyGoat to learn web security testing?',\n", + " 'PyGoat includes a series of lessons and challenges designed to teach you about web security testing techniques and common vulnerabilities.',\n", + " 'Is PyGoat suitable for beginners?',\n", + " 'Yes, PyGoat is designed to be accessible to beginners and experienced professionals alike.',\n", + " 'Where can I download PyGoat?',\n", + " 'You can download PyGoat from the official GitHub repository at https://github.com/OWASP/PyGoat',\n", + " 'Are there any resources available to help me get started with PyGoat?',\n", + " 'Yes, the PyGoat documentation includes a Getting Started guide and a list of additional resources to help you learn about web security testing.',\n", + " 'Can I contribute to PyGoat?',\n", + " 'Yes, PyGoat is an open-source project and welcomes contributions from anyone interested in improving the application.',\n", + "]\n" + ], + "metadata": { + "id": "o0XoIqiSxHDt" + }, + "execution_count": 35, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "import nltk\n", + "import logging\n", + "from collections import Counter\n", + "from operator import itemgetter\n", + "from chatterbot import ChatBot\n", + "from chatterbot.trainers import ListTrainer\n", + "from chatterbot.logic import BestMatch\n", + "from nltk.tokenize import word_tokenize\n", + "from nltk.corpus import stopwords\n", + "\n", + "# Download the required NLTK packages\n", + "nltk.download(\"punkt\")\n", + "nltk.download(\"averaged_perceptron_tagger\")\n", + "nltk.download(\"maxent_ne_chunker\")\n", + "nltk.download(\"words\")\n", + "\n", + "# Define your training data here\n", + "\n", + "# Create a new chatbot\n", + "logging.getLogger('chatterbot').setLevel(logging.ERROR)\n", + "chatbot = ChatBot(\n", + " \"PyGoatBot\",\n", + " storage_adapter=\"chatterbot.storage.SQLStorageAdapter\",\n", + " database_uri=\"sqlite:///database.sqlite3\",\n", + " logic_adapters=[\n", + " {\n", + " \"import_path\": \"chatterbot.logic.BestMatch\",\n", + " \"default_response\": \"I'm sorry, I'm not sure I understand.\",\n", + " \"maximum_similarity_threshold\": 0.90,\n", + " }\n", + " ],\n", + ")\n", + "\n", + "# Train the chatbot with the training data\n", + "trainer = ListTrainer(chatbot)\n", + "trainer.train(training_data)\n", + "nltk.download('stopwords')\n", + "\n", + "print(\"Welcome to PyGoatBot! Type 'q or exit' to quit.\")\n", + "print()\n", + "print(\"How can I help?\")\n", + "while True:\n", + " try:\n", + " user_input = input(\"You: \")\n", + " if user_input.lower() == \"exit\" or user_input.lower()==\"q\":\n", + " break\n", + " sentence = user_input\n", + " sentence = sentence.lower()\n", + " words = word_tokenize(sentence)\n", + " stop_words = set(stopwords.words('english'))\n", + " words = [word for word in words if not word in stop_words]\n", + " tagged_words = nltk.pos_tag(words)\n", + " keywords = []\n", + " for word, tag in tagged_words:\n", + " if tag.startswith('N') or tag.startswith('J'):\n", + " keywords.append(word)\n", + "\n", + "\n", + " if not keywords:\n", + " print(\"I'm sorry, I didn't find any important keywords in your input.\")\n", + " continue\n", + "\n", + " dict_que={}\n", + " for i in range(0, len(training_data), 2):\n", + " keyword_counts = 0\n", + " for keyword in keywords:\n", + " if keyword.lower() in training_data[i].lower():\n", + " keyword_counts += 1\n", + " dict_que[training_data[i]]=keyword_counts\n", + "\n", + " sorted_dict = sorted(dict_que.items(), key=lambda x: x[1], reverse=True)\n", + " top_questions=sorted_dict[0:4]\n", + "\n", + " if not top_questions:\n", + " print(\"I'm sorry, I couldn't find any relevant questions in my database.\")\n", + " continue\n", + " print()\n", + " print(\"--Available questions:--\")\n", + " for i, question in enumerate(top_questions):\n", + " print(f\" {i+1}. {question[0]}\")\n", + " print(\" 5. Main Menu\")\n", + " print()\n", + "\n", + " while True:\n", + " try:\n", + " question_index = int(input(\"Enter a number to select a question: \"))\n", + " if 1 <= question_index <= 4:\n", + " break\n", + " elif question_index==5:\n", + " print(\"Taking you to the main menu\")\n", + " break\n", + " \n", + " else:\n", + " print(\"Please enter a number between 1 and 4.\")\n", + "\n", + " except ValueError:\n", + " print(\"Please enter a valid number.\")\n", + "\n", + " if question_index!=5:\n", + " question = top_questions[question_index - 1][0]\n", + " response = chatbot.get_response(question)\n", + "\n", + " # Output\n", + " print(f\"PyGoatBot: {response}\")\n", + "\n", + " except (KeyboardInterrupt, EOFError):\n", + " break\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dklscBXxWCUB", + "outputId": "6953214c-1855-4b9f-9f7c-026c43f12850" + }, + "execution_count": 46, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "List Trainer: [####################] 100%" + ] + }, + { + "output_type": "stream", + "name": "stderr", + "text": [ + "[nltk_data] Downloading package punkt to /root/nltk_data...\n", + "[nltk_data] Package punkt is already up-to-date!\n", + "[nltk_data] Downloading package averaged_perceptron_tagger to\n", + "[nltk_data] /root/nltk_data...\n", + "[nltk_data] Package averaged_perceptron_tagger is already up-to-\n", + "[nltk_data] date!\n", + "[nltk_data] Downloading package maxent_ne_chunker to\n", + "[nltk_data] /root/nltk_data...\n", + "[nltk_data] Package maxent_ne_chunker is already up-to-date!\n", + "[nltk_data] Downloading package words to /root/nltk_data...\n", + "[nltk_data] Package words is already up-to-date!\n", + "[nltk_data] Downloading package averaged_perceptron_tagger to\n", + "[nltk_data] /root/nltk_data...\n", + "[nltk_data] Package averaged_perceptron_tagger is already up-to-\n", + "[nltk_data] date!\n", + "[nltk_data] Downloading package punkt to /root/nltk_data...\n", + "[nltk_data] Package punkt is already up-to-date!\n", + "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", + "[nltk_data] Package stopwords is already up-to-date!\n", + "[nltk_data] Downloading package stopwords to /root/nltk_data...\n", + "[nltk_data] Package stopwords is already up-to-date!\n" + ] + }, + { + "output_type": "stream", + "name": "stdout", + "text": [ + "\r\n", + "Welcome to PyGoatBot! Type 'q or exit' to quit.\n", + "You: How to contribute in pygoat?\n", + "\n", + "--Available questions:--\n", + " 1. Can I contribute to PyGoat?\n", + " 2. What is OWASP PyGoat?\n", + " 3. What types of vulnerabilities can PyGoat help me learn about?\n", + " 4. How can I use PyGoat to learn web security testing?\n", + " 5. Main Menu\n", + "\n", + "Enter a number to select a question: 1\n", + "PyGoatBot: Yes, PyGoat is an open-source project and welcomes contributions from anyone interested in improving the application.\n", + "You: How to download pygoat?\n", + "\n", + "--Available questions:--\n", + " 1. Where can I download PyGoat?\n", + " 2. What is OWASP PyGoat?\n", + " 3. What types of vulnerabilities can PyGoat help me learn about?\n", + " 4. How can I use PyGoat to learn web security testing?\n", + " 5. Main Menu\n", + "\n", + "Enter a number to select a question: how to download?\n", + "Please enter a valid number.\n", + "Enter a number to select a question: 1\n", + "PyGoatBot: You can download PyGoat from the official GitHub repository at https://github.com/OWASP/PyGoat\n", + "You: what is web security testing?\n", + "\n", + "--Available questions:--\n", + " 1. Why should I learn web security testing?\n", + " 2. How can I use PyGoat to learn web security testing?\n", + " 3. What is OWASP PyGoat?\n", + " 4. What types of vulnerabilities can PyGoat help me learn about?\n", + " 5. Main Menu\n", + "\n", + "Enter a number to select a question: 2\n", + "PyGoatBot: PyGoat includes a series of lessons and challenges designed to teach you about web security testing techniques and common vulnerabilities.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "jpYCc8FL6Gzi" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file