2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

5分で作る: Flask(Python)とHTMLを使ったChatGPTテキスト要約ツール

Posted at

HTMLとPython (Flask) を使用して要約アプリを作成する工程をまとめたものです

1 必要なライブラリをインストール。

pip install flask openai

2 HTMLの作成 (templates/index.html)

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>テキスト要約</title>
</head>
<body>
    <h1>テキストを要約します</h1>

    <form action="/" method="post">
        <textarea name="text_input" rows="5" cols="50"></textarea><br>
        <input type="submit" value="要約">
    </form>

    <h2>要約結果:</h2>
    <p>{{ summary }}</p>
</body>
</html>

3 Pythonコードの作成 (app.py)

from flask import Flask, render_template, request
import openai

openai.api_key = ''

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    summary = ""
    if request.method == 'POST':
        text = request.form['text_input']
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "あなたは優秀な要約をする人です。30文字以内で要約してください"},
                {"role": "user", "content": text},
            ]
        )
        summary = response['choices'][0]['message']['content']
    return render_template('index.html', summary=summary)

if __name__ == '__main__':
    app.run(debug=True)

4 アプリの実行:ターミナルで以下のコマンドを入力して、環境変数を設定。

$env:FLASK_APP = "app.py"
$env:FLASK_ENV = "development"
flask run

簡単!!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?