minnehwg's picture
Update app.py
4fcafd1 verified
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)