File size: 2,038 Bytes
db7f031
a16fb87
 
 
 
 
 
 
b6d7643
c18799d
b6d7643
 
 
 
 
 
db7f031
b6d7643
34f0c1d
b6d7643
 
 
 
 
71eb4da
 
 
b6d7643
71eb4da
 
34f0c1d
b6d7643
71eb4da
 
 
34f0c1d
 
 
 
 
 
 
71eb4da
b6d7643
34f0c1d
b6d7643
71eb4da
b6d7643
 
db7f031
 
b6d7643
 
 
 
 
 
 
 
 
 
 
b83e654
 
b6d7643
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import streamlit as st

st.set_page_config(
    page_title="Text Summarizer",
    page_icon="📝",
    layout="centered"
)

from transformers import BartTokenizer, BartForConditionalGeneration

@st.cache_resource
def load_model():
    model_name = "facebook/bart-large-cnn"
    tokenizer = BartTokenizer.from_pretrained(model_name)
    model = BartForConditionalGeneration.from_pretrained(model_name)
    return tokenizer, model

tokenizer, model = load_model()

def summarize(text: str, style: str):
    inputs = tokenizer.encode(text, return_tensors="pt", max_length=1024, truncation=True)
    input_length = len(inputs[0])

    if style == "Normal":
        max_length = int(input_length * 0.6)
        min_length = int(input_length * 0.5)
        length_penalty = 1.5
    elif style == "Precise":
        max_length = int(input_length * 0.45)
        min_length = int(input_length * 0.35)
        length_penalty = 1.2
    else:  # Accurate
        max_length = int(input_length * 0.4)
        min_length = int(input_length * 0.3)
        length_penalty = 1.0

    summary_ids = model.generate(
        inputs,
        max_length=max_length,
        min_length=min_length,
        length_penalty=length_penalty,
        num_beams=4,
        no_repeat_ngram_size=3,
        early_stopping=True,
    )
    summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)

    if not summary.endswith((".", "!", "?")):
        summary += "."
    return summary

st.title("🧠 Text Summarizer")
st.write("Summarize long text into concise and clear summaries using **BART**.")

user_input = st.text_area("Enter text to summarize:", height=200)
summary_style = st.selectbox("Choose summarization style:", ("Normal", "Precise", "Accurate"))

if st.button("Summarize"):
    if user_input.strip():
        with st.spinner("Summarizing... ⏳"):
            summary = summarize(user_input, summary_style)
        st.subheader("Summary:")
        st.write(summary)
    else:
        st.warning("⚠️ Please enter some text to summarize.")