{"id":25345,"date":"2024-05-29T20:43:55","date_gmt":"2024-05-29T11:43:55","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25345"},"modified":"2024-05-29T20:43:56","modified_gmt":"2024-05-29T11:43:56","slug":"gpt-2-chatbot","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=25345","title":{"rendered":"GPT-2 ChatBot"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>import nltk\nfrom transformers import pipeline\nfrom sklearn.feature_extraction.text import TfidfVectorizer\nfrom sklearn.linear_model import LogisticRegression\nimport numpy as np\nimport spacy\n\n# nltk\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\uff08\u521d\u56de\u306e\u307f\uff09\nnltk.download('punkt')\n\n# spaCy\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\nnlp = spacy.load(\"en_core_web_sm\")\n\n# \u30b5\u30f3\u30d7\u30eb\u30c7\u30fc\u30bf\uff08\u30a4\u30f3\u30c6\u30f3\u30c8\u3068\u305d\u306e\u30b5\u30f3\u30d7\u30eb\u6587\uff09\ntraining_sentences = &#91;\n    \"Hello\", \"Hi\", \"Hey\", \"Good morning\", \"Good evening\",\n    \"How are you?\", \"What's up?\", \"How's it going?\",\n    \"Bye\", \"Goodbye\", \"See you later\", \"Take care\",\n    \"Thank you\", \"Thanks\", \"I appreciate it\",\n    \"What's your name?\", \"Who are you?\",\n    \"What can you do?\", \"Tell me a joke\", \"Make me laugh\",\n    \"What's the weather like?\", \"How's the weather?\",\n    \"Book a flight\", \"I need to book a flight\", \"Can you book a flight for me?\"\n]\n\nintents = &#91;\n    \"greeting\", \"greeting\", \"greeting\", \"greeting\", \"greeting\",\n    \"how_are_you\", \"how_are_you\", \"how_are_you\",\n    \"goodbye\", \"goodbye\", \"goodbye\", \"goodbye\",\n    \"thanks\", \"thanks\", \"thanks\",\n    \"name\", \"name\",\n    \"capabilities\", \"joke\", \"joke\",\n    \"weather\", \"weather\",\n    \"book_flight\", \"book_flight\", \"book_flight\"\n]\n\n# \u7279\u5fb4\u62bd\u51fa\u5668\u3068\u5206\u985e\u5668\u306e\u30bb\u30c3\u30c8\u30a2\u30c3\u30d7\nvectorizer = TfidfVectorizer()\nX = vectorizer.fit_transform(training_sentences)\nclassifier = LogisticRegression()\nclassifier.fit(X, intents)\n\n# GPT-2 \u3092\u4f7f\u7528\u3057\u305f\u30c6\u30ad\u30b9\u30c8\u751f\u6210\u30d1\u30a4\u30d7\u30e9\u30a4\u30f3\u306e\u4f5c\u6210\nchatbot = pipeline(\"text-generation\", model=\"gpt2\")\n\n# \u30a4\u30f3\u30c6\u30f3\u30c8\u306b\u57fa\u3065\u304f\u5fdc\u7b54\nresponses = {\n    \"greeting\": &#91;\"Hello! How can I help you?\", \"Hi there! What can I do for you?\"],\n    \"how_are_you\": &#91;\"I'm just a bot, but I'm here to help you!\", \"I'm fine, thank you! How can I assist you today?\"],\n    \"goodbye\": &#91;\"Goodbye! Have a great day!\", \"See you later!\"],\n    \"thanks\": &#91;\"You're welcome!\", \"No problem!\"],\n    \"name\": &#91;\"I am your friendly chatbot.\", \"I'm an AI created to assist you.\"],\n    \"capabilities\": &#91;\"I can chat with you and help answer your questions!\", \"I'm here to assist you with various tasks.\"],\n    \"joke\": &#91;\"Why did the scarecrow win an award? Because he was outstanding in his field!\"],\n    \"weather\": &#91;\"The weather is nice today!\", \"It's a bit cloudy, but still good.\"],\n    \"book_flight\": &#91;\"Sure, I can help you with that. Where would you like to go?\"]\n}\n\n# \u672a\u77e5\u306e\u30a4\u30f3\u30c6\u30f3\u30c8\u306b\u5bfe\u3059\u308b\u30a8\u30e9\u30fc\u30ec\u30b9\u30dd\u30f3\u30b9\ndefault_responses = &#91;\"I'm not sure I understand. Can you please rephrase?\", \"Sorry, I don't have an answer for that.\"]\n\n# \u30a4\u30f3\u30c6\u30f3\u30c8\u8a8d\u8b58\ndef get_intent(user_input):\n    X_test = vectorizer.transform(&#91;user_input])\n    intent = classifier.predict(X_test)&#91;0]\n    return intent\n\n# \u30a8\u30f3\u30c6\u30a3\u30c6\u30a3\u8a8d\u8b58\ndef get_entities(user_input):\n    doc = nlp(user_input)\n    entities = {ent.label_: ent.text for ent in doc.ents}\n    return entities\n\n# \u5fdc\u7b54\u751f\u6210\ndef get_response(user_input):\n    intent = get_intent(user_input)\n    entities = get_entities(user_input)\n    \n    if intent in responses:\n        response = np.random.choice(responses&#91;intent])\n        if intent == \"book_flight\" and \"GPE\" in entities:\n            response = f\"Sure, I can help you book a flight to {entities&#91;'GPE']}. When would you like to travel?\"\n        return response\n    else:\n        response = chatbot(user_input, max_length=50, num_return_sequences=1)\n        return response&#91;0]&#91;'generated_text']\n\n# \u30e1\u30a4\u30f3\u95a2\u6570\ndef main():\n    print(\"Chatbot: Hello! How can I help you today? (Type 'exit' to quit)\")\n    \n    while True:\n        user_input = input(\"You: \")\n        if user_input.lower() == 'exit':\n            print(\"Chatbot: Goodbye!\")\n            break\n        response = get_response(user_input)\n        print(f\"Chatbot: {response}\")\n\nif __name__ == \"__main__\":\n    main()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[55,4],"tags":[3,39],"class_list":["post-25345","post","type-post","status-publish","format-standard","hentry","category-ai","category-programming","tag-programming","tag-python"],"aioseo_notices":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=25345"}],"version-history":[{"count":1,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25345\/revisions"}],"predecessor-version":[{"id":25346,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25345\/revisions\/25346"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25345"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}