본문 바로가기

PY(Python Image Processing)

LLM Summary prompt test to facebook/bart-large-cnn

728x90

[download bart-large-cnn to local model path]

facebook/bart-large-cnn at main (huggingface.co)

 

facebook/bart-large-cnn at main

Detected Pickle imports (3) "torch.FloatStorage", "collections.OrderedDict", "torch._utils._rebuild_tensor_v2" What is a pickle import?

huggingface.co

 

[app.py]

from transformers import BartForConditionalGeneration, BartTokenizer

# 모델과 토크나이저 로드
#model_name = 'facebook/bart-large-cnn'
local_model_path = 'D:/bart-large-cnn'
model = BartForConditionalGeneration.from_pretrained(local_model_path)
tokenizer = BartTokenizer.from_pretrained(local_model_path)

# 요약을 위한 함수 정의
def summarize_text(text, max_length=200, min_length=30, length_penalty=2.0):
    # 텍스트를 토큰화하고 텐서로 변환
    inputs = tokenizer.encode(text, return_tensors='pt', truncation=True, max_length=1024)
   
    # 요약 생성
    summary_ids = model.generate(inputs,
                                max_length=max_length,
                                min_length=min_length,
                                length_penalty=length_penalty,
                                num_beams=4,
                                early_stopping=True)
   
    # 생성된 요약을 디코딩
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary

# 예제 문서
document = """
Sports rehabilitation is one of the useful treatment methods for trainers, doctors, and coaches.
(Allmqu, 1978), When using sports rehabilitation and combining other treatment methods,
It can help recreational athletes and athletes return safely and quickly.
However, athletes with functional impairment consider the goals of rehabilitation differently than the general public.
Treatment should be done with the goal of recovering major function in a short period of time.
The injured area must be rehabilitated along with treatment, and early rehabilitation is
Harm can be reduced.
Functional rehabilitation does not wait until the injured area is completely healed, but rather
Rehabilitation begins during the treatment period. This will allow for a much faster recovery and tissue recovery.
Quality is improved and rehabilitation aims to shorten the recovery period. Also, any
Determine the propensity of injured athletes to return to sports after rehabilitation
Therefore, the intensity of exercise and the goals accordingly change, and the goals are as follows (Na Young-moo)
et al., 2002).
"""

# 문서 요약
summary = summarize_text(document)

# 결과 출력
print("Original Document:")
print(document)
print("\nSummary:")
print(summary)

 

[operation]

pip install transformers

python app.py

 

[result]

C:\Python311\Lib\site-packages\transformers\tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884
  warnings.warn(
Original Document:

Sports rehabilitation is one of the useful treatment methods for trainers, doctors, and coaches.
(Allmqu, 1978), When using sports rehabilitation and combining other treatment methods,
It can help recreational athletes and athletes return safely and quickly.
However, athletes with functional impairment consider the goals of rehabilitation differently than the general public.
Treatment should be done with the goal of recovering major function in a short period of time.
The injured area must be rehabilitated along with treatment, and early rehabilitation is
Harm can be reduced.
Functional rehabilitation does not wait until the injured area is completely healed, but rather
Rehabilitation begins during the treatment period. This will allow for a much faster recovery and tissue recovery.
Quality is improved and rehabilitation aims to shorten the recovery period. Also, any
Determine the propensity of injured athletes to return to sports after rehabilitation
Therefore, the intensity of exercise and the goals accordingly change, and the goals are as follows (Na Young-moo)
et al., 2002).


Summary:
Sports rehabilitation is one of the useful treatment methods for trainers, doctors, and coaches. Athletes with functional impairment consider the goals of rehabilitation differently than the general public.

728x90