-
Notifications
You must be signed in to change notification settings - Fork 0
/
infer.py
31 lines (27 loc) · 1.06 KB
/
infer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import torch
import streamlit as st
# from kobart import get_kobart_tokenizer
from transformers import BartForConditionalGeneration, PreTrainedTokenizerFast
from transformers.models.bart import BartForConditionalGeneration
@st.cache
def load_model():
model = BartForConditionalGeneration.from_pretrained('./kobart_summary')
# tokenizer = get_kobart_tokenizer()
return model
model = load_model()
# tokenizer = get_kobart_tokenizer()
tokenizer = PreTrainedTokenizerFast.from_pretrained('gogamza/kobart-base-v1')
st.title("KoBART 요약 Test")
text = st.text_area("방송 콘텐츠 내용 입력:")
st.markdown("## 원문")
st.write(text)
if text:
text = text.replace('\n', '')
st.markdown("## KoBART 요약 결과")
with st.spinner('processing..'):
input_ids = tokenizer.encode(text)
input_ids = torch.tensor(input_ids)
input_ids = input_ids.unsqueeze(0)
output = model.generate(input_ids, eos_token_id=1, max_length=512, num_beams=5)
output = tokenizer.decode(output[0], skip_special_tokens=True)
st.write(output)