[TIL]22.02.18 BERTを開く


import streamlit as st   # 앱을 만드는 미니멀한 프레임워크
import os
import torch
import nltk
import urllib.request
from models.model_builder import ExtSummarizer
from newspaper import Article
from ext_sum import summarize


def main():
    st.markdown("<h1 style='text-align: center;'>Extractive Summary✏️</h1>", unsafe_allow_html=True)

    # Download model
    if not os.path.exists('checkpoints/mobilebert_ext.pt'):
        download_model()

    # Load model
    model = load_model('mobilebert')

    # Input
    ## input type 버튼 생성해서 raw/url 클릭하는지에 따라 실행하는 방법
    input_type = st.radio("Input Type: ", ["URL", "Raw Text"])
    st.markdown("<h3 style='text-align: center;'>Input</h3>", unsafe_allow_html=True)

    if input_type == "Raw Text":
        with open("raw_data/input.txt") as f:
            sample_text = f.read()
        text = st.text_area("", sample_text, 200)
    else:
        url = st.text_input("", "https://www.cnn.com/2020/05/29/tech/facebook-violence-trump/index.html")
        st.markdown(f"[*Read Original News*]({url})")
        text = crawl_url(url)

    input_fp = "raw_data/input.txt"
    with open(input_fp, 'w') as file:
        file.write(text)

    # Summarize
    ## 결과물의 길이를 버튼으로
    sum_level = st.radio("Output Length: ", ["Short", "Medium"])
    ## 버튼에 따른 결과물 길이
    max_length = 3 if sum_level == "Short" else 5
    result_fp = 'results/summary.txt'
    ## 실제로 요약된 값
    summary = summarize(input_fp, result_fp, model, max_length=max_length)
    st.markdown("<h3 style='text-align: center;'>Summary</h3>", unsafe_allow_html=True)
    st.markdown(f"<p align='justify'>{summary}</p>", unsafe_allow_html=True)


def download_model():
    nltk.download('popular')
    url = 'https://www.googleapis.com/drive/v3/files/1umMOXoueo38zID_AKFSIOGxG9XjS5hDC?alt=media&key=AIzaSyCmo6sAQ37OK8DK4wnT94PoLx5lx-7VTDE'

    # These are handles to two visual elements to animate.
    weights_warning, progress_bar = None, None
    try:
        weights_warning = st.warning("Downloading checkpoint...")
        progress_bar = st.progress(0)
        with open('checkpoints/mobilebert_ext.pt', 'wb') as output_file:
            with urllib.request.urlopen(url) as response:
                length = int(response.info()["Content-Length"])
                counter = 0.0
                MEGABYTES = 2.0 ** 20.0
                while True:
                    data = response.read(8192)
                    if not data:
                        break
                    counter += len(data)
                    output_file.write(data)

                    # We perform animation by overwriting the elements.
                    weights_warning.warning("Downloading checkpoint... (%6.2f/%6.2f MB)" %
                        (counter / MEGABYTES, length / MEGABYTES))
                    progress_bar.progress(min(counter / length, 1.0))

    # Finally, we remove these visual elements by calling .empty().
    finally:
        if weights_warning is not None:
            weights_warning.empty()
        if progress_bar is not None:
            progress_bar.empty()


@st.cache(suppress_st_warning=True)
def load_model(model_type):
    checkpoint = torch.load(f'checkpoints/{model_type}_ext.pt', map_location='cpu')
    model = ExtSummarizer(device="cpu", checkpoint=checkpoint, bert_type=model_type)
    return model


def crawl_url(url):
    article = Article(url)
    article.download()
    article.parse()
    return article.text


if __name__ == "__main__":
    main()


https://zzsza.github.io/mlops/2021/02/07/python-streamlit-dashboard/
input
最初のエラー
Traceback (most recent call last):
  File "real_test.py", line 104, in <module>
    main()
  File "real_test.py", line 48, in main
    summary = summarize(input_fp, result_fp, model, max_length=max_length)
  File "/home/u7ryean/project-template/ai/bert-extractive-summarization/ext_sum.py", line 114, in summarize
    test(model, input_data, result_fp, max_length, block_trigram=True)
  File "/home/u7ryean/project-template/ai/bert-extractive-summarization/ext_sum.py", line 80, in test
    sent_scores, mask = model(src, segs, clss, mask, mask_cls)
  File "/home/u7ryean/anaconda3/envs/test/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/u7ryean/project-template/ai/bert-extractive-summarization/models/model_builder.py", line 47, in forward
    sents_vec = top_vec[torch.arange(top_vec.size(0)).unsqueeze(1), clss]
IndexError: tensors used as indices must be long, byte or bool tensors

成果物:MarianaMazzucato Government

PyTorch version 1.1.0 available.
PyTorch version 1.1.0 available.
loading configuration file checkpoints/mobilebert/config.json
Model config MobileBertConfig {
  "attention_probs_dropout_prob": 0.1,
  "classifier_activation": false,
  "embedding_size": 128,
  "hidden_act": "relu",
  "hidden_dropout_prob": 0.0,
  "hidden_size": 512,
  "initializer_range": 0.02,
  "intermediate_size": 512,
  "intra_bottleneck_size": 128,
  "key_query_shared_bottleneck": true,
  "layer_norm_eps": 1e-12,
  "max_position_embeddings": 512,
  "model_type": "bert",
  "normalization_type": "no_norm",
  "num_attention_heads": 4,
  "num_feedforward_networks": 4,
  "num_hidden_layers": 24,
  "pad_token_id": 0,
  "trigram_input": true,
  "true_hidden_size": 128,
  "type_vocab_size": 2,
  "use_bottleneck": true,
  "use_bottleneck_attention": false,
  "vocab_size": 30522
}

loading file https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt from cache at /home/u7ryean/.cache/torch/transformers/26bc1ad6c0ac742e9b52263248f6d0f00068293b33709fae12320c0e35ccfbbb.542ce4285a40d23a559526243235df47c5f75c197f04f37d1a0c124c32c9a084

成果物:input

PyTorch version 1.1.0 available.
PyTorch version 1.1.0 available.
loading configuration file checkpoints/mobilebert/config.json
Model config MobileBertConfig {
  "attention_probs_dropout_prob": 0.1,
  "classifier_activation": false,
  "embedding_size": 128,
  "hidden_act": "relu",
  "hidden_dropout_prob": 0.0,
  "hidden_size": 512,
  "initializer_range": 0.02,
  "intermediate_size": 512,
  "intra_bottleneck_size": 128,
  "key_query_shared_bottleneck": true,
  "layer_norm_eps": 1e-12,
  "max_position_embeddings": 512,
  "model_type": "bert",
  "normalization_type": "no_norm",
  "num_attention_heads": 4,
  "num_feedforward_networks": 4,
  "num_hidden_layers": 24,
  "pad_token_id": 0,
  "trigram_input": true,
  "true_hidden_size": 128,
  "type_vocab_size": 2,
  "use_bottleneck": true,
  "use_bottleneck_attention": false,
  "vocab_size": 30522
}

loading file https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased-vocab.txt from cache at /home/u7ryean/.cache/torch/transformers/26bc1ad6c0ac742e9b52263248f6d0f00068293b33709fae12320c0e35ccfbbb.542ce4285a40d23a559526243235df47c5f75c197f04f37d1a0c124c32c9a084

1.事実(Facts)

  • bert-extract-summaryコンピュータ上で
  • を実行
  • 対応モデルai modeltestブランチ
  • を作成してプッシュ
  • は、モデル構造を完全に分解するのではなく、まず結果値を決定し、テスト用のテキスト5個(追加)を4サンプル:
  • 2.感覚(Feeling)

  • 今日初めて襟を使いました!前回のデータ分析の時は、Jupeterで分析結果とデータセットを整理するだけでいいので、書くところがなく、Jupeterファイルもそのままネットから入れました…思ったほど面白くない!
  • でクローンしたファイルをGirlapにアップロードしたGitを繰り返し、問題が発生しました.フォルダが作成されますが、このフォルダは使用できません...Google検索で解決策を見つけました!一つのステップを踏んだだけで、何も消されたような気がしたので諦めました.しかし、それは正しい方法です.もう一度やり直すのは損をすることではなく、むしろ整理が早くなるので、あまり怖がらないでください.今私はニワトリで、間違いを犯してもニワトリのように間違いを犯すだけです.
  • 南に書かれたコードをブラウズし、複数のファイルを巡り、記憶し、整理します.おもしろい!
  • 仮想環境もvscode端末で回っています.このモデルにはWebページが回転するコードがありましたが、その後はすべて触れるのが難しいかもしれませんので、端末で結果値を見るしかありません...データアナリストからデータ科学者になり、今また全スタック開発者になりたい...?忙しいよ忙しいよ
  • 3.勉強(Findings)

  • グーグルを楽しんでください.そしてしっかりやって
  • 羽を使う方法!助けてくれた勝洙ありがとう
  • # 현재 내가 위치한 브랜치
    git branch
    # 브랜치를 새로 만들면서 브랜치 변경
    git checkout -b 브랜치명
    # 브랜치 삭제
    git branch -d 브랜치명
    
    git add .
    git commit -m "커밋할 메세지"
    git config --global user.name "내이름"
    git push
  • は今回のモデルで初めて見ました!ボタンを作ったり、値段をもらったりする過程は簡単そうです.
  • import streamlit as st # 앱을 만드는 미니멀한 프레임 워크

    4.うまくいった、うまくいかなかった原因の分析


    もし今日すべての計画を完成したら、それを達成できる成功要因は何ですか?
    もし願いが叶わなかったら、その理由は何ですか.同じ間違いを繰り返すことを避けるには、どのような面を改善する必要がありますか?
  • でクローンされたファイルを一つ一つ取り外して、頭の中でその様子を描きましたが、知らなかったのは、注釈も加わって、これは理解に大いに役立つようです.
  • もっと具体的なことだけを理解するのは時間の無駄ですか?いつもこのような考えがあるので、もっと深く勉強することができず、実際にバートに関する論文、論文の要約を見ても、何なのか分かりません.やっぱりこの分野は難しいもっと勉強しなければなりません.

    5.計画


    プロジェクト全体の進捗によって、明日からやること、次にやることは何ですか.
    あなたの計画はまだ有効ですか.修正は必要ですか?
  • 明日の勤務時間に、私たちはコーチに私たちの結果を聞いて、良いモデルを使ったかどうか、結果がきれいかどうか、性能を知るためにどんなコードを追加する必要があるかを聞くべきだと思います.個人的には決してCLINCODEではないと思いますが…^^助けてください