File size: 1,494 Bytes
dd96c05
 
 
 
 
 
 
 
 
 
 
 
4a180a4
dd96c05
 
4fcafd1
dd96c05
 
4a180a4
dd96c05
 
f9ab7ea
 
 
 
 
dd96c05
 
 
 
 
 
 
39df2a7
13dd9c7
dd96c05
f9ab7ea
 
 
dd96c05
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import streamlit as st
import torch

@st.cache_resource
def load_model(cp_path):
    model = AutoModelForSeq2SeqLM.from_pretrained(cp_path)
    return model
    
@st.cache_resource
def load_tokenizer(path):
    tokenizer = AutoTokenizer.from_pretrained(path)
    return tokenizer

cp_aug = 'minnehwg/finetune-newwiki-summarization-ver-augmented2'
cp_org = 'minnehwg/finetune-newwiki-summarization-ver1'

model_org = load_model(cp_org)
model_aug  = load_model(cp_aug)
tokenizer = load_tokenizer("VietAI/vit5-base")

def processed(text):
    processed_text = text.replace('\n', ' ')
    processed_text = processed_text.lower()
    return processed_text
    
def summarize(text, model, tokenizer, num_beams=4, device='cpu'):
    model.to(device)
    inputs = tokenizer.encode(text, return_tensors="pt", max_length=1024, truncation=True, padding = True).to(device)
    with torch.no_grad():
        summary_ids = model.generate(inputs, max_length=256, num_beams=num_beams)
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
    return summary
    
text = st.text_area('Nhập đoạn văn bản', height = 300)
if text:
    processed_text = processed(text)
    re1 = summarize(processed_text, model_org, tokenizer)
    re2 = summarize(processed_text, model_aug, tokenizer)
    out = {
        'Result from model with original data': re1,
        'Result from model with augmented data': re2
    }
    st.json(out)