File size: 1,332 Bytes
a134161
 
7f7d093
a134161
 
 
 
 
 
7f7d093
 
 
 
 
 
a134161
 
 
 
 
7f7d093
 
 
 
 
 
 
 
 
 
 
 
a134161
7f7d093
a134161
 
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
# streamlit_app.py
import streamlit as st
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

st.set_page_config(page_title="Text Summarizer", layout="centered")
st.title("πŸ“ Text Summarization App")

input_text = st.text_area("Enter text to summarize", height=200)

@st.cache_resource
def load_model():
    model = AutoModelForSeq2SeqLM.from_pretrained("google/pegasus-cnn_dailymail")
    tokenizer = AutoTokenizer.from_pretrained("google/pegasus-cnn_dailymail")
    return model, tokenizer

if st.button("Summarize"):
    if input_text.strip() == "":
        st.warning("Please enter some text.")
    else:
        try:
            with st.spinner("Summarizing..."):
                model, tokenizer = load_model()
                inputs = tokenizer(input_text, return_tensors="pt", truncation=True)
                summary_ids = model.generate(
                    inputs["input_ids"],
                    max_length=100,
                    min_length=30,
                    length_penalty=2.0,
                    num_beams=4,
                    early_stopping=True
                )
                summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
                st.subheader("Summary")
                st.success(summary)
        except Exception as e:
            st.error(f"Error: {e}")