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

RSSからYoutube/Podcast配信

翻訳

Google翻訳を使った手順

今回も現在使用している方法とは異なりますがGoogle翻訳を使用してテキスト翻訳します。
この作業にはGCPとBloggerの連携の手続きが先に必要です。

Pythonライブラリの追加

$ pip install google-api-python-client
$ pip install google-auth-httplib2
$ pip install google-auth-oauthlib 
$ pip install google-cloud-translate 

Pythonプログラム

test.pyに以下のコードを記述します。
Google翻訳を使用して英語から日本語に変換します。

test.py
def getTrans(text): trans = get_authenticated_service(TRANS_API_SERVICE_NAME,TRANS_API_VERSION) project=trans.projects() response=project.translateText( parent = 'projects/your_project_name', body={ 'contents': [text], 'sourceLanguageCode': 'en-US', 'targetLanguageCode': 'ja-JP', 'mimeType': 'text/plain' } ).execute() translations = response.get('translations') if translations == None or len(translations) == 0: print(response) return None translatedText = translations[0].get('translatedText') return translatedText
your_project_nameはGCPで作成したプロジェクト名(RSS2Podcast)をいれてください。
以前のgetBody()から上記のgetTrans()を呼び出します。
タイトルや本文に色々ゴミが入ってくるので軽く削除してます。
またブログアップロードする際に改行文字をHTMLの改行タグ(<BR>)に変更しています。
test.py
def getBody(link):    try :        res = requests.get(link)
        extractor.analyse(res.text)
        text, title = extractor.as_text()
        title = re.sub('[-|:|\||\[|\(|\{].*','',title)
        text = re.sub('&.*?;','',text)
        text = getSummary(text)
        title = getTrans(title)
        text = getTrans(text)
        return postBlog(title,text.replace('\n','<BR>'),'TECHNOLOGY')
    except Exception as e :
        print(e)
        return None
結果はこちら


これで要約&翻訳した記事を投稿できるようになりました。
次は音声作成に入ります。

参考URL:

Cloud Translation API  |  Google Cloud
 

このブログの人気の投稿

StableDiffusionを使った画像生成&動画生成

  StableDiffusionが世を賑わかせているので、便乗してニュースのテキストから画像を生成し、TTSの音声と併せて動画にしてみた。 まずは画像生成用のモジュールから。 ! pip install accelerate diffusers transformers scipy ftfy # make sure you're logged in with `huggingface-cli login` from  torch  import  autocast from  diffusers  import  StableDiffusionPipeline import  gc SDpipe = StableDiffusionPipeline.from_pretrained (      "stabilityai/stable-diffusion-2" ,     use_auth_token= "enter your token" ) .to ( "cuda" ) def   getImgFromPrompt ( prompt , imgName ) :     gc.collect ( generation= 0 )     gc.collect ( generation= 1 )     gc.collect ( generation= 2 )     image = SDpipe ( prompt , height= 512 ,  width= 512 ) .images [ 0 ]     display ( image )     image.save ( im...

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を作成しておいてください。 https://beta.openai.com/account/api-keys 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 )   ...