スキップしてメイン コンテンツに移動

RSSからYoutube/Podcast配信

GPT-3を使った翻訳・要約

GPT-3とは

「Generative Pre-trained Transformer - 3」の略で、イーロン・マスクなどが出資しているOpenAIという団体が出している自然言語処理モデルになります。
以前までは予約しないと使えないものでしたが、今はアカウントを作成すれば誰でも使用することができるようになったので、この翻訳・要約機能を試してみました。



OpenAIのアカウントを作成

Googleアカウントでログインできるため、Googleアカウントを持っている方ならそのまま「Continue with Google」で入ってください。

このままPlaygroundで遊んでも良いですが、Pythonプログラムに使用するため、以下のURLから「Create New Secret Key」を押してAPI-Keyを作成しておいてください。

Pythonライブラリ

openaiのライブラリやこれまでのRSSリーダー機能に必要なライブラリもインストールします。
$ pip install openai feedparser extractcontent3 

Pythonプログラム

RSSからURLを取得してHTML本文を取得する箇所に関しては以前のものを使いまわします。要約・翻訳のサンプルはPlaygroundから見れますがこれを少し手を加えつつ、パラメータをチューニングしておきました。

# coding: utf-8
import os
import openai
import requests
import feedparser
from extractcontent3 import ExtractContent
openai.api_key = "YourOpenAI-API-Key"
extractor = ExtractContent()
# オプション値を指定する
opt = {"threshold":50}
extractor.set_option(opt)
    
def openaiApi(cmd):
    response = openai.Completion.create(
    engine="davinci",
    prompt=cmd,
    temperature=0.0,
    max_tokens=200,
    top_p=1,
    frequency_penalty=2.0,
    presence_penalty=2.0,
    stop=["\n"]
    )
    #print(response)
    text = response.choices[0].text
    return text

def sumATrans(text):
    summary = openaiApi(text.replace("\n","") + "tl;dr:")
    print(summary)
    trans = openaiApi("English: " + summary + "\nJapanese: ")
    print(trans)

def getSummaryOpenAI(text):
    summary = openaiApi(text.replace("\n","") + "tl;dr:")
    return summary

def getTransOpenAI(text):
    trans = openaiApi("English: " + text + "\nJapanese: ")
    return trans

def getBody(link):
    try :
        res = requests.get(link)
        extractor.analyse(res.text)
        text, title = extractor.as_text()
        print("================================================")
        text = getSummaryOpenAI(text)
        print(text)
        title = getTransOpenAI(title)
        print(title)
        text = getTransOpenAI(text)
        print(text)

    except Exception as e :
        print(e)
        return None

def getRss():
    rssUrl = 'https://news.google.com/news/rss/headlines/section/topic/TECHNOLOGY'
    rssLang = '?hl=en-US&gl=US&ceid=US:en'
    feed = feedparser.parse(rssUrl + rssLang)
    for entry in feed.entries:
            link = entry.get('link')
            getBody(link)

getRss()


以下実行結果です。
================================================
 iOS 15.2 RC 2 and macOS 12.1 RC 2 are now available to developers, with the former adding a new privacy report feature for iPhone 13 Pro users
アップル、iOS 15.2 の最終版と macOS 12.1 を開発者向けにリリース - Mac Otakara
 iOS 15.2 RC 2 と macOS 12.1 RC 2 の両方が開発者向けに公開されました。前回とは異なり、iOS版では iPhone 13 Pro ユーザーの私的情報を収集する新機能も追加
================================================
 TCL has temporarily stopped selling its Google TV-powered televisions over performance and software issues. It plans to bring the 5-series and 6-series TVs back to Best Buy in the coming weeks, per 9to5 Google. Users have complained about the sets on Best Buy product pages and elsewhere, with one noting that "the OS is very slow." TCL says owners will see "marked improvements" via software updates.[Image credit: Engadget]
中国電機メーカー、Google TVを販売中止へ | ITmedia
 TCLは、性能やソフトウェアの問題でGoogle TVを搭載したテレビの販売を一時停止する。Best Buyに5シリーズと6シリーズの画面が近いうちに戻ってくる予定だ。ユーザからベストバイ用素材ペーパークラス上で不具合の投稿もありました;1人は「OS は遅い」と語っています[Image credit: Engadget]
================================================
 it's a new game in the Batman Arkham series, but it stars Wonder Woman. It looks like an open world action adventure with plenty of combat and exploration elements to keep you busy for hours on end. The trailer shows off some pretty impressive visuals too - we're talking lush jungles, ancient ruins and even underwater cities here folks! There are also hints at what appears to be stealth gameplay as well; sneaking up behind enemies before taking them out from afar or using your lasso skills against unsuspecting foes is all part of Diana Prince's repertoire apparently...
『ゲームアワーズ』の新しいトレイラーを全部見る | Rock Paper Shotgun
 アーケードゲームではなく、PS4/Xbox One用の新作として発表された『バットマン リターン・トゥ・ダークネス』の続編。今回もワンダーウーマンが主人公だそうです!

単純な機械翻訳/要約とは異なり、意味を理解して要約してくれている様に見えます。
出力されるテキスト量が制限されているのでまだ難しいですが、GPT-3で置き換えるのもありかもしれません。

このブログの人気の投稿

RSSからYoutube/Podcast配信

皆さん、情報収集はどの様にされていますでしょうか? 私は最先端に情報に触れる為、海外ニュースをRSSで購読しているのですが、私の英語力/語彙力では時間が掛かかってしょうがない。 また、できれば目で読むのではなく、音声で聞き流しながら通勤や他の作業中に行いたい。 という事で自動でニュースを収集・要約・翻訳し、それをブログ・ポッドキャスト・Youtubeに自動投稿するPythonプログラムを作成して時短化しました。 その手法を公開してますので参考にしてください。 Bloggerの立ち上げ方 GCPとBloggerの連携 情報収集自動化 Blogger自動投稿 本文要約 翻訳 音声作成 動画作成 Youtubeにアップロード Youtubeの投稿制限の回避方法 Podcast配信 翻訳・要約の改善(GPT-3) 以下のURLで上記から作成したブログやYoutubeを公開しています。参考までに見てください。 ブログ:海外ニュースを仕入れてお届け YouTube:海外Newsを仕入れてお届け Amazon Music:海外ニュースを仕入れてお届け。 Google Podcasts:海外ニュースを仕入れてお届け。 Apple Podcasts:海外ニュースを仕入れてお届け。

本文要約

要約 今回は現在使用している方法とは異なり、実装が簡単な要約方法になります。 参考元にはニューラルネットワーク(accel-brain-base)を使用した例などもありますので、そちらを参考にしてください。 Pythonライブラリの追加 以下のライブラリをインストールしてください。 $ pip install pysummarization Pythonプログラム test.pyに以下のコードを記述します。 ここでソース記事の各行を重みづけしてから、重要度の高い5つの行だけを選択します。 test.py from pysummarization.nlpbase.auto_abstractor import AutoAbstractor from pysummarization.tokenizabledoc.simple_tokenizer import SimpleTokenizer from pysummarization.abstractabledoc.top_n_rank_abstractor import TopNRankAbstractor def getSummary ( text ): # Object of automatic summarization. auto_abstractor = AutoAbstractor () # Set tokenizer. auto_abstractor . tokenizable_doc = SimpleTokenizer () # Set delimiter for making a list of sentence. auto_abstractor . delimiter_list = [ ". " , ". \n " , " \n " ] # Object of abstracting and filtering document. abstractable_doc = TopNRankAbstractor () # Summarize document. result_dict = auto_abstrac

Bloggerの立ち上げ

Bloggerとは BloggerはGoogleのサービスの一つで、無料でブログを始めることができます。 Googleアカウントが必要となりますので、ない方はアカウントの作成をお願いします。 Bloggerサイトへのログイン https://www.blogger.com/から「ブログの作成」を選択してください。 Googleアカウントを聞かれるので、Googleアカウントとパスワードを入力してください。 ブログの名前を入力 作成するブログの名前を決めます。これは後から変えられるのでとりあえずは書こうとしてる内容に沿ったものを記入してください。 ブログのURL(インターネット上の所在地)を作成します。 こちらも変更できる(※)のですがので、頻繁に変更すると読者や検索などから外されてしますので、個人名や法人名など変更しないことを前提に名付けた方が良いと思います。 (※「.blogspot.com」以外のカスタムドメインを自分でとって、それを設定することもできます) プロファイルの作成 ブログが作成されるとプロファイルの作成画面が出てきます。ブログ読者に見られることを意識して、ユーザー名やわかりやすい説明を記入しましょう。 以上でブログが作成できました。作成したURLをブラウザに打ち込んでみましょう! 貴方だけのブログサイトが立ち上がりました。 次は GCP(Google Cloud Platform)とBloggerを連携 させてみましょう。 参考: Blogger Help