waleed-12/Encoder-Decoder_Text-Summarization
0
1import streamlit as st2from transformers import AutoTokenizer, AutoModelForSeq2SeqLM3import torch4 5# ---- Configuration ----6MODEL_NAME = "AbdullahAlnemr1/flan-t5-summarizer" 7device = torch.device("cuda" if torch.cuda.is_available() else "cpu")8 9# ---- Load model and tokenizer ----10@st.cache_resource11def load_model():12 tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)13 model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float32)14 model.to(device)15 return tokenizer, model16 17tokenizer, model = load_model()18 19# ---- Streamlit App ----20st.title("Text Summarizer")21st.write("Generate concise summariy.")22 23# ---- Input Area ----24article = st.text_area("Enter the article to summarize:", height=250)25 26# ---- Parameters ----27max_input_len = 51228max_output_len = 15029 30# ---- Generate Summary ----31if st.button("Generate Summary"):32 if not article.strip():33 st.warning("Please enter some text to summarize.")34 else:35 with st.spinner("Generating summary..."):36 inputs = tokenizer(37 article,38 return_tensors="pt",39 max_length=max_input_len,40 truncation=True41 ).to(device)42 43 summary_ids = model.generate(44 **inputs,45 max_length=max_output_len,46 num_beams=4,47 length_penalty=2.0,48 early_stopping=True49 )50 51 summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)52 53 # ---- Output ----54 st.subheader("Generated Summary:")55 st.write(summary)