{"id":18947,"date":"2024-02-12T21:48:53","date_gmt":"2024-02-12T12:48:53","guid":{"rendered":"http:\/\/www.tyosuke20xx.com\/blog\/?p=18947"},"modified":"2024-02-12T23:24:00","modified_gmt":"2024-02-12T14:24:00","slug":"gpt-2%e3%81%a8wikipedia%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6%e3%83%a9%e3%83%b3%e3%83%80%e3%83%a0%e3%81%ab%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8bai","status":"publish","type":"post","link":"http:\/\/www.tyosuke20xx.com\/blog\/?p=18947","title":{"rendered":"GPT-2\u3068Wikipedia\u3092\u4f7f\u3063\u3066\u751f\u6210\u3059\u308bAI"},"content":{"rendered":"\n<p>GPT-2.py<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>from flask import Flask, render_template, request, redirect, url_for\nfrom transformers import GPT2Tokenizer, GPT2LMHeadModel\nimport wikipedia\n\napp = Flask(__name__)\n\n# GPT-2\u306e\u30c8\u30fc\u30af\u30ca\u30a4\u30b6\u30fc\u3068\u30e2\u30c7\u30eb\u3092\u30ed\u30fc\u30c9\ntokenizer = GPT2Tokenizer.from_pretrained(&quot;gpt2&quot;)\nmodel = GPT2LMHeadModel.from_pretrained(&quot;gpt2&quot;)\n\n@app.route(&#39;\/&#39;)\ndef index():\n    return render_template(&#39;index.html&#39;)\n\n@app.route(&#39;\/generate&#39;, methods=[&#39;POST&#39;])\ndef generate_text():\n    # \u30e6\u30fc\u30b6\u30fc\u304b\u3089\u306e\u5165\u529b\u3092\u53d6\u5f97\n    prompt_text = request.form[&#39;prompt&#39;]\n    \n    try:\n        # Wikipedia\u304b\u3089\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\n        wikipedia_text = wikipedia.summary(prompt_text)\n        \n        # \u30c6\u30ad\u30b9\u30c8\u306e\u751f\u6210\n        inputs = tokenizer.encode(wikipedia_text, return_tensors=&quot;pt&quot;)\n        outputs = model.generate(inputs, max_length=100, num_return_sequences=1, temperature=0.7)\n        \n        # \u751f\u6210\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u3066HTML\u30b3\u30fc\u30c9\u306b\u7d44\u307f\u8fbc\u3080\n        generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n        \n        # \u751f\u6210\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u3068Wikipedia\u306e\u30c6\u30ad\u30b9\u30c8\u3068\u5171\u306bHTML\u3092\u8fd4\u3059\n        return render_template(&#39;index.html&#39;, prompt_text=prompt_text, generated_text=generated_text, wikipedia_text=wikipedia_text)\n    \n    except wikipedia.exceptions.DisambiguationError as e:\n        # \u66d6\u6627\u6027\u304c\u3042\u308b\u5834\u5408\u306f\u3001\u5019\u88dc\u306e\u30ea\u30b9\u30c8\u3092\u8868\u793a\n        options = e.options\n        return render_template(&#39;disambiguation.html&#39;, options=options)\n    \n    except wikipedia.exceptions.PageError:\n        wikipedia_text = &quot;Wikipedia\u306b\u305d\u306e\u30c8\u30d4\u30c3\u30af\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002&quot;\n        return render_template(&#39;index.html&#39;, prompt_text=prompt_text, wikipedia_text=wikipedia_text)\n\n@app.route(&#39;\/generate_with_option\/&lt;option&gt;&#39;, methods=[&#39;GET&#39;])\ndef generate_with_option(option):\n    try:\n        # Wikipedia\u304b\u3089\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\n        wikipedia_text = wikipedia.summary(option)\n        \n        # \u30c6\u30ad\u30b9\u30c8\u306e\u751f\u6210\n        inputs = tokenizer.encode(wikipedia_text, return_tensors=&quot;pt&quot;)\n        outputs = model.generate(inputs, max_length=100, num_return_sequences=1, temperature=0.7)\n        \n        # \u751f\u6210\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u3092\u30c7\u30b3\u30fc\u30c9\u3057\u3066HTML\u30b3\u30fc\u30c9\u306b\u7d44\u307f\u8fbc\u3080\n        generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)\n        \n        # \u751f\u6210\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u3068Wikipedia\u306e\u30c6\u30ad\u30b9\u30c8\u3068\u5171\u306bHTML\u3092\u8fd4\u3059\n        return render_template(&#39;index.html&#39;, prompt_text=option, generated_text=generated_text, wikipedia_text=wikipedia_text)\n    \n    except wikipedia.exceptions.PageError:\n        wikipedia_text = &quot;Wikipedia\u306b\u305d\u306e\u30c8\u30d4\u30c3\u30af\u304c\u898b\u3064\u304b\u308a\u307e\u305b\u3093\u3067\u3057\u305f\u3002&quot;\n        return render_template(&#39;index.html&#39;, prompt_text=option, wikipedia_text=wikipedia_text)\n\nif __name__ == &#39;__main__&#39;:\n    app.run(debug=True)\n<\/code><\/pre><\/div>\n\n\n\n<p>templates\/index.html<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-html\" data-lang=\"HTML\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Generate Text&lt;\/title&gt;\n    &lt;style&gt;\n        body {\n            font-family: Arial, sans-serif;\n            margin: 0;\n            padding: 0;\n            background-color: #f4f4f4;\n        }\n\n        h1 {\n            text-align: center;\n            margin-top: 50px;\n        }\n\n        form {\n            max-width: 600px;\n            margin: 0 auto;\n            padding: 20px;\n            background-color: #fff;\n            border-radius: 8px;\n            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n        }\n\n        label {\n            font-weight: bold;\n        }\n\n        input[type=&quot;text&quot;] {\n            width: 100%;\n            padding: 10px;\n            margin: 10px 0;\n            border: 1px solid #ccc;\n            border-radius: 4px;\n        }\n\n        button {\n            padding: 10px 20px;\n            background-color: #007bff;\n            color: #fff;\n            border: none;\n            border-radius: 4px;\n            cursor: pointer;\n            transition: background-color 0.3s ease;\n        }\n\n        button:hover {\n            background-color: #0056b3;\n        }\n\n        .response {\n            max-width: 600px;\n            margin: 20px auto;\n            padding: 20px;\n            background-color: #fff;\n            border-radius: 8px;\n            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n        }\n\n        .generated-text {\n            margin-top: 20px;\n        }\n\n        .tweet-link {\n            display: block;\n            margin-top: 10px;\n            text-align: center;\n        }\n    &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Generate Text&lt;\/h1&gt;\n    &lt;form action=&quot;\/generate&quot; method=&quot;POST&quot;&gt;\n        &lt;label for=&quot;prompt&quot;&gt;Enter your prompt:&lt;\/label&gt;&lt;br&gt;\n        &lt;input type=&quot;text&quot; id=&quot;prompt&quot; name=&quot;prompt&quot;&gt;&lt;br&gt;&lt;br&gt;\n        &lt;button type=&quot;submit&quot;&gt;Generate&lt;\/button&gt;\n    &lt;\/form&gt;\n\n    &lt;!-- \u751f\u6210\u3055\u308c\u305f\u30c6\u30ad\u30b9\u30c8\u3092\u8868\u793a --&gt;\n    {% if generated_text %}\n    &lt;div class=&quot;response&quot;&gt;\n        &lt;h2&gt;Generated Text:&lt;\/h2&gt;\n        &lt;p class=&quot;generated-text&quot;&gt;{{ generated_text }}&lt;\/p&gt;\n        &lt;a href=&quot;https:\/\/twitter.com\/intent\/tweet?text={{ generated_text }}&quot; class=&quot;tweet-link&quot; target=&quot;_blank&quot;&gt;Tweet&lt;\/a&gt;\n    &lt;\/div&gt;\n    {% endif %}\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre><\/div>\n\n\n\n<p>templates\/disambiguation.html<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-html\" data-lang=\"HTML\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Disambiguation Page&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;h1&gt;Disambiguation&lt;\/h1&gt;\n    &lt;p&gt;Multiple meanings were found for the provided prompt. Please select the intended meaning:&lt;\/p&gt;\n    &lt;ul&gt;\n        {% for option in options %}\n            &lt;li&gt;&lt;a href=&quot;{{ url_for(&#39;generate_with_option&#39;, option=option) }}&quot;&gt;{{ option }}&lt;\/a&gt;&lt;\/li&gt;\n        {% endfor %}\n    &lt;\/ul&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>GPT-2.py templates\/index.html templates\/disambiguation.html<\/p>\n","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,81,80,4,65],"tags":[30,42,3,39],"class_list":["post-18947","post","type-post","status-publish","format-standard","hentry","category-ai","category-css","category-html","category-programming","category-python","tag-css","tag-html","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\/18947","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=18947"}],"version-history":[{"count":4,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/18947\/revisions"}],"predecessor-version":[{"id":18958,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/18947\/revisions\/18958"}],"wp:attachment":[{"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18947"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tyosuke20xx.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}