# -*- coding: utf-8 -*- """20231115_hf_space의 사본 Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/167WkIz-J7_z4FG65GkVPfkosxIXlKMQq """ # 기사 요약해주는 각자 개인 사이트 만들기 # 사이트: github pages: huggingface space import gradio as gr # Interface라는 클래스로 입출력 상자를 웹 엘리먼트로 자동 생성해줌 from transformers import PreTrainedTokenizerFast,BartForConditionalGeneration # PreTrainedTokenizerFast: 사전 훈련된 토크나이저로, 텍스트를 모델이 이해할 수 있는 형식으로 변환합니다. # BartForConditionalGeneration: BART 모델의 변형으로 요약, 번역, 텍스트 생성 등에 사용 # Bart는 encorder-decoder 모델의 예시 # from transformers import로 시작하는 import문을 보면 # 많은 경우 AutoTokenizer, AutoModel # tokenizer = AutoTokenizer.from_pretrained("model name") # Load Model and Tokenize tokenizer = PreTrainedTokenizerFast.from_pretrained("ainize/kobart-news") model = BartForConditionalGeneration.from_pretrained("ainize/kobart-news") # 원문을 받아서 요약문을 반환 def summ(txt): input_ids = tokenizer.encode(input_text, return_tensors="pt") summary_text_ids = model.generate( input_ids=input_ids, bos_token_id=model.config.bos_token_id, # BOS는 Beginning of Sentence eos_token_id=model.config.eos_token_id, # EOS는 End Of Sentence length_penalty=2.0, # 요약을 얼마나 짧게 할지 max_length=142, # min_length=56, # num_beams=4) # beam search -> 가지 수 라고 생각하면 됨. 가지 4개를 펼치고 그 각가지에서 4개를 펼친 후 총 16개중 가장 적합한 4개를 고른 가지를 펼쳐 반복 과정 return tokenizer.decode(summary_text_ids[0], skip_special_tokens=True) interface = gr.Interface(summ, [gr.Textbox(label = "original text")], [gr.Textbox(label = "summary")]) interface.launch()